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:
- import json
- from algosdk.v2client import algod
- from algosdk import mnemonic
- from algosdk import transaction
-
- algod_token = 'YOUR API KEY HERE'
- algod_address = 'https://testnet-algorand.api.purestake.io/ps2'
- 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.
- mnemonic_phrase = "YOUR MNEMONIC HERE";
- account_private_key = mnemonic.to_private_key(mnemonic_phrase)
- account_public_key = mnemonic.to_public_key(mnemonic_phrase)
- In mnemonic_phrase you need to write your Account Passphrase
Checking Algorand Wallet Balance
- account_info = algod_client.account_info(account_public_key)
- 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
-
- Account balance: 1997000 microAlgos
Step 5: Create the Client
To instantiate the client, enter:
- 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.
- params = algodclient.suggested_params()
-
- gh = params.gh
- first_valid_round = params.first
- last_valid_round = params.last
- fee = params.min_fee
- send_amount = 10
- note = "Hello World".encode()
-
- existing_account = account_public_key
- 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.
- tx = transaction.PaymentTxn(existing_account, fee, first_valid_round, last_valid_round, gh, send_to_address, send_amount, flat_fee=True)
- 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.
-
- def wait_for_confirmation(client, txid):
- last_round = client.status().get('last-round')
- txinfo = client.pending_transaction_info(txid)
- while not (txinfo.get('confirmed-round') and txinfo.get('confirmed-round') > 0):
- print('Waiting for confirmation')
- last_round += 1
- client.status_after_block(last_round)
- txinfo = client.pending_transaction_info(txid)
- print('Transaction confirmed in round', txinfo.get('confirmed-round'))
- return txinfo
Step 9: Submit and Verify Transaction
Finally, the signed transaction is sent to the blockchain.
- try:
- tx_confirm = algodclient.send_transaction(signed_tx)
- print('Transaction sent with ID', signed_tx.transaction.get_txid())
- wait_for_confirmation(algodclient, txid=signed_tx.transaction.get_txid())
- except Exception as e:
- 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:
- Transaction sent with ID UZCV4JQB2NJ7USKXZIG755TQMIU55BUTMFOYRD4QVYWVOT5GOWBQ
- Waiting for confirmation
- 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.
- import json
- from algosdk.v2client import algod
- from algosdk import mnemonic
- from algosdk import transaction
-
-
- def wait_for_confirmation(client, txid):
- last_round = client.status().get('last-round')
- txinfo = client.pending_transaction_info(txid)
- while not (txinfo.get('confirmed-round') and txinfo.get('confirmed-round') > 0):
- print('Waiting for confirmation')
- last_round += 1
- client.status_after_block(last_round)
- txinfo = client.pending_transaction_info(txid)
- print('Transaction confirmed in round', txinfo.get('confirmed-round'))
- return txinfo
-
-
- algod_token = 'YOUR API KEY HERE'
- algod_address = 'https://testnet-algorand.api.purestake.io/ps2'
- purestake_token = {'X-Api-key': algod_token}
-
-
- mnemonic_phrase = 'YOUR MNEMONIC HERE';
- account_private_key = mnemonic.to_private_key(mnemonic_phrase)
- account_public_key = mnemonic.to_public_key(mnemonic_phrase)
-
- algodclient = algod.AlgodClient(algod_token, algod_address, headers=purestake_token)
-
-
- account_info = algod_client.account_info(account_public_key)
- print("Account balance: {} microAlgos".format(account_info.get('amount')))
-
-
- params = algodclient.suggested_params()
-
- gh = params.gh
- first_valid_round = params.first
- last_valid_round = params.last
- fee = params.min_fee
- send_amount = 10
-
- existing_account = account_public_key
- send_to_address = 'ICO3XHZSIZTQ5N32VHZ6ORF3QG7QFJNHTHNECCYB4KNUKT725GP5NLQGUU'
-
-
- tx = transaction.PaymentTxn(existing_account, fee, first_valid_round, last_valid_round, gh, send_to_address, send_amount, flat_fee=True)
- signed_tx = tx.sign(account_private_key)
-
- try:
- tx_confirm = algodclient.send_transaction(signed_tx)
- print('Transaction sent with ID', signed_tx.transaction.get_txid())
- wait_for_confirmation(algodclient, txid=signed_tx.transaction.get_txid())
- except Exception as e:
- 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.