Tutorials
No Results
Tutorial Image

Beginner · 15 minutes or less

Creating a Python Transaction with the PureStake API

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

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 a couple of ways:

  1. You’ll need to set the token as a dict
  2. Later, you will explicitly set the ‘content-type’ for the transaction POST

To do that, enter the following:

from algosdk import encoding
from algosdk import transaction
from algosdk import kmd
from algosdk import algod
from algosdk import account
from algosdk import mnemonic
import json

algod_token = 'YOUR API KEY HERE'
algod_address = 'https://testnet-algorand.api.purestake.io/ps1'
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.

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:

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

4. 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()
gen = params["genesisID"]
gh = params["genesishashb64"]
first_valid_round = params["lastRound"]
last_valid_round = first_valid_round + 1000
fee = params["fee"]
send_amount = 1
existing_account = account_public_key
send_to_address = 'U2VHSZL3LNGATL3IBCXFCPBTYSXYZBW2J4OGMPLTA4NA2CB4PR7AW7C77E'

5. 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)
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:

# Function from Algorand Inc. - utility for waiting on a transaction confirmation
def wait_for_confirmation( algodclient, txid ):
   last_round = algodclient.status().get('lastRound')
   while True:
       txinfo = algodclient.pending_transaction_info(txid)
       if txinfo.get('round') and txinfo.get('round') > 0:
          print("Transaction {} confirmed in round {}.".format(txid, txinfo.get('round')))
          break
       else:
          print("Waiting for confirmation...")
          last_round += 1
          algodclient.status_after_block(last_round)

7. Submit and Verify Transaction

Lasty send in the signed transaction to the blockchain. Note the inclusion of a custom headers dict holding the ‘content-type’ of ‘application/x-binary’.

try:
   tx_confirm = algodclient.send_transaction(signed_tx, headers={'content-type': 'application/x-binary'})
   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:

Waiting for confirmation...
Transaction YFMPNMI773YHBM765ZZLA5PMBMQBRZNB3MCUFSYVRVGUW3DCZ6PQ confirmed in round 6007773.

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.

from algosdk import encoding
from algosdk import transaction
from algosdk import kmd
from algosdk import algod
from algosdk import account
from algosdk import mnemonic
import json


# Setup HTTP client w/guest key provided by PureStake
algod_token = 'YOUR API KEY HERE'
algod_address = 'https://testnet-algorand.api.purestake.io/ps1'
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()
gen = params["genesisID"]
gh = params["genesishashb64"]
first_valid_round = params["lastRound"]
last_valid_round = first_valid_round + 1000
fee = params["fee"]
send_amount = 1

existing_account = account_public_key
send_to_address = 'U2VHSZL3LNGATL3IBCXFCPBTYSXYZBW2J4OGMPLTA4NA2CB4PR7AW7C77E'

# Create and sign transaction
tx = transaction.PaymentTxn(existing_account, fee, first_valid_round, last_valid_round, gh, send_to_address, send_amount)
signed_tx = tx.sign(account_private_key)

# Function from Algorand Inc. - utility for waiting on a transaction confirmation
def wait_for_confirmation( algodclient, txid ):
   last_round = algodclient.status().get('lastRound')
   while True:
       txinfo = algodclient.pending_transaction_info(txid)
       if txinfo.get('round') and txinfo.get('round') > 0:
          print("Transaction {} confirmed in round {}.".format(txid, txinfo.get('round')))
          break
       else:
          print("Waiting for confirmation...")
          last_round += 1
          algodclient.status_after_block(last_round)



try:
   tx_confirm = algodclient.send_transaction(signed_tx, headers={'content-type': 'application/x-binary'})
   wait_for_confirmation(algodclient, txid = signed_tx.transaction.get_txid())
except Exception as e:
   print(e)

python

transactions

PureStake

v1 api

April 12, 2020