Cronos Quick Start
Cronos Quick Start
Goals
The goal of this quick start guide is to adapt the standard starter project in the Cronos Network and then begin indexing all transfers of Cro Crow Token.
Important
Cronos is an EVM compatible (Ethermint) chain, as such there are two options for indexing Cronos data. You can index chain data via the standard Cosmos RPC interface, or via Ethereum APIs. For Cronos, we provide a starter project for each.
Before we begin, make sure that you have initialised your project using the provided steps in the Start Here section. You must complete the suggested 4 steps for Cosmos users.
Now, let's move ahead in the process and update these configurations.
Previously, in the 1. Create a New Project section, you must have noted 3 key files. Let's begin updating them one by one.
Tips
The final code of this project can be found here.
1. Your Project Manifest File
The Project Manifest (project.ts
) file is an entry point to your project. It defines most of the details on how SubQuery will index and transform the chain data. For Cosmos chains, there are four types of mapping handlers (and you can have more than one in each project):
- BlockHanders: On each and every block, run a mapping function
- TransactionHandlers: On each and every transaction, run a mapping function
- MessageHandlers: On each and every message that matches optional filter criteria, run a mapping function
- EventHanders: On each and every event that matches optional filter criteria, run a mapping function
Note that the manifest file has already been set up correctly and doesn’t require significant changes, but you need to change the datasource handlers. This section lists the triggers that look for on the blockchain to start indexing.
Important
There are two versions of this file depending on your choice to index data via the ETH or Cosmos RPC
{
dataSources: [
{
kind: EthereumDatasourceKind.Runtime,
// Contract creation of Pangolin Token https://snowtrace.io/tx/0xfab84552e997848a43f05e440998617d641788d355e3195b6882e9006996d8f9
startBlock: 446,
options: {
// Must be a key of assets
abi: "erc20",
address: "0x5c7f8a570d578ed84e63fdfa7b1ee72deae1ae23",
// Wrapped CRO https://cronos.org/explorer/address/0x5C7F8A570d578ED84E63fdFA7b1eE72dEae1AE23
},
assets: new Map([["erc20", { file: "./erc20.abi.json" }]]),
mapping: {
file: "./dist/index.js",
handlers: [
{
kind: EthereumHandlerKind.Call,
handler: "handleTransaction",
filter: {
/**
* The function can either be the function fragment or signature
* function: '0x095ea7b3'
* function: '0x7ff36ab500000000000000000000000000000000000000000000000000000000'
*/
function: "approve(address guy, uint256 wad)",
},
},
{
kind: EthereumHandlerKind.Event,
handler: "handleLog",
filter: {
/**
* Follows standard log filters https://docs.ethers.io/v5/concepts/events/
* address: "0x60781C2586D68229fde47564546784ab3fACA982"
*/
topics: ["Transfer(address src, address dst, uint256 wad)"],
},
},
],
},
},
],
}
{
dataSources: [
{
kind: "cosmos/EthermintEvm",
startBlock: 446,
processor: {
file: "./node_modules/@subql/ethermint-evm-processor/dist/bundle.js",
options: {
abi: "erc20",
address: "0x5c7f8a570d578ed84e63fdfa7b1ee72deae1ae23", // Wrapped CRO
},
},
assets: new Map([["erc20", { file: "./erc20.abi.json" }]]),
mapping: {
file: "./dist/index.js",
handlers: [
{
handler: "handleEthermintEvmCall",
kind: "cosmos/EthermintEvmCall",
filter: {
// Either Function Signature strings or the function `sighash` to filter the function called on the contract
// https://docs.ethers.io/v5/api/utils/abi/fragments/#FunctionFragment
method: "approve(address guy, uint256 wad)",
},
},
{
handler: "handleEthermintEvmEvent",
kind: "cosmos/EthermintEvmEvent",
filter: {
// The topics filter follows the Ethereum JSON-PRC log filters
// https://docs.ethers.io/v5/concepts/events
// Example valid values:
// - '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
// - Transfer(address,address,u256)
topics: ["Transfer(address src, address dst, uint256 wad)"],
},
},
],
},
},
],
}
The above code defines that you will be running a handleTransfer
mapping function whenever there is an event emitted with the transfer
method. Check out our Manifest File documentation to get more information about the Project Manifest (project.ts
) file.
Note
Please note that Cro Crow token requires a specific ABI interface. You need to:
- Get the Cro Crow contract ABI. You can compare with the interface file from this Quick Start final repo.
- Create a file
cro-crow.abi.json
in the main project's directory. - Link this file as an erc20 asset in the manifest file.
2. Update Your GraphQL Schema File
The schema.graphql
file determines the shape of the data that you are using SubQuery to index, hence it's a great place to start. The shape of your data is defined in a GraphQL Schema file with various GraphQL entities.
Update the schema.graphql
file as follows. The aim is to index all transfers of Cro Crow Token.
type Transfer @entity {
id: ID! # Transfer hash
from: String!
to: String!
tokenId: BigInt!
}
Important
When you make any changes to the schema file, do not forget to regenerate your types directory.
yarn codegen
npm run-script codegen
You will find the generated models in the /src/types/models
directory.
If you're creating as an CosmWasm based project, this command will also generate types for your listed protobufs and save them into src/types
directory, providing you with more typesafety. Read about how this is done in Cosmos Codegen from CosmWasm Protobufs.
If you're creating as an EVM based project, this command will also generate ABI types and save them into src/types
using the npx typechain --target=ethers-v5
command, allowing you to bind these contracts to specific addresses in the mappings and call read-only contract methods against the block being processed.
It will also generate a class for every contract event to provide easy access to event parameters, as well as the block and transaction the event originated from. Read about how this is done in EVM Codegen from ABIs.
Check out our GraphQL Schema documentation to get more information on schema.graphql
file.
Now that you have made essential changes to the GraphQL Schema file, let’s go ahead with the next configuration.
3. Add a Mapping Function
Mapping functions determine how chain data is transformed into the optimised GraphQL entities that you previously defined in the schema.graphql
file.
Navigate to the default mapping function in the src/mappings
directory. You will see setup types for ABI TransferEventArgs
and ApproveCallArgs
. Delete those for approvals. You will also see two exported functions: handleEthermintEvmEvent
& handleEthermintEvmCall
or handleLog
& handleTransaction
. Delete them as well.
Important
There are two versions of this file depending on your choice to index data via the ETH or Cosmos RPC
Update your mapping files to match the following (note the additional imports):
import { Transfer } from "../types";
import { EthereumLog } from "@subql/types-ethereum";
import { BigNumber } from "@ethersproject/bignumber";
// Setup types from ABI
type TransferEventArgs = [string, string, BigNumber] & {
from: string;
to: string;
tokenId: BigNumber;
};
// Save all transfers
export async function handleTransfer(
log: EthereumLog<TransferEventArgs>
): Promise<void> {
const transfer = Transfer.create({
id: log.transactionHash,
from: log.args.from,
to: log.args.to,
tokenId: log.args.tokenId.toBigInt(),
});
await transfer.save();
}
import { Transfer } from "../types";
import { EthermintEvmEvent } from "@subql/ethermint-evm-processor";
import { BigNumber } from "ethers";
// Setup types from ABI
type TransferEventArgs = [string, string, BigNumber] & {
from: string;
to: string;
tokenId: BigNumber;
};
// Save all transfers
export async function handleTransfer(
event: EthermintEvmEvent<TransferEventArgs>
): Promise<void> {
const transfer = Transfer.create({
id: event.transactionHash,
from: event.args.from,
to: event.args.to,
tokenId: event.args.tokenId.toBigInt(),
});
await transfer.save();
}
Let’s understand how the above code works. Here, the function receives an EthereumLog
or EthermintEvmEvent
which includes data on the payload. We extract this data and then create a new Transfer
entity defined earlier in the schema.graphql
file. After that we use the .save()
function to save the new entity (SubQuery will automatically save this to the database). Check out our Mappings documentation and get information on the mapping functions in detail.
4. 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.
5. Run Your Project Locally with Docker
Whenever you create a new SubQuery Project, never forget to run it locally on your computer and test it. And using Docker is the most hassle-free way to do this.
docker-compose.yml
file defines all the configurations that control how a SubQuery node runs. For a new project, which you have just initialised, no major changes are needed.
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.
6. 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 query to understand how it works for your new SubQuery starter project. Don’t forget to learn more about the GraphQL Query language.
{
query {
transfers(first: 5) {
nodes {
id
to
from
tokenId
}
}
}
}
You will see the result similar to below:
{
"data": {
"transfers": {
"nodes": [
{
"id": "0xff2bcbf7445c48f95b9e9bb770076e1562db2b58881338ea65c8c60aae1f4d20",
"from": "0xe40E86209bf7A563B23dc5625ea968F9DD9269fA",
"to": "0x281c2b2a0d5a3db358356537Fb4E1ac6Df9715f0",
"tokenId": "1160"
},
{
"id": "0xfbc0594cde0776813f02804e816ecd153f0a3e201523479f93f85b5423e5e1c6",
"from": "0x9B94F48372f5ED14f860B86f606ffb61D908E4dC",
"to": "0x05d6889ea1593b6e58B3366A95Ac923FC00A37AA",
"tokenId": "4921"
},
{
"id": "0xc601f604b5c3a6c78257b0e946429d7085c7a9f04b4c985d499c1118465bc30f",
"from": "0x00779809C0089d269C719F5953F7528E4dcE1Bdc",
"to": "0x45DfaDC5e74f8Fb62Be7893aA7c1f34db7C26D8d",
"tokenId": "7085"
}
]
}
}
}
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 from bLuna.
Click here to learn what should be your next step in your SubQuery journey.