Astar (WASM) Quick Start
Astar (WASM) Quick Start
This quick start guide introduces SubQuery's Substrate WASM support by using an example project in Astar Network. The example project indexes all Transactions and Approvals from the Astar Wasm based lottery contract, as well as dApp staking events from Astar's dApp Staking functions.
Note
The final code of this project can be found here.
This project is unique, as it indexes data from both Astar's Substrate execution layer (native Astar pallets and runtime), with smart contract data from Astar's WASM smart contract layer, within the same SubQuery project and into the same dataset. A very similar approach can be take with indexing Astar's EVM layer too.
In the earlier Quickstart section , you should have taken note of three crucial files. To initiate the setup of a project from scratch, you can proceed to follow the steps outlined in the initialisation description.
Update Your GraphQL Schema File
The schema.graphql
file determines the shape of your data from SubQuery due to the mechanism of the GraphQL query language. Hence, updating the GraphQL Schema file is the perfect place to start. It allows you to define your end goal right at the start.
The Astar-wasm-starter project has four entities. A Transaction, Approval, DApp, and DAppReward (which has a foreign key to Dapp). These index basic block data such as the timestamp, heigh and hash along with from and contract addresses and the value.
type Transaction @entity {
id: ID! # Transaction hash
transactionHash: String
blockHeight: BigInt
blockHash: String
timestamp: Date
value: BigInt
from: String!
to: String!
contractAddress: String!
}
type Approval @entity {
id: ID! # Transaction hash
blockHeight: BigInt
value: BigInt
hash: String
owner: String!
spender: String!
contractAddress: String!
}
type DApp @entity {
id: ID! #EVM is a required field
accountID: String!
totalStake: BigInt!
}
type DAppReward @entity {
id: ID!
dApp: DApp!
accountID: String!
eraIndex: Int!
balanceOf: BigInt!
}
yarn codegen
npm run-script codegen
This action will generate a new directory (or update the existing one) named src/types
. Inside this directory, you will find automatically generated entity classes corresponding to each type defined in your schema.graphql
. These classes facilitate type-safe operations for loading, reading, and writing entity fields. You can learn more about this process in the GraphQL Schema section.
Check out the GraphQL Schema documentation to get in-depth information on schema.graphql
file.
Now that you have made essential changes to the GraphQL Schema file, let’s proceed ahead with the Mapping Function’s configuration.
The Project Manifest File
The Project Manifest (project.ts
) file works as an entry point to your project. It defines most of the details on how SubQuery will index and transform the chain data. For Substrate/Polkadot chains, there are three types of mapping handlers:
- BlockHanders: On each and every block, run a mapping function
- EventHandlers: On each and every Event that matches optional filter criteria, run a mapping function
- CallHanders: On each and every extrinsic call that matches optional filter criteria, run a mapping function
For EVM and WASM data processors on Substrate/Polkadot chains, there are only two types of mapping handlers:
- EventHandlers: On each and every Event that matches optional filter criteria, run a mapping function
- CallHanders: On each and every extrinsic call that matches optional filter criteria, run a mapping function
Substrate Manifest section
Since we are planning to index all Polkadot transfers, we need to update the datasources
section as follows:
{
dataSources: [
{
// This is the datasource for Astar's Native Substrate processor
kind: SubstrateDatasourceKind.Runtime,
startBlock: 87073,
mapping: {
file: "./dist/index.js",
handlers: [
{
kind: SubstrateHandlerKind.Event,
handler: "handleNewContract",
filter: {
module: "dappsStaking",
method: "NewContract",
},
},
{
kind: SubstrateHandlerKind.Event,
handler: "handleBondAndStake",
filter: {
module: "dappsStaking",
method: "BondAndStake",
},
},
{
kind: SubstrateHandlerKind.Event,
handler: "handleUnbondAndUnstake",
filter: {
module: "dappsStaking",
method: "UnbondAndUnstake",
},
},
{
kind: SubstrateHandlerKind.Event,
handler: "handleReward",
filter: {
module: "dappsStaking",
method: "Reward",
},
},
],
},
},
];
}
This indicates that you will be running a handleNewContract
mapping function whenever there is an event emitted from the NewContract
method on the dappsStaking
pallet. Similarly we will run other mapping functions for the three other events being emitted from the dappsStaking
to other mapping functions. This covers most interactions with the dApp staking feature that we are interested in.
Check out our Manifest File documentation to get more information about the Project Manifest (project.ts
) file.
WASM Manifest Section
If you're not using the WASM starter template then please add the Wasm Datasource as a dependency using yarn add @subql/substrate-wasm-processor
.
We are indexing all transfers and approve contract call events from the Astar contract bZ2uiFGTLcYyP8F88XzXa13xu5Mmp13VLiaW1gGn7rzxktc
. First, you will need to import the contract ABI defintion. You can copy the entire JSON and save it as a file ./erc20Metadata.abi.json
in the abis
directory.
This section in the Project Manifest now imports all the correct definitions and lists the triggers that we look for on the blockchain when indexing. We add another section the datasource beneath the above substrate manifest section.
{
dataSources: [
{
// This is the datasource for Astar's Wasm processor
kind: "substrate/Wasm",
startBlock: 3281780,
processor: {
file: "./node_modules/@subql/substrate-wasm-processor/dist/bundle.js",
options: {
abi: "erc20",
// contract: "a6Yrf6jAPUwjoi5YvvoTE4ES5vYAMpV55ZCsFHtwMFPDx7H" // Shibuya
contract: "bZ2uiFGTLcYyP8F88XzXa13xu5Mmp13VLiaW1gGn7rzxktc", // Mainnet,
},
},
assets: new Map([["erc20", { file: "./abis/erc20Metadata.abi.json" }]]),
mapping: {
file: "./dist/index.js",
handlers: [
{
handler: "handleWasmEvent",
kind: "substrate/WasmEvent",
filter: {
// contract: "a6Yrf6jAPUwjoi5YvvoTE4ES5vYAMpV55ZCsFHtwMFPDx7H" // Shibuya
contract: "bZ2uiFGTLcYyP8F88XzXa13xu5Mmp13VLiaW1gGn7rzxktc", // Mainnet
identifier: "Transfer",
},
},
{
handler: "handleWasmCall",
kind: "substrate/WasmEvent",
filter: {
selector: "0x681266a0",
method: "approve",
},
},
],
},
},
],
}
The above code indicates that you will be running a handleWasmEvent
mapping function whenever there is an Transfer
event on any transaction from the Astar contract. Similarly, we will run the handleWasmCall
mapping function whenever there is a approve
log on the same contract.
Check out our Substrate Wasm documentation to get more information about the Project Manifest (project.ts
) file for Substrate WASM contracts.
Add a Mapping Function
Mapping functions define how blockchain data is transformed into the optimised GraphQL entities that we previously defined in the schema.graphql
file.
Navigate to the default mapping function in the src/mappings
directory. There are multiple other exported functions such as handleWasmCall
, handleWasmEvent
, handleNewContract
, handleBondAndStake
, handleUnbondAndUnstake
, and handleReward
.
type ApproveCallArgs = [AccountId, Balance];
export async function handleWasmCall(
call: WasmCall<ApproveCallArgs>,
): Promise<void> {
logger.info(`Processing WASM Call at ${call.blockNumber}`);
const approval = new Approval(`${call.blockNumber}-${call.idx}`);
approval.hash = call.hash;
approval.owner = call.from.toString();
approval.contractAddress = call.dest.toString();
if (typeof call.data !== "string") {
const [spender, value] = call.data.args;
approval.spender = spender.toString();
approval.value = value.toBigInt();
} else {
logger.info(`Decode call failed ${call.hash}`);
}
await approval.save();
}
The handleWasmCall
function receives event data from the WASM execution environment whenever an call matches the filters that was specified previously in the project.ts
. It instantiates a new Approval
entity and populates the fields with data from the Wasm Call payload. Then the .save()
function is used to save the new entity (SubQuery will automatically save this to the database).
export async function handleBondAndStake(event: SubstrateEvent): Promise<void> {
logger.info(
`Processing new Dapp Staking Bond and Stake event at ${event.block.block.header.number}`,
);
const {
event: {
data: [accountId, smartContract, balanceOf],
},
} = event;
// Retrieve the dapp by its ID
let dapp: DApp = await DApp.get(smartContract.toString());
if (!dapp) {
dapp = DApp.create({
id: smartContract.toString(),
accountID: accountId.toString(),
totalStake: BigInt(0),
});
}
dapp.totalStake += (balanceOf as Balance).toBigInt();
await dapp.save();
}
The handleBondAndStake
function receives Substrate event data from the native Substrate environment whenever an event matches the filters that was specified previously in the project.ts
. It extracts the various data from the event payload (in Substrate it's stored as an array of Codecs), then checks if an existing DApp record exists. If none exists (e.g. it's a new dApp), then it instantiates a new one and then updates the total stake to reflect the new staking mount. Then the .save()
function is used to save the new/updated entity (SubQuery will automatically save this to the database).
Check out our mappings documentation for Substrate and the Substrate WASM data processor to get detailed information on mapping functions for each type.
Build Your Project
Next, build your work to run your new SubQuery project. Run the build command from the project's root directory as given here:
yarn build
npm run-script build
Important
Whenever you make changes to your mapping functions, you must rebuild your project.
Now, you are ready to run your first SubQuery project. Let’s check out the process of running your project in detail.
Whenever you create a new SubQuery Project, first, you must run it locally on your computer and test it and using Docker is the easiest and quickiest way to do this.
Run Your Project Locally with Docker
The docker-compose.yml
file defines all the configurations that control how a SubQuery node runs. For a new project, which you have just initialised, you won't need to change anything.
However, visit the Running SubQuery Locally to get more information on the file and the settings.
Run the following command under the project directory:
yarn start:docker
npm run-script start:docker
Note
It may take a few minutes to download the required images and start the various nodes and Postgres databases.
Query your Project
Next, let's query our project. Follow these three simple steps to query your SubQuery project:
Open your browser and head to
http://localhost:3000
.You will see a GraphQL playground in the browser and the schemas which are ready to query.
Find the Docs tab on the right side of the playground which should open a documentation drawer. This documentation is automatically generated and it helps you find what entities and methods you can query.
Try the following queries to understand how it works for your new SubQuery starter project. Don’t forget to learn more about the GraphQL Query language.
query {
transactions(first: 3, orderBy: BLOCK_HEIGHT_ASC) {
totalCount
nodes {
id
timestamp
blockHeight
transactionHash
blockHash
contractAddress
from
value
}
}
}
Tips
There is a Docs tab on the right side of the playground which should open a documentation drawer. This documentation is automatically generated and helps you find what entities and methods you can query. To learn more about the GraphQL Query language here.
You should see results similar to below:
{
"data": {
"transactions": {
"totalCount": 17,
"nodes": [
{
"id": "3281781-0",
"timestamp": "2023-04-04T14:37:54.532",
"blockHeight": "3281781",
"transactionHash": "0x4f57e6ab4e8337375871fe4c8f7ae2e71601ea7fbd135b6f8384eb30db31ec44",
"blockHash": "0x6d65fe39ae469afd74d32e34a61382b1bbda37983dea745ea2afe58e57d4afbc",
"contractAddress": "bZ2uiFGTLcYyP8F88XzXa13xu5Mmp13VLiaW1gGn7rzxktc",
"from": "WJWxmJ27TdMZqvzLx18sZpH9s5ir9irFm1LRfbDeByamdHf",
"value": "25000000000000000000"
},
{
"id": "3281792-0",
"timestamp": "2023-04-04T14:40:06.386",
"blockHeight": "3281792",
"transactionHash": "0xbe8d6f09a96ff44e732315fbeff2862e9bdeb8353612a0bfab10632c410d8135",
"blockHash": "0xaa09e8060068931a58a162c150ccb73e0b4de528185f1da92b049ab31c299e5a",
"contractAddress": "bZ2uiFGTLcYyP8F88XzXa13xu5Mmp13VLiaW1gGn7rzxktc",
"from": "aFNoZEM64m1ifrHAwEPEuhfRM5L7kjnPhmtYjZaQHX2zb6y",
"value": "32000000000000000000"
},
{
"id": "3281797-1",
"timestamp": "2023-04-04T14:41:06.786",
"blockHeight": "3281797",
"transactionHash": "0xfdb111a314ee4e4460a3f2ab06221d5985c50e8f5cbae5a12f4f73b222d5954c",
"blockHash": "0xeb4e49463e174fc993417e852f499ddc6e3c4a15f355a576a74772604f2132e5",
"contractAddress": "bZ2uiFGTLcYyP8F88XzXa13xu5Mmp13VLiaW1gGn7rzxktc",
"from": "aFNoZEM64m1ifrHAwEPEuhfRM5L7kjnPhmtYjZaQHX2zb6y",
"value": "57000000000000000000"
}
]
}
}
}
Note
The final code of this project can be found here.
What's next?
Congratulations! You have now a locally running SubQuery project that accepts GraphQL API requests for transferring data.
Tip
Find out how to build a performant SubQuery project and avoid common mistakes in Project Optimisation.
Click here to learn what should be your next step in your SubQuery journey.