Contract classes generated by squid-evm-typegen wrap eth_call state queries. They need a JSON-RPC endpoint to talk to: the Portal data source does not use one and the handler context provides no built-in chain access, so construct a client explicitly with RpcClient from @subsquid/rpc-client:
RPC_ETH_HTTP in three ways:
- for local runs, simply update the local
.envfile; - for squids deployed to Cloud define it as a secret on your Cloud account;
- if you are using the RPC addon, leave it to the Cloud to define it for you.
Contract class
The EVM contract state is accessed using the Contract class generated by squid-evm-typegen. Its constructor takes
- a chain access object - anything of the shape
{_chain: {client}}, whereclienthas acall(method, params)method; anRpcClientinstance fits; - a block reference - any object with a
heightfield; the state is queried at that block height; - the contract address.
squid-evm-typegen will contain the following class:
Contract with the RPC client and the current block, then query the contract state at that block:
Batch state queries
The MakerDAO Multicall contract was designed to batch multiple state queries into a single contract call. In the context of indexing, it normally significantly improves the indexing speed since JSON RPC calls are typically the bottleneck. Multicall contracts are deployed in many EVM chains, see the contract repo for addresses. You can use any of them with amulticall Typescript module that is generated when running squid-evm-typegen with --multicall option. The module exports a Multicall class with this method:
func: the contract function to be calledcalls: an array of tuples[contractAddress: string, args]. Each specified contract will be called with the specified arguments.pageSizeis an optional maximum number of calls per JSON-RPC request. Large page sizes may cause timeouts.
[function, address, arguments] tuples for
mixed calls. aggregate() has the same overloads but throws when a call fails.
Multicall is constructed exactly like the other generated Contract classes - with a chain access object, a block reference and the address of a deployed Multicall contract. A typical usage is as follows:
src/main.ts