transferSplToken method
Future<TransactionId>
transferSplToken({
- required Ed25519HDPublicKey mint,
- required Ed25519HDPublicKey destination,
- required int amount,
- required Wallet owner,
- String? memo,
- SignatureCallback? onSigned,
- Commitment commitment = Commitment.finalized,
Transfers amount SPL token with mint from this wallet to the
destination address with an optional memo.
For commitment parameter description see this document
Commitment.processed is not supported as commitment.
Implementation
Future<TransactionId> transferSplToken({
required Ed25519HDPublicKey mint,
required Ed25519HDPublicKey destination,
required int amount,
required Wallet owner,
String? memo,
SignatureCallback? onSigned,
Commitment commitment = Commitment.finalized,
}) async {
final associatedRecipientAccount = await getAssociatedTokenAccount(
owner: destination,
mint: mint,
commitment: commitment,
);
final associatedSenderAccount = await getAssociatedTokenAccount(
owner: owner.publicKey,
mint: mint,
commitment: commitment,
);
// Throw an appropriate exception if the sender has no associated
// token account
if (associatedSenderAccount == null) {
throw NoAssociatedTokenAccountException(owner.address, mint.toBase58());
}
// Also throw an adequate exception if the recipient has no associated
// token account
if (associatedRecipientAccount == null) {
throw NoAssociatedTokenAccountException(
destination.toBase58(),
mint.toBase58(),
);
}
final instruction = TokenInstruction.transfer(
source: Ed25519HDPublicKey.fromBase58(associatedSenderAccount.pubkey),
destination:
Ed25519HDPublicKey.fromBase58(associatedRecipientAccount.pubkey),
owner: owner.publicKey,
amount: amount,
);
final message = Message(
instructions: [
instruction,
if (memo != null && memo.isNotEmpty)
MemoInstruction(signers: [owner.publicKey], memo: memo),
],
);
return sendAndConfirmTransaction(
message: message,
signers: [owner],
onSigned: onSigned ?? ignoreOnSigned,
commitment: commitment,
);
}