Sending solana transactions with python

I want to buy Solana coins and nfts with python and I am not sure, how transactions via the blockchain exactly work. Let’s say I want to do this transaction: and this is my code:

import solana.system_program as sp
from solana.publickey import PublicKey
from solana.account import Account
from solana.rpc.api import Client
from solana.transaction import Transaction, TransactionInstruction, AccountMeta

# keypair = your key pair
cli = Client('')
account = Account(keypair[:32])
new_account = Account()
transaction = Transaction()
send_tx = cli.send_transaction(transaction, new_account)

I know that I don’t have enough solana in my test wallet right now, but it’s more about the general way to send transactions and to interact with program ids. There is some Data shown in the transaction, but I am not sure, if I have to send that too? And if I have to, where exactly and how do I include that? Does the data change for each transaction? I get the error message:

{'code': -32602, 'message': 'invalid transaction: index out of bounds'}


You’re on the right track here. If you want to mimic a similar transaction to the one referenced in Solscan, you’ll need to create the correct instruction, including the encoded data and accounts referenced. This will be a bit difficult without available Python bindings, but going off of the example, it would probably look a little bit like:

transaction = Transaction()
    [AccountMeta(PublicKey("3LdbrFBY7sZ71MtuZhrMzK4YLgxNcTrQ5wLNZHs7r85T"), False, False), AccountMeta(PublicKey("FABkSFDkF3Wz3CpC5JTmw44jeKvXKQvXgXFCyxhAE46X"), False, False), ...],

The important things to note:

  • the first field is a list of AccountMetas, same as in that transaction you linked
  • the second field is the program’s public key
  • the last field is the “instruction data”

You’ll have to also provide a valid signer / payer for the transaction, but it should go though after that!