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:
Enter the squid folder and install the dependencies:
-
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:
If you chose to configure the tool via CLI instead, do so now. Here’s an example:
See
npx squid-gen-abi --help for all options.
-
Prepare your squid for launching. If it is using a database, start a PostgreSQL container, then regenerate and apply migrations:
If it is storing its data to a dataset, strip the project folder of database-related facilities that are no longer needed.
-
Test the complete squid by running it locally. Start a processor with
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:
- if it is serving GraphQL, visit the local GraphiQL playground;
- 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:
-
archive is an endpoint URL of a SQD Network gateway. Find an appropriate gateway at the Supported networks page or with
sqd gateways.
-
target section describes how the scraped data should be stored. Set
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:
-
If a S3 URL is used, then the S3 region, endpoint and user credentials will be taken from the default environment variables. Fill your
.env file and/or set your SQD Cloud secrets accordingly.
-
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 Filesystem store 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:
- Remove unneeded files and packages.
- Replace
commands.json with the one from the file-store-parquet-example repo.
- In
squid.yaml, remove the deploy.addons section and replace the deploy.api section with
- Install any required
file-store packages.
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:
-
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.
Notes:
- This example is also susceptible to the integer length issue and will drop at least two events if used as-is, without widening the column types.
- The generated squid requires some variables to be set to connect to S3. Here’s an example of what
.env may look like:
WASM/ink!
- Index
Transfer events emitted by an ERC20 contract on Shibuya, save results to PostgreSQL. Do not forget to use the ink-abi template!
Note: you can get the ABI from the squid-gen repository: