Avalanche Quick Start
Avalanche Quick Start
In this Quick start guide, we're going to start with a simple Avalanche starter project and then finish by indexing some actual real data. This is an excellent basis to start with when developing your own SubQuery Project.
If your are looking for guides for Substrate/Polkadot, you can read the Substrate/Polkadot specific quick start guide.
At the end of this guide, you'll have a working SubQuery project running on a SubQuery node with a GraphQL endpoint that you can query data from.
If you haven't already, we suggest that you familiarise yourself with the terminology used in SubQuery.
The goal of this quick start guide is to index all Pangolin token Approve logs, it should only take 10-15 minutes
Preparation
Local Development Environment
- Node: A modern (e.g. the LTS version) installation of Node.
- Docker: This tutorial will use require Docker
Install the SubQuery CLI
Install SubQuery CLI globally on your terminal by using NPM:
# NPM
npm install -g @subql/cli
Please note that we DO NOT encourage the use of yarn global
for installing @subql/cli
due to its poor dependency management which may lead to an errors down the line.
You can then run help to see available commands and usage provide by CLI
subql help
Initialise the SubQuery Starter Project
Inside the directory in which you want to create a SubQuery project, simply run the following command to get started.
subql init
You'll be asked certain questions as the SubQuery project is initalised:
- Project Name: A name for your SubQuery project
- Network Family: The layer-1 blockchain network family that this SubQuery project will be developed to index, use the arrow keys on your keyboard to select from the options, for this guide we will use "Avalanche"
- Network: The specific network that this SubQuery project will be developed to index, use the arrow keys on your keyboard to select from the options, for this guide we will use "Avalanche"
- Template: Select a SubQuery project template that will provide a starting point to begin development, we suggest selecting the "Starter project"
- Git repository (Optional): Provide a Git URL to a repo that this SubQuery project will be hosted in (when hosted in SubQuery Explorer)
- RPC endpoint (Required): Provide a HTTPS URL to a running RPC endpoint that will be used by default for this project. This RPC node must be an archive node (have the full chain state). For this guide we will use the default value "avalanche.api.onfinality.io"
- Authors (Required): Enter the owner of this SubQuery project here (e.g. your name!)
- Description (Optional): You can provide a short paragraph about your project that describe what data it contains and what users can do with it
- Version (Required): Enter a custom version number or use the default (
1.0.0
) - License (Required): Provide the software license for this project or accept the default (
Apache-2.0
)
After the initialisation process is complete, you should see a folder with your project name has been created inside the directory. The contents of this directoy should be identical to what's listed in the Directory Structure.
Last, under the project directory, run following command to install the new project's dependencies.
::: code-tabs @tab:active yarn shell cd PROJECT_NAME yarn install
Making Changes to your Project
In the starter package that you just initialised, we have provided a standard configuration for your new project. You will mainly be working on the following files:
- The GraphQL Schema in
schema.graphql
- The Project Manifest in
project.yaml
- The Mapping functions in
src/mappings/
directory
The goal of this quick start guide is to adapt the standard starter project to index all Pangolin Approve
transaction logs.
Updating your GraphQL Schema File
The schema.graphql
file defines the various GraphQL schemas. Due to the way that the GraphQL query language works, the schema file essentially dictates the shape of your data from SubQuery. Its a great place to start becuase it allows you to define your end goal up front.
We're going to update the schema.graphql
file to remove all existing entities and read as follows
type PangolinApproval @entity {
id: ID!
transactionHash: String!
blockNumber: String!
blockHash: String!
addressFrom: String
addressTo: String
amount: String
}
Important: When you make any changes to the schema file, please ensure that you regenerate your types directory. Do this now.
You'll find the generated models in the /src/types/models
directory. For more information about the schema.graphql
file, check out our documentation under Build/GraphQL Schema
Updating the Project Manifest File
The Projet Manifest (project.yaml
) file can be seen as an entry point of your project and it defines most of the details on how SubQuery will index and transform the chain data.
We won't do many changes to the manifest file as it already has been setup correctly, but we need to change our handlers. Remember we are planning to index all Pangolin approval logs, as a result, we need to update the datasources
section to read the following.
dataSources:
- kind: avalanche/Runtime
startBlock: 57360 # Block when the Pangolin contract was created
options:
# Must be a key of assets
abi: erc20
## Pangolin token https://snowtrace.io/token/0x60781c2586d68229fde47564546784ab3faca982
address: "0x60781C2586D68229fde47564546784ab3fACA982"
assets:
erc20:
file: "./node_modules/@pangolindex/exchange-contracts/artifacts/contracts/pangolin-core/interfaces/IPangolinERC20.sol/IPangolinERC20.json"
mapping:
file: "./dist/index.js"
handlers:
- handler: handleLog
kind: avalanche/LogHandler
filter:
## Follows standard log filters https://docs.ethers.io/v5/concepts/events/
function: Approve(address spender, uint256 rawAmount)
# address: "0x60781C2586D68229fde47564546784ab3fACA982"
This means we'll run a handleLog
mapping function each and every time there is a approve
log on any transaction from the Pangolin contract.
For more information about the Project Manifest (project.yaml
) file, check out our documentation under Build/Manifest File
Add a Mapping Function
Mapping functions define how chain data is transformed into the optimised GraphQL entities that we have previously defined in the schema.graphql
file.
Navigate to the default mapping function in the src/mappings
directory. You'll see three exported functions, handleBlock
, handleLog
, and handleTransaction
. You can delete both the handleBlock
and handleTransaction
functions, we are only dealing with the handleLog
function.
The handleLog
function recieved event data whenever event matches the filters that we specify previously in our project.yaml
. We are going to update it to process all approval
transaction logs and save them to the GraphQL entities that we created earlier.
You can update the handleLog
function to the following (note the additional imports):
import { PangolinApproval } from "../types";
import { AvalancheLog } from "@subql/types-avalanche";
export async function handleLog(event: AvalancheLog): Promise<void> {
const pangolinApprovalRecord = new PangolinApproval(
`${event.blockHash}-${event.logIndex}`
);
pangolinApprovalRecord.transactionHash = event.transactionHash;
pangolinApprovalRecord.blockHash = event.blockHash;
pangolinApprovalRecord.blockNumber = event.blockNumber;
# topics store data as an array
pangolinApprovalRecord.addressFrom = event.topics[0];
pangolinApprovalRecord.addressTo = event.topics[1];
pangolinApprovalRecord.amount = event.topics[2];
await pangolinApprovalRecord.save();
}
What this is doing is receiving an Avalanche Log which includes the transation log data on the payload. We extract this data and then instantiate a new PangolinApproval
entity that we defined earlier in the schema.graphql
file. We add additional information and then use the .save()
function to save the new entity (SubQuery will automatically save this to the database).
For more information about mapping functions, check out our documentation under Build/Mappings
Build the Project
In order run your new SubQuery Project we first need to build our work. Run the build command from the project's root directory.
::: code-tabs @tab:active yarn shell yarn build
@tab npm shell npm run-script build
:::
Important: Whenever you make changes to your mapping functions, you'll need to rebuild your project
Running and Querying your Project
Run your Project with Docker
Whenever you create a new SubQuery Project, you should always run it locally on your computer to test it first. The easiest way to do this is by using Docker.
All configuration that controls how a SubQuery node is run is defined in this docker-compose.yml
file. For a new project that has been just initalised you won't need to change anything here, but you can read more about the file and the settings in our Run a Project section
Under the project directory run following command:
::: code-tabs @tab:active yarn shell yarn start:docker
@tab npm shell npm run-script start:docker
:::
It may take some time to download the required packages (@subql/node
, @subql/query
, and Postgres) for the first time but soon you'll see a running SubQuery node. Be patient here.
Query your Project
Open your browser and head to http://localhost:3000.
You should see a GraphQL playground is showing in the explorer and the schemas that are ready to query. On the top right of the playground, you'll find a Docs button that will open a documentation draw. Questa documentazione è generata automaticamente e vi aiuta a trovare quali entità e metodi potete interrogare.
For a new SubQuery starter project, you can try the following query to get a taste of how it works or learn more about the GraphQL Query language.
query {
pangolinApprovals(first: 5) {
nodes {
id
blockNumber
blockHash
transactionHash
addressFrom
addressTo
amount
}
}
}
Publish your SubQuery Project
SubQuery provides a free managed service when you can deploy your new project to. You can deploy it to SubQuery Managed Service and query it using our Explorer.
Read the guide to publish your new project to SubQuery Projects, Note that you must deploy via IPFS.
Next Steps
Congratulations, you now have a locally running SubQuery project that accepts GraphQL API requests for transfers data from bLuna.
Now that you've had an insight into how to build a basic SubQuery project, the question is where to from here? If you are feeling confident, you can jump into learning more about the three key files. The manifest file, the GraphQL schema, and the mappings file under the Build section of these docs.
Otherwise, continue to our Academy section where have more in depth workshops, tutorials, and example projects. There we'll look at more advanced modifications, and we'll take a deeper dive at running SubQuery projects by running readily available and open source projects.
Finally, if you're looking for more ways to run and publish your project, our Run & Publish section provides detailed informatation about all the ways to run your SubQuery project and other advanced GraphQL aggregation and subscription features.