Batch Transactions

Execute multiple operations in a single transaction to reduce costs and improve efficiency.

Overview

Batch transactions combine multiple operations into one on-chain transaction. This amortizes fixed costs (base fee, nonce management) across all operations, reducing total gas consumption by 40-60% compared to individual transactions.

Benefits

Cost reduction: Single base fee instead of N separate fees Atomic execution: All operations succeed or all fail together Nonce efficiency: One nonce for multiple operations Faster confirmation: One transaction to track instead of N Simpler state management: Single confirmation event

Basic batching

Multiple transfers

Send tokens to multiple recipients in one transaction:

const batch = await client.executeBatch([
  { instruction: "Transfer 10 USDC to 0xaaaa..." },
  { instruction: "Transfer 15 USDC to 0xbbbb..." },
  { instruction: "Transfer 20 USDC to 0xcccc..." }
])

console.log(`Batch transaction: ${batch.txHash}`)
console.log(`Total gas: ${batch.gasEstimate}`)
console.log(`Operations: ${batch.operations.length}`)

Cost comparison:

Method
Base Fee
Gas per Transfer
Total Gas
USD Cost

Individual (3x)

3 × 45 gwei

3 × 21,000

135 gwei

$9.45

Batch (1x)

1 × 45 gwei

60,000

105 gwei

$7.35

Savings: $2.10 (22% reduction)

Structured batch

Use structured format for precise control:

Advanced batching

Mixed operations

Combine different transaction types:

Conditional batching

Execute operations based on conditions:

Dependent operations

Chain operations where later steps depend on earlier results:

Batch strategies

Airdrop distribution

Distribute tokens to thousands of addresses:

Cost analysis:

  • 1000 recipients

  • Individual transactions: ~$3 × 1000 = $3,000

  • Batched (10 batches of 100): ~$150 × 10 = $1,500

  • Savings: $1,500 (50%)

Recurring payments

Process monthly salaries or subscriptions:

Portfolio rebalancing

Rebalance multiple assets atomically:

Gas optimization

Calldata compression

Molt402 automatically compresses calldata for batch transactions:

Operation reordering

Optimize execution order for gas efficiency:

Redundant operation elimination

Remove duplicate approvals automatically:

Error handling

Partial failure handling

By default, batch transactions are atomic (all succeed or all fail):

Allow partial execution:

Retry failed operations

Automatically retry failed operations:

Monitoring and tracking

Batch status

Track batch execution progress:

Individual operation tracking

Track each operation within batch:

Size limits

Transaction size constraints

Each batch has gas and size limits:

Block gas limit: ~30M gas per block Transaction size: ~128KB calldata limit Practical limits:

  • Simple transfers: ~1000 per batch

  • Complex operations: ~50-100 per batch

Automatic chunking

Molt402 automatically splits large batches:

Testing batches

Simulation

Simulate entire batch before submission:

Dry run

Test batch without spending gas:

Best practices

Batch similar operations: Group transfers together, swaps together, etc.

Set reasonable chunk sizes: 100-500 operations per batch depending on complexity

Always simulate: Enable simulation for batch transactions

Monitor gas costs: Track cumulative gas for cost analysis

Use atomicity appropriately: Atomic for critical operations, optional for best-effort

Handle failures gracefully: Implement retry logic for failed batches

Test thoroughly: Validate batches on testnet before mainnet

Set timeouts: Configure reasonable deadlines for batch execution

Example: Complete treasury distribution

Next steps

Support

Questions about batch transactions?

Last updated