[Project: Blockchain] Algorand Tutorial 2: Create PureStake Account and Python Transaction

Desc:

Algorand Blockchain using Python and PureStake API service for querying account balance and sending algo from one account to another. On TESTNET.

Inspired by original tutorial here: https://developer.algorand.org/tutorials/creating-python-transaction-purestake-api/

and here: https://www.purestake.com/blog/algorand-python-sdk/

and here: https://developer.purestake.io/code-samples

Branch: Main

Tag: –

Repo: https://github.com/csalinasonline/algoPyTxPureStakeApi.git

Forked: –

The Steps:

Install Python 3

Go here and install python 3.9.x for Win, Mac or Linux here: https://www.python.org/downloads/

Install miniconda

Go here and install miniconda 3.9.x for Win, Mac or Linux here: https://docs.conda.io/en/latest/miniconda.html

Setting Python Path

Setting path is tricky.

# Mac, Linux
export PATH=”$PATH:/usr/local/bin/python”

Windows 10 is more complicated.

  • Go into “Run” prompt and enter “sysdm.cpl
  • This should open up the System Properties window. Go to the Advanced tab and click the Environment Variables button.
  • In the System variable window, find the Path variable and click Edit:
  • Position your cursor at the end of the Variable value line and add the path to the python.exe file, preceded with the semicolon character (;).
  • Example:
    • ;C:PythonPython39

Deposit Algo into existing accounts

See create accounts [Project: Blockchain] Algorand Tutorial 1: Create Account

To fund accounts on TestNet go here for free algos (fake monies to work on the algorand blockchain): https://bank.testnet.algorand.network/

*Note these are TestNet Accounts so private and public key are not so important. Only for demonstration purposes ONLY!

Create a PureStake API Account

Register for a free account at https://developer.purestake.io/

Follow Prompts

Go thru all the prompts and save your API key for later reference.

Change to Target Directory

Where you want to put the project directory and its files.

# Mac, Linux and Windows
cd <your target dir>

Clone Repo

Copy the code.

# Mac, Linux and Windows
git clone https://github.com/csalinasonline/algoPyTxPureStakeApi.git

Change to Repo Directory

# Mac, Linux and Windows
cd algoPyTxPureStakeApi

Activating a Virtual Environment

Close current terminal. Change directory to ‘../algoPyTxPureStakeApi ‘

Turn on the sandbox.

# Mac, Linux and Windows
conda create -n algoPyTxPureStakeApi python=3.9
# activate environment
conda activate algoPyTxPureStakeApi

Run Pip

Install dependencies.

# Mac, Linux and Windows
pip install -r requirements.txt

Run program

Finally run the program.

# Mac, Linux and Windows
python main.py

If all went well we should see on the console a transaction of 1 micro algo to another account.

Sending 1 microalgo from 2DH43BSEWVQZ2XSVKWFMS5SGFDHV5EBXDSFCQHCODCPXJYIOGL7WEOID6I to OLOCVXHXMOLOV4BN2FHXS23S46UWJIKFDVZJ2WL3KOSX3JFRMTJWYW2OIY...Done.
Sent 1 microalgo in transaction: LQ3Q7OAZA6PPWSV6ANJBISVZIRFTE4STDDKJRCSN6BTO7Q5G7QCQ
Resulting balances
2DH43BSEWVQZ2XSVKWFMS5SGFDHV5EBXDSFCQHCODCPXJYIOGL7WEOID6I: 9992993 microalgo
OLOCVXHXMOLOV4BN2FHXS23S46UWJIKFDVZJ2WL3KOSX3JFRMTJWYW2OIY: 10000007 microalgo

Deactivating the Virtual Environment

Turn off sandbox.

conda deactivate

The Code

# ******************************************************************************
# Project : algoPyTxPureStakeApi
# Filename : main.py
# Author : csali
# Origin Date : 7/11/2021
# License : MIT
# Notes : Algorand Blockchain using Python and PureStake
# API service for querying account balance and sending algo from one account
# to another.
#
# Inspired by original tutorial here:
# https://developer.algorand.org/tutorials/creating-python
# -transaction-purestake-api/
#
# and here: https://www.purestake.com/blog/algorand-python-sdk/
#
# and here: https://developer.purestake.io/code-samples
# ******************************************************************************
from algosdk.v2client import algod
from algosdk import mnemonic
from algosdk import transaction
def main():
"""
Main hook
"""
# fund these 2 accounts with some initial algo via the dispenser:
# https://bank.testnet.algorand.network/
# address a: 2DH43BSEWVQZ2XSVKWFMS5SGFDHV5EBXDSFCQHCODCPXJYIOGL7WEOID6I
# address b: OLOCVXHXMOLOV4BN2FHXS23S46UWJIKFDVZJ2WL3KOSX3JFRMTJWYW2OIY
mnemonic_secret = "swarm transfer border digital gather wage blouse knee frozen cart taxi balance festival helmet radio ill bicycle notice fade hungry stomach shiver kidney abstract bronze"
account_a_private_key = mnemonic.to_private_key(mnemonic_secret)
account_a = mnemonic.to_public_key(mnemonic_secret)
account_b = 'OLOCVXHXMOLOV4BN2FHXS23S46UWJIKFDVZJ2WL3KOSX3JFRMTJWYW2OIY'
# setup client with PureStake key
purestake_key = 'INSERT YOUR PURESTAKE API KEY HERE'
endpoint_address = 'https://testnet-algorand.api.purestake.io/ps2&#39;
purestake_header = {'X-Api-key': algod_token }
algod_client = algod.AlgodClient(algod_token, endpoint_address, headers=purestake_header)
# get suggested parameters
params = algod_client.suggested_params()
gen = params.gen
gh = params.gh
first_valid_round = params.first
last_valid_round = params.last
tx_fee = params.min_fee
send_amount = 1
#
send_to_address = account_b
# create and sign transaction
tx = transaction.PaymentTxn(account_a, tx_fee, first_valid_round, last_valid_round, gh, send_to_address, send_amount, flat_fee=True)
signed_tx = tx.sign(account_a_private_key)
try:
# send the transaction
print("Sending " + str(send_amount) + " microalgo from " + account_a + " to " + account_b + "...", end='', flush=True)
# note that the PureStake api requires the content type for the following call to be set to application/x-binary
tx_confirm = algod_client.send_transaction(signed_tx)
algod_client.status_after_block(first_valid_round + 2) # wait 2 blocks to make sure our tx has been committed
print("Done.")
print("Sent " + str(send_amount) + " microalgo in transaction: " + str(tx_confirm))
print("")
# query resulting balances
result_a = algod_client.account_info(account_a)
result_b = algod_client.account_info(account_b)
print("Resulting balances")
print(result_a["address"] + ": " + str(result_a["amount"]) + " microalgo")
print(result_b["address"] + ": " + str(result_b["amount"]) + " microalgo")
except Exception as e:
print(e)
if __name__ == "__main__":
main()
view raw main.py hosted with ❤ by GitHub

About:

Medium: @csalinasonline
LinkedIn: https://www.linkedin.com/in/conrad-salinas-17a29327
Site: https://www.DeepHomebrew.com

Medium Link: https://link.medium.com/0fDwEPCkPhb
Site: https://deephomebrew.com/

Discover more from deepHomeBrew

Subscribe now to keep reading and get access to the full archive.

Continue reading