Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add docs for script composer #696

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,5 @@ Transactions have a couple of additional features which let them adapt to your n
1. [Multi-Agent Signatures](building-transactions/multi-agent-transactions.mdx) - Allowing multiple accounts to be used for a single contract.
2. [Sponsoring Transactions](building-transactions/sponsoring-transactions.mdx) - Have another account pay gas fees for this transaction.
3. [Batch Submit Transactions](building-transactions/batching-transactions.mdx) - How to send multiple transactions quickly from a single account.
4. [Binary Canonical Serialization (BCS)](building-transactions/bcs-format.mdx) - The format used to serialize data for Aptos transactions.
4. [Binary Canonical Serialization (BCS)](building-transactions/bcs-format.mdx) - The format used to serialize data for Aptos transactions.
5. [Composing multiple Move calls with ScriptComposer](building-transactions/script-composer.mdx) - Building more complex transaction payload that calls into multiple Move functions dynamically.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ export default {
"bcs-format": {
title: "BCS Format",
},
"script-composer": {
title: "Script Composer",
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
title: "Dynamically invoke chains of Move calls with ScriptComposer"
---

# Dynamically invoke chains of Move calls with ScriptComposer

In the naive api, you only get to specify one entry function to invoke for one transaction. An advanced builder might want to be able to invoke multiple **public** Move functions inside one transaction. This is now enabled by the new `scriptComposer` api provided in the transaction builder.

Here's an example how you can invoke the api:

```ts filename="example.ts"
const transaction = await aptos.transaction.build.scriptComposer({
sender: singleSignerED25519SenderAccount.accountAddress,
// The builder expects a closure to build up the move call sequence.
builder: async (builder) => {
// invoke 0x1::coin::withdraw. This function would return a value of a `coin` type.
const coin = await builder.addBatchedCalls({
function: "0x1::coin::withdraw",
functionArguments: [CallArgument.new_signer(0), 1],
typeArguments: ["0x1::aptos_coin::AptosCoin"],
});

// Passing the coin value to the 0x1::coin::coin_to_fungible_asset to convert a coin
// into fungible asset.
const fungibleAsset = await builder.addBatchedCalls({
function: "0x1::coin::coin_to_fungible_asset",
// coin[0] represents the first return value from the first call you added.
functionArguments: [coin[0]],
typeArguments: ["0x1::aptos_coin::AptosCoin"],
});

// Deposit the fungibleAsset converted from second call.
await builder.addBatchedCalls({
function: "0x1::primary_fungible_store::deposit",
functionArguments: [singleSignerED25519SenderAccount.accountAddress, fungibleAsset[0]],
typeArguments: [],
});
return builder;
},
});
```

Behind the scene, the SDK will invoke a WASM binary to compile the series of Move calls into a `CompiledScript`. This will guarantee that the type and ability safety of Move is still being honored during the construction process. For the SDK users, this means:
1. ability safety:
a. If returned value does not have Drop ability, the returned value need to be consumed by subsequent calls.
b. If returned value does not have Copy ability, the returned value can only be passed to subsequent calls once.
2. The caller will need to make sure they pass the right values as arguments to subsequent calls. In the previous example, `0x1::coin::coin_to_fungible_asset` will expect an argument of `Coin<AptosCoin>`.

This implements [AIP-102](https://github.com/aptos-foundation/AIPs/blob/main/aips/aip-102.md)
Loading