Working JavaScript scripts demonstrating common usage patterns. Each example shows a specific strategy built with the TransactionBuilder. Run them with Bun:
cd js/examples && bun run <script>.js| File | What it demonstrates |
|---|---|
js/examples/erc20_balance_transfer.js |
Read an ERC20 balance, then transfer that amount — the simplest descriptor chaining pattern |
js/examples/approve_and_deposit.js |
Approve token spending, then deposit to a vault — state-changing call followed by dependent state-changing call |
js/examples/multiple_swaps.js |
Multi-hop swap routing: read reserves, compute swap amounts, execute swaps across multiple DEXes |
js/examples/uniswap_v2_reserves.js |
Read Uniswap V2 pair reserves via static call, chain reserve data into subsequent calls |
js/examples/anvilFork.js |
Fork testing with Anvil — execute strategies against mainnet state |
js/examples/helpers.js |
Shared utilities: token approvals, contract wrappers, amount parsing |
js/examples/abis.js |
ABI definitions for common contracts (ERC20, Uniswap V2, Aave) |
const balance = builder.addCall(ERC20_ABI, token, "balanceOf", [user]);
builder.addCall(ERC20_ABI, token, "transfer", [recipient, balance]);The simplest descriptor chaining: static call returns a value, that value becomes an argument to a subsequent state-changing call.
const accountData = builder.addCall(aaveABI, pool, "getUserAccountData", [user]);
builder.addCall(aaveABI, pool, "borrow", [
dai,
accountData.availableBorrowsBase, // struct field access
2, 0, user
]);const names = builder.addCall(abi, registry, "getNames", []);
names.with_length(3); // array of 3 items
builder.addCall(abi, registry, "setFirst", [names[0]]); // index access- Examples assume a running Anvil fork (
run_anvil.shstarts one) — they connect tolocalhost:8545via ethers or viem - ABI files in
js/examples/abis.jsare hand-curated for the specific protocols used; add ABIs as needed - Amounts may be mainnet-dependent — examples work against forked state; adjust addresses and amounts for different chains
- Not all examples include value transfers — check
msgValuesin the built output before sending ETH
- Full TransactionBuilder API → docs/javascript.md
- Test patterns → docs/testing.md
- Architecture overview → docs/OVERVIEW.md