Skip to content

Commit

Permalink
Fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
runtian-zhou committed Nov 5, 2024
1 parent bd74a25 commit 65c3d96
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 11 deletions.
58 changes: 56 additions & 2 deletions src/api/transactionSubmission/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AptosScriptComposer>;
options?: InputGenerateTransactionOptions;
withFeePayer?: boolean;
}): Promise<SimpleTransaction> {
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,
Expand Down
24 changes: 20 additions & 4 deletions src/transactions/script-composer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CallArgument[]> {
// 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<CallArgument[]> {
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());
Expand Down
2 changes: 1 addition & 1 deletion src/transactions/transactionBuilder/remoteAbi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export async function fetchMoveFunctionAbi(
): Promise<FunctionABI> {
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) {
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e/transaction/transactionSubmission.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
});
Expand All @@ -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: [],
Expand Down

0 comments on commit 65c3d96

Please sign in to comment.