> ## Documentation Index
> Fetch the complete documentation index at: https://starkware-9575960b-starkzapv4.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Tx Builder

> Batch multiple operations into a single atomic transaction using the fluent TxBuilder API

<img src="https://mintcdn.com/starkware-9575960b-starkzapv4/Q0Mo-yiQRCwOcerg/assets/starkzap/tx-builder.png?fit=max&auto=format&n=Q0Mo-yiQRCwOcerg&q=85&s=14cc2768f8d7c8f34d1d409af2a6d961" alt="Tx Builder hero" width="1024" height="576" data-path="assets/starkzap/tx-builder.png" />

## Overview

The `TxBuilder` provides a fluent API for batching multiple operations into a single atomic transaction. This saves gas and guarantees all-or-nothing execution—either all operations succeed together, or none of them execute.

## Basic Usage

```typescript theme={null}
const tx = await wallet
  .tx()
  .enterPool(poolAddress, Amount.parse("100", STRK))
  .send();
await tx.wait();
```

## Mixing Operations

Combine transfers, staking, approvals, and raw calls:

```typescript theme={null}
const tx = await wallet
  .tx()
  // Transfer tokens to multiple recipients
  .transfer(USDC, [
    { to: alice, amount: Amount.parse("50", USDC) },
    { to: bob, amount: Amount.parse("25", USDC) },
  ])
  // Stake in a pool (auto-detects enter vs. add)
  .stake(poolAddress, Amount.parse("100", STRK))
  // Claim rewards
  .claimPoolRewards(anotherPoolAddress)
  // Add raw contract calls
  .add({
    contractAddress: "0xDEX_CONTRACT",
    entrypoint: "swap",
    calldata: [/* ... */],
  })
  .send();

await tx.wait();
```

The `.stake()` method is smart — it automatically calls `enter_delegation_pool` for new members or `add_to_delegation_pool` for existing members.

## Preflight with Builder

```typescript theme={null}
const builder = wallet
  .tx()
  .stake(poolAddress, amount)
  .transfer(USDC, { to: alice, amount: usdcAmount });

const result = await builder.preflight();
if (!result.ok) {
  console.error("Transaction would fail:", result.reason);
} else {
  const tx = await builder.send();
  await tx.wait();
}
```

## Fee Estimation

```typescript theme={null}
const fee = await wallet
  .tx()
  .transfer(USDC, { to: alice, amount })
  .stake(poolAddress, stakeAmount)
  .estimateFee();

console.log("Estimated fee:", fee.overall_fee);
```

## Extracting Calls

You can also extract the raw calls for inspection:

```typescript theme={null}
const calls = await wallet
  .tx()
  .transfer(USDC, { to: alice, amount })
  .enterPool(poolAddress, stakeAmount)
  .calls();

console.log(`${calls.length} calls in this transaction`);
```

## Available Builder Methods

| Method | Description |
| - | - |
| `.add(...calls)` | Add raw `Call` objects |
| `.approve(token, spender, amount)` | ERC20 approval |
| `.transfer(token, transfers)` | ERC20 transfer(s) |
| `.stake(pool, amount)` | Smart stake (enter or add based on membership) |
| `.enterPool(pool, amount)` | Enter pool as new member |
| `.addToPool(pool, amount)` | Add to existing pool position |
| `.claimPoolRewards(pool)` | Claim staking rewards |
| `.exitPoolIntent(pool, amount)` | Start exit process |
| `.exitPool(pool)` | Complete exit after window |
| `.swap(request)` | Provider-driven token swap — see [Swaps](/build/starkzap/swap) |
| `.lendDeposit(request)` | Lending deposit (supply) — see [Lending](/build/starkzap/lending) |
| `.lendWithdraw(request)` | Lending withdraw |
| `.lendWithdrawMax(request)` | Lending max withdraw |
| `.lendBorrow(request)` | Lending borrow |
| `.lendRepay(request)` | Lending repay |
| `.trovesDeposit(params)` | Troves strategy deposit — see [Troves](/build/starkzap/troves) |
| `.trovesWithdraw(params)` | Troves strategy withdraw — see [Troves](/build/starkzap/troves) |
| `.dcaCreate(request)` | Create a DCA (recurring buy) order — see [DCA](/build/starkzap/dollar-cost-average) |
| `.dcaCancel(request)` | Cancel a DCA order |
| `.confidentialFund(confidential, details)` | Fund confidential account — see [Tongo](/build/starkzap/privacy/tongo) |
| `.confidentialTransfer(confidential, details)` | Confidential transfer |
| `.confidentialWithdraw(confidential, details)` | Withdraw from confidential to public address |
| `.calls()` | Resolve all calls without sending |
| `.estimateFee()` | Estimate gas cost |
| `.preflight()` | Simulate the transaction |
| `.send(options?)` | Execute all calls atomically |

## Best Practices

1. **Use the transaction builder** for complex operations to save gas
2. **Always preflight** non-trivial batches before submitting
3. **Combine related operations** into a single transaction when possible
4. **Use `.estimateFee()`** to show users the cost before executing

## Next Steps

* Learn about [Transaction Execution](/build/starkzap/transactions) for direct execution methods
* Explore [Staking and Delegation](/build/starkzap/staking) for staking operations
* Check the [Bitcoin, Stablecoins, and Token](/build/starkzap/erc20) module for token operations
* [Lending](/build/starkzap/lending) — Batch deposit, withdraw, borrow, and repay with Vesu
* [Troves](/build/starkzap/troves) — Batch Troves deposits and withdrawals with other wallet operations
* [Tongo confidential transfers](/build/starkzap/privacy/tongo) — Fund, transfer, and withdraw with Tongo

<Note>
  Only Tongo batches. [STRK20 privacy pool](/build/starkzap/privacy/strk20) operations cannot go through the builder: the proof belongs to the transaction rather than to a call, so a privacy operation can never share a transaction with other calls.
</Note>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.