Send Tokens
Learn how to send SPL tokens between accounts using the Solana Python library.
Code Example
#!/usr/bin/env python3
"""
Solana Cookbook - How to Send Tokens
"""
import asyncio
from solana.rpc.async_api import AsyncClient
from solders.keypair import Keypair
from solders.pubkey import Pubkey
from solders.transaction import VersionedTransaction
from solders.message import MessageV0
from spl.token.instructions import transfer_checked
from spl.token.models import TransferCheckedParams
from spl.token.constants import TOKEN_PROGRAM_ID
async def main():
rpc = AsyncClient("https://api.devnet.solana.com")
sender = Keypair()
recipient = Keypair()
# Example token mint (USDC devnet)
token_mint = Pubkey.from_string("4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU")
# Example token accounts (would need to be created first)
sender_token_account = Pubkey.from_string("11111111111111111111111111111111")
recipient_token_account = Pubkey.from_string("11111111111111111111111111111111")
amount = 1_000_000 # 1 token (6 decimals)
decimals = 6
async with rpc:
# Get latest blockhash
latest_blockhash = await rpc.get_latest_blockhash()
# Create transfer instruction
transfer_instruction = transfer_checked(
TransferCheckedParams(
program_id=TOKEN_PROGRAM_ID,
source=sender_token_account,
mint=token_mint,
dest=recipient_token_account,
owner=sender.pubkey(),
amount=amount,
decimals=decimals,
)
)
# Create message
message = MessageV0.try_compile(
payer=sender.pubkey(),
instructions=[transfer_instruction],
address_lookup_table_accounts=[],
recent_blockhash=latest_blockhash.value.blockhash,
)
# Create and sign transaction
transaction = VersionedTransaction(message, [sender])
print(f"Sender: {sender.pubkey()}")
print(f"Recipient: {recipient.pubkey()}")
print(f"Token Mint: {token_mint}")
print(f"Transfer Amount: {amount / (10**decimals)} tokens")
print(f"Transaction created successfully")
if __name__ == "__main__":
asyncio.run(main())
Explanation
This example demonstrates how to send SPL tokens between accounts:
- Create RPC Client: Connect to the Solana devnet
- Setup Accounts: Define sender, recipient, and token mint
- Token Accounts: Specify source and destination token accounts
- Transfer Parameters: Set amount and decimals for the token
- Create Transfer Instruction: Use
transfer_checkedfor secure transfers - Compile Message: Create a message with the transfer instruction
- Create and Sign Transaction: Build and sign a versioned transaction
VersionedTransaction(message, [sender]) creates a signed transaction: the keypairs in the second argument sign the message.
Here, sender is both the fee payer and the owner authorizing the token transfer.
See the solders transaction reference
and partial signing tutorial
for details on signing with multiple parties.
Key Concepts
- SPL Tokens: Standard tokens on Solana (like ERC-20 on Ethereum)
- Token Mint: The address that identifies the token type
- Token Accounts: Accounts that hold tokens for a specific mint
- Transfer Checked: Safer transfer method that verifies mint and decimals
- Decimals: Number of decimal places for the token (e.g., 6 for USDC)
Usage
To run this example:
-
Install required dependencies:
pip install solana-py solders spl-token -
Ensure you have:
- Valid token accounts for sender and recipient
- Sufficient token balance in the sender's account
-
The correct token mint address
-
Run the script:
python send_tokens.py
Note: This example creates and signs the transaction locally but doesn't submit it. You'll need to create actual token accounts and call await rpc.send_transaction(transaction) to execute the transfer.