DEV Community

Cover image for Smart Contract Interaction: Calldata Encoding Tools for AI Agents
Wallet Guy
Wallet Guy

Posted on

Smart Contract Interaction: Calldata Encoding Tools for AI Agents

Smart contract interaction for DeFi developers has always been a maze of protocol-specific APIs, custom calldata encoding, and fragmented documentation across 15+ protocols. Whether you're building trading bots, yield strategies, or multi-chain applications, you end up maintaining separate integrations for Jupiter, Aave, Lido, Drift, Hyperliquid, and dozens of other protocols — each with their own quirks, parameters, and ABI formats.

Why Protocol Fragmentation Kills Productivity

The DeFi ecosystem spans multiple chains with incompatible transaction formats. Solana uses different instruction layouts than EVM chains. Jupiter's swap parameters look nothing like Uniswap's. Aave V3 has different method signatures than Compound. Your trading bot ends up with 13 different SDKs, each handling calldata encoding differently.

Then there's the operational overhead: monitoring gas prices across chains, handling failed transactions, managing approvals and slippage, keeping track of which protocols support which tokens. A simple arbitrage strategy becomes 2,000 lines of boilerplate before you write any actual trading logic.

One API for 15 DeFi Protocols

WAIaaS provides a unified interface for DeFi operations across EVM and Solana. Instead of learning Jupiter's API, then Aave's API, then Lido's staking interface, you make the same REST call regardless of the underlying protocol.

Here's swapping SOL for USDC on Jupiter:

curl -X POST http://127.0.0.1:3100/v1/actions/jupiter-swap/swap \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer wai_sess_<token>" \
  -d '{
    "inputMint": "So11111111111111111111111111111111111111112",
    "outputMint": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
    "amount": "1000000000"
  }'
Enter fullscreen mode Exit fullscreen mode

And here's lending USDC on Aave V3:

curl -X POST http://127.0.0.1:3100/v1/actions/aave-v3/supply \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer wai_sess_<token>" \
  -d '{
    "asset": "0xA0b86a33E6417c0F4Fe77c96dbD46bb1e4F8a4eE",
    "amount": "1000000000",
    "onBehalfOf": "<wallet-address>"
  }'
Enter fullscreen mode Exit fullscreen mode

Same authentication, same response format, same error handling — whether you're interacting with Solana programs or Ethereum contracts.

15 Protocols, Same Interface

WAIaaS integrates 15 DeFi protocol providers: aave-v3, across, dcent-swap, drift, erc8004, hyperliquid, jito-staking, jupiter-swap, kamino, lido-staking, lifi, pendle, polymarket, xrpl-dex, zerox-swap. Each provider exposes its functionality through the same /v1/actions/{provider}/{action} endpoint.

Want to check your DeFi positions across all protocols?

curl http://127.0.0.1:3100/v1/defi/positions \
  -H "Authorization: Bearer wai_sess_<token>"
Enter fullscreen mode Exit fullscreen mode

This returns lending positions from Aave and Kamino, staking positions from Lido and Jito, perpetual positions from Drift and Hyperliquid, and liquidity positions from DEXs — all in one response.

Smart Calldata Encoding

Behind the scenes, WAIaaS handles the complexity of encoding contract calls correctly. When you call the Aave V3 supply action, WAIaaS encodes the proper ABI call to the Pool contract, handles token approvals automatically, and manages gas estimation.

For more advanced use cases, you can encode arbitrary contract calls directly:

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": "ContractCall",
    "to": "0x87870bca3f3fd6335c3f4ce8392d69350b4fa4e2",
    "data": "0x617ba037000000000000000000000000a0b86a33e6417c0f4fe77c96dbd46bb1e4f8a4ee0000000000000000000000000000000000000000000000000000000077359400"
  }'
Enter fullscreen mode Exit fullscreen mode

The encode-calldata tool lets you build these calls programmatically:

curl -X POST http://127.0.0.1:3100/v1/calldata/encode \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer wai_sess_<token>" \
  -d '{
    "contractAddress": "0x87870bca3f3fd6335c3f4ce8392d69350b4fa4e2",
    "method": "supply",
    "params": ["0xA0b86a33E6417c0F4Fe77c96dbD46bb1e4F8a4eE", "2000000000", "0x123..."]
  }'
Enter fullscreen mode Exit fullscreen mode

Cross-Chain Operations Made Simple

