How to build an application using Algorand and PureStake API

Introduction

 
In this article, we will be going to learn how we can use Purestake API instead of Docker Sandbox to run Algorand Node.
 
To learn about what is Algorand here and to learn about Alogrand Python-SDK here
 

What is PureStake? 

 
The PureStake API service makes it simple to get up and running on the Algorand network rapidly. To provide developers simple access to Algorand, the service leverages the current PureStake infrastructure platform.
 
When we create an application, the free service tire gives us access to both the Algorand TestNet and the MainNet. Customers with the most stringent SLA, support, performance, and availability needs can contact us to customize a solution.
 
You can learn more about PureStake here
 

Steps to Create PureStake API Account and Get Your Unique API Key

 
Step 1
 
Register for an account at https://developer.purestake.io/signup. We can use the free tier with unlimited daily TestNet requests, up to 5,000 requests daily MainNet, and 5 requests per second for both TestNet and MainNet.
 
 
 
We will need a valid email address as we will need to verify it as part of the registration process. Scroll down to the Terms and Conditions to enable the "I agree to the Terms and Conditions" checkbox.
 
Note: We will get an email from [email protected] with a registration code that looks like this :
 
 
Note the code you sent and enter that code on the following screen when prompted
 
 
Step 2: Get Your Unique API Key
 
Once logged in, you will be able to access a different API key in your account. 
 
 
 
Here you will see your API key at the top of the screen. You will need to copy this key to access the PureStake API service.
 
Step 3: Set Configuration Values for the PureStake API
 
First, you need to set the default configuration values for the PureStake API. This process differs from the example posted by Algorand in that the token must be set as a dict. 
 
To do that, enter the following:
  1. import json  
  2. from algosdk.v2client import algod  
  3. from algosdk import mnemonic  
  4. from algosdk import transaction  
  5.   
  6. algod_token = 'YOUR API KEY HERE'  
  7. algod_address = 'https://testnet-algorand.api.purestake.io/ps2'  
  8. purestake_token = {'X-Api-key': algod_token}  
  • In algod_tokan you need to write your Purestake API Key
  • In Algod_address you need to write the IP address which API endpoints can be accessed 
Step 4: Recover Account
 
Now you’ll need to recover your account using your mnemonic.
 
Important Note: Mnemonics control your account. Never share them or store them insecurely. The code below is for demonstration purposes only.
  1. mnemonic_phrase = "YOUR MNEMONIC HERE";  
  2. account_private_key = mnemonic.to_private_key(mnemonic_phrase)  
  3. account_public_key = mnemonic.to_public_key(mnemonic_phrase)  
  •  In mnemonic_phrase you need to write your Account Passphrase

Checking Algorand Wallet Balance

  1. account_info = algod_client.account_info(account_public_key)    
  2. print("Account balance: {} microAlgos".format(account_info.get('amount')))  
  • We can get the account information of a wallet by using the command account_info. It will result in a JSON output.
Since only need to print the amount value, we will use get('amount'). Output will be
  1. #Account Balance  
  2. Account balance: 1997000 microAlgos    
Step 5: Create the Client
 
To instantiate the client, enter:
  1. algodclient = algod.AlgodClient(algod_token, algod_address, headers=purestake_token)  
Step 6: Get and Set Transaction Parameters
 
Get the transaction parameters from the blockchain using the client object and set the amount and destination address.
  1. params = algodclient.suggested_params()  
  2.   
  3. gh = params.gh  
  4. first_valid_round = params.first  
  5. last_valid_round = params.last  
  6. fee = params.min_fee  
  7. send_amount = 10
  8. note = "Hello World".encode()  
  9.   
  10. existing_account = account_public_key  
  11. send_to_address = 'ICO3XHZSIZTQ5N32VHZ6ORF3QG7QFJNHTHNECCYB4KNUKT725GP5NLQGUU'  
  • I have kept flat_fee to be the minimum possible.
  • I have kept the transaction fee and amount to be sent as 1000 microAlgos
  • I have kept the message to be sent with the transaction as "Hello World", and have encoded it before transmitting it.
You can change all values according to your need.
 
Step 7: Create and Sign Transaction
 
Create a new transaction using the parameters defined above and sign it.
  1. tx = transaction.PaymentTxn(existing_account, fee, first_valid_round, last_valid_round, gh, send_to_address, send_amount, flat_fee=True)  
  2. signed_tx = tx.sign(account_private_key)  
Step 8: Include Verification Method
 
