From 65c3d96e99c9ab2d46af279a56518bbf8726cba7 Mon Sep 17 00:00:00 2001 From: runtianz Date: Tue, 5 Nov 2024 12:10:15 -0800 Subject: [PATCH] Fix comments --- src/api/transactionSubmission/build.ts | 58 ++++++++++++++++++- src/transactions/script-composer/index.ts | 24 ++++++-- .../transactionBuilder/remoteAbi.ts | 2 +- .../transaction/transactionSubmission.test.ts | 8 +-- 4 files changed, 81 insertions(+), 11 deletions(-) diff --git a/src/api/transactionSubmission/build.ts b/src/api/transactionSubmission/build.ts index b4eaa71c1..83b3c4f47 100644 --- a/src/api/transactionSubmission/build.ts +++ b/src/api/transactionSubmission/build.ts @@ -101,14 +101,68 @@ export class Build { return generateTransaction({ aptosConfig: this.config, ...args }); } + /** + * Build a transaction from a series of Move calls. + * + * This function allows you to create a transaction with a list of Move calls. + * + * @param args.sender - The sender account address. + * @param args.builder - The closure to construct the list of calls. + * @param args.options - Optional transaction configurations. + * @param args.withFeePayer - Whether there is a fee payer for the transaction. + * + * @returns SimpleTransaction + * + * @example + * ```typescript + * import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk"; + * + * const config = new AptosConfig({ network: Network.TESTNET }); + * const aptos = new Aptos(config); + * + * async function runExample() { + * // Build a transaction from a chained series of Move calls. + * const transaction = await aptos.transaction.script_composer({ + * sender: "0x1", // replace with a real sender account address + * builder: builder: async (builder) => { + * const coin = await builder.addBatchedCalls({ + * function: "0x1::coin::withdraw", + * functionArguments: [CallArgument.new_signer(0), 1], + * typeArguments: ["0x1::aptos_coin::AptosCoin"], + * }); + * + * // Pass the returned value from the first function call to the second call + * const fungibleAsset = await builder.addBatchedCalls({ + * function: "0x1::coin::coin_to_fungible_asset", + * functionArguments: [coin[0]], + * typeArguments: ["0x1::aptos_coin::AptosCoin"], + * }); + * + * await builder.addBatchedCalls({ + * function: "0x1::primary_fungible_store::deposit", + * functionArguments: [singleSignerED25519SenderAccount.accountAddress, fungibleAsset[0]], + * typeArguments: [], + * }); + * return builder; + * }, + * options: { + * gasUnitPrice: 100, // specify your own gas unit price if needed + * maxGasAmount: 1000, // specify your own max gas amount if needed + * }, + * }); + * + * console.log(transaction); + * } + * runExample().catch(console.error); + * ``` + */ async script_composer(args: { sender: AccountAddressInput; builder: (builder: AptosScriptComposer) => Promise; options?: InputGenerateTransactionOptions; withFeePayer?: boolean; }): Promise { - let builder = new AptosScriptComposer(this.config); - builder = await args.builder(builder); + const builder = await args.builder(new AptosScriptComposer(this.config)); const bytes = builder.build(); const raw_txn = await generateRawTransaction({ aptosConfig: this.config, diff --git a/src/transactions/script-composer/index.ts b/src/transactions/script-composer/index.ts index 40d930bbc..1cfdde4b7 100644 --- a/src/transactions/script-composer/index.ts +++ b/src/transactions/script-composer/index.ts @@ -35,20 +35,36 @@ function convertCallArgument( } +// A wrapper class around TransactionComposer, which is a WASM library compiled +// from aptos-core/aptos-move/script-composer. +// +// This class allows the SDK caller to build a transaction that invokes multiple Move functions +// and allow for arguments to be passed around. export class AptosScriptComposer { private builder: TransactionComposer; private config: AptosConfig; - constructor(aptos_config: AptosConfig) { + constructor(aptosConfig: AptosConfig) { this.builder = TransactionComposer.single_signer(); - this.config = aptos_config; + this.config = aptosConfig; } - async add_batched_calls(input: InputBatchedFunctionData): Promise { + // Add a move function invocation to the TransactionComposer. + // + // Similar to how to create an entry function, the difference is that input arguments could + // either be a `CallArgument` which represents an abstract value returned from a previous Move call + // or the regular entry function arguments. + // + // The function would also return a list of `CallArgument` that can be passed on to future calls. + async addBatchedCalls(input: InputBatchedFunctionData): Promise { const { moduleAddress, moduleName, functionName } = getFunctionParts(input.function); const nodeUrl = this.config.getRequestUrl(AptosApiType.FULLNODE); - await this.builder.load_module(nodeUrl, `${moduleAddress }::${ moduleName}`); + + // Load the calling module into the builder. + await this.builder.load_module(nodeUrl, `${moduleAddress}::${moduleName}`); + + // Load the calling type arguments into the loader. if (input.typeArguments !== undefined) { for (const typeTag of input.typeArguments) { await this.builder.load_type_tag(nodeUrl, typeTag.toString()); diff --git a/src/transactions/transactionBuilder/remoteAbi.ts b/src/transactions/transactionBuilder/remoteAbi.ts index 5021efc07..578790ab6 100644 --- a/src/transactions/transactionBuilder/remoteAbi.ts +++ b/src/transactions/transactionBuilder/remoteAbi.ts @@ -107,7 +107,7 @@ export async function fetchMoveFunctionAbi( ): Promise { const functionAbi = await fetchFunctionAbi(moduleAddress, moduleName, functionName, aptosConfig); if (!functionAbi) { - throw new Error(`Could not find entry function ABI for '${moduleAddress}::${moduleName}::${functionName}'`); + throw new Error(`Could not find function ABI for '${moduleAddress}::${moduleName}::${functionName}'`); } const params: TypeTag[] = []; for (let i = 0; i < functionAbi.params.length; i += 1) { diff --git a/tests/e2e/transaction/transactionSubmission.test.ts b/tests/e2e/transaction/transactionSubmission.test.ts index c0a73051c..bd2800b6a 100644 --- a/tests/e2e/transaction/transactionSubmission.test.ts +++ b/tests/e2e/transaction/transactionSubmission.test.ts @@ -68,7 +68,7 @@ describe("transaction submission", () => { }); test("with batch payload", async () => { const builder = new AptosScriptComposer(aptos.config); - await builder.add_batched_calls({ + await builder.addBatchedCalls({ function: `${contractPublisherAccount.accountAddress}::transfer::transfer`, functionArguments: [CallArgument.new_signer(0), 1, receiverAccounts[0].accountAddress], }); @@ -93,19 +93,19 @@ describe("transaction submission", () => { const transaction = await aptos.transaction.build.script_composer({ sender: singleSignerED25519SenderAccount.accountAddress, builder: async (builder) => { - const coin = await builder.add_batched_calls({ + const coin = await builder.addBatchedCalls({ function: "0x1::coin::withdraw", functionArguments: [CallArgument.new_signer(0), 1], typeArguments: ["0x1::aptos_coin::AptosCoin"], }); - const fungibleAsset = await builder.add_batched_calls({ + const fungibleAsset = await builder.addBatchedCalls({ function: "0x1::coin::coin_to_fungible_asset", functionArguments: [coin[0]], typeArguments: ["0x1::aptos_coin::AptosCoin"], }); - await builder.add_batched_calls({ + await builder.addBatchedCalls({ function: "0x1::primary_fungible_store::deposit", functionArguments: [singleSignerED25519SenderAccount.accountAddress, fungibleAsset[0]], typeArguments: [],