Real-world examples of contract calls using OneBalance APIs. Each example shows the complete flow from preparing quotes to execution with actual API payloads.

Overview

These examples demonstrate the three-step flow for contract calls:

  1. Prepare - Generate user operation and typed data
  2. Sign - Sign the typed data (handled by your wallet/signer)
  3. Execute - Submit the signed quote for execution

For interactive testing, use the API Reference playground or copy the cURL commands directly.

DeFi Examples

Uniswap V3 Swap

Swap USDC for WETH on Base using Uniswap V3 router.

{
  "account": {
    "sessionAddress": "0x1cBFbFd62a276BF6D79d504eA4CA75a7baDcf5b1",
    "adminAddress": "0xc162a3cE45ad151eeCd0a5532D6E489D034aB3B8",
    "accountAddress": "0xE20295ec513DEf805D9c3083b0C8EaB64692D764"
  },
  "targetChain": "eip155:8453",
  "calls": [
    {
      "to": "0x2626664c2603336E57B271c5C0b26F421741e481",
      "data": "0x414bf389000000000000000000000000833589fcd6edb6e08f4c7c32d4f71b54bda029130000000000000000000000004200000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000bb8000000000000000000000000e20295ec513def805d9c3083b0c8eab64692d764000000000000000000000000000000000000000000000000000000006748d28800000000000000000000000000000000000000000000000000000000000f42400000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000000000000",
      "value": "0x0"
    }
  ],
  "allowanceRequirements": [
    {
      "assetType": "eip155:8453/erc20:0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
      "amount": "1000000",
      "spender": "0x2626664c2603336E57B271c5C0b26F421741e481"
    }
  ],
  "tokensRequired": [
    {
      "assetType": "eip155:8453/erc20:0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
      "amount": "1000000"
    }
  ]
}

After signing the typed data from the prepare response, execute the quote:

curl --request POST \
  --url https://be.onebalance.io/api/quotes/call-quote \
  --header 'Content-Type: application/json' \
  --header 'x-api-key: f9703eaqsbma20tmtphg2jirm0hk8z8v2hkodrfrvhfm6ziesi7p38u991bnih5f' \
  --data '{
  "account": {
    "sessionAddress": "0x1cBFbFd62a276BF6D79d504eA4CA75a7baDcf5b1",
    "adminAddress": "0xc162a3cE45ad151eeCd0a5532D6E489D034aB3B8",
    "accountAddress": "0xE20295ec513DEf805D9c3083b0C8EaB64692D764"
  },
  "chainOperation": {
    "userOp": {
      "sender": "0xe20295ec513def805d9c3083b0c8eab64692d764",
      "nonce": "0x1000000000000000000000000000000000000000000000000000000000000000",
      "factory": "0xd703aae79538628d27099b8c4f621be4ccd142d5",
      "factoryData": "0x...",
      "callData": "0xe9ae5c53...",
      "callGasLimit": "500000",
      "verificationGasLimit": "1000000",
      "preVerificationGas": "21000",
      "maxFeePerGas": "1000000000",
      "maxPriorityFeePerGas": "1000000000",
      "paymaster": "0xa784e6482bd5edbfe5991b18cbd545ebd46e1cc4",
      "paymasterVerificationGasLimit": "50000",
      "paymasterPostOpGasLimit": "0",
      "paymasterData": "0x",
      "signature": "0x1234...your_signature_here"
    },
    "typedDataToSign": {
      "domain": {
        "name": "RoleBasedECDSAValidator",
        "version": "1.4.3",
        "chainId": 8453,
        "verifyingContract": "0xA24bD06230f3F54e5bf266AE7A41750eE3b789FA"
      },
      "types": {
        "Approve": [
          {
            "name": "callDataAndNonceHash",
            "type": "bytes32"
          }
        ]
      },
      "primaryType": "Approve",
      "message": {
        "callDataAndNonceHash": "0xaab2ddac63e4a7e25b7d339cacdff2f9280025cef27699ffe5f5e9e9625b15eb"
      }
    },
    "assetType": "eip155:8453/erc20:0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
    "amount": "1000000"
  },
  "tamperProofSignature": "0x5678...tamper_proof_signature",
  "fromAggregatedAssetId": "ds:usdc"
}'

