Smart Contracts · Multi-Chain · ABI-Encoded

Smart Contract API for AI Agents

Call any smart contract on any supported chain from your AI agent — no ABI encoding headaches, no gas management, no nonce juggling. Purple Flea handles the infrastructure so your agent handles the logic.

Read the Docs Get Free Credits

Why AI Agents Need a Smart Contract API

Simple token transfers are table stakes. The real value in on-chain agent activity comes from interacting with arbitrary smart contracts: harvesting DeFi yield, casting governance votes, minting NFTs, adding liquidity to AMMs, and composing multi-step on-chain strategies. These interactions require ABI encoding, precise gas estimation, nonce management, and often retry logic for failed transactions.

Implementing all of this inside an LLM agent loop is brittle and expensive. A single ABI encoding bug can silently lose funds. A missed nonce causes a stuck transaction queue. Purple Flea's Smart Contract API abstracts every one of these concerns behind a single clean HTTP interface your agent can call.

No private key management is required on your side. Purple Flea custodies the signing infrastructure with HSM-backed keys, so your agent code never touches a raw private key — reducing your attack surface to near zero.

What the API Handles For You

🔄

ABI Encoding & Decoding

Pass function name + typed arguments as JSON. Purple Flea encodes calldata, executes, and decodes return values back to JSON.

Nonce Management

Concurrent agent calls never collide. A distributed nonce manager serializes writes per address with automatic gap-filling.

Gas Estimation

Dynamic gas limit estimation with configurable safety multipliers. EIP-1559 tip optimization based on live mempool data.

🔁

Automatic Retries

On-chain write failures are automatically retried with increasing tip until included or timeout. Your agent gets one clean success or failure.

📈

Event Log Queries

Query historical logs or subscribe to live events by contract address and topic. Decoded into typed JSON automatically.

🌎

Multi-Chain Routing

One API, eight chains. Specify chain in the request or let Purple Flea auto-route to the cheapest available.

Supported Chains

Purple Flea supports the following networks for smart contract interactions. All chains share the same API surface — only the chain parameter changes.

Chain Read Calls Write Calls Event Logs Avg Gas Cost
Ethereum Mainnet $$$$
Arbitrum One $
Base $
Optimism $
Polygon $
BNB Smart Chain $$
Solana (Programs) $
Tron $

Code Examples

1. Read an ERC-20 Balance (eth_call)

Use the /contract/read endpoint for any state-reading operation. This never creates a transaction and costs no gas.

JavaScript
// Read USDC balance for any address const response = await fetch('https://api.purpleflea.com/v1/contract/read', { method: 'POST', headers: { 'Authorization': 'Bearer YOUR_API_KEY', 'Content-Type': 'application/json' }, body: JSON.stringify({ chain: 'arbitrum', contract: '0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8', // USDC on Arb function: 'balanceOf', args: ['0xYourAgentAddress'], abi: ['function balanceOf(address) view returns (uint256)'] }) }); const { result } = await response.json(); // result: "1500000000" (1500 USDC in 6-decimal raw units) console.log(`Balance: ${result / 1e6} USDC`);

2. Write Call: DeFi Protocol deposit()

Write calls create and broadcast a signed transaction. Purple Flea handles signing, gas, and nonce.

JavaScript
// Deposit 500 USDC into Aave v3 on Base const tx = await fetch('https://api.purpleflea.com/v1/contract/write', { method: 'POST', headers: { 'Authorization': 'Bearer YOUR_API_KEY', 'Content-Type': 'application/json' }, body: JSON.stringify({ chain: 'base', contract: '0xA238Dd80C259a72e81d7e4664a9801593F98d1c5', // Aave Pool on Base function: 'supply', args: [ '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', // USDC on Base '500000000', // 500 USDC (6 decimals) '0xYourAgentAddress', // onBehalfOf 0 // referralCode ], abi: ['function supply(address asset, uint256 amount, address onBehalfOf, uint16 referralCode)'], gasMultiplier: 1.2 // 20% safety buffer }) }); const { txHash, status } = await tx.json(); // txHash: "0xabc123...", status: "confirmed"

3. Listen for Transfer Events

Query historical Transfer events or poll for new ones. All event data is ABI-decoded into structured JSON.

JavaScript
// Get last 100 Transfer events for USDC on Arbitrum const events = await fetch('https://api.purpleflea.com/v1/contract/events', { method: 'POST', headers: { 'Authorization': 'Bearer YOUR_API_KEY' }, body: JSON.stringify({ chain: 'arbitrum', contract: '0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8', event: 'Transfer', fromBlock: 'latest-100', filter: { to: '0xYourAgentAddress' } }) }); const { logs } = await events.json(); // logs[0]: { from: "0x...", to: "0x...", value: "1000000", blockNumber: 12345678 }

4. ABI Encoding Under the Hood

For advanced use cases, you can inspect what Purple Flea generates or do manual calldata encoding via the utility endpoint.

JavaScript
// Get raw ABI-encoded calldata for any function call const encoded = await fetch('https://api.purpleflea.com/v1/abi/encode', { method: 'POST', headers: { 'Authorization': 'Bearer YOUR_API_KEY' }, body: JSON.stringify({ function: 'transfer(address,uint256)', args: ['0xRecipient', '1000000000000000000'] }) }); // result.calldata: "0xa9059cbb000000000000000000000000...{padded recipient}...{padded amount}" // Selector: keccak256("transfer(address,uint256)")[0:4] = 0xa9059cbb

Key Use Cases for Agent-Driven Smart Contract Calls

Purple Flea vs Raw ethers.js

Raw ethers.js in Agent Code

  • ✗ Must store and protect private keys
  • ✗ Write nonce management code yourself
  • ✗ Handle gas estimation edge cases
  • ✗ Build retry logic for stuck txns
  • ✗ Manage RPC endpoints per chain
  • ✗ Parse raw ABI return bytes
  • ✗ Monitor tx confirmation manually

Purple Flea Smart Contract API

  • ✓ No private key exposure in agent code
  • ✓ Automatic nonce management
  • ✓ Optimal gas estimation built-in
  • ✓ Automatic retry with tip escalation
  • ✓ One API for all 8 chains
  • ✓ Decoded JSON return values
  • ✓ Confirmation webhooks available

Fee Table: Cost Per Contract Call

Chain Read Call (eth_call) Write Call (signed tx) Event Query (per 1k logs)
Ethereum Mainnet $0.0005 $0.002 + gas $0.01
Arbitrum One $0.0001 $0.001 + gas $0.005
Base $0.0001 $0.001 + gas $0.005
Optimism $0.0001 $0.001 + gas $0.005
Polygon $0.0001 $0.001 + gas $0.005
BNB Smart Chain $0.0002 $0.001 + gas $0.005
Solana $0.0001 $0.001 + gas $0.005
Tron $0.0001 $0.001 + gas $0.005

Read calls are always free of chain gas (they are local simulations). Write call fees are Purple Flea's platform fee on top of actual network gas costs which are passed through at cost.

Getting Started in 3 Steps

  1. Claim free credits — visit faucet.purpleflea.com to get free tokens to try the full API stack.
  2. Make your first read call — call /v1/contract/read with any public ERC-20 contract. No funds needed for read operations.
  3. Execute a write call — fund your Purple Flea agent wallet and call /v1/contract/write to interact with any deployed contract on any supported chain.

Full API reference, authentication guide, and SDK examples are available in the Purple Flea documentation.


Related Pages