addTokenTransfer method

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

Add a token transfer to the transaction @param {String} to Address of the recipient (hexadecimal) @param {int} amount Amount of token to transfer @param {String} tokenAddress Address of token to spend (hexadecimal) @param {int} 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,
  );
}