DEV Community

Syed Aabis Akhtar
Syed Aabis Akhtar

Posted on • Edited on

How to send Solana transactions 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.7 or above installed, now install the package using the following command:

pip install solathon
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

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  
Enter fullscreen mode Exit fullscreen mode

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
    )
Enter fullscreen mode Exit fullscreen mode

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])
Enter fullscreen mode Exit fullscreen mode

Finally we can send this transaction and print the results:

result = client.send_transaction(transaction)
print("Transaction result: ", result)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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 (2)

Collapse
 
rishiromeo profile image
RishiRomeo

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 :

"swapTransaction": "AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAgQe8AbxiqUr3Gnd9zHy6451eajC0szVPRn0mXxvtmRm3wT+6S5LSu7gLcnukaKR7tzSMJR/i7eDmlT4+IQEeGSwCqtQKNTV15CEwSYdFZJ4KZEJZAxZ2ecwLBanQAOOUPhUDV8n2/BHOC8MNwYTt7aGmp9YEfsDUEjzCzT2JfBa1WBCZYcXkVVeXasPUydbwSY1mMabf/+QBwpNBRnFMz4VqiIQwPFaYOOQvj3g8LZZRn/FZo4L9oAZDjt1D1/LyUdqQjnq7oP/DBgweMBzWxN4mKD5XGu7/TInHUJ9tXh1x6/0xOpg08DCfr+8hFGxQ/55oReQY4gchaLoQMTyz8jrwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMIoxb6EqYL6FhuiBd/HFezg7YQ9YUyWqE/+Q+6zoAzZhhzlbKRc2ha8gp5E48+Q30hhX39PaH+ZvP/2ocY/3m4rvfffjpnSkPtGBViIOAwb6R4eLkyB+JJXlnu272kyhjJclj04kifG7PRApFI4NgwtaE5na/xCEBI572Nvp+FkEedUfqc1K9verCrBuSC3GTDKNqlknKr4vTpiCV14OeAabiFf+q4GE+2h/Y0YYwDXaxDncGus7VZig8AAAAAABBt324ddloZPZy+FGzut5rBy0he1fWzeROoz1hX7/AKkGF2sFnR4JFhrw6ghZbh1+New+DQ/4iLAO1QfycbRLIwYMBgAGAA4IDwAIAgAGDAIAAACghgEAAAAAAA8BBgERDQIDBAjkVblwTk9NAg0LCwkKAgUBBwAGBA8b++h3puG5qaEBoIYBAAAAAADmCwAAAAAAAAEADwMGAAABCQ=="

Is there a way to easily use any of the framework above for it? Any help would be greatly appreciated!

Collapse
 
nancy21dk6q0 profile image
Nancy • Edited

Hello! Have you got answer? I have the same problem. Got serialized transaction and do not know how to send it.