DeFi strategies often require moving assets between chains. WAIaaS integrates both LI.FI and Across protocols for cross-chain bridging, so you can bridge from Ethereum to Arbitrum and immediately start trading on GMX — all through the same API.

# Bridge USDC from Ethereum to Arbitrum
curl -X POST http://127.0.0.1:3100/v1/actions/lifi/bridge \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer wai_sess_<token>" \
  -d '{
    "fromChain": "ethereum-mainnet",
    "toChain": "arbitrum-mainnet",
    "fromToken": "0xA0b86a33E6417c0F4Fe77c96dbD46bb1e4F8a4eE",
    "toToken": "0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8",
    "amount": "1000000000"
  }'
Enter fullscreen mode Exit fullscreen mode

WAIaaS supports 2 chain types (solana, evm) with 18 networks, so your trading strategies can span the entire DeFi ecosystem.

TypeScript SDK for Complex Strategies

For more sophisticated trading bots, the TypeScript SDK provides 40+ methods with full type safety:

import { WAIaaSClient } from '@waiaas/sdk';

const client = new WAIaaSClient({
  baseUrl: 'http://127.0.0.1:3100',
  sessionToken: process.env.WAIAAS_SESSION_TOKEN,
});

// Execute multiple DeFi actions in sequence
async function yieldStrategy() {
  // 1. Swap ETH for USDC
  const swapTx = await client.executeAction('zerox-swap', 'swap', {
    sellToken: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
    buyToken: '0xA0b86a33E6417c0F4Fe77c96dbD46bb1e4F8a4eE',
    sellAmount: '1000000000000000000'
  });

  // 2. Supply USDC to Aave
  const lendTx = await client.executeAction('aave-v3', 'supply', {
    asset: '0xA0b86a33E6417c0F4Fe77c96dbD46bb1e4F8a4eE',
    amount: '2000000000',
    onBehalfOf: await client.getAddress()
  });

  // 3. Check new positions
  const positions = await client.getDeFiPositions();
  console.log('New lending APY:', positions.lending[0].apy);
}
Enter fullscreen mode Exit fullscreen mode

The SDK handles authentication, retries, and error parsing automatically. No need to manage HTTP clients, parse API responses, or handle authentication tokens.

Transaction Simulation and Safety

Before executing expensive DeFi transactions, use the dry-run API to simulate outcomes:

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": "ContractCall",
    "to": "0x87870bca3f3fd6335c3f4ce8392d69350b4fa4e2",
    "data": "0x617ba037...",
    "dryRun": true
  }'
Enter fullscreen mode Exit fullscreen mode

This returns gas estimates, potential errors, and expected state changes without broadcasting the transaction. Perfect for testing complex multi-step strategies.

The policy engine adds another layer of protection with 21 policy types. Set spending limits, whitelist approved contracts, or require human approval for large positions:

curl -X POST http://127.0.0.1:3100/v1/policies \
  -H "Content-Type: application/json" \
  -H "X-Master-Password: my-secret-password" \
  -d '{
    "walletId": "<wallet-uuid>",
    "type": "CONTRACT_WHITELIST",
    "rules": {
      "contracts": [
        {"address": "0x87870bca3f3fd6335c3f4ce8392d69350b4fa4e2", "name": "Aave Pool", "chain": "ethereum"},
        {"address": "JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4", "name": "Jupiter", "chain": "solana"}
      ]
    }
  }'
Enter fullscreen mode Exit fullscreen mode

Quick Start for DeFi Developers

Get your unified DeFi API running in 5 minutes:

  1. Start WAIaaS with Docker:
   git clone https://github.com/minhoyoo-iotrust/WAIaaS.git
   cd WAIaaS
   docker compose up -d
Enter fullscreen mode Exit fullscreen mode
  1. Create wallets and sessions:
   npm install -g @waiaas/cli
   waiaas quickset --mode mainnet
Enter fullscreen mode Exit fullscreen mode
  1. Test a DeFi action:
   curl http://127.0.0.1:3100/v1/defi/positions \
     -H "Authorization: Bearer wai_sess_<your-token>"
Enter fullscreen mode Exit fullscreen mode
  1. Install the SDK for your trading bot:
   npm install @waiaas/sdk
Enter fullscreen mode Exit fullscreen mode
  1. Start building cross-protocol strategies using the same API for all 15 providers.

Your next DeFi project just got 10x simpler. One API, 15 protocols, infinite possibilities. Check out the full documentation and source code at GitHub, or explore the interactive API reference at waiaas.ai.

Top comments (0)