Get started with python using Algorand SDK-V1, Day-6: Code Refactoring, Creating Modules In Python, Transfer Algo Between Three Accounts Simultaneously And Checking Account Statuses.
Recall from last post, I explained private, public keys/mnemonic phrase relationship, where I converted from private key to mnemonic seeds, from mnemonic seeds to private key and from mnemonic phrase to public key. Find paths to previous lessons listed below.
What You Will Learn Today
- Code Refactoring
- Creating Modules In Python
- Send ALGO Token From Account A To account B To C.
- Check Account’ Status.
As an upcoming Algorand developer, using Python for writing smart contract , you will find this resource useful as you progress in your programming endeavor/career. What cannot be taken away from you is the fundamental knowledge you will gain and its right application. Before we proceed to another topic, we will need to refactor our previous codes.
What Is Code Refactoring And Why It Is Needed?
In programming or software design, when you refactor codes, you are simply restructuring existing code by changing the pattern or design without corresponding change in the output or behavior of the program or code. The intention is to improve the design or implementation of the program and the original functionality is preserved. It helps in avoiding code redundancy i.e avoiding unnecessary repetition of codes.
Fig.1 Taking this tutorial as an instance, as we create and open different files for different purposes, we will need to rewrite
lines 2 to 10 in fig 1 above every time we need to connect to Algorand testnet/node to submit a transaction. How about creating a function in a module that will contain these codes so we can reuse it each time the need for connection arises? Back in our code editor (VScode), In fig.2 I have created a file/module named
connect.py, in which is contained a class
Connect(), inside the class there is a function named
connectToNetwork() that does the job. Only a function would do but in future, we might need some of the properties contained in the class.
Fig.2 Line 12 returns the connection we need which is
algod.AlgodClient(algod_token, algod_address, myProject_token). What Has Changed!
Nothing much except that we created a class and a function that queries Algorand network and returns the response we need being what is required to use a function in other files.
Notice that I have changed the previous file name from
pythonTutorial.py so you don’t get confused. In
pythonTutorial.py, we imported the module
connect and from it, we are able to access
connectToNetwork() function. So far so good, the behavior of our program has not changed but for the code, therefore, refactoring can be said to have taken place.
Sending Algo Token From Account A To Account B
To send Algo Token from one account to another, we will require two already generated wallets. I have gone ahead to refactor previous codes that generated a wallet address and a private key for us into a function that generates simultaneously two wallets with private keys –
lines 10 to 27in fig.4. For this tutorial, we need a book to record details of the accounts generated hence the
accounts variable of type
line 4. The function
generateAccounts() creates two wallets from
lines 12 and 13, holds each account in variables
accounts_2, then stores them in
accounts. The need for the global variable
accounts is to enable us access it easily from other files/modules.
There is slight redundancy in the above code. As an assignment towards the next lesson, try to refactor it to test your understanding. Leave comment in section below or send me a direct email or contact me on whatsaApp for those already following my tutorials, you have my whatsApp detail.
Now, we need a set up to effect a transaction. I have created a module for this purpose named
sendTransaction.py file – fig.5.
line 1, we imported modules
encodingfrom algorand SDK,
Connectfrom self-created module
- Set a default account from which we will initiate a transfer to
account_1, from there to
account_2, and its private key
private_key_alcto approve Algo transfer.
- Lines 8 to 12 for use in
line 15allowing the network to suggest transaction parameters. Check resources for more information on transaction fields.
- Activate network connection –
txnis a type dictionary encompassing suggested parameters will be returned by the network including ones we defined ==> (
notefields). We will get the defined parameters as at when the function is called – a simple hack for auto-dual transfer between 3 accounts.
trxnprepares and encode the required transaction data. Notice trxn is preceded by double asterisks? In Python, it is a way of taking in arbitrary arguments or object with several properties. It is usually represented in parameter as
signTrxnsigns the transaction with account’s private key. Remember, we are expecting
trxn_idgets transaction ID.
- In the
tryblock, we provide for error that may be encountered while sending transaction.
headers is essential if you are using a third party API service such as Purestake.
- In Fig.5, we execute all other files/modules.
prints the result to the consoleand
prints an empty line.
16prints account information of default account from which we will initiate the first transfer.
19gets address of generated account_1, stores it in
20gets address of generated account_2, stores it in
21stores private key of account_1 since we will send token to account_2.
sendTransaction()initiating the first transfer, and we parsed into it expected arguments –
amount. Next, print the result.
Line 26saves us the time for writing code for waiting for confirmation before we send the second transaction in
line 30. On Algorand, block finality are swift standing between 4 to 8 seconds. We need time to wait for the first transaction to confirm before initiating another transfer from
time.sleep(60). Notice we import time module on the top of the file –
line 4. Argument
sleep()function is inn seconds so we would have enough time for the first transaction to confirm before the next transaction is executed
35prints current status of all accounts in this context. Below you can read from the output. Fig.7
Get Testnet Token
Need to try this out? Get free testnet token from Algorand dispenser after checking the captcha. Fig.8
Special thanks to developers friend on Algorand developers’ forum for support – @Tim, @fabrice @liz @RusselFustino .