Skip to main content

Osmosis Quick Start

SubQuery TeamAbout 3 min

Osmosis Quick Start

Osmosisopen in new window is a DEX built on the Cosmos ecosystem. It is designed to allow users to trade tokens from different blockchains that are part of the Cosmos ecosystem. Osmosis uses the IBC protocol to enable the transfer of assets between different blockchains, including Cosmos Hubopen in new window, Akash, and others.

This guide acts as your entrance to a detailed tutorial for configuring a SubQuery indexer that is specifically designed to index swaps occurring on Osmosis. Upon completing this guide, you will possess a solid understanding of the process for indexing data related to a complex DEX such as Osmosis.

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.

Tips

The final code of this project can be found hereopen in new window. We also offer a pre-recorded workshopopen in new window for this sample project, simplifying the process of keeping up with it.

Check out how to get the Cosmos Osmosis starter project running

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 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 the manifest file looks for on the blockchain to start indexing.

dataSources: [
  {
    kind: CosmosDatasourceKind.Runtime,
    startBlock: 11253914,
    mapping: {
      file: "./dist/index.js",
      handlers: [
        {
          handler: "handleMessage",
          kind: CosmosHandlerKind.Message,
          filter: {
            type: "/osmosis.gamm.v1beta1.MsgSwapExactAmountIn",
          },
        },
      ],
    },
  },
];

Within the provided code snippet, we've established a single handler handleMessage, which will execute every time a message of the MsgSwapExactAmountIn type is detected. This handler is sufficient to monitor and record swaps within Osmosis. Check out our Manifest File documentation to get more information about the Project Manifest (project.ts) file.

Check out our Manifest File documentation to get more information about the Project Manifest (project.ts) file.

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.

For this project, you'll need to modify your schema.graphql file as follows.

type Swap @entity {
  id: ID!
  sender: String!
  txHash: String!
  blockHeight: BigInt!
  tokenInDenom: String
  tokenInAmount: BigInt
  tokenOutMin: BigInt!
  swapRoutes: [SwapRoute] @derivedFrom(field: "swap") #This is virtual field
}

type SwapRoute @entity {
  id: ID!
  pool: Pool!
  swap: Swap!
  tokenInDenom: String
  tokenOutDenom: String!
}

type Pool @entity {
  id: ID!
  swapRoutes: [SwapRoute] @derivedFrom(field: "pool") #This is virtual field
}

Since we're indexing all swaps, we have a Swap entity that comprises a number of properties, including the sender, ammounts, swapRoutes, and so forth. We also deriving SwapRoute as a separate entity as it carries important business information. Finally, we also declaring a Pool entity and connnect it to SwapRoute so that we can get all the routes that a particular pool take place it.

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

You will find the generated models in the /src/types/models directory. You can conveniently import all these entities from the following directory:

// Import entity types generated from the GraphQL schema
import { Pool, Swap, SwapRoute } from "../types";

As you're creating a new 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. 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. The following chain types are used in this example project:

    chaintypes: new Map([
      [
        "osmosis.gamm.v1beta1",
        {
          file: "./proto/osmosis/gamm/v1beta1/tx.proto",
          messages: ["MsgSwapExactAmountIn"],
        },
      ],
      [
        " osmosis.poolmanager.v1beta1",
        {
          // needed by MsgSwapExactAmountIn
          file: "./proto/osmosis/poolmanager/v1beta1/swap_route.proto",
          messages: ["SwapAmountInRoute"],
        },
      ],
      [
        "cosmos.base.v1beta1",
        {
          // needed by MsgSwapExactAmountIn
          file: "./proto/cosmos/base/v1beta1/coin.proto",
          messages: ["Coin"],
        },
      ],
    ]),

The relevant types can be imported from the directory with the newly generated code:

// Import a smart contract event class generated from provided ABIs
import { MsgSwapExactAmountInMessage } from "../types/CosmosMessageTypes";

Check out the GraphQL Schema documentation to get in-depth information on schema.graphql file.

Now that you have made essential changes to the GraphQL Schema file, let’s proceed ahead with the Mapping Function’s 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.

Navigate to the default mapping function in the src/mappings directory and update your mapping files to match the following (note the additional imports):

import { MsgSwapExactAmountInMessage } from "../types/CosmosMessageTypes";
import { Pool, Swap, SwapRoute } from "../types";

async function checkGetPool(id: string): Promise<Pool> {
  // Check that the pool exists and create new ones if now
  let pool = await Pool.get(id);
  if (!pool) {
    pool = new Pool(id);
    await pool.save();
  }
  return pool;
}

