AI agents excel at analyzing data and making decisions, but when it comes to executing multiple blockchain operations together, they often need to submit transactions one by one—risking partial failures and inconsistent states. WAIaaS batch transactions solve this by letting your AI agent bundle multiple operations into a single atomic execution, ensuring either all operations succeed or all fail together.
Why Atomic Execution Matters for AI Agents
When your AI agent manages DeFi strategies or executes complex trading logic, individual transactions can fail at any step. Imagine your agent trying to:
- Approve USDC for a DEX
- Swap USDC for ETH
- Stake the ETH on Lido
If step 2 fails after step 1 succeeds, your agent is left with an approved token but no swap—creating an inconsistent state that requires manual cleanup. Batch transactions eliminate this problem by wrapping all operations in a single atomic unit.
How WAIaaS Batch Transactions Work
WAIaaS provides a Batch transaction type that executes multiple operations atomically. All operations share the same gas fees, execute in sequence, and either all succeed or all revert together.
Here's how to create a batch transaction that swaps tokens and then stakes them:
curl -X POST http://127.0.0.1:3100/v1/transactions/send \
-H "Content-Type: application/json" \
-H "Authorization: Bearer wai_sess_<token>" \
-d '{
"type": "Batch",
"operations": [
{
"type": "Approve",
"spender": "0xA0b86a33E6441e53fb96C9e3fF0F30b19b8fBf20",
"token": "0xA0b86a33E6441e53fb96C9e3fF0F30b19b8fBf20",
"amount": "1000000000"
},
{
"type": "ContractCall",
"to": "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D",
"data": "0x38ed1739...",
"value": "0"
}
]
}'
Building Complex DeFi Strategies
Your AI agent can combine multiple DeFi actions into sophisticated strategies. Here's a batch transaction that implements a "buy the dip and stake" strategy:
import { WAIaaSClient } from '@waiaas/sdk';
const client = new WAIaaSClient({
baseUrl: 'http://127.0.0.1:3100',
sessionToken: process.env.WAIAAS_SESSION_TOKEN,
});
// AI agent detects market dip and executes multi-step strategy
const batchTx = await client.sendTransaction({
type: 'Batch',
operations: [
// Step 1: Approve USDC for Jupiter swap
{
type: 'Approve',
spender: 'JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4',
token: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
amount: '1000000000' // 1000 USDC
},
// Step 2: Swap USDC for SOL
{
type: 'ContractCall',
to: 'JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4',
data: await buildJupiterSwapData({
inputMint: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', // USDC
outputMint: 'So11111111111111111111111111111111111111112', // SOL
amount: '1000000000'
})
},
// Step 3: Stake received SOL with Jito
{
type: 'ContractCall',
to: 'Jito4APyf642JPZPx3hGc6WWJ8zPKtRbRs4P815Awbb',
data: await buildJitoStakeData({
amount: 'ALL' // Stake all received SOL
})
}
]
});
MCP Integration for Claude
If you're using Claude Desktop with MCP, your AI agent can execute batch transactions through natural language:
{
"mcpServers": {
"waiaas": {
"command": "npx",
"args": ["-y", "@waiaas/mcp"],
"env": {
"WAIAAS_BASE_URL": "http://127.0.0.1:3100",
"WAIAAS_SESSION_TOKEN": "wai_sess_<your-token>"
}
}
}
}
Now Claude can execute complex batch operations:
User: "Swap 100 USDC for ETH and then stake it all on Lido in one transaction"
Claude: I'll execute a batch transaction to swap USDC for ETH and stake it atomically...
→ Claude calls send_batch tool with approve + swap + stake operations
→ All operations execute together or fail together
→ Returns transaction ID for monitoring
Policy-Aware Batch Execution
WAIaaS evaluates policies against the entire batch, not individual operations. This means your SPENDING_LIMIT policy considers the total USD value across all operations:
curl -X POST http://localhost:3100/v1/policies \
-H 'Content-Type: application/json' \
-H 'X-Master-Password: <password>' \
-d '{
"walletId": "<wallet-uuid>",
"type": "SPENDING_LIMIT",
"rules": {
"instant_max_usd": 100,
"delay_max_usd": 1000,
"delay_seconds": 300
}
}'
A batch transaction worth $500 total will trigger the delay tier, regardless of how many individual operations it contains.
Error Handling and Rollback
When any operation in a batch fails, the entire transaction reverts. Your AI agent gets clear error information:
try {
const result = await client.sendTransaction({
type: 'Batch',
operations: [/* ... */]
});
} catch (error) {
if (error instanceof WAIaaSError) {
console.error(`Batch failed: [${error.code}] ${error.message}`);
// Common codes: INSUFFICIENT_BALANCE, POLICY_DENIED, CONTRACT_REVERTED
// All operations rolled back automatically
}
}
Gas Optimization
Batch transactions use significantly less gas than individual transactions because:
- Single gas payment covers all operations
- No redundant transaction overhead
- Optimized call data packing
- Reduced blockchain state changes
For a typical approve + swap + stake sequence, batching saves 30-40% on gas costs.
Multi-Network Batch Operations
While individual batch transactions execute on a single network, your AI agent can coordinate cross-chain strategies by submitting batch transactions to multiple networks simultaneously:
// Execute coordinated strategy across chains
const [ethBatch, solBatch] = await Promise.all([
ethClient.sendTransaction({
type: 'Batch',
operations: [/* ETH DeFi operations */]
}),
solClient.sendTransaction({
type: 'Batch',
operations: [/* Solana DeFi operations */]
})
]);
Quick Start: Your First Batch Transaction
Here's how to get your AI agent executing batch transactions in minutes:
- Start WAIaaS daemon:
npm install -g @waiaas/cli
waiaas init
waiaas start
waiaas quickset --mode mainnet
- Set up MCP for Claude (auto-generated config):
waiaas mcp setup --all
- Test batch execution in Claude:
"Create a batch transaction that approves 100 USDC for Jupiter and then swaps it for SOL"
- Monitor the transaction:
# Claude will show the transaction ID
curl http://127.0.0.1:3100/v1/transactions/<tx-id> \
-H "Authorization: Bearer wai_sess_<token>"
- Check results:
curl http://127.0.0.1:3100/v1/wallet/balance \
-H "Authorization: Bearer wai_sess_<token>"
Your AI agent now has atomic transaction capabilities that eliminate partial failure states and optimize gas usage across complex DeFi operations.
What's Next
Batch transactions are just the beginning of sophisticated AI agent finance. Explore advanced patterns like conditional execution, time-delayed strategies, and cross-protocol arbitrage in the WAIaaS documentation.
Ready to give your AI agent atomic transaction powers? Check out the WAIaaS GitHub repository for the complete implementation, or visit waiaas.ai to get started with the hosted version.
Top comments (0)