In this tutorial, we are going to be seeing how we can send a transaction in the Solana blockchain using Python!
For this, you do not need to know any complexity or what happens behind the scenes. We will be using Solathon, which is an easy to use, feature rich SDK for Solana in Python.
Make sure you have Python 3.7 or above installed, now install the package using the following command:
pip install solathon
First of all we would need to create a client, which we will use to interact with the Solana's JSON RPC. For testing purposes, we will be using devnet as it doesn't require you to have real SOL balance. The following code creates a client on devnet along with all the imports we will be requiring later on:
from solathon.core.instructions import transfer from solathon import Client, Transaction, PublicKey, Keypair client = Client("https://api.devnet.solana.com")
Now, we would need to define three things; first of all, the account which is sending the transaction, second is the account which will receive the transaction, and third is the amount of money we want to send in lamports (which is the standard unit for amounts in transactions on Solana blockchain). Have a look at the code below:
sender = Keypair.from_private_key("you_private_key_here") receiver = PublicKey("receiver_public_key") amount = 100
Here, the sender needs to be a keypair, which is a combination of public key and private key, you would need to get your private key from your wallet in order to initialize your Keypair object. Then we need the public key of the account whom we wish to send the transaction to, finally we have the amount which we want to transfer in lamports. You can use the sol_to_lamport function provided by Solathon for easy conversions.
Now, each transaction requires you to have certain instructions defined which needs to be executed, here we just need one instruction and that is transfer:
instruction = transfer( from_public_key=sender.public_key, to_public_key=receiver, lamports=100 )
Now, we need to initialize our transaction object with our list of instructions and signers (which will be only the sender in this case):
transaction = Transaction(instructions=[instruction], signers=[sender])
Finally we can send this transaction and print the results:
result = client.send_transaction(transaction) print("Transaction result: ", result)
Here is the entire code:
from solathon.core.instructions import transfer from solathon import Client, Transaction, PublicKey, Keypair client = Client("https://api.devnet.solana.com") sender = Keypair.from_private_key("your_private_key") receiver = PublicKey("receiver_public_key") amount = 100 instruction = transfer( from_public_key=sender.public_key, to_public_key=receiver, lamports=100 ) transaction = Transaction(instructions=[instruction], signers=[sender]) result = client.send_transaction(transaction) print("Transaction response: ", result)
You can also check out the official documentation for more details: Solathon docs for sending transaction
How easy it that? Hope so I was able to make things clear and the tutorial was helpful, if there is something which still confuses you or you have any query in general, feel free to ask any questions. See you soon!
Top comments (1)
Hey! Thanks so much for this tutorial! Was wondering if you know how to use this same framework for a Swap transaction out of Jupiter.ag API? So I have a dummy swap txn :
Is there a way to easily use any of the framework above for it? Any help would be greatly appreciated!