Solana SPL token transfer with Python

HxKs...4ybK
6 Sept 2022
54

This blog is a step by step guide on how to transfer tokens on Python programmatically. This is particularly useful if you want to automate the way you send your tokens or if you are distributing tokens at scale.

Step 1: install Solana python package

pip install solana

Run this on your terminal in order to install the Solana python package. Using this package allows you transfer tokens and also other operations. You can check out their documentation here.

Step 2: choose your SPL token

from solana.publickey import PublicKey
MINT = PublicKey("<TOKEN_ADDRESS>")

This is used for pointing to your Solana token that you want to send to your destination. You can check our my previous guide on how to create your own token.

Step 3: choose your Solana Cluster

from solana.rpc.api import Client
CLIENT = Client("https://api.mainnet-beta.solana.com")

This is used for picking your Solana cluster. This will set it to the devnet cluster so you won’t spend real Solana. If you are going to transfer tokens over the “real” Solana network, use this cluster instead: https://api.mainnet-beta.solana.com.

Step 4: choose your Solana wallet

from solana.keypair import Keypair
source_path = "<PATH_TO_YOUR_SOLANA_WALLET>"
source_key = eval(open(source_path, "r").readline())
SOURCE = Keypair.from_secret_key(bytes(source_key))

This will regenerate your Solana wallet based on your private key that you stored locally. My previous blog (step 5 and 6) goes through how to generate your Solana wallet. Ensure that your wallet contains some Solana and the token that you want to transfer.

Step 5: create token accounts

def get_or_create_associated_token_account(public_key: PublicKey):
    try:
        return TOKEN.create_associated_token_account(public_key)
    except:
        return get_associated_token_address(public_key, MINT)
# your source account
source_public_key = PublicKey(SOURCE.public_key)
# your destination account
dest_public_key = PublicKey("<DESTINATION_WALLET>")
source_token_account = get_associated_token_address(source_public_key, MINT)
dest_token_account = get_or_create_associated_token_account(dest_public_key)

This code will ensure that both your own and your receiver’s wallet has a token account, in order to store your Solana token. The get_or_create_associated_token_account function is a helper function for you to get or create the token account. If it is the first time transferring to your receiver, you will most likely need to create an account for them and this will cost some SOL (around 0.002 SOL).

Step 6: transfer tokens

from spl.token.client import Token
from spl.token.constants import TOKEN_PROGRAM_ID
import math
TOKEN = Token(conn=CLIENT, pubkey=MINT, program_id=TOKEN_PROGRAM_ID, payer=SOURCE)

# Amount to transfer
amount = 100

TOKEN.transfer(
    source=source_token_account,
    dest=dest_token_account,
    owner=SOURCE,
    amount=int(amount*math.pow(10, 9))
)

This code will transfer tokens from your source token account to your receiver’s token account, based on the addresses you obtained in the previous step. There are a few key things to note:
Since you are transferring the tokens, you will be paying the transaction fees on Solana, however, it is extremely cheap (0.00001 SOL).
You will need to ensure the amount transferred reflects the number of decimals of your token. For example, if your token has 9 decimals, you will need to multiply your amount by 10 ^ 9.
Because of these nuances, you should try sending tokens via the devnet first and see whether the amount is correct.

You have transferred your tokens 🎉

Now that you know how to transfer your tokens, what will you do with it? Comment below and let me know!

Helpful resources:

https://open.bulbapp.io/p/deaa9e9e-ca1a-4fe0-a6ac-aeaadab9f061/how-to-mint-a-crypto-token-on-solana
https://michaelhly.github.io/solana-py/spl/token/instructions/

Write & Read to Earn with BULB.

Join now

Enjoy this blog? Subscribe to Johnson Chau

6 Comments