After the transaction is completed and sent, this method is called to verify that the transaction is included in the blockchain. 
  1. # Function from Algorand Inc. - utility for waiting on a transaction confirmation  
  2. def wait_for_confirmation(client, txid):  
  3.     last_round = client.status().get('last-round')  
  4.     txinfo = client.pending_transaction_info(txid)  
  5.     while not (txinfo.get('confirmed-round'and txinfo.get('confirmed-round') > 0):  
  6.         print('Waiting for confirmation')  
  7.         last_round += 1  
  8.         client.status_after_block(last_round)  
  9.         txinfo = client.pending_transaction_info(txid)  
  10.     print('Transaction confirmed in round', txinfo.get('confirmed-round'))  
  11.     return txinfo  
Step 9: Submit and Verify Transaction
 
Finally, the signed transaction is sent to the blockchain. 
  1. try:  
  2.     tx_confirm = algodclient.send_transaction(signed_tx)  
  3.     print('Transaction sent with ID', signed_tx.transaction.get_txid())  
  4.     wait_for_confirmation(algodclient, txid=signed_tx.transaction.get_txid())  
  5. except Exception as e:  
  6.     print(e)  
In the above code, we are waiting for the transaction to get submitted and verified from the testnet. I have kept it under try-except so as to catch any error or exception which may cause hindrance. 
 
You should see a response similar to the following:
  1. Transaction sent with ID UZCV4JQB2NJ7USKXZIG755TQMIU55BUTMFOYRD4QVYWVOT5GOWBQ  
  2. Waiting for confirmation  
  3. Transaction confirmed in round 21443800  
Step 10: Run All of the Code Together
 
If you’d rather just drop in the code in its entirety, copy it below. You will need to enter your API key, mnemonic and other details to work properly.
  1. import json    
  2. from algosdk.v2client import algod    
  3. from algosdk import mnemonic    
  4. from algosdk import transaction    
  5.     
  6. # Function from Algorand Inc.    
  7. def wait_for_confirmation(client, txid):    
  8.     last_round = client.status().get('last-round')    
  9.     txinfo = client.pending_transaction_info(txid)    
  10.     while not (txinfo.get('confirmed-round'and txinfo.get('confirmed-round') > 0):    
  11.         print('Waiting for confirmation')    
  12.         last_round += 1    
  13.         client.status_after_block(last_round)    
  14.         txinfo = client.pending_transaction_info(txid)    
  15.     print('Transaction confirmed in round', txinfo.get('confirmed-round'))    
  16.     return txinfo    
  17.     
  18. # Setup HTTP client w/guest key provided by PureStake    
  19. algod_token = 'YOUR API KEY HERE'    
  20. algod_address = 'https://testnet-algorand.api.purestake.io/ps2'    
  21. purestake_token = {'X-Api-key': algod_token}    
  22.     
  23. # Initalize throw-away account for this example - check that is has funds before running script    
  24. mnemonic_phrase = 'YOUR MNEMONIC HERE';    
  25. account_private_key = mnemonic.to_private_key(mnemonic_phrase)    
  26. account_public_key = mnemonic.to_public_key(mnemonic_phrase)    
  27.     
  28. algodclient = algod.AlgodClient(algod_token, algod_address, headers=purestake_token)    
  29.   
  30. #Account Balance   
  31. account_info = algod_client.account_info(account_public_key)    
  32. print("Account balance: {} microAlgos".format(account_info.get('amount')))  
  33.     
  34. # get suggested parameters from Algod    
  35. params = algodclient.suggested_params()    
  36.     
  37. gh = params.gh    
  38. first_valid_round = params.first    
  39. last_valid_round = params.last    
  40. fee = params.min_fee    
  41. send_amount = 10    
  42.     
  43. existing_account = account_public_key    
  44. send_to_address = 'ICO3XHZSIZTQ5N32VHZ6ORF3QG7QFJNHTHNECCYB4KNUKT725GP5NLQGUU'    
  45.     
  46. # Create and sign transaction    
  47. tx = transaction.PaymentTxn(existing_account, fee, first_valid_round, last_valid_round, gh, send_to_address, send_amount, flat_fee=True)    
  48. signed_tx = tx.sign(account_private_key)    
  49.     
  50. try:    
  51.     tx_confirm = algodclient.send_transaction(signed_tx)    
  52.     print('Transaction sent with ID', signed_tx.transaction.get_txid())    
  53.     wait_for_confirmation(algodclient, txid=signed_tx.transaction.get_txid())    
  54. except Exception as e:    
  55.     print(e)    

Conclusion

 
Thank you for reading the article, hope you got to know about PureStake Algorand, and how we can use PureStake Algorand Python SDK to create a simple application.
 
We will continue the Algorand series.