Skip to content

Instantly share code, notes, and snippets.

@ruiqic
Last active January 8, 2025 22:04
Show Gist options
  • Save ruiqic/11ce1b53a9da7984c0d6ba101bba637f to your computer and use it in GitHub Desktop.
Save ruiqic/11ce1b53a9da7984c0d6ba101bba637f to your computer and use it in GitHub Desktop.
Simple python script to transfer an SPL token using solana-py library. Keypair for Pub and Private key are to generated/derived using bip_utils library.
from solana.rpc.async_api import AsyncClient
from solana.transaction import Transaction
from spl.token.instructions import get_associated_token_address, transfer_checked, TransferCheckedParams
from spl.token.constants import TOKEN_PROGRAM_ID
from solders.pubkey import Pubkey
from solders.keypair import Keypair
from solders.compute_budget import set_compute_unit_limit, set_compute_unit_price
from bip_utils import Bip44
import asyncio
async def transfer_spl_token(sender_bip44_ctx : Bip44,
recipient_pubkey : Pubkey,
token_mint : Pubkey,
amount : int,
decimals: int,
endpoint : str = "https://api.devnet.solana.com",
compute_unit_limit : int = 10_000,
compute_unit_price : int = 0):
connection = AsyncClient(endpoint)
private_key_seed = sender_bip44_ctx.PrivateKey().Raw()
keypair = Keypair.from_seed(private_key_seed)
sender_token_address = get_associated_token_address(keypair.pubkey(), token_mint)
recipient_token_address = get_associated_token_address(recipient_pubkey, token_mint)
blockhash_resp = await connection.get_latest_blockhash()
recent_blockhash = blockhash_resp.value.blockhash
ixs = []
if compute_unit_limit > 0:
ixs.append(set_compute_unit_limit(compute_unit_limit))
if compute_unit_price > 0:
ixs.append(set_compute_unit_price(compute_unit_price))
ixs.append(
transfer_checked(
TransferCheckedParams(
source=sender_token_address,
dest=recipient_token_address,
owner=keypair.pubkey(),
mint=token_mint,
amount=amount,
decimals=decimals,
program_id=TOKEN_PROGRAM_ID
)
)
)
transaction = Transaction(
instructions=ixs,
recent_blockhash=recent_blockhash,
fee_payer=keypair.pubkey()
)
transaction.sign(keypair)
response = await connection.send_transaction(transaction, keypair)
return response
# Example usage:
# Specify sender keypair
from bip_utils import Bip39MnemonicGenerator, Bip39SeedGenerator, Bip39WordsNum, Bip44, Bip44Coins
sender_mnemonic = Bip39MnemonicGenerator().FromWordsNumber(Bip39WordsNum.WORDS_NUM_12)
sender_seed_bytes = Bip39SeedGenerator(sender_mnemonic).Generate()
sender_bip44_ctx = Bip44.FromPrivateKey(sender_seed_bytes[:32], Bip44Coins.SOLANA)
# Specify recipient address
recipient_mnemonic = Bip39MnemonicGenerator().FromWordsNumber(Bip39WordsNum.WORDS_NUM_12)
recipient_seed_bytes = Bip39SeedGenerator(recipient_mnemonic).Generate()
recipient_bip44_ctx = Bip44.FromPrivateKey(recipient_seed_bytes[:32], Bip44Coins.SOLANA)
recipient_pubkey = Pubkey.from_string(recipient_bip44_ctx.PublicKey().ToAddress())
# Specify token, amount, and endpoint
token_mint = Pubkey.from_string("4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU")
amount = 50
decimals = 6
endpoint = "https://api.devnet.solana.com"
out = asyncio.run(
transfer_spl_token(
sender_bip44_ctx=sender_bip44_ctx,
recipient_pubkey=recipient_pubkey,
token_mint=token_mint,
amount=amount,
decimals=decimals,
endpoint=endpoint,
compute_unit_limit=10_000,
compute_unit_price=1_000_000
)
)
print(out)
@umair313
Copy link

This is not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment