From 2072d8e2b22a48405a56360e31b60b7cd527c649 Mon Sep 17 00:00:00 2001 From: bgodlin <37313677+bgodlin@users.noreply.github.com> Date: Thu, 25 Jul 2024 07:57:47 +0200 Subject: [PATCH] Featuring Coreum Quickstart (#542) --- docs/.vuepress/sidebar.ts | 4 + .../quickstart_chains/cosmos-coreum.md | 181 ++++++++++++++++++ 2 files changed, 185 insertions(+) create mode 100644 docs/indexer/quickstart/quickstart_chains/cosmos-coreum.md diff --git a/docs/.vuepress/sidebar.ts b/docs/.vuepress/sidebar.ts index d1851a79e32..cd12707b8ff 100644 --- a/docs/.vuepress/sidebar.ts +++ b/docs/.vuepress/sidebar.ts @@ -263,6 +263,10 @@ export const getSidebar = (locale: string) => text: "Archway", link: `${locale}/indexer/quickstart/quickstart_chains/cosmos-archway.md`, }, + { + text: "Coreum", + link: `${locale}/indexer/quickstart/quickstart_chains/cosmos-coreum.md`, + }, { text: "Cronos (EVM)", link: `${locale}/indexer/quickstart/quickstart_chains/cosmos-cronos.md`, diff --git a/docs/indexer/quickstart/quickstart_chains/cosmos-coreum.md b/docs/indexer/quickstart/quickstart_chains/cosmos-coreum.md new file mode 100644 index 00000000000..a1dc80cb2c5 --- /dev/null +++ b/docs/indexer/quickstart/quickstart_chains/cosmos-coreum.md @@ -0,0 +1,181 @@ +# Coreum Quick Start + +The goal of this quick start guide is to index all transfer events and messages on the [Coreum network](https://www.coreum.com/). + + + +In every SubQuery project, there are 3 key files to update. Let's begin updating them one by one. + +::: tip +The final code of this project can be found [here](https://github.com/subquery/cosmos-subql-starter/tree/main/Coreum/coreum-starter). +::: + + + +```ts +dataSources: [ + { + kind: CosmosDatasourceKind.Runtime, + startBlock: 14731000, + mapping: { + file: "./dist/index.js", + handlers: [ + { + handler: "handleEvent", + kind: CosmosHandlerKind.Event, + filter: { + type: "transfer", + messageFilter: { + type: "/cosmos.bank.v1beta1.MsgSend", + }, + }, + }, + { + handler: "handleMessage", + kind: CosmosHandlerKind.Message, + filter: { + type: "/cosmos.bank.v1beta1.MsgSend", + }, + }, + ] +``` + +In the code above we have defined two handlers. `handleEvent` will be executed whenever a `transfer` type is detected within a message filter of `/cosmos.bank.v1beta1.MsgSend` type. `handleMessage` is the other handler that will be triggered when a `/cosmos.bank.v1beta1.MsgSend` filter type is triggered. These handlers ared used to track the transfers and messages within the Coreum network. + + + + + +For this project, you'll need to modify your schema.graphql file as follows. Since we're indexing all transfer events & messages on the Coreum network, we have a TransferEvent and Message entity that contain a number of fields, including blockHeight, recipient/to, sender/from, and amount: + +```graphql +type TransferEvent @entity { + id: ID! + blockHeight: BigInt! + txHash: String! + recipient: String! + sender: String! + amount: String! +} + +type Message @entity { + id: ID! + blockHeight: BigInt! + txHash: String! + from: String! + to: String! + amount: String! +} +``` + + + + + + + + + +Navigate to the default mapping function in the `src/mappings` directory and update your mapping files to match the following (**note the additional imports**): + +```ts +import { TransferEvent, Message } from "../types"; +import { + CosmosEvent, + CosmosBlock, + CosmosMessage, + CosmosTransaction, +} from "@subql/types-cosmos"; +import { MsgSend } from "../types/proto-interfaces/cosmos/bank/v1beta1/tx"; + +export async function handleMessage( + msg: CosmosMessage +): Promise { + logger.info(`Messsage found at ${msg.block.blockId}`); + const messageRecord = Message.create({ + id: `${msg.tx.hash}-${msg.idx}`, + blockHeight: BigInt(msg.block.block.header.height), + txHash: msg.tx.hash, + from: msg.msg.decodedMsg.fromAddress, + to: msg.msg.decodedMsg.toAddress, + amount: JSON.stringify(msg.msg.decodedMsg.amount), + }); + + await messageRecord.save(); +} + +export async function handleEvent(event: CosmosEvent): Promise { + logger.info(`Event found at ${event.block.blockId}`); + const eventRecord = TransferEvent.create({ + id: `${event.tx.hash}-${event.msg.idx}-${event.idx}`, + blockHeight: BigInt(event.block.block.header.height), + txHash: event.tx.hash, + recipient: "", + amount: "", + sender: "", + }); + for (const attr of event.event.attributes) { + switch (attr.key) { + case "recipient": + eventRecord.recipient = attr.value; + break; + case "amount": + eventRecord.amount = attr.value; + break; + case "sender": + eventRecord.sender = attr.value; + break; + default: + break; + } + } + await eventRecord.save(); +} +``` + +In the Coreum SubQuery project, we have two main functions, namely `handleMessage` and `handleEvent`, that were defined in the `src/mappings/mappingHandlers.ts` file. + +The `handleMessage` function is triggered when a `/cosmos.bank.v1beta1.MsgSend` type message is detected. It receives a message of type `CosmosMessage`, and then extracts key data points such as blockHeight, transaction hash, from, to and amount from the `msg` object. + +The `handleEvent` function is also triggered when a `/cosmos.bank.v1beta1.MsgSend` type message is detected for a transfer. It receives an event of type `CosmosEvent`, and then it also extracts blockHeight, transaction hash, from, to and amount from the `event` object. + + + + + + + + + +```graphql +{ + query { + transferEvents(first: 2) { + nodes { + id + blockHeight + txHash + recipient + sender + amount + } + } + messages(first: 2) { + nodes { + id + blockHeight + txHash + from + to + amount + } + } + } +} +``` + +::: tip +The final code of this project can be found [here](https://github.com/subquery/cosmos-subql-starter/tree/main/Coreum/coreum-starter). +::: + +