I'm currently on a project that makes me need to do a swap transaction between SOL and a specific token (specifying the token's address) through raydium, and we are using python to build this interface. Would it be possible to perform this swap operation using Python code? Do you have any example code, SDK or lib available for this?

  • I am also trying to make swap in python and js web3 node and rust Commented Jan 31 at 22:30

2 Answers 2


Basic Steps:

  1. Import pool keys from here.
  2. Create instruction with imported pool keys and get Associated token accounts or the token account or create a wrapped solana account.
  3. Create close WSOL acocunt instructions
  4. Create a transaction e.g. txn = Transaction()
  5. Pass the instructions
  6. Sign the txn
  7. SendTransaction and confirm

More information about the swap can be found in raydium sdk which is in Typescript or you can read through my code for raydium swap but its not well organized or so called clean code.

Main issues with raydium swap from my experience, if you are trying to retrieve pool keys simultaneously in multiple threads for multiple coins, the api.raydium.io will rate limit you and it takes long time to get the keys due to which transaction execution time is to infinity and beyond.

  • Thanks friend! I will try your code! I noticed you are building a sniping bot, that looks interesting. Do you have any means of contact like Telegram that you can share? I'm also a programmer and I have some good contacts, I believe we can think about some degen things together Commented Dec 28, 2023 at 12:57
  • check my profiles :)
    – kokiez
    Commented Dec 28, 2023 at 13:21
  • Please fix the capitalization errors, otherwise looks good
    – Ahri
    Commented Dec 28, 2023 at 20:31

To perform a swap transaction between SOL and a specific token (specifying the token's address) through Raydium using Python, you can use the Solana Python SDK. Below is an example code snippet to help you get started:

from solana.rpc.api import Client
from solana.keypair import Keypair
from solana.publickey import PublicKey
from solana.transaction import Transaction, TransactionInstruction
from solana.system_program import TransferParams, transfer
from solana.utils.helpers import decode_byte_string
from solana.token.async_instructions import transfer as async_transfer
from solana.token.async_client import AsyncToken
from solana.token.async_utils import get_associated_token_address
from solana.token.async_instructions.initialize_account import InitializeAccountParams
from solana.token.async_instructions.mint_to import MintTo

async def swap_on_raydium(wallet_keypair, token_mint_address, token_amount):
    async with AsyncClient('https://api.devnet.solana.com') as client:
        async with AsyncToken(client) as token_client:
            token_account_address = await get_associated_token_address(

            # Check if the token account exists, if not create it
                token_account_info = await token_client.get_account_info(token_account_address)
                create_account_ix = InitializeAccountParams(
                txn = Transaction()
                            lamports=await token_client.get_min_balance_rent_exemption(
                await client.send_transaction(txn, wallet_keypair)

            # Get the token account balance
            token_account_balance = token_client.parse_token_account_info(

            if token_account_balance < token_amount:
                raise Exception("Insufficient token balance")

            # Swap SOL for the specified token
            swap_ix = async_transfer(
            txn = Transaction()

            # Sign and send the transaction
            await client.send_transaction(txn, wallet_keypair)

            return txn.signatures

# Example usage
wallet_keypair = Keypair.from_seed(b'your_wallet_seed_bytes')
token_mint_address = PublicKey('your_token_mint_address')
token_amount = 1000000  # 1 SOL in lamports
swap_tx_signatures = await swap_on_raydium(wallet_keypair, token_mint_address, token_amount)
print("Swap transaction signatures:", swap_tx_signatures)

Replace your_wallet_seed_bytes with your wallet's seed bytes and your_token_mint_address with the token's mint address. Make sure to install the required packages:

pip install solana

This example code uses the Solana Python SDK to perform a swap on Raydium. It checks if the user's token account exists and creates it if necessary, then transfers the specified amount of SOL to the token account.

Keep in mind that you need to have enough SOL in your wallet to cover the swap transaction fees.

  • This code dont work Commented Jan 9 at 13:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.