Transfer Tokens
The transferTokens
function transfers tokens from one account to another. The sender must sign the transaction.
Usage
const signature = await connection.transferTokens({
sender: senderWallet,
destination: recipientAddress,
mintAddress: tokenMint,
amount: 1_000_000n,
maximumClientSideRetries: 0,
});
Parameters
sender
:KeyPairSigner
- Signer that owns the tokens and will sign the transactiondestination
:Address
- Address to receive the tokensmintAddress
:Address
- Address of the token mintamount
:bigint
- Amount of tokens to transfer (in base units)maximumClientSideRetries
:number
(optional) - Maximum number of times to retry sending the transaction (default: 0)abortSignal
:AbortSignal | null
(optional) - Signal to abort the transaction (default: null)
Returns
Returns a Promise<Signature>
- The transaction signature that can be used to look up the transaction.
Example
// Create wallets for sender and recipient
const [sender, recipient] = await Promise.all([
connection.createWallet({
airdropAmount: lamports(1n * SOL),
}),
connection.createWallet({
airdropAmount: lamports(1n * SOL),
}),
]);
// Create a new token mint
const mintAddress = await connection.createTokenMint(
sender, // sender will be the mint authority
9, // decimals
"My Token",
"TKN",
"https://example.com/token.json",
);
// Mint some tokens to the sender's account
await connection.mintTokens(mintAddress, sender, 100n, sender.address);
// Transfer 50 tokens from sender to recipient with retries
const signature = await connection.transferTokens({
sender,
destination: recipient.address,
mintAddress,
amount: 50n,
maximumClientSideRetries: 3,
});
Error Handling
The function will throw an error if:
- The sender doesn’t have sufficient tokens
- The sender lacks sufficient SOL to pay for the transaction
- The destination account doesn’t exist
- The RPC connection fails
- The maximum number of retries is exceeded
Last updated on