Generate a ready-to-use indexer from a contract ABI with the squid generation tools.
The squid-gen tools generate a legacy pre-Portal squid built on the deprecated v2 gateway + SQD_API_KEY stack. The generated project is incompatible with the Portal-based flow used throughout the rest of this section. To modernize the generated code, follow the gateway-to-Portal migration guide.
V2 gateway requests require an API key. Set SQD_API_KEY in .env before running gateway-based examples, or pass apiKey: process.env.SQD_API_KEY in GatewaySettings.
As of 2025-01-01 we no longer maintain the old squid-gen tool.
SQD provides tools for generating ready-to-use squids that index events and function calls of smart contracts. EVM/Solidity and WASM/ink! smart contracts are supported. The tools can be configured to make squids that save data to a PostgreSQL database or to a file-based dataset. All that is required is NodeJS, Squid CLI and, if your squid will be using a database, Docker.Squid generation procedure is very similar for both contract types. Here are the steps:
Create a new blank squid with sqd init using a suitable template:
# for EVM/Solidity contractssqd init my-squid -t abi# OR# for WASM/ink! contractssqd init my-squid -t https://github.com/subsquid-labs/squid-ink-abi-template
Enter the squid folder and install the dependencies:
cd my-squidnpm ci
Write the configuration of the future squid to squidgen.yaml. Retrieve any necessary contract ABIs and store them at ./abi.Alternatively, skip to the next step and specify the configuration via CLI. Note: some features will not be available.
Generate and build the squid code:
npx squid-gen config squidgen.yaml
npm run build
If you chose to configure the tool via CLI instead, do so now. Here’s an example:
Test the complete squid by running it locally. Start a processor with
node -r dotenv/config lib/main.js
If your squid will be serving GraphQL also run npx squid-graphql-server in a separate terminal. Make sure that the squid saves the requested data to its target:
for PostgreSQL-based squids you can also connect to the database with PGPASSWORD=postgres psql -U postgres -p 23798 -h localhost squid and take a look at the contents;
if it is storing data to a file-based dataset, wait for the first filesystem sync then verify that all the expected files are present and contain the expected data. If your squid produces data at a low rate, you may have to tweak the chunkSizeMb setting and/or add a ctx.store.setForceFlush() call to manually write dataset chunks at appropriate intervals.
At this point your squid is ready. You can run it on your own infrastructure or deploy it to SQD Cloud.
Configuration
A valid config for the squid-gen config is a YAML file with the following sections:
target section describes how the scraped data should be stored. Set
target: type: postgres
to use a PostgreSQL database that can be presented to users as a GraphQL API or used as-is. Another option is to store the data to a file-based dataset.
contracts is a list of contracts to be indexed. Define the following fields for each contract:
name
address
range (optional): block range to be indexed. An object with from and to properties, each of which can be omitted. Defaults to indexing the whole chain.
abi (optional on EVM): path to the contract JSON ABI. If omitted for an EVM contract, the tool will attempt to fetch the ABI by address from the Etherscan API or a compatible alternative set by the etherscanApi root option.
proxy (EVM-only): when indexing a proxy contract for events or calls defined in the implementation, set this option to its address and the address option to the address of the implementation contract. That way the tool will retrieve the ABI of the implementation and use it to index the output of the proxy.
events (optional): a list of events to be indexed or a boolean value. Set to true to index all events defined in the ABI. Defaults to false, meaning that no events are to be indexed.
functions (EVM-only, optional): a list of functions the calls of which are to be indexed or a boolean value. Set to true to index calls of all functions defined in the ABI. Defaults to false, meaning that no function calls are to be indexed.
etherscanApi (EVM-only, optional): Etherscan API-compatible endpoint to fetch contract ABI by a known address. Default: https://api.etherscan.io/.
file-store targets
Currently the only file-based data target type supported by squid-gen packages is parquet. When used, it requires that the path field is also set alongside type. A path can be a local path or an URL pointing to a folder within a bucket on an S3-compatible cloud service.Support for file-store is in alpha stage. Known caveats:
Unlike their PostgreSQL-powered equivalents, the squids that use file-store may not write their data often. You may have to configure the chunkSizeMb parameter of the Database class and/or call ctx.store.setForceFlush() when appropriate to strike an acceptable balance between the lag of the indexed data and the number of files in the resulting dataset. See the Filesystem datasets overview for details.
For parquet targets, the Decimal(38) column type is used by the code generator to represent uint256. This is done for compatibility reasons: very few tools seem to support reading wider decimals from Parquet files. If you’re getting a lot of errors containing value ... does not fit into Decimal(38, 0), consider replacing the Decimal(38) column type with Decimal(78) or String() at src/table.ts.
At the moment, squids generated with file-based data targets contain a lot of facilities for managing the database and have to be stripped of them before use.
Strip the squid folder for file-store
Steps to convert a squid made with a database-enabled template for use with file-store:
In squid.yaml, remove the deploy.addons section and replace the deploy.api section with
api: cmd: [ "sleep", "3600" ]
Install any required file-store packages.
# if target.type was `parquet`npm install @subsquid/file-store-parquet# if target.path was an S3 URLnpm install @subsquid/file-store-s3
Configuration examples
EVM/Solidity
Index LiquidationCall events of the AAVE V2 Lending Pool contract, starting from block 11362579 when it was deployed. Save the results to PostgreSQL. Use the ABI located at ./abi/aave-v2-pool.json.
Index events and function calls by the DPX contract (a proxy) on Arbitrum, based on the ABI of the implementation contract retrieved from Arbiscan API. Save the results to Parquet files at ’./data’.
Note: this example is known to run into the integer length issue described in the file-store targets section. One way to make it work is to widen all Decimal column types from 38 to 78 symbols:
sed -i -e 's/38/78/g' src/table.ts
Index all events and function calls of the Positions NFT and Factory contracts of Uniswap, send the results to the uniswap-data folder of the subsquid-testing-data bucket.