Aave V3 Supply

Supply USDC to Aave V3 on Base to earn yield.

{
  "account": {
    "sessionAddress": "0x1cBFbFd62a276BF6D79d504eA4CA75a7baDcf5b1",
    "adminAddress": "0xc162a3cE45ad151eeCd0a5532D6E489D034aB3B8",
    "accountAddress": "0xE20295ec513DEf805D9c3083b0C8EaB64692D764"
  },
  "targetChain": "eip155:8453",
  "calls": [
    {
      "to": "0xA238Dd80C259a72e81d7e4664a9801593F98d1c5",
      "data": "0x617ba037000000000000000000000000833589fcd6edb6e08f4c7c32d4f71b54bda02913000000000000000000000000000000000000000000000000000000000098968000000000000000000000000e20295ec513def805d9c3083b0c8eab64692d7640000000000000000000000000000000000000000000000000000000000000000",
      "value": "0x0"
    }
  ],
  "allowanceRequirements": [
    {
      "assetType": "eip155:8453/erc20:0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
      "amount": "10000000",
      "spender": "0xA238Dd80C259a72e81d7e4664a9801593F98d1c5"
    }
  ],
  "tokensRequired": [
    {
      "assetType": "eip155:8453/erc20:0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
      "amount": "10000000"
    }
  ]
}

NFT Examples

Mint NFT Collection

Mint 2 NFTs from a collection on Base with ETH payment.

{
  "account": {
    "sessionAddress": "0x1cBFbFd62a276BF6D79d504eA4CA75a7baDcf5b1",
    "adminAddress": "0xc162a3cE45ad151eeCd0a5532D6E489D034aB3B8",
    "accountAddress": "0xE20295ec513DEf805D9c3083b0C8EaB64692D764"
  },
  "targetChain": "eip155:8453",
  "calls": [
    {
      "to": "0x1234567890123456789012345678901234567890",
      "data": "0xa0712d680000000000000000000000000000000000000000000000000000000000000002",
      "value": "0x1bc16d674ec80000"
    }
  ],
  "allowanceRequirements": [],
  "tokensRequired": [
    {
      "assetType": "eip155:8453/slip44:60",
      "amount": "2000000000000000000"
    }
  ]
}

Gaming Examples

In-Game Purchase

Purchase game item #1 using USDC as payment token.

{
  "account": {
    "sessionAddress": "0x1cBFbFd62a276BF6D79d504eA4CA75a7baDcf5b1",
    "adminAddress": "0xc162a3cE45ad151eeCd0a5532D6E489D034aB3B8",
    "accountAddress": "0xE20295ec513DEf805D9c3083b0C8EaB64692D764"
  },
  "targetChain": "eip155:8453",
  "calls": [
    {
      "to": "0x1234567890123456789012345678901234567890",
      "data": "0x96a81f59000000000000000000000000000000000000000000000000000000000000000100000000000000000000000833589fcd6edb6e08f4c7c32d4f71b54bda02913000000000000000000000000000000000000000000000000000000000098968",
      "value": "0x0"
    }
  ],
  "allowanceRequirements": [
    {
      "assetType": "eip155:8453/erc20:0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
      "amount": "10000000",
      "spender": "0x1234567890123456789012345678901234567890"
    }
  ],
  "tokensRequired": [
    {
      "assetType": "eip155:8453/erc20:0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
      "amount": "10000000"
    }
  ]
}

Basic Account Examples

Basic accounts use a simpler structure with just signer and account addresses.

Token Transfer with Basic Account

Transfer USDC tokens using a basic account configuration.