export async function handleMessage(
  msg: MsgSwapExactAmountInMessage,
): Promise<void> {
  // You can see an example record here https://www.mintscan.io/osmosis/txs/6A22C6C978A96D99FCB08826807C6EB1DCBDCEC6044C35105B624A81A1CB6E24?height=9798771
  logger.info(`New Swap Message received at block ${msg.block.header.height}`);
  logger.info(msg.tx.hash.toString());
  // logger.info(JSON.stringify(msg.tx.tx.events)); // You can use this to preview the data

  // We first create a new swap record
  const swap = Swap.create({
    id: `${msg.tx.hash}-${msg.idx}`,
    txHash: msg.tx.hash,
    blockHeight: BigInt(msg.block.block.header.height),
    sender: msg.msg.decodedMsg.sender,
    tokenInDenom: msg.msg.decodedMsg.tokenIn?.denom,
    tokenInAmount: msg.msg.decodedMsg.tokenIn
      ? BigInt(msg.msg.decodedMsg.tokenIn.amount)
      : undefined,
    tokenOutMin: BigInt(msg.msg.decodedMsg.tokenOutMinAmount),
  });

  // Save this to the DB
  await swap.save();

  // Create swap routes from the array on the message
  let lastTokenOutDenom = swap.tokenInDenom;
  for (const route of msg.msg.decodedMsg.routes) {
    const index = msg.msg.decodedMsg.routes.indexOf(route);
    // Check that the pool aready exists
    const pool = await checkGetPool(route.poolId.toString());

    const swapRoute = SwapRoute.create({
      id: `${msg.tx.hash}-${msg.idx}-${index}`,
      poolId: pool.id,
      swapId: swap.id,
      tokenInDenom: lastTokenOutDenom,
      tokenOutDenom: route.tokenOutDenom,
    });
    lastTokenOutDenom = route.tokenOutDenom;
    await swapRoute.save();
  }
}

The provided code has a single handler handleMessage - the main function responsible for handling swap messages. It takes a MsgSwapExactAmountInMessage as input, which represents a message related to a swap. Then, it creates a new Swap record based on arguments derived from message data, and this record is then saved to a database. It also iterates through the routes specified in the message, creating SwapRoute records for each route. These records are associated with the corresponding Pool and Swap, and they are also saved to the database. To check if a pool with a given id exists, the handler uses checkGetPool function. If a pool doesn't exist, a new pool is created and saved.

🎉 Now, you've effectively developed the handling logic for the Osmosis swaps and populated queryable entities, such as Swap, Pool and SwapRoute. This means you're ready to move on to the construction phase to test the indexer's functionality thus far.

Check out our Mappings documentation and get information on the mapping functions in detail.

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.

Swaps, Routes and Pools

Request

{
  swaps(first: 1) {
    nodes {
      id
      sender
      tokenInAmount
      tokenInDenom
      tokenOutMin
      txHash
      blockHeight
      swapRoutes {
        nodes {
          id
          poolId
          pool {
            id
          }
          swapId
          tokenInDenom
          tokenOutDenom
        }
      }
    }
  }
}

Response

{
  "data": {
    "swaps": {
      "nodes": [
        {
          "id": "02CB82FD9D669300D7454A25398568C763F839E61D6F5E3195474857B2B67C38-0",
          "sender": "osmo12ndfvm5pmpl0w9gpz7qep5q44exfkz8ulywz4a",
          "tokenInAmount": "294778916",
          "tokenInDenom": "ibc/1480B8FD20AD5FCAE81EA87584D269547DD4D436843C1D20F15E00EB64743EF4",
          "tokenOutMin": "296218304",
          "txHash": "02CB82FD9D669300D7454A25398568C763F839E61D6F5E3195474857B2B67C38",
          "blockHeight": "11253952",
          "swapRoutes": {
            "nodes": [
              {
                "id": "02CB82FD9D669300D7454A25398568C763F839E61D6F5E3195474857B2B67C38-0-0",
                "poolId": "3",
                "pool": {
                  "id": "3"
                },
                "swapId": "02CB82FD9D669300D7454A25398568C763F839E61D6F5E3195474857B2B67C38-0",
                "tokenInDenom": "ibc/1480B8FD20AD5FCAE81EA87584D269547DD4D436843C1D20F15E00EB64743EF4",
                "tokenOutDenom": "uosmo"
              },
              {
                "id": "02CB82FD9D669300D7454A25398568C763F839E61D6F5E3195474857B2B67C38-0-1",
                "poolId": "678",
                "pool": {
                  "id": "678"
                },
                "swapId": "02CB82FD9D669300D7454A25398568C763F839E61D6F5E3195474857B2B67C38-0",
                "tokenInDenom": "uosmo",
                "tokenOutDenom": "ibc/D189335C6E4A68B513C10AB227BF1C1D38C746766278BA3EEB4FB14124F1D858"
              }
            ]
          }
        }
      ]
    }
  }
}

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.