Day-4 - Getting started with Python using Algorand : Creating first transaction on Algorand.
Full tutorial code for previous lesson is available on my Github.
Previously On:
| DAY 1 | DAY 2 | Day 3 |
|---|---|---|
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?
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:
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 importaccount,encoding,algod,mnemonic,kmd, andwalletfromalgosdk.Line 2, we importtransactionfromalgosdk.future.
For this section, we only need theaccountandalgodclasses to complete the transaction. The rest will be used for different purposes which I will explain later.Line 6: variablealgod_addressholds 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: variablealgod_tokenis a placeholder for an access key(gotten from Purestake) required for the API to pass through.Line 8: variablemyProject_tokenholding 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: variablealgod_clientis 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_keyandaddressare passed into a function calledgenerate_accountwhich is a method of the classaccount. The.sign is used for accessing methods/functions in a class. A wallet is created for us by invoking theaccount.generate_accountmethod and finally we print out the private and address to the wallet onlines14and15.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
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 - " ".
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.
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.
Check if a key exist
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.
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..`
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.
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
| Medium | |
|---|---|
Leave Day-4 - Getting started with Python using Algorand : Creating first transaction on Algorand. to:
Read more #algorand posts
Best Posts From Bob-elr
We have not curated any of bob-elr's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From Bob-elr
- Overdraft - Smart Contract Example Using Pyteal: Algorand's Sophisticated Use Case
- Getting Along With Python : For and while loops, *Args. **kwargs and Order of Code execution - a treat With Using ALgorand Python SDk -
- Getting Started With Python Using Algorand Python SDK - Day 8
- GSWPUA-Day-7: Multisig account concept with example and use case, how to create a transaction group and likely error with description
- GSWPUAlgorand Day-6: Code Refactoring, Creating Modules In Python, Transfer Algo Between Three Accounts Simultaneously And Checking Account Statuse.
- Day-5 - Getting started with Python using Algorand: Private Key/Mnemonic Phrase Relationship, keyword explained
- Day-4 - Getting started with Python using Algorand : Creating first transaction on Algorand.
- Getting started with Python using Algorand - Day 3
- Getting started with Python - Day 2
- Getting started with Python using Algorand - Day 1: set up environment installing SDKs
- Create Decentralized Identity with FlexfinTx - Algorand's Use Case.
- BLOCKCHAIN TRILEMMA : ALGORAND'S UNTAPPED SOLUTION
- Morning is the best time to study for me. It might take time, but I'll be what I ...
- Protecting against Corona virus... Hahaha, lmao funniest I've seen today.
- Onward looking to my first job as a smart contract developer. I never shop learning. ...
- Beautiful like DIAMOND IN THE SKY. Got a new friend here. Celebrating you. Wish you ...
- #throwback back in 2018, miss all my steemit friends. Will forever be grateful to ...
- Object Design: What it really is.
- Hi friends, Been a while. Shoutout to everyone making appics lovely. Here is pics ...
- One of the coolest moments I've had this year is this with a friend. She is super ...