Docs
Search⌘ K
  • Home
  • About The Graph
  • Supported Networks
  • Protocol Contracts
  • AI Tooling
  • Subgraphs
    • Substreams
      • Indexer Software
        • Gateway Software
          • Data Services
            • Resources
              Subgraphs > Guides & Best Practices

              4 minutes

              Improve Indexing Speed by Avoiding eth_calls

              An eth_call is a call from a Subgraph handler to a blockchain node to read contract state. Each one is an RPC round trip that takes a significant amount of time, so eth_calls are the most common cause of slow syncs. Where you can, design smart contracts to emit all the data your Subgraph needs, so handlers never need to call the contract.

              Why eth_calls Slow Down Indexing

              Subgraphs are optimized to index event data emitted from smart contracts. A Subgraph can also index the data coming from an eth_call, however, this can significantly slow down Subgraph indexing as eth_calls require making external calls to smart contracts. Their speed depends not on the Subgraph but on the connectivity and responsiveness of the node being queried. Minimizing or eliminating eth_calls significantly improves indexing speed.

              What an eth_call Looks Like

              eth_calls are often necessary when the data required for a Subgraph is not available through emitted events. For example, consider a scenario where a Subgraph needs to identify whether ERC20 tokens are part of a specific pool, but the contract only emits a basic Transfer event that does not contain that data:

              1event Transfer(address indexed from, address indexed to, uint256 value);

              Suppose the tokens’ pool membership is determined by a state variable named getPoolInfo. In this case, the handler needs an eth_call to read it:

              1import { Address } from '@graphprotocol/graph-ts'2import { ERC20, Transfer } from '../generated/ERC20/ERC20'3import { TokenTransaction } from '../generated/schema'45export function handleTransfer(event: Transfer): void {6  let transaction = new TokenTransaction(event.transaction.hash.toHex())78  // Bind the ERC20 contract instance to the given address:9  let instance = ERC20.bind(event.address)1011  // Retrieve pool information via eth_call12  let poolInfo = instance.getPoolInfo(event.params.to)1314  transaction.pool = poolInfo.toHexString()15  transaction.from = event.params.from.toHexString()16  transaction.to = event.params.to.toHexString()17  transaction.value = event.params.value1819  transaction.save()20}

              This works, but every Transfer now waits on an RPC call, which slows indexing.

              How to Eliminate eth_calls

              Ideally, the smart contract should be updated to emit all necessary data within events. For instance, modifying the smart contract to include pool information in the event could eliminate the need for eth_calls:

              1event TransferWithPool(address indexed from, address indexed to, uint256 value, bytes32 indexed poolInfo);

              With this update, the Subgraph can directly index the required data without external calls:

              1import { Address } from '@graphprotocol/graph-ts'2import { ERC20, TransferWithPool } from '../generated/ERC20/ERC20'3import { TokenTransaction } from '../generated/schema'45export function handleTransferWithPool(event: TransferWithPool): void {6  let transaction = new TokenTransaction(event.transaction.hash.toHex())78  transaction.pool = event.params.poolInfo.toHexString()9  transaction.from = event.params.from.toHexString()10  transaction.to = event.params.to.toHexString()11  transaction.value = event.params.value1213  transaction.save()14}

              This is much faster because the handler no longer makes any eth_calls.

              How to Reduce eth_calls You Cannot Remove

              If you cannot change the contract, cut calls in this order:

              1. Read from the event: Check event.params before you add any call.
              2. Cache values that never change: Fetch values such as name, symbol, and decimals once, when the entity is created, and reuse the stored value.
              3. Compute in the Subgraph: Derive values such as totalSupply from mint and burn events instead of calling the contract.
              4. Declare the calls you keep (see the next section).

              For more strategies, read Improve Subgraph Indexing Performance Easily: Reduce eth_calls.

              Reducing the Runtime Overhead of eth_calls

              For the eth_calls that can not be eliminated, the runtime overhead they introduce can be minimized by declaring them in the manifest. When Graph Node processes a block, it runs all declared eth_calls in parallel before the handlers run. Calls that are not declared are executed sequentially when handlers run. The runtime improvement comes from performing calls in parallel rather than sequentially - that helps reduce the total time spent in calls but does not eliminate it completely.

              You can declare eth_calls only for event handlers, and only with specVersion 1.2.0 or later. In the manifest, add a calls section to the handler:

              1event: TransferWithPool(indexed address,indexed address,uint256,indexed bytes32)2handler: handleTransferWithPool3calls:4  ERC20.poolInfo: ERC20[event.address].getPoolInfo(event.params.to)

              The calls entry is the call declaration. The part before the colon is a text label used only in error messages. The part after the colon has the form Contract[address].function(params). Permissible values for address and params are event.address and event.params.<name>.

              The handler itself accesses the result of this eth_call exactly as in the previous section by binding to the contract and making the call. Graph Node caches the results of declared eth_calls in memory, so the call in the handler reads the cached result instead of making an RPC call.

              Learn More

              • Declared eth_calls: the manifest reference.
              • Guides & Best Practices overview: the full checklist and how this page fits with the rest of the section.
              ⁠Edit on GitHub⁠

              OverviewImmutable Entities and Bytes IDs
              On this page
              • Why eth_calls Slow Down Indexing
              • What an eth_call Looks Like
              • How to Eliminate eth_calls
              • How to Reduce eth_calls You Cannot Remove
              • Reducing the Runtime Overhead of eth_calls
              • Learn More
              The GraphStatusTestnetBrand AssetsForumSecurityPrivacy PolicyTerms of Service