Overview
Events tell you what happened, but often you also need the contract’s state at that moment: a holder’s token balance after a transfer, a token’stotalSupply, a pool’s reserves, an oracle price. On-chain, you read that state by calling a view function with the eth_call JSON-RPC method.
The _gs_eth_call SQL function makes that call from inside a Turbo SQL transform. You give it a Solidity function signature, a contract address, arguments and a block number. It ABI-encodes the call, sends it to an RPC endpoint you choose, and returns the decoded result as a new column. You don’t need a separate fetcher service.
This guide builds a pipeline that streams USDC transfers on Ethereum and adds the recipient’s USDC balance at the end of the transfer’s block, then shows how to read multiple return values, choose the block, batch calls with Multicall3, and keep RPC usage under control.
How it works
For each row that reaches it,_gs_eth_call:
- Encodes the function signature and arguments into call data.
- Sends an
eth_calltorpc_urlagainst the contractaddressatblock_number. - Decodes the response using the return types in the signature and returns them as an
ARRAY<VARCHAR>, one element per return value.
NULL for that row. A row-level failure never fails the batch or the pipeline.
Prerequisites
- The Turbo CLI extension installed, and a Goldsky account logged in to your project.
- An EVM JSON-RPC endpoint for the chain you are reading. We recommend Edge RPC. Create an endpoint in the dashboard or with
goldsky edge create, and use its URL in the formhttps://edge.goldsky.com/standard/evm/1?key=YOUR_EDGE_KEY. Any other publichttp(s)://endpoint works too. Private and loopback addresses are blocked. - A sink for the results. This guide uses PostgreSQL through a secret named
MY_POSTGRES. To try the pipeline without a database, swap the sink for a blackhole sink and watch the output with live inspect.
The pipeline sends the RPC requests with your endpoint, so your RPC provider bills the usage. With Edge that is per request; see Edge pricing. Every row that reaches
_gs_eth_call can cost a call, so filter rows down before calling it. See Keep RPC usage under control.Write the function signature
_gs_eth_call takes a human-readable Solidity signature that lists both the input types and the return types:
Use only types, with no parameter names and no spaces. The return types are required, because the function uses them to decode the response.
Pass the arguments as an array of strings in the order the signature declares them, for example
ARRAY[recipient] for balanceOf(address). Cast numeric arguments to text, for example ARRAY[CAST(token_id AS TEXT)]. Pass NULL for functions that take no arguments.
Step 1: Create the pipeline
Create a file namedusdc-balances.yaml. Replace YOUR_EDGE_KEY with your Edge key, or replace the whole URL with another provider’s Ethereum endpoint.
usdc-balances.yaml
What each piece does
What each piece does
sources.usdc_transfers: the ERC-20 transfers dataset for Ethereum, filtered at the source to the USDC contract.start_at: latestprocesses only new transfers going forward._gs_eth_call(...): callsbalanceOf(recipient)on the USDC contract at the end of the transfer’s block and returns an array with one element, the decodeduint256.[1]: picks the first (and only) return value. Arrays in Turbo SQL are 1-indexed.recipient_balance: the balance in USDC base units (6 decimals) as a decimal string, orNULLif the call failed. It is the balance at the end of the block, not right after this transfer. If the recipient moves USDC again later in the same block, this value already includes that. See Choose the block.
Step 2: Deploy and inspect
Validate, then apply:recipient_balance is NULL, check the RPC URL and key first, then the function signature.
Step 3: Query the results
The return values are strings, so cast them before doing arithmetic:CAST(... AS DECIMAL(78, 0)). See Large numbers.
Read multiple return values
When a function returns several values, list every return type in the signature. The result array has one element per type, in the same order. Call the function once in a subquery, then pick the elements you need. This example reads the reserves of the Uniswap V2 USDC/WETH pair at every new Ethereum block:uniswap-v2-reserves.yaml
Choose the block
The last argument sets the block whose state the call reads:
Pass the row’s block for results that are consistent with the event, including when you backfill from
start_at: earliest. A call at NULL reads whatever the chain head is when the pipeline processes the row, so its result depends on when the row is processed.
Historical calls need an RPC endpoint that serves the state for that block. Calls for old blocks on a node that has pruned that state fail and return
NULL.Batch calls with Multicall3
By default,_gs_eth_call sends one eth_call request per row. Set the optional sixth argument, use_multicall, to TRUE to batch eligible rows into Multicall3 calls instead. The optional seventh argument, max_calls_per_multicall, sets how many calls go into each batch. The default is 50.
eth_call. A failed batch therefore costs one request plus one per call in the batch, and takes longer. Individual calls that fail inside a successful batch return NULL for their rows.
Only raise the batch size if you have measured RPC throughput as your bottleneck. Larger batches are more likely to hit the gas limit and fall back to direct calls. When in doubt, leave the optional arguments off.
Keep RPC usage under control
Every row that reaches_gs_eth_call costs RPC usage, and a busy dataset can produce thousands of rows per second. Within each batch, Turbo deduplicates calls with the same contract, call data and block, so identical calls are sent once. Distinct calls still cost one each. Reduce the rows before the call:
- Filter at the source. A source
filter:such asaddress = '0x...'limits the stream to the contracts you care about before any transform runs. - Filter in SQL. Put conditions in the
WHEREclause of the same query, or in an earlier transform, so the call only runs for rows you keep. - Watch a changing set of addresses. Store the addresses in a dynamic table and filter with
WHERE dynamic_table_check('watched_addresses', recipient). You can add and remove addresses without redeploying. The native balances guide shows the full pattern.
Handle failed calls
A call that reverts, times out, or can’t be decoded returnsNULL for its row instead of failing the pipeline. An invalid rpc_url is different: it raises an error for the whole transform. Decide what a NULL should mean for your sink:
- To drop rows whose call failed, wrap the call in a subquery and filter with
WHERE result IS NOT NULL, as the reserves example does. - If the sink upserts on a key, such as one row per holder, drop failed rows. Otherwise a failed call replaces a good value with
NULL. - To keep every event and backfill the value later, keep the row and store the
NULL.
Related
_gs_eth_callreference- Native balances guide, which uses
_gs_evm_get_balancesfor native ETH balances - Dynamic tables
- SQL transforms
- Edge RPC