Skip to main content

Multichain Quick Start - IBC Transfers

SubQuery TeamAbout 3 min

Multichain Quick Start - IBC Transfers

Tips

The final code of this project can be found here:

IBC Starter Exampleopen in new window

This tutorial provides a comprehensive guide on establishing a multi-chain indexer for indexing Inter-Blockchain Communication (IBC) activities among Cosmos Zones. The tutorial demonstrates the integration of bi-directional transfers between Osmosis and Cosmos Hub, while also highlighting the flexibility to effortlessly include additional chains.

Upon completing this guide, you will gain insights into effectively correlating event data across multiple networks. Furthermore, you'll acquire the knowledge to configure a SubQuery indexer, enabling the monitoring, tracking, and aggregation of events from various Cosmos blockchains within a unified entity.

Important

This project operates across multiple chains, making it more complex than other single chain examples.

Info

This network is based on the Cosmos SDK, which means you can index chain data via the standard Cosmos RPC interface.

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 stepsopen in new window for Cosmos users.

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.

The Multichain project contains multiple manifest files, with support for the following handlers:

  • 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

Beginning with Osmosis, the manifest file for this will be as follows:

osmosis.yaml
dataSources:
  - kind: cosmos/Runtime
    startBlock: 12427162
    mapping:
      file: ./dist/index.js
      handlers:
        - handler: handleOsmosisReceiveEvent
          kind: cosmos/EventHandler
          filter:
            type: recv_packet
            messageFilter:
              type: /ibc.core.channel.v1.MsgRecvPacket
        - handler: handleOsmosisSendEvent
          kind: cosmos/EventHandler
          filter:
            type: send_packet
            messageFilter:
              type: /ibc.applications.transfer.v1.MsgTransfer

As evident, we are in search of two types of events – namely, send_packet and recv_packet – representing outgoing and incoming transfers, respectively. The information from these logs will subsequently undergo comparison with the data emitted on the opposite end of the transfer.

Then, create a multi-chain manifest file. After, following the steps outlined here, start adding the new networks. After you successfuly apply the correct entities for each chain, you will end up with a single subquery-multichain.yaml file that we'll map to the individual chain manifest files. This multi-chain manifest file will look something like this:

subquery-multichain.yaml
specVersion: 1.0.0
query:
  name: "@subql/query"
  version: "*"
projects:
  - osmosis.yaml
  - cosmoshub.yaml

Now, we have to indicate that we want to handle the same data from Cosmos Hub, which data will be matched with Osmosis's smart contract data. The manifest file for Cosmos Hub will have the following look:

cosmoshub.yaml
dataSources:
  - kind: cosmos/Runtime
    startBlock: 17934016
    mapping:
      file: ./dist/index.js
      handlers:
        - handler: handleCosmosHubSendEvent
          kind: cosmos/EventHandler
          filter:
            type: send_packet
            messageFilter:
              type: /ibc.applications.transfer.v1.MsgTransfer
        - handler: handleCosmosHubReceiveEvent
          kind: cosmos/EventHandler
          filter:
            type: recv_packet
            messageFilter:
              type: /ibc.core.channel.v1.MsgRecvPacket

Here, again we are relying to the data of the same events. Events of both chanins will be processed asynchronously, without a specific order, and will be matched according to their data.

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.

type BridgeEvent @entity {
  id: ID!
  sender: User!
  receiver: User!
  sourceChain: String
  sourceChainTransaction: String
  destinationChain: String
  destinationChainTransaction: String
  amount: BigInt
}

type User @entity {
  id: ID! # Wallet Address
}

The primary event is the BridgeEvent, which contains information about the execution of the transfer on both sides. It will be completely populated only upon successful indexing on both ends. Further details about the logic will be discussed later.

SubQuery simplifies and ensures type-safety when working with GraphQL entities, actions, and transactions.

yarn
yarn 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.

