An Interest In:
Web News this Week
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
- March 15, 2024
- March 14, 2024
How to send Solana transaction using Python
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.6 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 transferfrom solathon import Client, Transaction, PublicKey, Keypairclient = 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 instruction 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 transferfrom solathon import Client, Transaction, PublicKey, Keypairclient = 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 documentation for sending a transaction: https://solathon.vercel.app/clients/client#send_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!
Original Link: https://dev.to/0xbolt/how-to-send-solana-transaction-using-python-1dii
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To