|
| 1 | +# AlgoKit Utils TypeScript Examples |
| 2 | + |
| 3 | +Runnable code examples demonstrating every major feature of the `@algorandfoundation/algokit-utils` TypeScript library. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This folder contains 116 self-contained examples organized into 9 categories. Each example is a standalone TypeScript file that demonstrates specific functionality, progressing from basic to advanced usage within each category. |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +- Node.js >= 20.0 |
| 12 | +- [AlgoKit LocalNet](https://github.com/algorandfoundation/algokit-cli) running (for network examples) |
| 13 | + |
| 14 | +Some examples (marked "No LocalNet required") work with pure utility functions and don't need a running network. |
| 15 | + |
| 16 | +## Quick Start |
| 17 | + |
| 18 | +```bash |
| 19 | +# From the examples directory |
| 20 | +cd examples |
| 21 | + |
| 22 | +# Run a single example |
| 23 | +npx tsx transact/01-payment-transaction.ts |
| 24 | + |
| 25 | +# Run all examples in a category |
| 26 | +./transact/verify-all.sh |
| 27 | + |
| 28 | +# Run all examples |
| 29 | +./verify-all.sh |
| 30 | +``` |
| 31 | + |
| 32 | +## Examples by Package |
| 33 | + |
| 34 | +### ABI (`abi/`) |
| 35 | + |
| 36 | +ABI type parsing, encoding, and decoding following the ARC-4 specification. |
| 37 | + |
| 38 | +| File | Description | |
| 39 | +|------|-------------| |
| 40 | +| `01-type-parsing.ts` | Parse ABI type strings into type objects with `ABIType.from()` | |
| 41 | +| `02-primitive-types.ts` | Encode/decode uint, bool, and byte types | |
| 42 | +| `03-address-type.ts` | Encode/decode Algorand addresses (32-byte public keys) | |
| 43 | +| `04-string-type.ts` | Encode/decode dynamic strings with length prefix | |
| 44 | +| `05-static-array.ts` | Fixed-length arrays like `byte[32]` and `uint64[3]` | |
| 45 | +| `06-dynamic-array.ts` | Variable-length arrays with head/tail encoding | |
| 46 | +| `07-tuple-type.ts` | Encode/decode tuples with mixed types | |
| 47 | +| `08-struct-type.ts` | Named structs with field metadata | |
| 48 | +| `09-struct-tuple-conversion.ts` | Convert between struct objects and tuple arrays | |
| 49 | +| `10-bool-packing.ts` | Efficient bool array packing (8 bools per byte) | |
| 50 | +| `11-abi-method.ts` | Parse method signatures and compute 4-byte selectors | |
| 51 | +| `12-avm-types.ts` | AVM-specific types (AVMBytes, AVMString, AVMUint64) | |
| 52 | +| `13-type-guards.ts` | Type guard functions for argument/type categorization | |
| 53 | +| `14-complex-nested.ts` | Deeply nested types combining arrays, tuples, structs | |
| 54 | +| `15-arc56-storage.ts` | ARC-56 storage helpers for contract state inspection | |
| 55 | + |
| 56 | +### Algo25 (`algo25/`) |
| 57 | + |
| 58 | +Mnemonic and seed conversion utilities following the Algorand 25-word mnemonic standard. No LocalNet required. |
| 59 | + |
| 60 | +| File | Description | |
| 61 | +|------|-------------| |
| 62 | +| `01-mnemonic-from-seed.ts` | Convert 32-byte seed to 25-word mnemonic | |
| 63 | +| `02-seed-from-mnemonic.ts` | Convert 25-word mnemonic back to 32-byte seed | |
| 64 | +| `03-secret-key-to-mnemonic.ts` | Convert 64-byte secret key to mnemonic | |
| 65 | +| `04-master-derivation-key.ts` | MDK alias functions for wallet derivation workflows | |
| 66 | +| `05-error-handling.ts` | Handle invalid words, checksums, and seed lengths | |
| 67 | + |
| 68 | +### Algod Client (`algod_client/`) |
| 69 | + |
| 70 | +Algorand node operations and queries using the AlgodClient. |
| 71 | + |
| 72 | +| File | Description | |
| 73 | +|------|-------------| |
| 74 | +| `01-node-health-status.ts` | Check node health with `healthCheck()`, `ready()`, `status()` | |
| 75 | +| `02-version-genesis.ts` | Get node version and genesis configuration | |
| 76 | +| `03-ledger-supply.ts` | Query total, online, and circulating supply | |
| 77 | +| `04-account-info.ts` | Get account balances, assets, and application state | |
| 78 | +| `05-transaction-params.ts` | Get suggested params for transaction construction | |
| 79 | +| `06-send-transaction.ts` | Submit transactions and wait for confirmation | |
| 80 | +| `07-pending-transactions.ts` | Query pending transactions in the mempool | |
| 81 | +| `08-block-data.ts` | Retrieve block info, hash, and transaction IDs | |
| 82 | +| `09-asset-info.ts` | Get asset parameters by ID | |
| 83 | +| `10-application-info.ts` | Get application state and parameters by ID | |
| 84 | +| `11-application-boxes.ts` | Query application box storage | |
| 85 | +| `12-teal-compile.ts` | Compile TEAL source and disassemble bytecode | |
| 86 | +| `13-simulation.ts` | Simulate transactions before submitting | |
| 87 | +| `14-state-deltas.ts` | Get ledger state changes for rounds/transactions | |
| 88 | +| `15-transaction-proof.ts` | Get Merkle proofs for transaction inclusion | |
| 89 | +| `16-lightblock-proof.ts` | Get light block header proofs for state verification | |
| 90 | +| `17-state-proof.ts` | Get cryptographic state proofs for cross-chain verification | |
| 91 | +| `18-devmode-timestamp.ts` | Control block timestamps in DevMode | |
| 92 | +| `19-sync-round.ts` | Manage node sync round for storage optimization | |
| 93 | + |
| 94 | +### Algorand Client (`algorand_client/`) |
| 95 | + |
| 96 | +High-level AlgorandClient API for simplified blockchain interactions. |
| 97 | + |
| 98 | +| File | Description | |
| 99 | +|------|-------------| |
| 100 | +| `01-client-instantiation.ts` | Create AlgorandClient via defaultLocalNet, testNet, mainNet, fromEnvironment, fromConfig | |
| 101 | +| `02-algo-amount.ts` | AlgoAmount utility for safe ALGO/microALGO arithmetic and formatting | |
| 102 | +| `03-signer-config.ts` | Configure transaction signers with setDefaultSigner, setSignerFromAccount, setSigner | |
| 103 | +| `04-params-config.ts` | Configure suggested params: validity window, caching, cache timeout | |
| 104 | +| `05-account-manager.ts` | Create/import accounts: random, mnemonic, KMD, multisig, logicsig, rekeyed | |
| 105 | +| `06-send-payment.ts` | Send ALGO payments with amount, note, and closeRemainderTo | |
| 106 | +| `07-send-asset-ops.ts` | ASA operations: create, config, opt-in, transfer, freeze, clawback, destroy | |
| 107 | +| `08-send-app-ops.ts` | Application operations: create, update, call, opt-in, close-out, delete | |
| 108 | +| `09-create-transaction.ts` | Create unsigned transactions for inspection and custom signing workflows | |
| 109 | +| `10-transaction-composer.ts` | Build atomic transaction groups with newGroup(), simulate(), send() | |
| 110 | +| `11-asset-manager.ts` | Query assets and perform bulk opt-in/opt-out operations | |
| 111 | +| `12-app-manager.ts` | Query app info, global/local state, box storage, compile TEAL | |
| 112 | +| `13-app-deployer.ts` | Idempotent app deployment with update/replace strategies | |
| 113 | +| `14-client-manager.ts` | Access raw algod/indexer/kmd clients and typed app clients | |
| 114 | +| `15-error-transformers.ts` | Register custom error transformers for enhanced debugging | |
| 115 | + |
| 116 | +### Common (`common/`) |
| 117 | + |
| 118 | +Utility functions and helpers. No LocalNet required. |
| 119 | + |
| 120 | +| File | Description | |
| 121 | +|------|-------------| |
| 122 | +| `01-address-basics.ts` | Parse, validate, and compare addresses with `Address` class | |
| 123 | +| `02-address-encoding.ts` | Encode/decode addresses, compute application addresses | |
| 124 | +| `03-array-utilities.ts` | Compare and concatenate byte arrays | |
| 125 | +| `04-constants.ts` | Protocol constants (limits, sizes, separators) | |
| 126 | +| `05-crypto-hash.ts` | SHA-512/256 hashing for transaction IDs and checksums | |
| 127 | +| `06-logger.ts` | Logger interface for consistent SDK logging | |
| 128 | +| `07-json-bigint.ts` | Parse/stringify JSON with BigInt support | |
| 129 | +| `08-msgpack.ts` | MessagePack encoding for transaction serialization | |
| 130 | +| `09-primitive-codecs.ts` | Codecs for numbers, strings, bytes, addresses | |
| 131 | +| `10-composite-codecs.ts` | Array, Map, and Record codecs | |
| 132 | +| `11-model-codecs.ts` | Object model codecs with field metadata | |
| 133 | +| `12-sourcemap.ts` | Map TEAL program counters to source locations | |
| 134 | + |
| 135 | +### Indexer Client (`indexer_client/`) |
| 136 | + |
| 137 | +Blockchain data queries using the IndexerClient. |
| 138 | + |
| 139 | +| File | Description | |
| 140 | +|------|-------------| |
| 141 | +| `01-health-check.ts` | Check indexer health status | |
| 142 | +| `02-account-lookup.ts` | Lookup and search accounts | |
| 143 | +| `03-account-assets.ts` | Query account asset holdings and created assets | |
| 144 | +| `04-account-applications.ts` | Query account app relationships and local state | |
| 145 | +| `05-account-transactions.ts` | Get account transaction history | |
| 146 | +| `06-transaction-lookup.ts` | Lookup single transaction by ID | |
| 147 | +| `07-transaction-search.ts` | Search transactions with filters | |
| 148 | +| `08-asset-lookup.ts` | Lookup and search assets | |
| 149 | +| `09-asset-balances.ts` | Get all holders of an asset | |
| 150 | +| `10-asset-transactions.ts` | Get transactions for a specific asset | |
| 151 | +| `11-application-lookup.ts` | Lookup and search applications | |
| 152 | +| `12-application-logs.ts` | Query application log emissions | |
| 153 | +| `13-application-boxes.ts` | Search application box storage | |
| 154 | +| `14-block-lookup.ts` | Lookup block information | |
| 155 | +| `15-block-headers.ts` | Search block headers | |
| 156 | +| `16-pagination.ts` | Handle pagination with limit/next parameters | |
| 157 | + |
| 158 | +### KMD Client (`kmd_client/`) |
| 159 | + |
| 160 | +Key Management Daemon operations for wallet and key management. |
| 161 | + |
| 162 | +| File | Description | |
| 163 | +|------|-------------| |
| 164 | +| `01-version.ts` | Get KMD server version information | |
| 165 | +| `02-wallet-management.ts` | Create, list, rename, and get wallet info | |
| 166 | +| `03-wallet-sessions.ts` | Manage wallet handle tokens (init, renew, release) | |
| 167 | +| `04-key-generation.ts` | Generate deterministic keys in a wallet | |
| 168 | +| `05-key-import-export.ts` | Import external keys and export private keys | |
| 169 | +| `06-key-listing-deletion.ts` | List and delete keys from a wallet | |
| 170 | +| `07-master-key-export.ts` | Export master derivation key for backup | |
| 171 | +| `08-multisig-setup.ts` | Create multisig accounts with M-of-N threshold | |
| 172 | +| `09-multisig-management.ts` | List, export, and delete multisig accounts | |
| 173 | +| `10-transaction-signing.ts` | Sign transactions using wallet keys | |
| 174 | +| `11-multisig-signing.ts` | Sign multisig transactions (partial + complete) | |
| 175 | +| `12-program-signing.ts` | Create delegated logic signatures | |
| 176 | +| `13-multisig-program-signing.ts` | Create delegated multisig logic signatures | |
| 177 | + |
| 178 | +### Testing (`testing/`) |
| 179 | + |
| 180 | +Testing utilities for mock server setup and Vitest integration. No LocalNet required. |
| 181 | + |
| 182 | +| File | Description | |
| 183 | +|------|-------------| |
| 184 | +| `01-configuration-constants.ts` | Configuration constants for mock server setup | |
| 185 | +| `02-test-data-constants.ts` | Test data constants matching HAR file responses | |
| 186 | +| `03-health-check.ts` | Check mock server availability with `checkServerHealth()` | |
| 187 | +| `04-mock-server-connection.ts` | Connect to mock servers with `getMockServer()` | |
| 188 | +| `05-global-setup-factory.ts` | Create custom Vitest global setups with `createGlobalSetup()` | |
| 189 | +| `06-prebuilt-global-setups.ts` | Pre-built global setups for algod, indexer, kmd | |
| 190 | +| `07-integration-example.ts` | Complete integration example with all components | |
| 191 | + |
| 192 | +### Transact (`transact/`) |
| 193 | + |
| 194 | +Low-level transaction construction and signing. |
| 195 | + |
| 196 | +| File | Description | |
| 197 | +|------|-------------| |
| 198 | +| `01-payment-transaction.ts` | Send ALGO between accounts | |
| 199 | +| `02-payment-close.ts` | Close account by transferring all remaining ALGO | |
| 200 | +| `03-asset-create.ts` | Create Algorand Standard Assets (ASA) | |
| 201 | +| `04-asset-transfer.ts` | Opt-in and transfer assets between accounts | |
| 202 | +| `05-asset-freeze.ts` | Freeze and unfreeze asset holdings | |
| 203 | +| `06-asset-clawback.ts` | Clawback assets using clawback address | |
| 204 | +| `07-atomic-group.ts` | Group transactions atomically (all-or-nothing) | |
| 205 | +| `08-atomic-swap.ts` | Swap ALGO for ASA between two parties | |
| 206 | +| `09-single-sig.ts` | Create ed25519 keypairs and sign transactions | |
| 207 | +| `10-multisig.ts` | Create and use 2-of-3 multisig accounts | |
| 208 | +| `11-logic-sig.ts` | Use logic signatures to authorize transactions | |
| 209 | +| `12-fee-calculation.ts` | Estimate size and calculate transaction fees | |
| 210 | +| `13-encoding-decoding.ts` | Serialize/deserialize transactions to msgpack | |
| 211 | +| `14-app-call.ts` | Deploy and interact with smart contracts | |
| 212 | + |
| 213 | +## Shared Utilities |
| 214 | + |
| 215 | +The `shared/` directory contains common utilities: |
| 216 | + |
| 217 | +- **`utils.ts`** - Helper functions for output, client creation, and common operations |
| 218 | +- **`constants.ts`** - LocalNet configuration (ports, tokens) |
| 219 | +- **`artifacts/`** - TEAL smart contract files for testing |
| 220 | + |
| 221 | +## Development |
| 222 | + |
| 223 | +### Adding New Examples |
| 224 | + |
| 225 | +1. Create a file following naming: `NN-descriptive-name.ts` |
| 226 | +2. Add JSDoc header describing the example |
| 227 | +3. Add to the category's `verify-all.sh` script |
| 228 | + |
| 229 | +### Example Header Format |
| 230 | + |
| 231 | +```typescript |
| 232 | +/** |
| 233 | + * Example: [Title] |
| 234 | + * |
| 235 | + * This example demonstrates [description]. |
| 236 | + * - Key operation 1 |
| 237 | + * - Key operation 2 |
| 238 | + * |
| 239 | + * Prerequisites: |
| 240 | + * - LocalNet running (or "No LocalNet required") |
| 241 | + */ |
| 242 | +``` |
| 243 | + |
| 244 | +### Running Tests |
| 245 | + |
| 246 | +```bash |
| 247 | +npm run typecheck # Type check all examples |
| 248 | +npm run verify-all # Run all verification scripts |
| 249 | +``` |
| 250 | + |
| 251 | +## License |
| 252 | + |
| 253 | +MIT - see [LICENSE](../LICENSE) for details. |
0 commit comments