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.
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
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.
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 defwait_for_confirmation(client, txid): last_round = client.status().get('last-round') txinfo = client.pending_transaction_info(txid) whilenot (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 inround10461447
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.
# 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)