Algorand Python Code Along Series: Beginner Friendly Part — I

Write your first Algorand transaction in Python using v2 client: Code walkthrough

Code Walkthrough: Algorand Transaction

Hello everyone, here is the code walkthrough of the Python code to create a transaction using the Algorand v2 client.

Here is the github link to download the code:

Before running the transactions, you need to create the accounts through which the transactions will happen by running the ‘create_accounts.py’ file. Once you create the accounts, make sure to fill up some your accounts with some test algos by using the dispenser(copy paste your account address here).

Dispenser link: https://bank.testnet.algorand.network/

Step 1: Import all the libraries. Since we are using the algod v2 client, we will import the v2client from the sdk.

from algosdk.v2client import algod

from algosdk import kmd, mnemonic

from algosdk.wallet import Wallet

from algosdk.future import transaction

Step 2: In this step, you can need to get your KMD token and KMD address from your local node and create a kmd client. This kmd client will be used to create your wallet and manage the accounts in your wallet.

kmd_token = “//Your local kmd token”

kmd_address = “//Your local kmd address"

kcl = kmd.KMDClient(kmd_token, kmd_address)

Step 3:Get the algod address and the algod token using PureStake API. Using the address and token, we create an algod client for performing transactions.

algod_address = “https://testnet-algorand.api.purestake.io/ps2"

algod_token = “”

headers = { “X-API-Key”: “//Your API Key Here”,}

algod_client = algod.AlgodClient(algod_token, algod_address, headers)

Step 4: Get the suggested parameters from the algod client. We will use these to create a transaction at a later stage.

sp = algod_client.suggested_params()

Step 5: In this step, we will get your wallet and the associated accounts with your wallet that you had created by running the ‘create_accounts.py’ file.

# parameters to the Wallet function: wallet_name, wallet_password, kmd_client

wallet = Wallet(“//Your wallet name here”, “//Your wallet password here”, kcl)

wallet = Wallet(“//Your wallet name here”, “//Your wallet password here”, kcl)

accounts = wallet.list_keys()
# get wallet information

info = wallet.info()print(info)

Step 6: Retrieve your account details and create your transaction using the suggested parameters from the algod client that we retrieved in Step 4.

sender_address = accounts[0]

print(“Sender account:”, sender_address)

receiver_address = accounts[1]print(“Sender account:”, receiver_address)

send_amount = 100 #You can keep the amount flexible according to your experiments

txn = transaction.PaymentTxn(sender_address, sp, receiver_address, send_amount)

Step 7: In order to sign the transactions, we need to retrieve the private key of the sender account.

walletid = wallet.id

wallethandle = kcl.init_wallet_handle(walletid, “// Your wallet password”)

accountkey = kcl.export_key(wallethandle, “// Your wallet password”, accounts[0])

mn = mnemonic.from_private_key(accountkey)

sk = mnemonic.to_private_key(mn)

Step 8: Sign the transaction using the above generated secret key and send the transaction.

signed_tx = txn.sign(sk)

try:

tx_confirm = algod_client.send_transaction(signed_tx, headers={‘content-type’: ‘application/x-binary’})

print(tx_confirm)

except Exception as e:

print(e)

If the transaction is successful, you will get the transaction id printed on your screen. You can copy paste the transaction address on the algoexplorer and verify the details of your transaction.

P.S: I ran all of my transactions on the testnet. Make sure that your local node is set to testnet as well as your algoexplorer is set to testnet while verifying your transaction.

Happy Coding!

For more information on Algorand, please visit the developer docs at: https://developer.algorand.org/docs/reference/sdks/#python

You can also connect with me on educationwithgyan42@gmail.com if you would like to chat more or connect with me regarding any queries.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store