NEAR Quick Start
NEAR Quick Start
The goal of this quick start guide is to index all price submissions from priceoracle.near on NEAR's mainnet - it's a great way to quickly learn how SubQuery works on a real world hands-on example.
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.
Please initialise a NEAR Network project. Now, let's move forward and update these configurations.
Note
The final code of this project can be found here.
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.
Remove all existing entities and update the schema.graphql
file as follows, here you can see we are indexing all oracles that submit prices on the chain, as well as each individual price submission made to NEAR's price oracle:
type Oracle @entity {
id: ID! # We'll use the account_id of the oracle as the ID
creator: String!
blockHeight: BigInt!
timestamp: BigInt!
}
type Price @entity {
id: ID!
oracle: Oracle! # The oracle that reported this price
assetID: String!
price: Int!
decimals: Int!
blockHeight: BigInt!
timestamp: BigInt!
}
Note
Importantly, these relationships can not only establish one-to-many connections but also extend to include many-to-many associations. To delve deeper into entity relationships, you can refer to this section. If you prefer a more example-based approach, our dedicated Hero Course Module can provide further insights.
SubQuery simplifies and ensures type-safety when working with GraphQL entities, actions, and transactions.
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.
Now that you have made essential changes to the GraphQL Schema file, let’s move forward to the next file.
Your Project Manifest File
The Project Manifest 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 NEAR, there are three types of mapping handlers (and you can have more than one in each project):
- BlockHandler: On each and every block, run a mapping function
- TransactionHandlers: On each and every transaction that matches optional filter criteria, run a mapping function
- ActionHandlers: On each and every transaction action that matches optional filter criteria, run a mapping function
We are indexing all transactions sent to the priceoracle.near
address.
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.
Since you are going to index all priceoracle.near
transactions, you need to update the datasources
section as follows:
{
dataSources: [
{
kind: NearDatasourceKind.Runtime,
// You can set any start block you want here. This block was when app.nearcrowd.near was created https://nearblocks.io/txns/6rq4BNMpr8RwxKjfGYbruHhrL1ETbNzeFwcppGwZoQBY
startBlock: 84662303,
mapping: {
file: "./dist/index.js",
handlers: [
{
handler: "handleNewOracle",
kind: NearHandlerKind.Action,
filter: {
type: "FunctionCall",
methodName: "add_oracle",
receiver: "priceoracle.near",
},
},
{
handler: "handleNewPrice",
kind: NearHandlerKind.Action,
filter: {
type: "FunctionCall",
methodName: "report_prices",
receiver: "priceoracle.near",
},
},
],
},
},
],
}
The above code indicates that you will be running a handleNewPrice
mapping function whenever there is transaction made to the priceoracle.near
address that includes an action with the method name report_prices
. Additionally we run the handleNewOracle
mapping function whenever there is transaction made to the priceoracle.near
address that includes an action with the method name add_oracle
.
Check out our Manifest File documentation to get more information about the Project Manifest (project.ts
) file.
Next, let’s proceed ahead with the Mapping Function’s configuration.
Follow these steps to add a mapping function:
Navigate to the default mapping function in the src/mappings
directory. You will be able to see three exported functions: handleBlock
, handleAction
, and handleAction
. Delete both the handleBlock
and handleAction
functions as you will only deal with the handleAction
function.
The handleAction
function receives event data whenever an event matches the filters, which you specified previously in the project.ts
. Let’s make changes to it, process the relevant transaction action, and save them to the GraphQL entities created earlier.
Update the handleAction
function as follows (note the additional imports and renaming of functions to handleNewOracle
and handleNewPrice
):
import { FunctionCall, NearAction, NearTransaction } from "@subql/types-near";
import { Oracle, Price } from "../types";
type NewOracle = {
account_id: string;
};
type NewPrices = {
prices: {
asset_id: string;
price: {
multiplier: string;
decimals: number;
};
}[];
};
export async function handleNewOracle(action: NearAction): Promise<void> {
// Data is encoded in base64 in the args, so we first decode it and parse into the correct type
const payload: NewOracle = action.action.args.toJson();
if (payload.account_id && action.transaction) {
logger.info(
`Handling new oracle ${payload.account_id} at ${action.transaction.block_height}`,
);
await checkAndCreateOracle(payload.account_id, action.transaction);
}
}
export async function handleNewPrice(action: NearAction): Promise<void> {
// Data is encoded in base64 in the args, so we first decode it and parse into the correct type
const payload: NewPrices = action.action.args.toJson();
if (action.transaction) {
logger.info(
`Handling new price action at ${action.transaction.block_height}`,
);
await checkAndCreateOracle(
action.transaction.signer_id,
action.transaction,
);
payload.prices.map(async (p, index) => {
await Price.create({
id: `${action.transaction.result.id}-${action.id}-${index}`,
oracleId: action.transaction.signer_id.toLowerCase(),
assetID: p.asset_id,
price: parseInt(p.price.multiplier),
decimals: p.price.decimals,
blockHeight: BigInt(action.transaction.block_height),
timestamp: BigInt(action.transaction.timestamp),
}).save();
});
}
}
async function checkAndCreateOracle(
account_id: string,
transaction: NearTransaction,
): Promise<void> {
const oracle = await Oracle.get(account_id.toLowerCase());
if (!oracle) {
// We need to create a new one
await Oracle.create({
id: account_id.toLowerCase(),
creator: transaction.signer_id,
blockHeight: BigInt(transaction.block_height),
timestamp: BigInt(transaction.timestamp),
}).save();
}
}
Let’s understand how the above code works.
For the handleNewOracle
mapping function, the function receives a new NearAction
payload. The data on this is a JSON payload, so we parse into the correct NewOracle
type via JSON. We then run the checkAndCreateOracle
to ensure that we create the oracle if we don't already have it (it checks if it already exists before creating a new Oracle
entity).
For the handleNewPrice
mapping function, the function receives a new NearAction
payload. The data on this is a JSON payload, so we parse into the correct NewPrices
type via JSON. We then run the checkAndCreateOracle
to ensure that the oracle we are listing this price for is already known since it's a foreign key (it checks if it already exists before creating a new Oracle
entity). Finally, for each price submission in the array, we create the price and save it to the store (Note that SubQuery will automatically save this to the database).
Note
For more information on mapping functions, please refer to our Mappings documentation.
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 {
prices(first: 50, orderBy: BLOCK_HEIGHT_DESC) {
nodes {
id
assetID
price
decimals
oracleId
oracle {
id
}
}
}
oracles(first: 50, orderBy: PRICES_COUNT_DESC) {
nodes {
id
creator
blockHeight
timestamp
}
}
}
You will see the result similar to below:
{
"data": {
"prices": {
"nodes": [
{
"id": "FkMfPrritmGbi7dboMHwscDTQFaA2w64E2BMTv2A9V6-0-2",
"assetID": "dac17f958d2ee523a2206206994597c13d831ec7.factory.bridge.near",
"price": 10006,
"decimals": 10,
"oracleId": "npo-aurora.near",
"oracle": {
"id": "npo-aurora.near"
}
},
{
"id": "FkMfPrritmGbi7dboMHwscDTQFaA2w64E2BMTv2A9V6-0-1",
"assetID": "aurora",
"price": 163242,
"decimals": 20,
"oracleId": "npo-aurora.near",
"oracle": {
"id": "npo-aurora.near"
}
},
{
"id": "FkMfPrritmGbi7dboMHwscDTQFaA2w64E2BMTv2A9V6-0-3",
"assetID": "a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48.factory.bridge.near",
"price": 9999,
"decimals": 10,
"oracleId": "npo-aurora.near",
"oracle": {
"id": "npo-aurora.near"
}
}
]
},
"oracles": {
"nodes": [
{
"id": "zerkalo.near",
"creator": "zerkalo.near",
"blockHeight": "83600017",
"timestamp": "1674465246692488448"
},
{
"id": "gloriafoster.near",
"creator": "gloriafoster.near",
"blockHeight": "83600014",
"timestamp": "1674465243614499584"
},
{
"id": "pythia.near",
"creator": "pythia.near",
"blockHeight": "83600024",
"timestamp": "1674465254364237568"
},
{
"id": "npo-aurora.near",
"creator": "npo-aurora.near",
"blockHeight": "83600043",
"timestamp": "1674465275463554048"
}
]
}
}
}
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.