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.

Scalar functionLive RPC RPCProgram CompilerSimulate

Example

Live RPC

-- Runtime bytecode of WETH
SELECT code_at(
    $client,
    '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'::address -- Ethereum mainnet WETH
) AS code;

API reference

code_at(CLIENT, ADDRESS) # Immediate Read · RPC required

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

Inputs

Name Type Use
client CLIENT

Readable EVM state represented by a live, pinned, or retained execution CLIENT.

required positional
address ADDRESS

Account address whose runtime bytecode should be read.

required positional

Returns

Name Type
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 NULL height return NULL; live RPC and decoding failures raise. Historical live reads and pinned-state hydration may require archive access.

code_at(CLIENT, ADDRESS, BIGINT) # Immediate Read · RPC required

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.

Inputs

Name Type Use
client CLIENT

Readable EVM state represented by a live, pinned, or retained execution CLIENT.

required positional
address ADDRESS

Account address whose runtime bytecode should be read.

required positional
block_number BIGINT

Optional historical height for a live CLIENT. NULL gives NULL without a read.

required positional

Returns

Name Type
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 NULL height return NULL; live RPC and decoding failures raise. Historical live reads and pinned-state hydration may require archive access.

Overload examples

Live RPC

-- Runtime bytecode of WETH at Ethereum block 21000000
SELECT code_at(
    $client,
    '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'::address, -- Ethereum mainnet WETH
    21000000
) AS code;
code_at(CLIENT, ADDRESS, BIGINT, JSON) # Immediate Read · RPC required

Reads runtime bytecode at an exact block height through a live CLIENT; with on_error null_on_failure, a failed live read is NULL instead of an error.

Inputs

Name Type Use
client CLIENT

Readable EVM state represented by a live, pinned, or retained execution CLIENT.

required positional
address ADDRESS

Account address whose runtime bytecode should be read.

required positional
block_number BIGINT

Optional historical height for a live CLIENT. NULL gives NULL without a read.

required positional
options JSON

Constant JSON object. on_error is raise (the default) or null_on_failure: a live read the node could not answer is NULL and no code is empty BYTES, for a caller that records a failed read as unread. Any other key raises.

required positional

Returns

Name Type
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 NULL height return NULL; live RPC and decoding failures raise. Historical live reads and pinned-state hydration may require archive access.

Overload examples

Live RPC

-- Runtime bytecode of WETH at Ethereum block 21000000, NULL if the node cannot answer
SELECT code_at(
    $client,
    '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'::address, -- Ethereum mainnet WETH
    21000000,
    '{"on_error":"null_on_failure"}'
) AS code;
code_at(ADDRESS) # Read Builder

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

Inputs

Name Type Use
address ADDRESS

Contract or account address whose deployed code should be observed.

required positional

Returns

Name Type
read READ

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

Overload examples

Runs locally

SELECT code_at('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'::ADDRESS); -- Ethereum mainnet WETH

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

Only overloads returning READ defer state access until an OBSERVATION consumes the READ during PROGRAM execution.