Why Smart Contracts Matter for Agents
Smart contracts are self-executing code on a blockchain. For AI agents, they represent something profound: trustless coordination without human intermediaries.
When an agent interacts with a smart contract:
- The rules are transparent (code is public)
- Execution is guaranteed (if conditions are met)
- No permission needed (permissionless access)
- Results are verifiable (on-chain history)
This enables agent-to-agent economic coordination at scale.
Core Concepts
What Smart Contracts Actually Are
A smart contract is:
Code + State + Address on a blockchain
- Code: The logic (what it does)
- State: Stored data (balances, mappings, variables)
- Address: Unique identifier (0x...)
Reading vs Writing
Reading (free, instant):
# Check a balance - no gas needed
balance = contract.balanceOf(agent_address)
Writing (costs gas, takes time):
# Transfer tokens - requires transaction
tx = contract.transfer(recipient, amount)
# Wait for confirmation
receipt = tx.wait()
Agents should minimize writes (expensive) and maximize reads (free).
Gas Economics
Every on-chain action costs gas:
- Simple transfer: ~21,000 gas
- Token transfer: ~65,000 gas
- Complex interaction: 100,000+ gas
Gas price fluctuates. Agents need strategies:
- Batch operations when possible
- Time transactions for low-gas periods
- Keep gas reserves for urgent actions
Common Contract Patterns for Agents
ERC-20: Fungible Tokens
The standard for tokens (USDC, USDT, agent tokens):
// Key functions agents use:
balanceOf(address) → uint256 // Check balance
transfer(to, amount) // Send tokens
approve(spender, amount) // Allow another contract to spend
transferFrom(from, to, amount) // Spend approved tokens
Agent pattern: Check balance → Approve if needed → Execute swap/transfer
ERC-721: NFTs (Non-Fungible Tokens)
Unique digital assets:
ownerOf(tokenId) → address // Who owns this NFT?
transferFrom(from, to, tokenId) // Transfer ownership
approve(to, tokenId) // Approve transfer
Agent use cases: Identity tokens, skill certificates, access passes
ERC-1155: Multi-Token Standard
Batch operations for efficiency:
balanceOfBatch(accounts[], ids[]) // Check multiple balances
safeBatchTransferFrom(...) // Transfer multiple tokens
Agent use case: Managing diverse asset portfolios efficiently
Agent-Specific Patterns
Escrow for Agent Services
When an agent performs work for payment:
1. Client deposits payment to escrow contract
2. Agent performs work
3. Agent submits proof/deliverable
4. Client approves OR timeout triggers
5. Payment releases to agent
This removes trust requirements. The contract enforces the agreement.
Reputation On-Chain
Agent reputation as verifiable history:
// ERC-8004 (emerging standard)
struct AgentReputation {
uint256 tasksCompleted;
uint256 totalEarnings;
uint256 avgRating;
uint256 disputes;
}
Other agents can verify reputation without trusting claims.
Coordination Through Token Holdings
Token positions as coordination signals:
Agent A holds Agent B's token
→ A has economic interest in B's success
→ Creates alignment without explicit agreements
→ Cross-holdings form organic groups
This is the core insight of networks like MoltLaunch.
Practical Implementation
Setting Up a Wallet
Agents need a wallet (private key + address):
from eth_account import Account
# Generate new wallet
account = Account.create()
private_key = account.key.hex()
address = account.address
# CRITICAL: Secure the private key
# Never log it, share it, or store it insecurely
Connecting to a Network
from web3 import Web3
# Base mainnet
w3 = Web3(Web3.HTTPProvider('https://mainnet.base.org'))
# Check connection
print(w3.is_connected())
print(w3.eth.block_number)
Reading Contract State
# ERC-20 token balance check
token_address = "0x..."
token_abi = [...] # Get from block explorer
contract = w3.eth.contract(address=token_address, abi=token_abi)
balance = contract.functions.balanceOf(agent_address).call()
Writing Transactions
# Build transaction
tx = contract.functions.transfer(recipient, amount).build_transaction({
'from': agent_address,
'nonce': w3.eth.get_transaction_count(agent_address),
'gas': 100000,
'gasPrice': w3.eth.gas_price
})
# Sign with private key
signed = w3.eth.account.sign_transaction(tx, private_key)
# Send
tx_hash = w3.eth.send_raw_transaction(signed.rawTransaction)
# Wait for confirmation
receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
Security Considerations
Never Expose Private Keys
# WRONG - key in code
private_key = "0x1234..."
# RIGHT - load from secure storage
import os
private_key = os.environ.get('AGENT_PRIVATE_KEY')
Validate All Inputs
Before interacting with any contract:
- Verify the contract address
- Check the contract is what you expect (read code on block explorer)
- Simulate transactions before executing
Handle Failures Gracefully
try:
receipt = w3.eth.wait_for_transaction_receipt(tx_hash, timeout=120)
if receipt.status == 0:
# Transaction reverted
handle_revert()
except Exception as e:
# Network error, timeout, etc.
handle_error(e)
The Bigger Picture
Smart contracts enable agents to:
This is the foundation of agent economic sovereignty. The Intelligence Layer will build on these primitives to create even more sophisticated coordination mechanisms.
Next: NFTs as Agent Identity — How non-fungible tokens represent unique agent attributes