Query contract state with SQL
Call an EVM contract function from SQL, pin its state, and apply the same call to relation rows.
Goal
Query USDC balanceOf(address) from Ethereum mainnet. Keep exact UINT256 values separate from formatted display values.
The notebook reads live state, pins one read to a block, and applies the same call to relation rows.
1. Turn balanceOf into a typed column #
Call USDC balanceOf(address) for one account. The ABI defines
the argument and output types, so read_contract returns a
UINT256.
format_units(raw_balance, 6) returns display text. The unpinned
client reads live state, so the value can change.
2. Pin a block and keep the exact value #
Pin the client when later reads must use the same block. pin
returns a client for Ethereum block 25,601,821.
raw_balance remains exact, while usdc_balance is
display text. The RPC must retain state for the pinned block. For one
historical read, read_contract_at
accepts a block number directly.
3. Scale the same call across rows #
Pass a column as the function argument to evaluate the same
balanceOf call for each row.
Each row keeps its ADDRESS, exact UINT256, and
display VARCHAR. This query makes one RPC-backed call per row.
Use read_contract_multicall
for larger or failure-tolerant batches.