Skip to content

Payment Transaction

← Back to Transactions

This example demonstrates how to send ALGO between accounts using the transact package. It shows the low-level transaction construction pattern with:

  • Transaction wrapper with PaymentTransactionFields for receiver and amount
  • TransactionType.Payment for the transaction type
  • assign_fee() to set transaction fee from suggested params
  • Manual signing and submission Prerequisites:
  • LocalNet running (via algokit localnet start)

From the repository’s examples directory:

Terminal window
cd examples
uv run python transact/01_payment_transaction.py

View source on GitHub

01_payment_transaction.py
# ruff: noqa: N999, C901, PLR0912, PLR0915, PLR2004
"""
Example: Payment Transaction
This example demonstrates how to send ALGO between accounts using the transact package.
It shows the low-level transaction construction pattern with:
- Transaction wrapper with PaymentTransactionFields for receiver and amount
- TransactionType.Payment for the transaction type
- assign_fee() to set transaction fee from suggested params
- Manual signing and submission
Prerequisites:
- LocalNet running (via `algokit localnet start`)
"""
from shared import (
create_algod_client,
format_algo,
print_error,
print_header,
print_info,
print_step,
print_success,
shorten_address,
wait_for_confirmation,
)
from algokit_transact import PaymentTransactionFields, Transaction, TransactionType, assign_fee
from algokit_utils import AlgorandClient
def main() -> None:
print_header("Payment Transaction Example")
# Step 1: Initialize clients
print_step(1, "Initialize Algod Client")
algod = create_algod_client()
algorand = AlgorandClient.default_localnet()
try:
algod.status()
print_info("Connected to LocalNet Algod")
except Exception as e:
print_error(f"Failed to connect to LocalNet: {e}")
print_info("Make sure LocalNet is running (e.g., algokit localnet start)")
return
# Step 2: Get a funded account from KMD (sender)
print_step(2, "Get Funded Account from KMD")
sender = algorand.account.localnet_dispenser()
sender_info = algorand.account.get_information(sender.addr)
print_info(f"Sender address: {shorten_address(sender.addr)}")
print_info(f"Sender balance: {format_algo(sender_info.amount)}")
# Step 3: Generate a new receiver account
print_step(3, "Generate Receiver Account")
receiver = algorand.account.random()
print_info(f"Receiver address: {shorten_address(receiver.addr)}")
# Check initial receiver balance (should be 0)
receiver_info_before = algorand.account.get_information(receiver.addr)
receiver_balance_before = receiver_info_before.amount.micro_algo
print_info(f"Receiver initial balance: {format_algo(receiver_balance_before)}")
# Step 4: Get suggested transaction parameters
print_step(4, "Get Suggested Transaction Parameters")
sp = algod.suggested_params()
print_info(f"First valid round: {sp.first_valid}")
print_info(f"Last valid round: {sp.last_valid}")
print_info(f"Min fee: {sp.min_fee} microALGO")
# Step 5: Create payment transaction
print_step(5, "Create Payment Transaction")
payment_amount = 1_000_000 # 1 ALGO in microALGO
# Create the transaction with Transaction wrapper and PaymentTransactionFields
transaction_without_fee = Transaction(
transaction_type=TransactionType.Payment,
sender=sender.addr,
first_valid=sp.first_valid,
last_valid=sp.last_valid,
genesis_hash=sp.genesis_hash,
genesis_id=sp.genesis_id,
payment=PaymentTransactionFields(
receiver=receiver.addr,
amount=payment_amount,
),
)
print_info(f"Transaction type: {transaction_without_fee.transaction_type}")
print_info(f"Amount: {format_algo(payment_amount)}")
print_info(f"Receiver: {shorten_address(receiver.addr)}")
# Step 6: Assign fee using suggested params
print_step(6, "Assign Transaction Fee")
transaction = assign_fee(
transaction_without_fee,
fee_per_byte=sp.fee,
min_fee=sp.min_fee,
)
print_info(f"Assigned fee: {transaction.fee} microALGO")
# Step 7: Sign the transaction
print_step(7, "Sign Transaction")
signed_txns = sender.signer([transaction], [0])
tx_id = transaction.tx_id()
print_info(f"Transaction ID: {tx_id}")
print_info("Transaction signed successfully")
# Step 8: Submit transaction and wait for confirmation
print_step(8, "Submit Transaction and Wait for Confirmation")
algod.send_raw_transaction(signed_txns[0])
print_info("Transaction submitted to network")
# Wait for confirmation using the utility function
pending_info = wait_for_confirmation(algod, tx_id)
confirmed_round = pending_info.confirmed_round
print_info(f"Transaction confirmed in round: {confirmed_round}")
# Step 9: Verify receiver balance increased
print_step(9, "Verify Receiver Balance")
receiver_info_after = algorand.account.get_information(receiver.addr)
receiver_balance_after = receiver_info_after.amount.micro_algo
print_info(f"Receiver balance after: {format_algo(receiver_balance_after)}")
balance_increase = receiver_balance_after - receiver_balance_before
print_info(f"Balance increase: {format_algo(balance_increase)}")
# Verify the balance increased by the sent amount
if balance_increase == payment_amount:
print_success(f"Payment of {format_algo(payment_amount)} completed successfully!")
else:
raise ValueError(f"Expected balance increase of {payment_amount}, but got {balance_increase}")
print_success("Payment transaction example completed!")
if __name__ == "__main__":
main()