Transfer a solana token from my wallet to another in python

2k views Asked by At

So I am working on a script that transfers tokens (custom-made tokens not owned by me) from my wallet to another in python using solana.py. How do I go about doing that? (By tokens I mean, tokens like Dust, Luv, or any other solana token)

where do I get the info needed regarding the tokens like token mint, token account address, etc. from? And what does that info mean?

I have found this code snippet for transferring tokens from one wallet to another, but I am not sure about what things are here and where do I get them from, like token account, owner, etc.

I think this code snippet is for token owners, but I not the token owner, i have to token in my wallet I just want to transfer it to another wallet

from spl.token.constants import TOKEN_PROGRAM_ID
from spl.token.instructions import transfer_checked, TransferCheckedParams

from solana.rpc.commitment import Confirmed
from solana.rpc.api import Client
from solana.rpc.types import TxOpts
from solana.keypair import Keypair
from solana.publickey import PublicKey
from solana.transaction import Transaction


from_token_account = PublicKey("Brnvzh...bGPED")
to_token_account = PublicKey("6Uij3...Ahmtp")
from_wallet_address = PublicKey("rjPKeL...wedQp")
mint_public_id = PublicKey("4qYnL....Pt4taGk")
SECRET_KEY = bytes([43,124,...,3,226,229,189]) #from the account you are sending from. AKA owner account. You will find this in id.json 

transaction = Transaction()
transaction.add(
    transfer_checked(
        TransferCheckedParams(
            TOKEN_PROGRAM_ID, #DON'T WORRY ABOUT THIS! DON'T TOUCH IT!
            from_token_account, #Its not your wallet address! Its the token account address!
            mint_public_id, # token address 
            to_token_account, # to the receiving token account.
            from_wallet_address, # wallet address connected to the from_token_account. needs to have SOL
            1, #amount of tokens to send.
            9, #default decimal places. Don't touch in it most cases
            [] #default. Don't touch it in most cases

        )
    )
)
client = Client(endpoint="https://api.devnet.solana.com", commitment=Confirmed) #devnet you can change it to the main net if you want
owner = Keypair.from_secret_key(SECRET_KEY) # <-- need the keypair for the token owner here! [20,103,349, ... 230,239,239]
client.send_transaction(
    transaction, owner, opts=TxOpts(skip_confirmation=False, preflight_commitment=Confirmed)) #don't touch it in most cases.
1

There are 1 answers

1
Joseph DKE On

To send tokens on Solana using python, kindly refer to this. The code snippet you're using is from an older version of solana py.

prerequisites: pip install solana

Example code:

from solana.rpc.api import Client
from spl.token.client import Token
from solders.pubkey import Pubkey
from solders.keypair import Keypair

mint = Pubkey.from_string("<token_address>") #eg: https://solscan.io/token/FpekncBMe3Vsi1LMkh6zbNq8pdM6xEbNiFsJBRcPbMDQ**
program_id = Pubkey.from_string("<program_id>") #eg: https://solscan.io/account/**TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA**

privkey='<wallet_private_key>'
key_pair = Keypair.from_base58_string(privkey)

solana_client = Client("https://api.mainnet-beta.solana.com")
spl_client = Token(conn=solana_client, pubkey=mint, program_id=program_id, payer=key_pair)

source = Pubkey.from_string('<wallet_address>')
dest = Pubkey.from_string('<wallet_address>')

try:
    source_token_account = spl_client.get_accounts_by_owner(owner=source, commitment=None, encoding='base64').value[0].pubkey
except:
    source_token_account = spl_client.create_associated_token_account(owner=source, skip_confirmation=False, recent_blockhash=None)
try:
    dest_token_account = spl_client.get_accounts_by_owner(owner=dest, commitment=None, encoding='base64').value[0].pubkey
except:
    dest_token_account = spl_client.create_associated_token_account(owner=dest, skip_confirmation=False, recent_blockhash=None)

amount = <actual_amount_in_float>

transaction = spl_client.transfer(source=source_token_account, dest=dest_token_account, owner=key_pair, amount=int(float(amount)*1000000000), multi_signers=None, opts=None, recent_blockhash=None)
print(transaction)