If you've expressed a preference to employ the Cosmos message based on the provided proto files, this command will also generate types for your listed protobufs and save them into src/types directory, providing you with more typesafety. For example, you can find Osmosis' protobuf definitions in the official documentationopen in new window. Read about how this is done in Cosmos Codegen from CosmWasm Protobufs and Cosmos Manifest File Configuration.

Now that you have made essential changes to the GraphQL Schema file, let’s go ahead with the next configuration.

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.

Note

Check out our Mappings documentation to get more information on mapping functions.

Navigate to the default mapping function in the src/mappings directory. Setting up mappings for this the Cosmos chains is straightforward. In this instance, the mappings are stored within the src/mappings directory, with the sole mapping file being mappingHandlers.ts. Now, let's take a closer look at it:

mappingHandlers.ts
import { CosmosEvent } from "@subql/types-cosmos";
import { User, BridgeEvent } from "../types";

interface EssentialValues {
  sender?: string;
  amount?: number;
  receiver?: string;
  sequence?: string;
}

async function checkGetUser(user: string): Promise<User> {
  let userRecord = await User.get(user.toLowerCase());
  if (!userRecord) {
    userRecord = User.create({
      id: user.toLowerCase(),
    });
    await userRecord.save();
  }
  return userRecord;
}

async function getEssensialValues(
  event: CosmosEvent,
): Promise<EssentialValues> {
  let sender;
  let amount;
  let receiver;
  let sequence;
  for (const attr of event.event.attributes) {
    switch (attr.key) {
      case "packet_data":
        sender = JSON.parse(attr.value)["sender"];
        receiver = JSON.parse(attr.value)["receiver"];
        amount = JSON.parse(attr.value)["amount"];
        break;
      case "packet_sequence":
        sequence = attr.value;
        break;
      default:
        break;
    }
  }
  return { sender, amount, receiver, sequence };
}

async function populateValuesFromSource(
  sender: string,
  amount: string,
  receiver: string,
  sequence: string,
  event: CosmosEvent,
) {
  let bridgeTransactionRecord = await BridgeEvent.get(sequence);
  if (!bridgeTransactionRecord) {
    bridgeTransactionRecord = BridgeEvent.create({
      id: sequence,
      senderId: (await checkGetUser(sender)).id,
      receiverId: (await checkGetUser(receiver)).id,
      sourceChain: event.block.header.chainId,
      sourceChainTransaction: event.tx.hash.toString(),
      amount: BigInt(amount),
    });
  } else {
    bridgeTransactionRecord.sourceChain = event.block.header.chainId;
    bridgeTransactionRecord.sourceChainTransaction = event.tx.hash.toString();
  }
  await bridgeTransactionRecord.save();
}

async function populateValuesFromDestination(
  sender: string,
  amount: string,
  receiver: string,
  sequence: string,
  event: CosmosEvent,
) {
  let bridgeTransactionRecord = await BridgeEvent.get(sequence);
  if (!bridgeTransactionRecord) {
    bridgeTransactionRecord = BridgeEvent.create({
      id: sequence,
      senderId: (await checkGetUser(sender)).id,
      receiverId: (await checkGetUser(receiver)).id,
      destinationChain: event.block.header.chainId,
      destinationChainTransaction: event.tx.hash.toString(),
      amount: BigInt(amount),
    });
  } else {
    bridgeTransactionRecord.destinationChain = event.block.header.chainId;
    bridgeTransactionRecord.destinationChainTransaction =
      event.tx.hash.toString();
  }
  await bridgeTransactionRecord.save();
}

export async function handleOsmosisReceiveEvent(
  event: CosmosEvent,
): Promise<void> {
  logger.info(
    `Handling an incoming transfer event on Osmosis from ${event.tx.hash.toString()}`,
  );

  const { sender, amount, receiver, sequence } =
    await getEssensialValues(event);
  logger.info(sender);
  logger.info(sequence);
  logger.info(receiver);
  logger.info(amount);
  if (sequence && sender && receiver && amount) {
    populateValuesFromDestination(
      sender,
      amount.toString(),
      receiver,
      sequence,
      event,
    );
  }
}

