Skip to content

Commit

Permalink
--wip-- [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-haynes committed Aug 26, 2024
1 parent afd3392 commit 1290913
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 168 deletions.
8 changes: 4 additions & 4 deletions packages/client/src/interfaces/dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type {
BlockReference,
BlockResult,
FinalExecutionOutcome,
QueryResponseKind,
BlockReference,
BlockResult,
FinalExecutionOutcome,
QueryResponseKind,
} from '@near-js/types';
import type { SignedTransaction } from '@near-js/transactions';
import type { PublicKey } from '@near-js/crypto';
Expand Down
70 changes: 35 additions & 35 deletions packages/client/src/transactions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,52 @@ import { signAndSendFromComposer } from './sign_and_send';
import { AddFunctionCallAccessKeyParams, SignAndSendNonceParams } from '../interfaces';

async function getComposerResult({ composer, nonce, blockReference, deps }: { composer: TransactionComposer } & SignAndSendNonceParams) {
const { result } = await signAndSendFromComposer({
composer,
nonce,
blockReference,
deps,
});
const { result } = await signAndSendFromComposer({
composer,
nonce,
blockReference,
deps,
});

return result;
return result;
}

export async function functionCall({ sender, receiver, method, args, gas, deposit, nonce, blockReference, deps }: FunctionCallParams) {
return getComposerResult({
composer: TransactionComposer.init({ sender, receiver })
.functionCall(method, args, gas, deposit),
nonce,
blockReference,
deps,
});
return getComposerResult({
composer: TransactionComposer.init({ sender, receiver })
.functionCall(method, args, gas, deposit),
nonce,
blockReference,
deps,
});
}

export async function transfer({ sender, receiver, amount, nonce, blockReference, deps }: TransferParams) {
return getComposerResult({
composer: TransactionComposer.init({ sender, receiver })
.transfer(amount),
nonce,
blockReference,
deps,
});
return getComposerResult({
composer: TransactionComposer.init({ sender, receiver })
.transfer(amount),
nonce,
blockReference,
deps,
});
}

export async function addFullAccessKey({ account, publicKey, nonce, blockReference, deps }: AddFullAccessKeyParams) {
return getComposerResult({
composer: TransactionComposer.init({ sender: account, receiver: account })
.addFullAccessKey(publicKey),
nonce,
blockReference,
deps,
});
return getComposerResult({
composer: TransactionComposer.init({ sender: account, receiver: account })
.addFullAccessKey(publicKey),
nonce,
blockReference,
deps,
});
}

export async function addFunctionCallAccessKey({ account, publicKey, contract, methodNames, allowance, nonce, blockReference, deps }: AddFunctionCallAccessKeyParams) {
return getComposerResult({
composer: TransactionComposer.init({ sender: account, receiver: account })
.addFunctionCallAccessKey(publicKey, contract, methodNames, allowance),
nonce,
blockReference,
deps,
});
return getComposerResult({
composer: TransactionComposer.init({ sender: account, receiver: account })
.addFunctionCallAccessKey(publicKey, contract, methodNames, allowance),
nonce,
blockReference,
deps,
});
}
176 changes: 88 additions & 88 deletions packages/client/src/transactions/composer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { PublicKey } from '@near-js/crypto';
import {
Action,
actionCreators,
buildDelegateAction,
DelegateAction,
Signature,
Transaction,
Action,
actionCreators,
buildDelegateAction,
DelegateAction,
Signature,
Transaction,
} from '@near-js/transactions';
import { baseDecode, DEFAULT_FUNCTION_CALL_GAS } from '@near-js/utils';
import { DEFAULT_META_TRANSACTION_BLOCK_HEIGHT_TTL } from '../constants';
Expand All @@ -20,102 +20,102 @@ interface TransactionOptions {
}

export class TransactionComposer {
private actions: Action[] = [];
receiver: string | undefined;
sender: string | undefined;
blockHeader: BlockHeader | undefined;
nonce: bigint | undefined;
publicKey: PublicKey | undefined;
private actions: Action[] = [];
receiver: string | undefined;
sender: string | undefined;
blockHeader: BlockHeader | undefined;
nonce: bigint | undefined;
publicKey: PublicKey | undefined;

static init(transaction: TransactionOptions) {
const composer = new TransactionComposer();
composer.receiver = transaction.receiver;
composer.sender = transaction.sender;
composer.blockHeader = transaction.blockHeader;
composer.nonce = transaction.nonce;
composer.publicKey = transaction.publicKey;
static init(transaction: TransactionOptions) {
const composer = new TransactionComposer();
composer.receiver = transaction.receiver;
composer.sender = transaction.sender;
composer.blockHeader = transaction.blockHeader;
composer.nonce = transaction.nonce;
composer.publicKey = transaction.publicKey;

return composer;
}
return composer;
}

private buildTransactionObject(transaction?: TransactionOptions) {
const hash = (transaction?.blockHeader?.hash || this.blockHeader?.hash)!;
return {
actions: this.actions,
blockHash: baseDecode(hash),
nonce: (transaction?.nonce || this.nonce)!,
publicKey: (transaction?.publicKey || this.publicKey)!,
receiverId: (transaction?.receiver || this.receiver)!,
signerId: (transaction?.sender || this.sender)!,
};
}
private buildTransactionObject(transaction?: TransactionOptions) {
const hash = (transaction?.blockHeader?.hash || this.blockHeader?.hash)!;
return {
actions: this.actions,
blockHash: baseDecode(hash),
nonce: (transaction?.nonce || this.nonce)!,
publicKey: (transaction?.publicKey || this.publicKey)!,
receiverId: (transaction?.receiver || this.receiver)!,
signerId: (transaction?.sender || this.sender)!,
};
}

toTransaction(transaction?: TransactionOptions): Transaction {
return new Transaction(this.buildTransactionObject(transaction));
}
toTransaction(transaction?: TransactionOptions): Transaction {
return new Transaction(this.buildTransactionObject(transaction));
}

toDelegateAction(transaction?: TransactionOptions): DelegateAction {
const { actions, nonce, publicKey, receiverId, signerId } = this.buildTransactionObject(transaction);
const blockHeader = transaction?.blockHeader || this.blockHeader;
toDelegateAction(transaction?: TransactionOptions): DelegateAction {
const { actions, nonce, publicKey, receiverId, signerId } = this.buildTransactionObject(transaction);
const blockHeader = transaction?.blockHeader || this.blockHeader;

return buildDelegateAction({
actions,
maxBlockHeight: BigInt(blockHeader!.height) + DEFAULT_META_TRANSACTION_BLOCK_HEIGHT_TTL,
nonce: BigInt(nonce) + BigInt(1),
publicKey,
receiverId,
senderId: signerId,
});
}
return buildDelegateAction({
actions,
maxBlockHeight: BigInt(blockHeader!.height) + DEFAULT_META_TRANSACTION_BLOCK_HEIGHT_TTL,
nonce: BigInt(nonce) + BigInt(1),
publicKey,
receiverId,
senderId: signerId,
});
}

addFullAccessKey(publicKey: PublicKey) {
this.actions.push(actionCreators.addKey(publicKey, actionCreators.fullAccessKey()));
return this;
}
addFullAccessKey(publicKey: PublicKey) {
this.actions.push(actionCreators.addKey(publicKey, actionCreators.fullAccessKey()));
return this;
}

addFunctionCallAccessKey(publicKey: PublicKey, contractId: string, methodNames: string[], allowance?: bigint) {
const accessKey = actionCreators.functionCallAccessKey(contractId, methodNames, allowance)
this.actions.push(actionCreators.addKey(publicKey, accessKey));
return this;
}
addFunctionCallAccessKey(publicKey: PublicKey, contractId: string, methodNames: string[], allowance?: bigint) {
const accessKey = actionCreators.functionCallAccessKey(contractId, methodNames, allowance);
this.actions.push(actionCreators.addKey(publicKey, accessKey));
return this;
}

createAccount() {
this.actions.push(actionCreators.createAccount());
return this;
}
createAccount() {
this.actions.push(actionCreators.createAccount());
return this;
}

deleteAccount(beneficiaryId: string) {
this.actions.push(actionCreators.deleteAccount(beneficiaryId));
return this;
}
deleteAccount(beneficiaryId: string) {
this.actions.push(actionCreators.deleteAccount(beneficiaryId));
return this;
}

deleteKey(publicKey: PublicKey) {
this.actions.push(actionCreators.deleteKey(publicKey));
return this;
}
deleteKey(publicKey: PublicKey) {
this.actions.push(actionCreators.deleteKey(publicKey));
return this;
}

deployContract(code: Uint8Array) {
this.actions.push(actionCreators.deployContract(code));
return this;
}
deployContract(code: Uint8Array) {
this.actions.push(actionCreators.deployContract(code));
return this;
}

functionCall(method: string, args: object, gas: bigint = DEFAULT_FUNCTION_CALL_GAS * BigInt(10), deposit = BigInt(0)) {
this.actions.push(actionCreators.functionCall(method, args, gas, deposit));
return this;
}
functionCall(method: string, args: object, gas: bigint = DEFAULT_FUNCTION_CALL_GAS * BigInt(10), deposit = BigInt(0)) {
this.actions.push(actionCreators.functionCall(method, args, gas, deposit));
return this;
}

signedDelegate(delegateAction: DelegateAction, signature: Signature) {
this.actions.push(actionCreators.signedDelegate({ delegateAction, signature }));
return this;
}
signedDelegate(delegateAction: DelegateAction, signature: Signature) {
this.actions.push(actionCreators.signedDelegate({ delegateAction, signature }));
return this;
}

stake(stake: bigint, publicKey: PublicKey) {
this.actions.push(actionCreators.stake(stake, publicKey));
return this;
}
stake(stake: bigint, publicKey: PublicKey) {
this.actions.push(actionCreators.stake(stake, publicKey));
return this;
}

transfer(deposit: bigint) {
this.actions.push(actionCreators.transfer(deposit));
return this;
}
transfer(deposit: bigint) {
this.actions.push(actionCreators.transfer(deposit));
return this;
}
}
Loading

0 comments on commit 1290913

Please sign in to comment.