Skip to content

A sample WASM Squid indexing an ERC-20 simple contract, deployed on Shibuya (Astar network testnet)

License

Notifications You must be signed in to change notification settings

subsquid-labs/squid-ink-template

Repository files navigation

WASM squid template (FireSquid edition)

This is a squid template for indexing Ink!-based contracts, supported e.g. by the Astar and Shibuya network. This template indexes a sample ERC-20 Ink!-based smart contract token transfers over the Shibuya network and serves them via graphql API.

For more details, inspect Squid SDK docs, including the dedicated page on Ink! support and the Ink! indexing tutorial.

Dependencies: Node.js v16 or newer, Git, Docker.

Quickstart

# 0. Install @subsquid/cli a.k.a. the sqd command globally
npm i -g @subsquid/cli

# 1. Retrieve the template
sqd init my_squid_name -t frontier-evm
cd my_squid_name

# 2. Install dependencies
npm i

# 3. Start a Postgres database container and detach
sqd up

# 4. Build the project
sqd build

# 5. Start both the squid processor and the GraphQL server
sqd run .

A GraphiQL playground will be available at localhost:4350/graphql.

You can also start squid services one by one:

sqd process
sqd serve

Dev flow

1. Define database schema

Start development by defining the schema of the target database via schema.graphql. Schema definition consists of regular graphql type declarations annotated with custom directives. Full description of schema.graphql dialect is available here.

2. Generate TypeORM classes

Mapping developers use TypeORM EntityManager to interact with target database during data processing. All necessary entity classes are generated by the squid framework from schema.graphql. This is done by running sqd codegen command.

3. Generate database migrations

All database changes are applied through migration files located at db/migrations. squid-typeorm-migration(1) tool provides several commands to drive the process.

## drop create the database
sqd down
sqd up

## replace any old schemas with a new one made from the entities
sqd migration:generate

See docs on database migrations for more details.

4. Import the contract ABI and generate interfaces to decode events

It is necessary to import the respective ABI definition to decode WASM logs. For this template we used standard ERC20 interface, see abi/erc20.json.

To generate a type-safe facade class to decode EVM logs, use squid-ink-typegen(1):

npx squid-ink-typegen --abi abi/erc20.json --output src/abi/erc20.ts

Note on ink! v5

Interfaces generated for contracts is written in ink! v5 or newer are somewhat different from those made for old contracts:

decodeEvent(data: Bytes): Event

becomes

decodeEvent(data: Bytes, topics: Bytes[]): Event

The topics can be requested in processor configuration:

processor.setFields({
  event: {
    topics: true,
  }
})

See this folder for a complete example.

Project conventions

Squid tools assume a certain project layout:

  • All compiled js files must reside in lib and all TypeScript sources in src. The layout of lib must reflect src.
  • All TypeORM classes must be exported by src/model/index.ts (lib/model module).
  • Database schema must be defined in schema.graphql.
  • Database migrations must reside in db/migrations and must be plain js files.
  • sqd(1) and squid-*(1) executables consult .env file for environment variables.

About

A sample WASM Squid indexing an ERC-20 simple contract, deployed on Shibuya (Astar network testnet)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published