I have been struggling for nights to get a swap on solana network running in python 3.10.2 installed solana, solders and Raydium but I can't get it to work. this is what I have:
import time
from raydium import buy_token, sell_token
from solana.rpc.api import Client, Keypair
from config import PRIVATE_KEY, WALLET_ADDRESS, RPC_URL, TOKEN_ADDRESS
def execute_trade(token_address, amount, action_type):
solana_client = Client(RPC_URL)
keypair = Keypair.from_base58_string(PRIVATE_KEY)
amount_tokens_bought = 0
try:
if action_type == "buy":
print(f"Buying {amount} SOL worth of tokens...")
amount_tokens_bought = buy_token(
solana_client,
None, # AsyncClient is not used in synchronous context
token_address,
keypair,
amount,
debug=True
)
elif action_type == "sell":
print(f"Selling {amount} tokens...")
amount_tokens_bought = sell_token(
solana_client,
None, # AsyncClient is not used in synchronous context
token_address,
keypair,
amount,
debug=True
)
else:
print(f"Error: Unknown action_type '{action_type}'. Must be 'buy' or 'sell'.")
return None
print(f"{action_type.capitalize()} completed successfully.")
except Exception as e:
print(f"Error: {e}")
return amount_tokens_bought
# Ensure script runs the main logic properly
#if __name__ == "__main__":
# Example usage: Buy 0.001 SOL worth of tokens
# amount_bought = execute_trade(TOKEN_ADDRESS, 0.001, "buy")
# print(f"Amount of tokens bought: {amount_bought}")
# Example usage: Sell 100 tokens
# amount_sold = execute_trade(TOKEN_ADDRESS, 100, "sell")
# print(f"Amount of tokens sold: {amount_sold}")
response is this:
RPC Error: [SendTransactionPreflightFailureMessage { message: "Transaction simulation failed: Error processing Instruction 3: Provided owner is not allowed", data: RpcSimulateTransactionResult(RpcSimulateTransactionResult { err: Some(InstructionError(3, IllegalOwner)), logs: Some(["Program 11111111111111111111111111111111 invoke [1]", "Program 11111111111111111111111111111111 success", "Program 11111111111111111111111111111111 invoke [1]", "Program 11111111111111111111111111111111 success", "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [1]", "Program log: Instruction: InitializeAccount", "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 3442 of 200037 compute units", "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success", "Program ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL invoke [1]", "Program log: Create", "Program ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL consumed 5110 of 196595 compute units", "Program ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL failed: Provided owner is not allowed"]), accounts: None, units_consumed: Some(8852), return_data: None, inner_instructions: None }) }]... Retrying..
config file: RPC_URL = "https://api.mainnet-beta.solana.com"
TOKEN_ADDRESS = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" # USCD
PRIVATE_KEY = "Is set correct" WALLET_ADDRESS = "Is set correct"
Any help is very much appreciated to get a swap running.
Paul.