addTokenTransfer method

Transaction addTokenTransfer(
  1. String to,
  2. int amount,
  3. String tokenAddress, {
  4. int tokenId = 0,
})

Add a token transfer to the transaction

  • to : Address of the recipient (hexadecimal)
  • amount : Amount of token to transfer
  • tokenAddress : Address of token to spend (hexadecimal)
  • tokenId : ID of the token to use (default to 0)

Implementation

Transaction addTokenTransfer(
  String to,
  int amount,
  String tokenAddress, {
  int tokenId = 0,
}) {
  if (!isHex(to)) {
    throw const FormatException("'to' must be an hexadecimal string");
  }

  if (!isHex(tokenAddress)) {
    throw const FormatException(
      "'tokenAddress' must be an hexadecimal string",
    );
  }

  if (tokenId.isNaN && tokenId < 0) {
    throw Exception("'tokenId' must be a valid integer >= 0");
  }

  final tokenTransfer = TokenTransfer(
    amount: amount,
    tokenAddress: tokenAddress,
    to: to,
    tokenId: tokenId,
  );

  final newTokenTransfer = data!.ledger!.token!.transfers..add(tokenTransfer);
  return copyWith.data!.ledger!.token!(
    transfers: newTokenTransfer,
  );
}