transfer method

Future<String> transfer(
  1. AptosAccount from,
  2. String to,
  3. BigInt amount, {
  4. BigInt? maxGasAmount,
  5. BigInt? gasUnitPrice,
  6. BigInt? expireTimestamp,
  7. String? coinType,
  8. bool createReceiverIfMissing = false,
})

Generate, sign, and submit a transaction to the Aptos blockchain API to transfer coins from one account to another. By default it transfers 0x1::aptos_coin::AptosCoin, but you can specify a different coin type with the coinType argument.

You may set createReceiverIfMissing to true if you want to create the receiver account if it does not exist on chain yet. If you do not set this to true, the transaction will fail if the receiver account does not exist on-chain.

Implementation

Future<String> transfer(
  AptosAccount from,
  String to,
  BigInt amount,{
  BigInt? maxGasAmount,
  BigInt? gasUnitPrice,
  BigInt? expireTimestamp,
  String? coinType,
  bool createReceiverIfMissing = false
}) async {
  coinType ??= AptosClient.APTOS_COIN;

  final func = createReceiverIfMissing ? "0x1::aptos_account::transfer_coins" : "0x1::coin::transfer";

  final config = ABIBuilderConfig(
    sender: from.address,
    maxGasAmount: maxGasAmount,
    gasUnitPrice: gasUnitPrice,
    expSecFromNow: expireTimestamp
  );

  final builder = TransactionBuilderRemoteABI(aptosClient, config);
  final rawTxn = await builder.build(
    func,
    [coinType],
    [to, amount]
  );

  final bcsTxn = AptosClient.generateBCSTransaction(from, rawTxn);
  final resp = await aptosClient.submitSignedBCSTransaction(bcsTxn);
  return resp["hash"];
}