Skip to main content

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’s totalSupply, 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:
  1. Encodes the function signature and arguments into call data.
  2. Sends an eth_call to rpc_url against the contract address at block_number.
  3. Decodes the response using the return types in the signature and returns them as an ARRAY<VARCHAR>, one element per return value.
If the call for a row fails (bad signature, revert, RPC error, decode error), the function returns NULL for that row. A row-level failure never fails the batch or the pipeline.
rpc_url must be a valid http(s):// URL written as a constant. If it is NULL, varies per row, or fails endpoint validation (for example a private or loopback address), the transform raises an execution error instead of returning NULL rows.

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 form https://edge.goldsky.com/standard/evm/1?key=YOUR_EDGE_KEY. Any other public http(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 named usdc-balances.yaml. Replace YOUR_EDGE_KEY with your Edge key, or replace the whole URL with another provider’s Ethereum endpoint.
usdc-balances.yaml
  • sources.usdc_transfers: the ERC-20 transfers dataset for Ethereum, filtered at the source to the USDC contract. start_at: latest processes only new transfers going forward.
  • _gs_eth_call(...): calls balanceOf(recipient) on the USDC contract at the end of the transfer’s block and returns an array with one element, the decoded uint256.
  • [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, or NULL if 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:
Watch the enriched rows as they flow through the transform:
If every 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:
To do the math inside the pipeline instead, convert the value to a 256-bit integer with 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
This pipeline makes one RPC call per block.
Each element is stringified on its own. Scalar return types (uint*, int*, address, bool, string, bytes) come back intact. For nested return types such as string[] or tuples, commas and brackets inside an element aren’t escaped. Prefer one call per scalar field when you need to parse the result reliably.

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.
With batching on, the Multicall3 contract calls your target contract, so the target sees Multicall3 as msg.sender instead of the RPC caller. A view function whose result depends on msg.sender can return a different value or revert. Only turn batching on for functions that are independent of the caller, such as balanceOf(address), which takes the account as an argument.
If the whole Multicall3 request fails, for example with an RPC error or because it hits the node’s per-request gas limit, Turbo retries every call in that batch as a separate direct 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 as address = '0x...' limits the stream to the contracts you care about before any transform runs.
  • Filter in SQL. Put conditions in the WHERE clause 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 returns NULL 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.
Can’t find what you’re looking for? Reach out to us at support@goldsky.com for help.