Skip to content

Transactions

← Back to Examples Overview

Low-level transaction construction and signing.

ExampleDescription
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 class with TransactionType.Payment
  • PaymentTransactionFields for receiver and amount
  • assignFee() to set transaction fee from suggested params
  • generateAddressWithSigners for signing
Payment with Close

This example demonstrates how to close an account by transferring all remaining ALGO to another account using the closeRemainderTo field in PaymentTransactionFields.

Key concepts:

  • closeRemainderTo: Specifies an account to receive all remaining ALGO after the transaction
  • When an account is closed, its balance becomes 0
  • The close-to account receives: (original balance - sent amount - fee)
Asset Create

This example demonstrates how to create a new Algorand Standard Asset (ASA) using the transact package. It shows the low-level transaction construction pattern with:

  • Transaction class with TransactionType.AssetConfig
  • AssetConfigTransactionFields with all configuration options
  • Retrieving created asset ID from pending transaction info
  • Verifying asset parameters and creator holdings
Asset Transfer

This example demonstrates the full asset transfer flow using the transact package: 1. Create a new Algorand Standard Asset (ASA) 2. Opt-in: receiver sends 0 amount of the asset to themselves 3. Transfer assets from creator to the opted-in receiver 4. Verify receiver’s asset balance after transfer

Uses Transaction class with TransactionType.AssetTransfer and AssetTransferTransactionFields.

Asset Freeze

This example demonstrates how to freeze and unfreeze asset holdings using the transact package: 1. Create an asset with freeze address set 2. Transfer assets to another account 3. Freeze the account’s asset holdings (prevent transfers) 4. Verify frozen account cannot transfer 5. Unfreeze the account’s asset holdings 6. Verify account can transfer after unfreeze

Uses Transaction class with TransactionType.AssetFreeze and AssetFreezeTransactionFields.

Asset Clawback

This example demonstrates how to clawback assets from an account using the clawback address and the transact package: 1. Create an asset with clawback address set 2. Transfer assets to a target account 3. Clawback assets from target account using assetSender field 4. Verify target account balance decreased 5. Verify clawback receiver received the assets

Uses Transaction class with TransactionType.AssetTransfer and the assetSender field in AssetTransferTransactionFields for clawback operations.

Atomic Transaction Group

This example demonstrates how to group multiple transactions atomically. All transactions in a group either succeed together or fail together. It shows:

  • Creating multiple payment transactions
  • Using groupTransactions() to assign a group ID
  • Signing all transactions with the same signer
  • Submitting as a single atomic group
Atomic Swap

This example demonstrates how to perform an atomic swap of ALGO for ASA between two parties. In an atomic swap:

  • Party A sends ASA to Party B
  • Party B sends ALGO to Party A
  • Each party signs ONLY their own transaction
  • Signatures are combined and submitted together
  • Both transfers succeed or both fail (atomicity)

Key difference from regular atomic groups: different parties sign different transactions.

Single Signature

This example demonstrates how to create an ed25519 keypair and sign transactions using the low-level transact package APIs.

Key concepts:

  • Creating a keypair using tweetnacl (ed25519 signature scheme)
  • Using generateAddressWithSigners() to derive an Algorand address from the public key
  • Understanding the relationship between ed25519 public key and Algorand address
  • Signing transactions with a raw ed25519 signer function
Multisig

This example demonstrates how to create and use a 2-of-3 multisig account.

Key concepts:

  • Creating a MultisigAccount with version, threshold, and addresses
  • Deriving the multisig address from the participant addresses
  • Signing transactions with a subset of participants (2 of 3)
  • Demonstrating that insufficient signatures (1 of 3) will fail
Logic Signature

This example demonstrates how to use a logic signature (lsig) to authorize transactions.

Key concepts:

  • Compiling a TEAL program using algod.tealCompile()
  • Creating a LogicSig from compiled program bytes
  • Understanding the logic signature address (derived from program hash)
  • Funding and using a logic signature as a standalone account
  • Creating a delegated logic signature where an account delegates signing to a program

Logic signatures allow transactions to be authorized by a program instead of (or in addition to) a cryptographic signature. This enables smart contracts that can hold and send funds based purely on program logic.

Fee Calculation

This example demonstrates how to estimate transaction size and calculate fees using the transact package:

  • estimateTransactionSize() to get estimated byte size
  • calculateFee() with different fee parameters
  • assignFee() to set fee on transaction
  • How feePerByte, minFee, extraFee, and maxFee work
  • Compare estimated vs actual transaction sizes
Encoding/Decoding

This example demonstrates how to serialize and deserialize transactions using the transact package:

  • encodeTransaction() to get msgpack bytes with TX prefix
  • encodeTransactionRaw() to get msgpack bytes without prefix
  • decodeTransaction() to reconstruct transaction from bytes
  • encodeSignedTransaction() and decodeSignedTransaction() for signed transactions
  • txId() for calculating transaction ID
Application Call

This example demonstrates how to deploy and interact with a smart contract on Algorand using the transact package:

  • Compile simple approval and clear TEAL programs using algod.tealCompile()
  • Create an app with TransactionType.AppCall and OnApplicationComplete.NoOp
  • Use AppCallTransactionFields with approvalProgram, clearStateProgram,

globalStateSchema, and localStateSchema

  • Retrieve the created app ID from pending transaction info
  • Call the app with application arguments
  • Demonstrate OnApplicationComplete.OptIn for local state
  • Delete the app at the end

Run any example from the repository’s examples directory:

Terminal window
cd examples
npm run example transact/01-*.ts