Query contract state with SQL
Call an EVM contract function from SQL, pin the read to one block, and use a column as the function argument.
Goal
Query USDC balanceOf(address) from Ethereum mainnet. Keep exact UINT256 values separate from formatted display values.
The notebook starts with a live read, repeats it at a pinned block, then uses an account column as the balanceOf argument.
1. Read balanceOf as UINT256 #
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 #
Use pin to resolve Ethereum
block 25,601,821 and return a client fixed to that block.
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. Call balanceOf for each input row #
Pass the account column as the function argument. read_contract evaluates balanceOf once per row.
Each result row contains the account as ADDRESS, the raw balance as UINT256, and the formatted
balance as VARCHAR.
The query makes one RPC-backed call per row. Use read_contract_multicall
to batch calls and inspect failures per input.