Transferring the Fund

Like the previous step, this is another helper step that constructs a transaction transferring the coins from Alice to Bob. The SDK provides a helper function to generate a transferCoinTransaction transaction that can be simulated or submitted to chain.


Once a transaction has been submitted to chain, the API will return a transaction hash that can be used in the subsequent step to check on the transaction status.

The Aptos blockchain does perform a handful of validation checks on submission; and if any of those fail, the user will instead be given an error. These validations use the transaction signature and unused sequence number, and submitting the transaction to the appropriate chain.


const transaction = await aptos.transferCoinTransaction({
sender: alice,
recipient: bob.accountAddress,
amount: TRANSFER_AMOUNT,
});
const pendingTxn = await aptos.signAndSubmitTransaction({
signer: alice,
transaction,
});


Behind the scenes, the transferCoinTransaction function generates a transaction payload that can be simulated or submitted to chain:


export async function transferCoinTransaction(args: {
aptosConfig: AptosConfig;
sender: Account;
recipient: AccountAddressInput;
amount: AnyNumber;
coinType?: MoveStructId;
options?: InputGenerateTransactionOptions;
}): Promise<SingleSignerTransaction> {
const { aptosConfig, sender, recipient, amount, coinType, options } = args;
const coinStructType = coinType ?? APTOS_COIN;
const transaction = await generateTransaction({
aptosConfig,
sender: sender.accountAddress,
data: {
function: "0x1::aptos_account::transfer_coins",
typeArguments: [coinStructType],
functionArguments: [recipient, amount],
},
options,
});
return transaction;
}


Breaking the above down into pieces

transfer_coins internally is a EntryFunction in the Aptos Account Move module, i.e. an entry function in Move that is directly callable.

The Move function is stored on the aptos_account module: 0x1::aptos_account.

The transfer_coins functions uses the Coin Move module Because the Coin module can be used by other coins, the transferCoinTransaction must explicitly specify which coin type to transfer. If not specified with coinType it defaults to 0x1::aptos_coin::AptosCoin.


Waiting for transaction resolution

In the TypeScript SDK, just calling waitForTransaction is sufficient to wait for the transaction to complete. The function will return the Transaction returned by the API once it is processed (either successfully or unsuccessfully) or throw an error if processing time exceeds the timeout.


const response = await aptos.waitForTransaction({
transactionHash: pendingTxn.hash,
});