# code_at

Reads an account's runtime bytecode from live RPC state, the exact state identified by a pinned CLIENT, or retained execution state. Without a CLIENT, it builds a reusable READ and does not access state.

Canonical HTML: <https://determica.com/docs/functions/code_at>

- Kind: scalar function
- Category: [Chain reads](/docs/category/chain-reads.md)
- Tags: RPC, Program Compiler, Simulate

Reads an account's runtime bytecode from live RPC state, the exact state identified by a pinned CLIENT, or retained execution state. Without a CLIENT, it builds a reusable READ and does not access state.

## Overload 1: code_at(CLIENT, ADDRESS)

```sql
code_at(CLIENT, ADDRESS)
```

Reads the CLIENT's current state context. For live clients that means latest; pinned and execution clients retain their own exact state identity.

- Kind: scalar
- Execution context: Live RPC
- Behavior: Immediate Read
- Execution modes: Live Client, Pinned Client, Execution Client
- Input shape: Scalar Arguments
- Planning contract: Runtime
- Cardinality: One Per Input
- Determinism: External State
- Return shape: Scalar
- Schema stability: Fixed
- Stability: Stable
- Side effects: network_io

### Requirements

- client_state_context
- rpc

### Risks

- provider_limits
- chain_reorganization

### Inputs

- `client` (CLIENT; required; positional) — Readable EVM state represented by a live, pinned, or retained execution CLIENT.
- `address` (ADDRESS; required; positional) — Account address whose runtime bytecode should be read.

### Returns

Deployed bytecode

Returns runtime bytecode as BYTES. A live RPC no-code response is NULL; pinned and execution state represent known no-code as empty BYTES. NULL or empty client, NULL address, and live RPC/decoding failures return NULL. Historical live reads and pinned-state hydration may require archive access.

- `code` (BYTES)

Deployed bytecode. Returns runtime bytecode as BYTES. A live RPC no-code response is NULL; pinned and execution state represent known no-code as empty BYTES. NULL or empty client, NULL address, and live RPC/decoding failures return NULL. Historical live reads and pinned-state hydration may require archive access.

### Examples

_Named parameters · Needs RPC · RPC required_

```sql
SELECT code_at(
    $client, -- client
    '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'::address -- Ethereum mainnet WETH
) AS code;
```

## Overload 2: code_at(CLIENT, ADDRESS, BIGINT)

```sql
code_at(CLIENT, ADDRESS, BIGINT)
```

Reads runtime bytecode at an exact block height through a live CLIENT. Pinned and execution CLIENTs reject an explicit height because their state identity is already fixed.

- Kind: scalar
- Execution context: Live RPC
- Behavior: Immediate Read
- Execution modes: Live Client, Pinned Client, Execution Client
- Input shape: Scalar Arguments
- Planning contract: Runtime
- Cardinality: One Per Input
- Determinism: External State
- Return shape: Scalar
- Schema stability: Fixed
- Stability: Stable
- Side effects: network_io

### Requirements

- client_state_context
- rpc

### Risks

- provider_limits
- chain_reorganization

### Inputs

- `client` (CLIENT; required; positional) — Readable EVM state represented by a live, pinned, or retained execution CLIENT.
- `address` (ADDRESS; required; positional) — Account address whose runtime bytecode should be read.
- `block_number` (BIGINT; required; positional) — Optional historical height for a live CLIENT. NULL is equivalent to omitting the argument.

### Returns

Deployed bytecode

Returns runtime bytecode as BYTES. A live RPC no-code response is NULL; pinned and execution state represent known no-code as empty BYTES. NULL or empty client, NULL address, and live RPC/decoding failures return NULL. Historical live reads and pinned-state hydration may require archive access.

- `code` (BYTES)

Deployed bytecode. Returns runtime bytecode as BYTES. A live RPC no-code response is NULL; pinned and execution state represent known no-code as empty BYTES. NULL or empty client, NULL address, and live RPC/decoding failures return NULL. Historical live reads and pinned-state hydration may require archive access.

### Examples

_Named parameters · Needs RPC · RPC required_

```sql
SELECT code_at(
    $client, -- client
    '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'::address, -- Ethereum mainnet WETH
    21000000 -- Ethereum mainnet block number
) AS code;
```

## Overload 3: code_at(ADDRESS)

```sql
code_at(ADDRESS)
```

Records one address for a later account-code lookup; construction does not access state.

- Kind: scalar
- Execution context: Live RPC
- Behavior: Read Builder
- Execution modes: Read Constructor
- Input shape: Scalar Arguments
- Planning contract: Deferred Execution
- Cardinality: One Per Input
- Determinism: Deterministic
- Return shape: Scalar
- Schema stability: Fixed
- Stability: Stable
- Side effects: None documented

### Requirements

- rpc

### Inputs

- `address` (ADDRESS; required; positional) — Contract or account address whose deployed code should be observed.

### Returns

Account-code READ

Returns a READ whose successful EVM_VALUE contains the account's runtime bytecode.

- `read` (READ)

Account-code READ. Returns a READ whose successful EVM_VALUE contains the account's runtime bytecode.

## Guidance

### Historical code

The two-argument overload reads latest from a live CLIENT, so its result can change between runs.

Use block_number for reproducible bytecode evidence. Older heights may require an archive-capable provider.

Relation-sized attached pinned reads may share one hash-pinned JSON-RPC batch; each request retains the CLIENT's block hash and canonicality requirement.

### State selection

An explicit block number is valid only for a live CLIENT. Supplying one for pinned or execution state raises STATE_SOURCE_INCONSISTENT.

Pinned reads use the CLIENT's block hash and canonicality requirement. An inconsistent identity or provider result raises STATE_SOURCE_INCONSISTENT.

### Hydration and retained state

A detached pinned CLIENT can use retained state only; a cache miss raises HYDRATION_UNAVAILABLE. Attached pinned hydration can also raise HYDRATION_UNAVAILABLE when its provider cannot be reached.

Pinned hydration reports HISTORICAL_STATE_UNAVAILABLE when the provider does not retain the required state.

Execution reads use retained execution state and raise CHECKPOINT_UNAVAILABLE when that state cannot be resolved.

### Evaluation

The READ accesses state only when an OBSERVATION consumes it during PROGRAM execution.

## Additional examples

```sql
SELECT code_at('0x0000000000000000000000000000000000000001'::ADDRESS);
```

## Related functions

- [evm_disassemble](/docs/functions/evm_disassemble.md) — Usually Before
- [storage_at](/docs/functions/storage_at.md) — See Also