export async function handleCosmosHubReceiveEvent(
  event: CosmosEvent,
): Promise<void> {
  logger.info(
    `Handling an incoming transfer event on Cosmos Hub from ${event.tx.hash.toString()}`,
  );

  const { sender, amount, receiver, sequence } =
    await getEssensialValues(event);
  if (sequence && sender && receiver && amount) {
    populateValuesFromDestination(
      sender,
      amount.toString(),
      receiver,
      sequence,
      event,
    );
  }
}

export async function handleCosmosHubSendEvent(
  event: CosmosEvent,
): Promise<void> {
  logger.info(
    `Handling an outgoing transfer event on Cosmos Hub from ${event.tx.hash.toString()}`,
  );

  const { sender, amount, receiver, sequence } =
    await getEssensialValues(event);

  if (sequence && sender && receiver && amount) {
    populateValuesFromSource(
      sender,
      amount.toString(),
      receiver,
      sequence,
      event,
    );
  }
}

export async function handleOsmosisSendEvent(
  event: CosmosEvent,
): Promise<void> {
  logger.info(
    `Handling an outgoing transfer event on Osmosis from ${event.tx.hash.toString()}`,
  );

  const { sender, amount, receiver, sequence } =
    await getEssensialValues(event);

  if (sequence && sender && receiver && amount) {
    populateValuesFromSource(
      sender,
      amount.toString(),
      receiver,
      sequence,
      event,
    );
  }
}

This mapping file is designed to handle events related to IBC transfers. The code imports necessary types and includes custom types, such as User and BridgeEvent. An EssentialValues interface is defined to represent crucial attributes like sender, amount, receiver, and sequence.

The script includes several event-handling functions, namely handleOsmosisReceiveEvent, handleCosmosHubReceiveEvent, handleCosmosHubSendEvent, and handleOsmosisSendEvent. These functions log information about incoming or outgoing transfer events and then call the appropriate functions to populate or update values in the BridgeEvent entity.

The code has the utility functions like checkGetUser, which asynchronously checks if a user exists in the database. If not, it creates a new user record. Following this, the getEssentialValues function extracts essential values (sender, amount, receiver, sequence) from a Cosmos event's attributes.

Two functions, namely populateValuesFromSource and populateValuesFromDestination, asynchronously populate or update values in the BridgeEvent entity based on whether it's the source or destination chain. In this example, we utilise the sequence value to correlate transfers across diverse networks.

Tips

The final code of this project can be found hereopen in new window.

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
yarn 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
yarn 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:

  1. Open your browser and head to http://localhost:3000.

  2. You will see a GraphQL playground in the browser and the schemas which are ready to query.

  3. 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.

Request

query {
  bridgeEvents {
    nodes {
      id
      senderId
      receiverId
      sourceChain
      sourceChainTransaction
      destinationChain
      destinationChainTransaction
      amount
    }
  }
}

Response

{
  "data": {
    "bridgeEvents": {
      "nodes": [
        {
          "id": "2443408",
          "senderId": "cosmos1nlj3wgunmjehnpue8a98wsflnx26tfhxmhfzhc",
          "receiverId": "osmo1ln7rurzjr4x5403qhpyuma0x53dgkzkk0vewf3xr7rc24s57yzvshuqzdv",
          "sourceChain": "cosmoshub-4",
          "sourceChainTransaction": "3002CF4484AB82F8F995CC54FDD5B359327F89D861A0F9A7EF17475554A4BC8B",
          "destinationChain": "osmosis-1",
          "destinationChainTransaction": "9B98ED344801C4EB22047E455F36435FC008192985151B7DA70EA67246E1AD04",
          "amount": "3000000"
        }
      ]
    }
  }
}

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.