Read a contract like a table
Query live contract state as typed SQL, then compose it with balances, logs, calldata, simulations, and policy checks.
Goal
Call any read-only Solidity function from SQL and get typed values back. If a contract exposes a view or pure function, read_contract can call it through RPC and return the decoded output as a SQL value.
read_contract performs an eth_call: it ABI-encodes the function call, sends it through $transport, decodes the response, and returns a value you can use in SQL. It does not send a transaction or require a wallet signature.
Use it for token metadata, balances, oracle values, vault parameters, pool state, or risk inputs. Start with a direct call when you only need a few values.
Usage
The basic call shape matches the contract function:
read_contract($transport, contract_address, abi_json, 'functionName', ...function_args)
For a no-argument function, pass the transport, contract address, ABI, and function name. This query reads USDC metadata directly from Ethereum mainnet.
For single-output functions like name() and decimals(), read_contract returns the value itself.
token_name | decimals
-----------|---------
USD Coin | 6
Passing arguments
If the Solidity function takes inputs, pass one SQL value per ABI input after the function name. The ABI determines how each value is encoded.
This example calls balanceOf(address) on USDC for Vitalik’s address, then formats the raw uint256 with USDC’s 6 decimals.
vitalik_usdc
------------
...
The address argument is cast to ADDRESS so the SQL value matches the ABI input type. For functions with more inputs, keep adding arguments in ABI order.
If you need a block override, pass an options JSON before the function arguments:
read_contract(
$transport,
contract_address,
abi_json,
'balanceOf',
json_object('block_number', 21000000),
account_address
)
Return value
read_contract returns a typed SQL value, not raw RPC hex.
- One ABI output returns the value directly.
- Multiple ABI outputs return a
STRUCT. - Named ABI outputs become struct fields you can project with dot notation.
Query: Read pool state and compute a price
The same pattern works for protocol-specific state. Uniswap v4 pools are read through the StateView contract with a poolId; v4 pools do not have individual pool contract addresses.
This query calls getSlot0(bytes32), receives a struct, projects its fields, and computes the WETH/USDC spot price in SQL.
Read the output
Multi-output calls, like getSlot0, return a struct. Project fields with pool_state.field_name, then compute with normal SQL expressions.
weth_price_usdc | tick | lp_fee | protocol_fee
----------------|--------|--------|-------------
... | ... | ... | ...
Once a contract read is decoded, it behaves like any other SQL value: project it, format it, join it, feed it into policy checks, or combine it with simulations.