From d57680187a38ae586394b382f3f1dcf15963722a Mon Sep 17 00:00:00 2001 From: godlin Date: Mon, 20 Nov 2023 20:41:31 +0100 Subject: [PATCH] Add Multi-Chain IBC Transfers Example --- .../.github/workflows/cli-deploy.yml | 33 ++++ .../.github/workflows/pr.yml | 24 +++ .../workflows/scripts/publish-deploy.sh | 14 ++ Multi-Chain/osmosis-cosmos-bridge/.gitignore | 59 ++++++ Multi-Chain/osmosis-cosmos-bridge/LICENSE | 21 ++ Multi-Chain/osmosis-cosmos-bridge/README.md | 79 ++++++++ .../osmosis-cosmos-bridge/cosmoshub.yaml | 40 ++++ .../osmosis-cosmos-bridge/docker-compose.yml | 90 +++++++++ .../docker/load-extensions.sh | 5 + .../docker/pg-Dockerfile | 12 ++ .../osmosis-cosmos-bridge/osmosis.yaml | 37 ++++ .../osmosis-cosmos-bridge/package.json | 37 ++++ .../osmosis-cosmos-bridge/schema.graphql | 18 ++ .../osmosis-cosmos-bridge/src/index.ts | 2 + .../src/mappings/mappingHandlers.ts | 183 ++++++++++++++++++ .../src/test/mappingHandlers.test.ts | 12 ++ .../subquery-multichain.yaml | 7 + .../osmosis-cosmos-bridge/tsconfig.json | 20 ++ 18 files changed, 693 insertions(+) create mode 100644 Multi-Chain/osmosis-cosmos-bridge/.github/workflows/cli-deploy.yml create mode 100644 Multi-Chain/osmosis-cosmos-bridge/.github/workflows/pr.yml create mode 100644 Multi-Chain/osmosis-cosmos-bridge/.github/workflows/scripts/publish-deploy.sh create mode 100644 Multi-Chain/osmosis-cosmos-bridge/.gitignore create mode 100644 Multi-Chain/osmosis-cosmos-bridge/LICENSE create mode 100644 Multi-Chain/osmosis-cosmos-bridge/README.md create mode 100644 Multi-Chain/osmosis-cosmos-bridge/cosmoshub.yaml create mode 100644 Multi-Chain/osmosis-cosmos-bridge/docker-compose.yml create mode 100644 Multi-Chain/osmosis-cosmos-bridge/docker/load-extensions.sh create mode 100644 Multi-Chain/osmosis-cosmos-bridge/docker/pg-Dockerfile create mode 100644 Multi-Chain/osmosis-cosmos-bridge/osmosis.yaml create mode 100644 Multi-Chain/osmosis-cosmos-bridge/package.json create mode 100644 Multi-Chain/osmosis-cosmos-bridge/schema.graphql create mode 100644 Multi-Chain/osmosis-cosmos-bridge/src/index.ts create mode 100644 Multi-Chain/osmosis-cosmos-bridge/src/mappings/mappingHandlers.ts create mode 100644 Multi-Chain/osmosis-cosmos-bridge/src/test/mappingHandlers.test.ts create mode 100644 Multi-Chain/osmosis-cosmos-bridge/subquery-multichain.yaml create mode 100644 Multi-Chain/osmosis-cosmos-bridge/tsconfig.json diff --git a/Multi-Chain/osmosis-cosmos-bridge/.github/workflows/cli-deploy.yml b/Multi-Chain/osmosis-cosmos-bridge/.github/workflows/cli-deploy.yml new file mode 100644 index 000000000..5edba7c00 --- /dev/null +++ b/Multi-Chain/osmosis-cosmos-bridge/.github/workflows/cli-deploy.yml @@ -0,0 +1,33 @@ +name: "CLI deploy" + +on: + workflow_dispatch: + inputs: + projectName: + description: "Project name" + required: true + type: string +jobs: + deploy: + name: CLI Deploy + runs-on: ubuntu-latest + environment: + name: DEPLOYMENT + env: + SUBQL_ACCESS_TOKEN: ${{ secrets.SUBQL_ACCESS_TOKEN }} + steps: + - uses: actions/checkout@v2 + - name: Setup Node.js environment + uses: actions/setup-node@v2 + with: + node-version: 16 + - run: yarn + - name: Codegen + run: yarn codegen + - name: Version + run: npx subql --version + - name: repo + run: echo ${{github.repository}} + - name: Publish and Deploy + run: | + sh .github/workflows/scripts/publish-deploy.sh -o ${{github.repository}} -p ${{github.event.inputs.projectName}} diff --git a/Multi-Chain/osmosis-cosmos-bridge/.github/workflows/pr.yml b/Multi-Chain/osmosis-cosmos-bridge/.github/workflows/pr.yml new file mode 100644 index 000000000..93b6fc5b5 --- /dev/null +++ b/Multi-Chain/osmosis-cosmos-bridge/.github/workflows/pr.yml @@ -0,0 +1,24 @@ +name: PR +on: + pull_request: + paths-ignore: + - ".github/workflows/**" +jobs: + pr: + name: pr + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Setup Node.js environment + uses: actions/setup-node@v2 + with: + node-version: 16 + - run: yarn + - name: Codegen + run: yarn codegen + - name: Build + run: yarn build + - name: Install subql-node-cosmos + run: yarn global add @subql/node-cosmos + - name: Run tests with Subquery Node + run: subql-node-cosmos test -f ${{ github.workspace }} diff --git a/Multi-Chain/osmosis-cosmos-bridge/.github/workflows/scripts/publish-deploy.sh b/Multi-Chain/osmosis-cosmos-bridge/.github/workflows/scripts/publish-deploy.sh new file mode 100644 index 000000000..adae267d9 --- /dev/null +++ b/Multi-Chain/osmosis-cosmos-bridge/.github/workflows/scripts/publish-deploy.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +while getopts p:o: flag +do + case "${flag}" in + p) PROJECTNAME=${OPTARG};; + o) ORG=${OPTARG};; + *) echo "Usage: $0 [-p projectname] [-o org]" && exit 1;; + esac +done + +IPFSCID=$(npx subql publish -o -f .) + +npx subql deployment:deploy -d --ipfsCID="$IPFSCID" --projectName="${PROJECTNAME}" --org="${ORG%/*}" \ No newline at end of file diff --git a/Multi-Chain/osmosis-cosmos-bridge/.gitignore b/Multi-Chain/osmosis-cosmos-bridge/.gitignore new file mode 100644 index 000000000..af37ddbf4 --- /dev/null +++ b/Multi-Chain/osmosis-cosmos-bridge/.gitignore @@ -0,0 +1,59 @@ + +# These are some examples of commonly ignored file patterns. +# You should customize this list as applicable to your project. +# Learn more about .gitignore: +# https://www.atlassian.com/git/tutorials/saving-changes/gitignore + +# Node artifact files +node_modules/ +dist/ + +# lock files +yarn.lock +package-lock.json + +# Compiled Java class files +*.class + +# Compiled Python bytecode +*.py[cod] + +# Log files +*.log + +# Package files +*.jar + +# Generated files +target/ +dist/ +src/types +project.yaml + +# JetBrains IDE +.idea/ + +# Unit test reports +TEST*.xml + +# Generated by MacOS +.DS_Store + +# Generated by Windows +Thumbs.db + +# Applications +*.app +*.exe +*.war + +# Large media files +*.mp4 +*.tiff +*.avi +*.flv +*.mov +*.wmv + +.data +dist diff --git a/Multi-Chain/osmosis-cosmos-bridge/LICENSE b/Multi-Chain/osmosis-cosmos-bridge/LICENSE new file mode 100644 index 000000000..76e70ccd3 --- /dev/null +++ b/Multi-Chain/osmosis-cosmos-bridge/LICENSE @@ -0,0 +1,21 @@ +MIT LICENSE + +Copyright 2020-2021 SubQuery Pte Ltd authors & contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Multi-Chain/osmosis-cosmos-bridge/README.md b/Multi-Chain/osmosis-cosmos-bridge/README.md new file mode 100644 index 000000000..5f367137e --- /dev/null +++ b/Multi-Chain/osmosis-cosmos-bridge/README.md @@ -0,0 +1,79 @@ +# SubQuery - Multi-Chain. Example project for IBC transfers between Osmosis and Cosmos Hub + +[SubQuery](https://subquery.network) is a fast, flexible, and reliable open-source data indexer that provides you with custom APIs for your web3 project across all of our supported networks. To learn about how to get started with SubQuery, [visit our docs](https://academy.subquery.network). + +**The SubQuery project is responsible for indexing all IBC transfer events occurring between Cosmos Hub and Osmosis** + +## Start + +First, install SubQuery CLI globally on your terminal by using NPM `npm install -g @subql/cli` + +You can either clone this GitHub repo, or use the `subql` CLI to bootstrap a clean project in the network of your choosing by running `subql init` and following the prompts. + +Don't forget to install dependencies with `npm install` or `yarn install`! + +## Editing your SubQuery project + +Although this is a working example SubQuery project, you can edit the SubQuery project by changing the following files: + +- The project manifest in `project.ts` defines the key project configuration and mapping handler filters +- The GraphQL Schema (`schema.graphql`) defines the shape of the resulting data that you are using SubQuery to index +- The Mapping functions in `src/mappings/` directory are typescript functions that handle transformation logic + +SubQuery supports various layer-1 blockchain networks and provides [dedicated quick start guides](https://academy.subquery.network/quickstart/quickstart.html) as well as [detailed technical documentation](https://academy.subquery.network/build/introduction.html) for each of them. + +## Run your project + +_If you get stuck, find out how to get help below._ + +The simplest way to run your project is by running `yarn dev` or `npm run-script dev`. This does all of the following: + +1. `yarn codegen` - Generates types from the GraphQL schema definition and contract ABIs and saves them in the `/src/types` directory. This must be done after each change to the `schema.graphql` file or the contract ABIs +2. `yarn build` - Builds and packages the SubQuery project into the `/dist` directory +3. `docker-compose pull && docker-compose up` - Runs a Docker container with an indexer, PostgeSQL DB, and a query service. This requires [Docker to be installed](https://docs.docker.com/engine/install) and running locally. The configuration for this container is set from your `docker-compose.yml` + +You can observe the three services start, and once all are running (it may take a few minutes on your first start), please open your browser and head to [http://localhost:3000](http://localhost:3000) - you should see a GraphQL playground showing with the schemas ready to query. [Read the docs for more information](https://academy.subquery.network/run_publish/run.html) or [explore the possible service configuration for running SubQuery](https://academy.subquery.network/run_publish/references.html). + +## Query your project + +For this project, you can try to query with the following GraphQL code to get a taste of how it works. + +```graphql +query { + cosmosToOsmosisBridges { + nodes { + id + senderId + receiverId + sourceChain + sourceChainTransaction + destinationChain + destinationChainTransaction + amount + } + } +} +``` + +You can explore the different possible queries and entities to help you with GraphQL using the documentation draw on the right. + +## Publish your project + +SubQuery is open-source, meaning you have the freedom to run it in the following three ways: + +- Locally on your own computer (or a cloud provider of your choosing), [view the instructions on how to run SubQuery Locally](https://academy.subquery.network/run_publish/run.html) +- By publishing it to our enterprise-level [Managed Service](https://managedservice.subquery.network), where we'll host your SubQuery project in production ready services for mission critical data with zero-downtime blue/green deployments. We even have a generous free tier. [Find out how](https://academy.subquery.network/run_publish/publish.html) +- [Coming Soon] By publishing it to the decentralised [SubQuery Network](https://subquery.network/network), the most open, performant, reliable, and scalable data service for dApp developers. The SubQuery Network indexes and services data to the global community in an incentivised and verifiable way + +## What Next? + +Take a look at some of our advanced features to take your project to the next level! + +- [**Multi-chain indexing support**](https://academy.subquery.network/build/multi-chain.html) - SubQuery allows you to index data from across different layer-1 networks into the same database, this allows you to query a single endpoint to get data for all supported networks. +- [**Dynamic Data Sources**](https://academy.subquery.network/build/dynamicdatasources.html) - When you want to index factory contracts, for example on a DEX or generative NFT project. +- [**Project Optimisation Advice**](https://academy.subquery.network/build/optimisation.html) - Some common tips on how to tweak your project to maximise performance. +- [**GraphQL Subscriptions**](https://academy.subquery.network/run_publish/subscription.html) - Build more reactive front end applications that subscribe to changes in your SubQuery project. + +## Need Help? + +The fastest way to get support is by [searching our documentation](https://academy.subquery.network), or by [joining our discord](https://discord.com/invite/subquery) and messaging us in the `#technical-support` channel. diff --git a/Multi-Chain/osmosis-cosmos-bridge/cosmoshub.yaml b/Multi-Chain/osmosis-cosmos-bridge/cosmoshub.yaml new file mode 100644 index 000000000..e9c8ec2d0 --- /dev/null +++ b/Multi-Chain/osmosis-cosmos-bridge/cosmoshub.yaml @@ -0,0 +1,40 @@ +specVersion: 1.0.0 +name: cosmoshub-ibc +version: 0.0.1 +runner: + node: + name: "@subql/node-cosmos" + version: "*" + query: + name: "@subql/query" + version: "*" +repository: "https://github.com/subquery/juno-subql-starter" +schema: + file: ./schema.graphql +network: + chainId: cosmoshub-4 + # This endpoint must be a public non-pruned archive node + # We recommend providing more than one endpoint for improved reliability, performance, and uptime + # Public nodes may be rate limited, which can affect indexing speed + # When developing your project we suggest getting a private API key + endpoint: ["https://cosmos-rpc.publicnode.com:443"] + # Optionally provide the HTTP endpoint of a full chain dictionary to speed up processing + dictionary: "https://api.subquery.network/sq/subquery/cosmos-hub-dictionary" +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 diff --git a/Multi-Chain/osmosis-cosmos-bridge/docker-compose.yml b/Multi-Chain/osmosis-cosmos-bridge/docker-compose.yml new file mode 100644 index 000000000..02d212585 --- /dev/null +++ b/Multi-Chain/osmosis-cosmos-bridge/docker-compose.yml @@ -0,0 +1,90 @@ +version: "3" + +services: + postgres: + build: + context: . + dockerfile: ./docker/pg-Dockerfile + ports: + - 5432:5432 + volumes: + - .data/postgres:/var/lib/postgresql/data + environment: + POSTGRES_PASSWORD: postgres + healthcheck: + test: ["CMD-SHELL", "pg_isready -U postgres"] + interval: 5s + timeout: 5s + retries: 5 + + subquery-node-osmosis: + image: subquerynetwork/subql-node-cosmos:latest + depends_on: + "postgres": + condition: service_healthy + restart: always + environment: + DB_USER: postgres + DB_PASS: postgres + DB_DATABASE: postgres + DB_HOST: postgres + DB_PORT: 5432 + volumes: + - ./:/app + command: + - ${SUB_COMMAND:-} # set SUB_COMMAND env variable to "test" to run tests + - --multi-chain + - --db-schema=multi-transfers + - --disable-historical + - -f=app/osmosis.yaml + - --batch-size=1 + healthcheck: + test: ["CMD", "curl", "-f", "http://subquery-node:3000/ready"] + interval: 3s + timeout: 5s + retries: 10 + + subquery-node-cosmoshub: + image: subquerynetwork/subql-node-cosmos:latest + depends_on: + "postgres": + condition: service_healthy + restart: always + environment: + DB_USER: postgres + DB_PASS: postgres + DB_DATABASE: postgres + DB_HOST: postgres + DB_PORT: 5432 + volumes: + - ./:/app + command: + - ${SUB_COMMAND:-} # set SUB_COMMAND env variable to "test" to run tests + - --multi-chain + - --db-schema=multi-transfers + - --disable-historical + - -f=app/cosmoshub.yaml + - --batch-size=1 + healthcheck: + test: ["CMD", "curl", "-f", "http://subquery-node:3000/ready"] + interval: 3s + timeout: 5s + retries: 10 + + graphql-engine: + image: onfinality/subql-query:latest + ports: + - 3000:3000 + depends_on: + "postgres": + condition: service_healthy + restart: always + environment: + DB_USER: postgres + DB_PASS: postgres + DB_DATABASE: postgres + DB_HOST: postgres + DB_PORT: 5432 + command: + - --name=multi-transfers + - --playground diff --git a/Multi-Chain/osmosis-cosmos-bridge/docker/load-extensions.sh b/Multi-Chain/osmosis-cosmos-bridge/docker/load-extensions.sh new file mode 100644 index 000000000..6d33f8638 --- /dev/null +++ b/Multi-Chain/osmosis-cosmos-bridge/docker/load-extensions.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <=3.0.0" + query: + name: "@subql/query" + version: "*" +schema: + file: ./schema.graphql +network: + chainId: osmosis-1 + endpoint: + - >- + https://osmosis-rpc.publicnode.com:443 + dictionary: https://api.subquery.network/sq/subquery/cosmos-osmosis-dictionary +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 diff --git a/Multi-Chain/osmosis-cosmos-bridge/package.json b/Multi-Chain/osmosis-cosmos-bridge/package.json new file mode 100644 index 000000000..826397f87 --- /dev/null +++ b/Multi-Chain/osmosis-cosmos-bridge/package.json @@ -0,0 +1,37 @@ +{ + "name": "osmosis-cosmos-ibc", + "version": "0.0.1", + "description": "This project can be use as a starting point for developing your Cosmos (Cosmos Hub) based SubQuery project", + "main": "dist/index.js", + "scripts": { + "build": "subql build", + "codegen": "subql codegen", + "start:docker": "docker-compose pull && docker-compose up --remove-orphans", + "dev": "subql codegen && subql build && docker-compose pull && docker-compose up --remove-orphans", + "prepack": "rm -rf dist && npm run build", + "test": "subql build && subql-node-cosmos test" + }, + "homepage": "https://github.com/subquery/cosmos-subql-starter", + "repository": "github:subquery/cosmos-subql-starter", + "files": [ + "dist", + "schema.graphql", + "project.yaml" + ], + "author": "SubQuery Team", + "license": "MIT", + "devDependencies": { + "@cosmjs/stargate": "^0.28.9", + "@subql/cli": "latest", + "@subql/testing": "latest", + "@subql/node-cosmos": "latest", + "typescript": "^5.2.2" + }, + "dependencies": { + "@subql/types-cosmos": "latest", + "@types/node": "^17.0.21", + "pino": "^7.8.0", + "ts-proto": "^1.112.1", + "tslib": "^2.3.1" + } +} \ No newline at end of file diff --git a/Multi-Chain/osmosis-cosmos-bridge/schema.graphql b/Multi-Chain/osmosis-cosmos-bridge/schema.graphql new file mode 100644 index 000000000..9058f3c49 --- /dev/null +++ b/Multi-Chain/osmosis-cosmos-bridge/schema.graphql @@ -0,0 +1,18 @@ +# To improve query performance, we strongly suggest adding indexes to any field that you plan to filter or sort by +# Add the `@index` or `@index(unique: true)` annotation after any non-key field +# https://academy.subquery.network/build/graphql.html#indexing-by-non-primary-key-field + +type CosmosToOsmosisBridge @entity { + id: ID! + sender: User! + receiver: User! + sourceChain: String + sourceChainTransaction: String + destinationChain: String + destinationChainTransaction: String + amount: BigInt +} + +type User @entity { + id: ID! # Wallet Address +} diff --git a/Multi-Chain/osmosis-cosmos-bridge/src/index.ts b/Multi-Chain/osmosis-cosmos-bridge/src/index.ts new file mode 100644 index 000000000..fb59776f0 --- /dev/null +++ b/Multi-Chain/osmosis-cosmos-bridge/src/index.ts @@ -0,0 +1,2 @@ +//Exports all handler functions +export * from "./mappings/mappingHandlers"; diff --git a/Multi-Chain/osmosis-cosmos-bridge/src/mappings/mappingHandlers.ts b/Multi-Chain/osmosis-cosmos-bridge/src/mappings/mappingHandlers.ts new file mode 100644 index 000000000..18716ccbd --- /dev/null +++ b/Multi-Chain/osmosis-cosmos-bridge/src/mappings/mappingHandlers.ts @@ -0,0 +1,183 @@ +import { CosmosEvent } from "@subql/types-cosmos"; +import { User, CosmosToOsmosisBridge } from "../types"; + +interface EssentialValues { + sender?: string; + amount?: number; + receiver?: string; + sequence?: string; +} + +async function checkGetUser(user: string): Promise { + 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 { + 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 CosmosToOsmosisBridge.get(sequence); + if (!bridgeTransactionRecord) { + bridgeTransactionRecord = CosmosToOsmosisBridge.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 CosmosToOsmosisBridge.get(sequence); + if (!bridgeTransactionRecord) { + bridgeTransactionRecord = CosmosToOsmosisBridge.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 { + logger.warn( + `Handling an incoming transfer event on Osmosis from ${event.tx.hash.toString()}` + ); + + const { sender, amount, receiver, sequence } = await getEssensialValues( + event + ); + logger.warn(sender); + logger.warn(sequence); + logger.warn(receiver); + logger.warn(amount); + if (sequence && sender && receiver && amount) { + populateValuesFromDestination( + sender, + amount.toString(), + receiver, + sequence, + event + ); + } +} + +export async function handleCosmosHubReceiveEvent( + event: CosmosEvent +): Promise { + logger.warn( + `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 { + logger.warn( + `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 { + logger.warn( + `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 + ); + } +} diff --git a/Multi-Chain/osmosis-cosmos-bridge/src/test/mappingHandlers.test.ts b/Multi-Chain/osmosis-cosmos-bridge/src/test/mappingHandlers.test.ts new file mode 100644 index 000000000..42f41c3fb --- /dev/null +++ b/Multi-Chain/osmosis-cosmos-bridge/src/test/mappingHandlers.test.ts @@ -0,0 +1,12 @@ +import { subqlTest } from "@subql/testing"; + +/* +// https://academy.subquery.network/build/testing.html +subqlTest( + "testName", // test name + 1000003, // block height to process + [], // dependent entities + [], // expected entities + "handleEvent" //handler name +); +*/ diff --git a/Multi-Chain/osmosis-cosmos-bridge/subquery-multichain.yaml b/Multi-Chain/osmosis-cosmos-bridge/subquery-multichain.yaml new file mode 100644 index 000000000..78e9924ad --- /dev/null +++ b/Multi-Chain/osmosis-cosmos-bridge/subquery-multichain.yaml @@ -0,0 +1,7 @@ +specVersion: 1.0.0 +query: + name: '@subql/query' + version: '*' +projects: + - osmosis.yaml + - cosmoshub.yaml diff --git a/Multi-Chain/osmosis-cosmos-bridge/tsconfig.json b/Multi-Chain/osmosis-cosmos-bridge/tsconfig.json new file mode 100644 index 000000000..f3019d720 --- /dev/null +++ b/Multi-Chain/osmosis-cosmos-bridge/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "esModuleInterop": true, + "declaration": true, + "importHelpers": true, + "resolveJsonModule": true, + "module": "commonjs", + "outDir": "dist", + "rootDir": "src", + "target": "es2017", + "strict": true + }, + "include": [ + "src/**/*", + "node_modules/@subql/types-core/dist/global.d.ts", + "node_modules/@subql/types-cosmos/dist/global.d.ts" + ] +}