> ## Documentation Index
> Fetch the complete documentation index at: https://cobo.com/products/agentic-wallet/manual/llms.txt
> Use this file to discover all available pages before exploring further.

# Smart contracts and contract calls

> What smart contracts are, how to read and call them, and the ERC-20 approval pattern.

## What is a smart contract

A **smart contract** is a program deployed on a blockchain at a specific address. Like a wallet address, a contract address is a 42-character hex string on EVM chains — but unlike a wallet, it has no private key and cannot initiate transactions on its own. It only executes when called by an external account or another contract.

When you send a transaction to a contract address with encoded function data, the contract executes that function. The execution is deterministic — every node on the network reaches the same result. Once executed, the result is permanent.

Contracts are the building blocks of all of DeFi: Uniswap's swap logic lives in contracts, Aave's lending pool is a contract, and every ERC-20 token is a contract.

## Read functions vs. write functions

| Type                 | Gas required | Changes state | Examples                                         |
| -------------------- | ------------ | ------------- | ------------------------------------------------ |
| **Read (view/pure)** | No           | No            | `balanceOf()`, `getReserves()`                   |
| **Write**            | Yes          | Yes           | `transfer()`, `swap()`, `deposit()`, `approve()` |

Read calls are free and instant — they query the current state of the chain without submitting a transaction. Write calls submit a signed transaction and cost gas. With Cobo Agentic Wallet, all write calls go through the policy engine before being signed and broadcast.

## What is an ABI

An **ABI** (Application Binary Interface) is a description of a contract's functions — their names, parameter types, and return types. Think of it as the contract's instruction manual: it tells your code how to talk to the contract.

Without the ABI, you would need to manually encode raw bytes to call a contract. With the ABI, libraries like `eth_abi` (Python) and `viem` (TypeScript) handle all the encoding for you.

ABIs are publicly available for most major protocols on block explorers like Etherscan (look for the "Contract" tab on any verified contract).

## How contract calls work

### EVM chains

To call a function on a smart contract, you send a transaction containing **calldata** — a hex-encoded byte string that specifies which function to call and what arguments to pass.

Calldata is structured as:

* A **4-byte function selector** — derived from the function's name and parameter types
* **Encoded arguments** — each parameter encoded according to its type

Libraries like `eth_abi` (Python) and `viem` (TypeScript) handle this encoding for you. You rarely need to encode manually.

### Solana

On Solana, what EVM calls "smart contracts" are called **programs**. The interaction model is different: instead of calldata, you send a list of **instructions**, each specifying the program to invoke, an **account list** of all accounts the program can read or modify, and the instruction data.

A key difference from EVM: Solana programs are stateless. All state is stored in separate accounts, not inside the program itself. This is why every Solana transaction must explicitly list the accounts it will touch.

## The ERC-20 approval pattern

Unlike native ETH, ERC-20 tokens can only be moved by the token owner — or by a contract the owner has explicitly authorized. DeFi protocols cannot pull tokens from your wallet until you call `approve` on the token contract first.

This means most DeFi interactions are a **two-step process**:

1. **Approve** — call `approve` on the token contract to authorize the DeFi protocol to spend up to a certain amount of your tokens.
2. **Interact** — call the DeFi protocol function (swap, deposit, repay, etc.).

Both steps are separate on-chain transactions, each requiring gas.

<Tip>
  Approving a very large amount (sometimes called "infinite approval") means you only need to do the approve step once per protocol. However, it carries more risk — if the protocol is ever exploited, the attacker can drain your full approved amount. Many users prefer to approve only what they need for each transaction.
</Tip>

## Events

Contracts emit **events** when significant state changes occur. Events are stored in the transaction receipt and can be monitored by off-chain services.

Common examples:

| Event      | Contract   | Fired when                          |
| ---------- | ---------- | ----------------------------------- |
| `Transfer` | Any ERC-20 | Tokens move between addresses       |
| `Swap`     | Uniswap    | A trade is executed                 |
| `Supply`   | Aave       | A user deposits collateral          |
| `Approval` | Any ERC-20 | A spend allowance is set or updated |

For agents, events are particularly useful as triggers: an agent can listen for a specific event — such as a price threshold being crossed or a position becoming undercollateralized — and automatically execute a follow-up transaction in response.

Block explorers display events in the transaction receipt, making it easy to see exactly what happened in a given transaction.

## Finding contract addresses and ABIs

* **Block explorers**: Search for the protocol name on [Etherscan](https://etherscan.io), [Basescan](https://basescan.org), or [Polygonscan](https://polygonscan.com). Verified contracts show their full ABI and source code.
* **Protocol documentation**: Uniswap, Aave, Curve, and other major protocols publish official deployment address lists in their documentation.

<Warning>
  Contract addresses are network-specific. The Uniswap router on Base mainnet is a different address than on Base Sepolia testnet. Always verify you are using the address for the correct network.
</Warning>
