Create Publication

We are looking for publications that demonstrate building dApps or smart contracts!
See the full list of Gitcoin bounties that are eligible for rewards.

Tutorial Thumbnail
Beginner · 15 minutes or less

Setting the Transaction Fee with Python

This tutorial is intended to help you understand the difference between using a flat fee versus a suggested fee with any transaction. The sample also shows how to set these fees with Python.

Requirements

Background

All transactions on the Algorand blockchain require a fee, which must be greater than or equal to the minimum transaction fee (currently 1000 microAlgos). The SDKs provide two primary methods to determine the fee for your transaction. One method uses the suggested fee-per-byte and the other allows you to specify a flat fee. See Fees to learn more about how these are calculated and why you may want to use one over the other. This tutorial also creates and submits the transaction. See Your First Transaction for details on transactions.

Steps

1. Connect to Algod and Get Suggested Params

The Rest API provides a method to get the suggested fee per byte of a transaction (suggested_fee). It is important to realize that this number is a fee per byte and not a total fee. For transactions of normal size (1kb) this is currently 1000 microAlgos. For transactions that are larger (note field usage and smart contracts) this fee may be higher than the minimum transaction. You can also set the fee per byte to 0 and the system will automatically set the fee to the current minimum transaction fee. The code below sets up a connection to the algod process, gets the suggested transaction parameters and the suggested transaction fee per byte.

#/usr/bin/python3
import json
import time
import base64
import os
from algosdk import algod
from algosdk import mnemonic
from algosdk import transaction
from algosdk import encoding
from algosdk import account

# utility to connect to node
def connect_to_network():
    algod_address = "http://<your-algod-host>:<your-algod-port>"
    algod_token = "<our-api-token>"
    algod_client = algod.AlgodClient(algod_token, algod_address)
    return algod_client

# group transactions           
def sample() :

    # connect to node
    acl = connect_to_network()

    # get suggested parameters
    params = acl.suggested_params()
    gen = params["genesisID"]
    gh = params["genesishashb64"]
    last_round = params["lastRound"]
    fee = acl.suggested_fee()["fee"]


# run    
sample()

2. Recover Sender Account

Recover the account that is going to submit the transaction. Make sure to use the dispenser to fund the recovered account.

#/usr/bin/python3
import json
import time
import base64
import os
from algosdk import algod
from algosdk import mnemonic
from algosdk import transaction
from algosdk import encoding
from algosdk import account

# utility to connect to node
def connect_to_network():
    algod_address = "http://<your-algod-host>:<your-algod-port>"
    algod_token = "<our-api-token>"
    algod_client = algod.AlgodClient(algod_token, algod_address)
    return algod_client

# group transactions           
def sample() :

    # connect to node
    acl = connect_to_network()

    # get suggested parameters
    params = acl.suggested_params()
    gen = params["genesisID"]
    gh = params["genesishashb64"]
    last_round = params["lastRound"]
    fee = acl.suggested_fee()["fee"]

    # recover a account    
    passphrase = (
    "portion never forward pill lunch organ biology"
    " weird catch curve isolate plug innocent skin grunt"
    " bounce clown mercy hole eagle soul chunk type absorb trim")
    pk_account_a = mnemonic.to_private_key(passphrase)
    account_a = account.address_from_private_key(pk_account_a)  

# run    
sample()


Learn More
- Add Funds using Dispenser

3. Construct Transaction with Fee

The transaction can now be created. The fee can be set to the suggested fee per byte or optionally you can set a flat fee.

Use a suggested fee per byte

#/usr/bin/python3
import json
import time
import base64
import os
from algosdk import algod
from algosdk import mnemonic
from algosdk import transaction
from algosdk import encoding
from algosdk import account

# utility to connect to node
def connect_to_network():
    algod_address = "http://<your-algod-host>:<your-algod-port>"
    algod_token = "<our-api-token>"
    algod_client = algod.AlgodClient(algod_token, algod_address)
    return algod_client

# group transactions           
def sample() :

    # connect to node
    acl = connect_to_network()

    # get suggested parameters
    params = acl.suggested_params()
    gen = params["genesisID"]
    gh = params["genesishashb64"]
    last_round = params["lastRound"]
    fee = acl.suggested_fee()["fee"]

    # recover a account    
    passphrase = (
    "portion never forward pill lunch organ biology"
    " weird catch curve isolate plug innocent skin grunt"
    " bounce clown mercy hole eagle soul chunk type absorb trim")
    pk_account_a = mnemonic.to_private_key(passphrase)
    account_a = account.address_from_private_key(pk_account_a)  
    amount = 10000
    receiver = "GD64YIY3TWGDMCNPP553DZPPR6LDUSFQOIJVFDPPXWEG3FVOJCCDBBHU5A"
    # create transaction
    txn = transaction.PaymentTxn(account_a, fee, last_round, last_round+100, gh, receiver, amount)
# run    
sample()

Using a Flat Fee

To use a flat fee, set the fee to a specific value. You will also need to pass the flat_fee parameter to the PaymentTxn function. flat_fee should be set to True.

    fee = 1000
    amount = 10000
    receiver = "GD64YIY3TWGDMCNPP553DZPPR6LDUSFQOIJVFDPPXWEG3FVOJCCDBBHU5A"
    # create transaction
    txn = transaction.PaymentTxn(account_a, fee, last_round, last_round+100, gh, receiver, amount, flat_fee=True)

4. Sign and Submit Transaction

Sign and submit the create transaction.

#/usr/bin/python3
import json
import time
import base64
import os
from algosdk import algod
from algosdk import mnemonic
from algosdk import transaction
from algosdk import encoding
from algosdk import account

# utility to connect to node
def connect_to_network():
    algod_address = "http://<your-algod-host>:<your-algod-port>"
    algod_token = "<our-api-token>"
    algod_client = algod.AlgodClient(algod_token, algod_address)
    return algod_client

# group transactions           
def sample() :

    # connect to node
    acl = connect_to_network()

    # get suggested parameters
    params = acl.suggested_params()
    gen = params["genesisID"]
    gh = params["genesishashb64"]
    last_round = params["lastRound"]
    fee = acl.suggested_fee()["fee"]

    # recover a account    
    passphrase = (
    "portion never forward pill lunch organ biology"
    " weird catch curve isolate plug innocent skin grunt"
    " bounce clown mercy hole eagle soul chunk type absorb trim")
    pk_account_a = mnemonic.to_private_key(passphrase)
    account_a = account.address_from_private_key(pk_account_a)  
    amount = 10000
    receiver = "GD64YIY3TWGDMCNPP553DZPPR6LDUSFQOIJVFDPPXWEG3FVOJCCDBBHU5A"
    # create transaction
    txn = transaction.PaymentTxn(account_a, fee, last_round, last_round+100, gh, receiver, amount)

    # sign transaction1
    stxn = txn.sign(pk_account_a)
    # send them over network
    sent = acl.send_transaction(stxn)
    # print txid
    print(sent) 

# run    
sample()