{
  "account": {
    "type": "kernel-v3.1-ecdsa",
    "signerAddress": "0x5d6fb4eb211a6a2e406a1111b54d26c534753c8e",
    "accountAddress": "0xb7bc0d7baf6761c302ff6772dfd8f9e22ec706e7"
  },
  "targetChain": "eip155:8453",
  "calls": [
    {
      "to": "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
      "data": "0xa9059cbb000000000000000000000000742d35cc6634c0532925a3b844bc454e4438f44e0000000000000000000000000000000000000000000000000000000000989680",
      "value": "0x0"
    }
  ],
  "allowanceRequirements": [],
  "tokensRequired": [
    {
      "assetType": "eip155:8453/erc20:0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
      "amount": "10000000"
    }
  ]
}

Multi-Step Operations

Batch Token Transfers

Send tokens to multiple recipients in a single transaction.

{
  "account": {
    "sessionAddress": "0x1cBFbFd62a276BF6D79d504eA4CA75a7baDcf5b1",
    "adminAddress": "0xc162a3cE45ad151eeCd0a5532D6E489D034aB3B8",
    "accountAddress": "0xE20295ec513DEf805D9c3083b0C8EaB64692D764"
  },
  "targetChain": "eip155:8453",
  "calls": [
    {
      "to": "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
      "data": "0xa9059cbb000000000000000000000000742d35cc6634c0532925a3b844bc454e4438f44e0000000000000000000000000000000000000000000000000000000000989680",
      "value": "0x0"
    },
    {
      "to": "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
      "data": "0xa9059cbb000000000000000000000000456d35cc6634c0532925a3b844bc454e4438f44e0000000000000000000000000000000000000000000000000000000000989680",
      "value": "0x0"
    },
    {
      "to": "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
      "data": "0xa9059cbb000000000000000000000000789d35cc6634c0532925a3b844bc454e4438f44e0000000000000000000000000000000000000000000000000000000000989680",
      "value": "0x0"
    }
  ],
  "allowanceRequirements": [],
  "tokensRequired": [
    {
      "assetType": "eip155:8453/erc20:0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
      "amount": "30000000"
    }
  ]
}

Integration Helpers

TypeScript Helper Functions

Utility functions to simplify contract call integration.

helper-functions.ts
import { encodeFunctionData, parseAbi } from 'viem';

// Generate transfer call data
export function createTransferCall(
  tokenAddress: string,
  recipient: string,
  amount: bigint
) {
  return {
    to: tokenAddress,
    data: encodeFunctionData({
      abi: parseAbi(['function transfer(address to, uint256 amount)']),
      functionName: 'transfer',
      args: [recipient, amount]
    }),
    value: '0x0'
  };
}

// Generate approve call data
export function createApprovalRequirement(
  tokenAddress: string,
  spender: string,
  amount: bigint,
  chainId: number
) {
  return {
    assetType: `eip155:${chainId}/erc20:${tokenAddress}`,
    amount: amount.toString(),
    spender
  };
}

// Generate Uniswap V3 swap call data
export function createUniswapV3Swap(
  tokenIn: string,
  tokenOut: string,
  fee: number,
  recipient: string,
  amountIn: bigint,
  amountOutMinimum: bigint,
  deadline: number
) {
  const swapRouter = '0x2626664c2603336E57B271c5C0b26F421741e481';
  
  return {
    to: swapRouter,
    data: encodeFunctionData({
      abi: parseAbi([
        'function exactInputSingle((address,address,uint24,address,uint256,uint256,uint256,uint160)) returns (uint256)'
      ]),
      functionName: 'exactInputSingle',
      args: [{
        tokenIn,
        tokenOut,
        fee,
        recipient,
        deadline,
        amountIn,
        amountOutMinimum,
        sqrtPriceLimitX96: 0n
      }]
    }),
    value: '0x0'
  };
}

Production Best Practices

Validation

Always validate addresses, amounts, and parameters before preparing quotes. Use checksummed addresses.

Error Handling

Implement proper error handling for failed preparations, rejections, and execution failures.

Gas Estimation

OneBalance handles gas estimation, but monitor network congestion and adjust timeouts accordingly.

Testing

Test all operations with small amounts on testnet before production deployment.

Next Steps: Learn about troubleshooting common issues or explore advanced patterns for complex use cases.