Algorand Purestake Transaction

Creating a Python Transaction with the PureStake API

by Francisco Gamundi

Creating a transaction and sending it in to the network programmatically are foundational tasks for working with a blockchain. Using the PureStake API allows a developer full access to the complete Algorand MainNet/Testnet ledgers without running any infrastructure, but does require a few minor changes to the code.

Requirements

Background

Steps

1. Set Configuration Values for the PureStake API

2. Recover Account

3. Create the Client

4. Get and Set Transaction Parameters

5. Create and Sign Transaction

6. Include Verification Method

7. Submit and Verify Transaction

8. Run All of the Code Together

1. Set Configuration Values for the PureStake API

First, you’ll need to set the default configuration values for PureStake’s API. This process varies from the Algorand-published examples in that you’ll need to set the token as a dict.

To do that, enter the following:

1
2
3
4
5
6
7
8
9
10
import json
import time
import base64
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}

2. Recover Account

Now you’ll need to recover your account using your mnemonic.

Important Note: Mnemonics control your account. Never share them or store insecurely. The code below is for demonstration purposes only.

1
2
3
mnemonic_phrase = "YOUR MNEMONIC HERE";
account_private_key = mnemonic.to_private_key(mnemonic_phrase)
account_public_key = mnemonic.to_public_key(mnemonic_phrase)

3. Create the Client

To instantiate the client, enter:

1
algodclient = algod.AlgodClient(algod_token, algod_address, headers=purestake_token)

With the escrow stateless smart contract, we complete all of the PyTeal code that is part of the NFTMarketplace application. This code will run on the Algorand blockchain. The only thing that is left for us to do is to implement the communication with the contracts.

4. Get and Set Transaction Parameters

Get the transaction parameters from the blockchain using the client object and set the amount and destination address.

1
2
3
4
5
6
7
8
9
10
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 = 'AEC4WDHXCDF4B5LBNXXRTB3IJTVJSWUZ4VJ4THPU2QGRJGTA3MIDFN3CQA'

5. Create and Sign Transaction

Create a new transaction using the parameters defined above and sign it.

1
2
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)

6. Include Verification Method

After the transaction is complete and sent in, this method is called to verify that the transaction has been included in the blockchain:

1
2
3
4
5
6
7
8
9
10
11
# Function from Algorand Inc. - utility for waiting on a transaction confirmation
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

7. Submit and Verify Transaction

Lasty send in the signed transaction to the blockchain.

1
2
3
4
5
6
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)

You should see a response similar to this one:

1
2
3
Transaction sent with ID E6DNGM6TMV4JRUASPF3Z6WZG3R7WWF7BUW63RRBFVVPDGA5VVRQQ
Waiting for confirmation
Transaction confirmed in round 10461447

8. Run All of the Code Together

If you’d rather just drop in the code in its entirety, copy below. You’ll need to enter your API key, your mnemonic, and some other specifics in order for it to function properly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import json
import time
import base64
from algosdk.v2client import algod
from algosdk import mnemonic
from algosdk import transaction

# Function from Algorand Inc.
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

# Setup HTTP client w/guest key provided by PureStake
algod_token = 'YOUR API KEY HERE'
algod_address = 'https://testnet-algorand.api.purestake.io/ps2'
purestake_token = {'X-Api-key': algod_token}

# Initalize throw-away account for this example - check that is has funds before running script
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)

# get suggested parameters from Algod
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 = 'AEC4WDHXCDF4B5LBNXXRTB3IJTVJSWUZ4VJ4THPU2QGRJGTA3MIDFN3CQA'

# Create and sign transaction
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)

Reference: https://developer.algorand.org/tutorials/creating-python-transaction-purestake-api/?from_query=purestake#1-set-configuration-values-for-the-purestake-api