Day-4 - Getting started with Python using Algorand : Creating first transaction on Algorand.

in #python5 years ago (edited)
In the past series, we set up our environment to write Python code using Algorand SDKs in VSCode. If you haven't seen, paths to previous series are tabulated below. With this, we can be sure that our code will run successfully if we deploy it on Algorand testnet as we had it set up. As a reminder, these tutorials are to make you a good Algorand developer using Python programming language by giving you in-depth of foundational knowledge with assumption that you have little or no previous idea in programming perhaps Python(taking you from absolute beginner to intermediate). To get the best, please follow all steps diligently and accordingly.

Full tutorial code for previous lesson is available on my Github.

image src


Previously On:


DAY 1DAY 2Day 3
image.pngimage.pngimage.png

Contents

  • Run first transaction on Algorand testnet
  • Others.

Run first transaction on Algorand testnet

While we learn the basis of Python, it is of great advantage if we work real-life examples. We will run our first transaction on Algorand testnet with python code by Creating a wallet i.e generating a private key and a public key (account address) that will be able to hold ALGO Token on the testnet. Remember, in this lessonwe declared some variables to hold the parameters we need to pass into a class's method to established a connection to Algorand testnet using a third party service - Purestake. If you're lost or confused how I arrived at this point, please refer to this article. Aaarrhh! I don't what is testnet. No worries! Here I go...

What Is Testnet?

A testnet mirrors an exact copy of a network or blockchain except that it is not intended for production. That is, it has exactly every attributes of the original called Mainnet but for the tokens which has no real value. Ethereum network has a mainnet and testnet. The Ether token on testnet is worthless having no real value hence cannot be traded. But as the name implies, it is used primarily for testing decentralized applications before deploying on mainnet.


Following this code, we need only the first 7 lines of code. You can comment out the rest code for the purpose of this section.

 1.  from algosdk import account, encoding, algod, mnemonic, kmd, wallet
 2.  from algosdk.future import transaction

 3.  algod_address = "https://testnet-algorand.api.purestake."
 4.  algod_token = "eVXi2wPlDE8uF15mkil5Z2........"
 5.  myProject_token = {"X-API-Key": algod_token}

 6.  algod_client = algod.AlgodClient(algod_token, algod_address, myProject_token) 

Create a new file and name it day_4.py. Copy the above line s of code and paste in the day_4.py file. Add the following as in image:


dy4.JPG

Yay! It worked! We just made our first transaction on Algorand Testnet. Don't get lost, I'll explain in detail what just happened.

  • From line 1, we import account, encoding, algod, mnemonic, kmd, and wallet from algosdk.

  • Line 2, we import transaction from algosdk.future.
    For this section, we only need the account and algod classes to complete the transaction. The rest will be used for different purposes which I will explain later.

  • Line 6: variable algod_address holds an API - URL containing an access . to Purestake with which we will make a request to Algorand testnet. Note that the Algod API is used for querying historical information from the blockchain, getting information about blocks and transactions, and sending transactions.

  • Line 7: variable algod_token is a placeholder for an access key(gotten from Purestake) required for the API to pass through.

  • Line 8: variable myProject_token holding a data in dictionary format which maps the key "X-API-KEY" to the secret key needed as the third argument we need to pass in to the AlgodClient.

  • Line 10: variable algod_client is where the actual connection takes place. It takes in three variables and run a computation which connects us to the testnet.

  • Line 13: variable names - private_key and address are passed into a function called generate_account which is a method of the class account. The . sign is used for accessing methods/functions in a class. A wallet is created for us by invoking the account.generate_accountmethod and finally we print out the private and address to the wallet on lines 14 and 15.

  • In the terminal, you could see the result printed out for us as:

Private key : AV08C3aqF+Ey6KlimV97iOnmOHCbC8OH9fndMTnjL/O1AP+RAm77emGkzR3SWRVqU+k/owg9WT5k9UhXPfjokA==

Address: WUAP7EICN35XUYNEZUO5EWIVNJJ6SP5DBA6VSPTE6VEFOPPY5CILVXG4XM


Dictionary In Python


image.png src

Python dictionary is an unordered collection of items of any data type. Its structure requires a key and a value pair to be displayed as a dictionary while other compound data types have only value as an element. To retrieve a value in a dictionary, a key is expected often enclosed in an inverted comma or quotaion mark - " ".

image.png

Dictionary can accept arbitrary number of data types as depicted above. From the day_4.py file, user_profile is a variable/placeholder for dictionary containing 5 keys pointing to 5 values of types: string, string, integer, list(of 3 items) and a dictionary(of 3 items). A dictionary can be nested more deeper based on the nature and arrangement of data it is meant for. To access an item in a dictionary, you should refer to its key.

Example: name = user_profile["name"]

Or

name = user_profile.get["name"]

A dictionary by default has a get property which can be invoked to extract a value provided the key enclosed in a square bracket.


Changing A Value In Dictionary

To change the value of an item in a dictionary, simply use the placeholder followed by the key to the value you wish to change enclosed in square brackets and assign it a new value of any type.

image.png


Ways to Loop through a dictionary

Print out all the values in a dictionary: To achieve this, we use a for loop to iterate over every items in the dictionary. Passing x and y into the for loop will by default assigns the first variable to the key and second to the value. I will expatiate more on for loop in the next lesson.

image.png


Check if a key exist

image.png

Using the If statement in line 33 tests how true is the statement. Different from an ambiguous statement, python will return either yes or no. Ensure that the key you are trying to search is properly as alteration in the letters for instance, replacing h in "hobbies" with H will evaluate to false.


Line 36 and 37, excepting the keys, prints out all the values in the user_profile dictionary.

image.png

Getting the number of Items in the user_profile dictionary. requires that we use the len() function which is a readily available inbuilt function from python module..`

image.png


Conditions In Python


Conditions in Python evaluate either true or false. It is represented in 1 and 0. Condition is also used to test the genuineness of an expression or existence of a true value, result of which returns true or otherwise in the absence of it.

To keep it short, I will continue on this topic in the next series.



src

Algorand is a technology company that built and developed the world’s first open, permissionless, pure proof-of-stake blockchain protocol that, without forking, provides the necessary security, scalability, and decentralization needed for today’s economy. With an award-winning team, we enable traditional finance and decentralized financial businesses to embrace the world of frictionless finance.


More information about Algorand