Is it possible to call two different functions in one transaction? #107
Answered
by
gregnazario
gregnazario
asked this question in
Questions
-
From DjBarons on Discord
|
Beta Was this translation helpful? Give feedback.
Answered by
gregnazario
Mar 26, 2024
Replies: 1 comment
-
Move scriptsYes, it's possible with a Move script. Check out about Move scripts here: https://aptos.dev/move/move-on-aptos/scripts/ Move script Examplescript {
use aptos_framework::aptos_account;
/// Transfers the same amount to two accounts
fun transfer_two<Coin>(caller: &signer, receiver1: address, receiver2: address, amount: u64) {
// Send amount to receiver1
aptos_account::transfer_coins<Coin>(caller, receiver1, amount);
// Send amount to receiver2
aptos_account::transfer_coins<Coin>(caller, receiver2, amount);
}
} Typescript Associated:First precompile the script above:
Now we go to typescript // Save the script as hex
const script = "0xa11ceb0b0600000006010002030206040802050a0f07191d0836200000000103010100000204060c0505030001090003060c05030d6170746f735f6163636f756e740e7472616e736665725f636f696e73000000000000000000000000000000000000000000000000000000000000000101000001090a000b010a0338000b000b020b03380002";
// Initialize your account and the receiver addresses
const sender = Account.fromPrivateKey(...);
// Build the transaction
const transaction = await aptos.transaction.build.simple({
sender: sender.accountAddress,
data: {
bytecode: script,
typeArguments: ["0x1::aptos_coin::AptosCoin"], // Transfer APT
functionArguments: ["0x1", "0x2", 500], // Send 0.000005 APT to 0x1 and 0x2
},
});
// Sign and submit the transaction
const pendingTransaction = await aptos.signAndSubmitTransaction({ signer, transaction });
// Wait for the transaction to finish
const response = await aptos.waitForTransaction({ transactionHash: pendingTransaction.hash });
// At this point your transaction is done! |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
gregnazario
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Move scripts
Yes, it's possible with a Move script. Check out about Move scripts here:
https://aptos.dev/move/move-on-aptos/scripts/
Move script Example
Typescript Associated:
First precompile the script above: