From d9cfbe7362cd5c963529837736945f774b9062bd Mon Sep 17 00:00:00 2001 From: godlin Date: Fri, 19 Jan 2024 13:26:18 +0100 Subject: [PATCH 1/2] improved token inputs/outputs --- Osmosis/osmosis-dex-data/project.ts | 2 +- Osmosis/osmosis-dex-data/schema.graphql | 6 +- .../src/mappings/mappingHandlers.ts | 120 ++++++++---------- 3 files changed, 56 insertions(+), 72 deletions(-) diff --git a/Osmosis/osmosis-dex-data/project.ts b/Osmosis/osmosis-dex-data/project.ts index 407aaa11c..c9f7ebf82 100644 --- a/Osmosis/osmosis-dex-data/project.ts +++ b/Osmosis/osmosis-dex-data/project.ts @@ -67,7 +67,7 @@ const project: CosmosProject = { dataSources: [ { kind: CosmosDatasourceKind.Runtime, - startBlock: 13200000, + startBlock: 13338604, mapping: { file: "./dist/index.js", handlers: [ diff --git a/Osmosis/osmosis-dex-data/schema.graphql b/Osmosis/osmosis-dex-data/schema.graphql index 23bd0f425..fe9471177 100644 --- a/Osmosis/osmosis-dex-data/schema.graphql +++ b/Osmosis/osmosis-dex-data/schema.graphql @@ -34,9 +34,11 @@ type SwapRoute @entity { id: ID! pool: Pool! swap: Swap! - tokenInDenom: String - tokenOutDenom: String + # tokenInDenom: String + # tokenOutDenom: String poolLiquidity: Float + tokensIn: String + tokensOut: String } type Pool @entity { diff --git a/Osmosis/osmosis-dex-data/src/mappings/mappingHandlers.ts b/Osmosis/osmosis-dex-data/src/mappings/mappingHandlers.ts index 12fbb4611..42cc3ff8b 100644 --- a/Osmosis/osmosis-dex-data/src/mappings/mappingHandlers.ts +++ b/Osmosis/osmosis-dex-data/src/mappings/mappingHandlers.ts @@ -1,7 +1,8 @@ import * as messages from "../types/CosmosMessageTypes"; import { Pool, Swap, SwapRoute, Direction, Message } from "../types"; -import { CosmosBlock } from "@subql/types-cosmos"; +import { CosmosBlock, CosmosMessage } from "@subql/types-cosmos"; import fetch from "node-fetch"; +import { Event } from "../types/proto-interfaces/tendermint/abci/types"; interface DataItem { time: string; @@ -109,44 +110,66 @@ function createSwap( }); } +async function createSwapRoutes(msg: CosmosMessage, swap: Swap) { + let stringified = msg.tx.tx.log; + if (stringified != undefined) { + let parsed = JSON.parse(stringified); + for (const obj of parsed) { + const { _, events } = obj; + let eventIndex: number = 0; + for (const event of events) { + const typedEvent = event as Event; + if (typedEvent.type === "token_swapped") { + // logger.warn(`eventIndex ${eventIndex}`); + let poolId: string = ""; + let tokensIn: string = ""; + let tokensOut: string = ""; + for (const attr of typedEvent.attributes) { + // logger.warn(`key: ${attr.key}, value: ${attr.value}`); + switch (attr.key.toString()) { + case "pool_id": + poolId = attr.value.toString(); + break; + case "tokens_in": + tokensIn = attr.value.toString(); + break; + case "tokens_out": + tokensOut = attr.value.toString(); + break; + } + } + const [pool, liquidity] = await checkGetPool(poolId, msg.block); + const swapRoute = SwapRoute.create({ + id: `${msg.tx.hash}-${msg.idx}-${eventIndex}`, + poolId: pool.id, + swapId: swap.id, + poolLiquidity: liquidity, + tokensIn: tokensIn, + tokensOut: tokensOut, + }); + await swapRoute.save(); + logger.info(msg.tx.hash); + eventIndex += 1; + } + } + } + } +} + export async function handleMsgSwapExactAmountIn( msg: messages.osmosis.gamm.v1beta1.tx.MsgSwapExactAmountInMessage ): Promise { logger.info( `Processing MsgSwapExactAmountIn at block ${msg.block.header.height.toString()}` ); - // We first create a new swap record const swap = createSwap(msg, Direction.IN, Message.MsgSwapExactAmountIn); swap.tokenInDenom = msg.msg.decodedMsg.tokenIn?.denom; swap.tokenInAmount = msg.msg.decodedMsg.tokenIn ? BigInt(msg.msg.decodedMsg.tokenIn.amount) : undefined; swap.tokenOutMin = BigInt(msg.msg.decodedMsg.tokenOutMinAmount); - - // Save this to the DB await swap.save(); - - // Create swap routes from the array on the message - let currentTokenInDenom = 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, liquidity] = await checkGetPool( - route.poolId.toString(), - msg.block - ); - - const swapRoute = SwapRoute.create({ - id: `${msg.tx.hash}-${msg.idx}-${index}`, - poolId: pool.id, - swapId: swap.id, - tokenInDenom: currentTokenInDenom, - tokenOutDenom: route.tokenOutDenom, - poolLiquidity: liquidity, - }); - currentTokenInDenom = route.tokenOutDenom; - await swapRoute.save(); - } + createSwapRoutes(msg, swap); } export async function handleMsgSwapExactAmountOut( @@ -155,38 +178,14 @@ export async function handleMsgSwapExactAmountOut( logger.info( `Processing MsgSwapExactAmountOut at block ${msg.block.header.height.toString()}` ); - // We first create a new swap record const swap = createSwap(msg, Direction.OUT, Message.MsgSwapExactAmountOut); swap.tokenOutDenom = msg.msg.decodedMsg.tokenOut.denom; swap.tokenOutAmount = msg.msg.decodedMsg.tokenOut ? BigInt(msg.msg.decodedMsg.tokenOut.amount) : undefined; swap.tokenInMax = BigInt(msg.msg.decodedMsg.tokenInMaxAmount); - - // Save this to the DB await swap.save(); - - // Create swap routes from the array on the message - let currentTokenOutDenom = swap.tokenOutDenom; - for (const route of msg.msg.decodedMsg.routes) { - const index = msg.msg.decodedMsg.routes.indexOf(route); - // Check that the pool aready exists - const [pool, liquidity] = await checkGetPool( - route.poolId.toString(), - msg.block - ); - - const swapRoute = SwapRoute.create({ - id: `${msg.tx.hash}-${msg.idx}-${index}`, - poolId: pool.id, - swapId: swap.id, - tokenInDenom: route.tokenInDenom, - tokenOutDenom: currentTokenOutDenom, - poolLiquidity: liquidity, - }); - currentTokenOutDenom = route.tokenInDenom; - await swapRoute.save(); - } + createSwapRoutes(msg, swap); } export async function handleMsgJoinSwapShareAmountOut( @@ -195,27 +194,10 @@ export async function handleMsgJoinSwapShareAmountOut( logger.info( `Processing MsgJoinSwapShareAmountOut at block ${msg.block.header.height.toString()}` ); - // We first create a new swap record const swap = createSwap(msg, Direction.IN, Message.MsgJoinSwapShareAmountOut); swap.tokenInDenom = msg.msg.decodedMsg.tokenInDenom; swap.tokenInMax = BigInt(msg.msg.decodedMsg.tokenInMaxAmount); swap.tokenOutAmount = BigInt(msg.msg.decodedMsg.shareOutAmount); - - // Save this to the DB await swap.save(); - - // Create swap routes from the array on the message - const [pool, liquidity] = await checkGetPool( - msg.msg.decodedMsg.poolId.toString(), - msg.block - ); - - const swapRoute = SwapRoute.create({ - id: `${msg.tx.hash}-${msg.idx}`, - poolId: pool.id, - swapId: swap.id, - tokenInDenom: msg.msg.decodedMsg.tokenInDenom, - poolLiquidity: liquidity, - }); - await swapRoute.save(); + createSwapRoutes(msg, swap); } From 0bfb9a345c0bb7a5e8e9bd9840c97fe5ead06b00 Mon Sep 17 00:00:00 2001 From: godlin Date: Fri, 19 Jan 2024 16:32:16 +0100 Subject: [PATCH 2/2] support of `./proto/osmosis/poolmanager/v1beta1` --- Osmosis/osmosis-dex-data/project.ts | 25 ++- .../osmosis/poolmanager/v1beta1/tx.proto | 152 ++++++++++++++++++ Osmosis/osmosis-dex-data/schema.graphql | 1 + .../src/mappings/mappingHandlers.ts | 9 +- 4 files changed, 183 insertions(+), 4 deletions(-) create mode 100644 Osmosis/osmosis-dex-data/proto/osmosis/poolmanager/v1beta1/tx.proto diff --git a/Osmosis/osmosis-dex-data/project.ts b/Osmosis/osmosis-dex-data/project.ts index c9f7ebf82..0a41690ca 100644 --- a/Osmosis/osmosis-dex-data/project.ts +++ b/Osmosis/osmosis-dex-data/project.ts @@ -38,7 +38,7 @@ const project: CosmosProject = { endpoint: ["https://rpc.osmosis.zone:443"], chaintypes: new Map([ [ - "osmosis.gamm.v1beta1", + "osmosis.gamm.v1beta1Swaps", { file: "./proto/osmosis/gamm/v1beta1/tx.proto", messages: [ @@ -48,6 +48,13 @@ const project: CosmosProject = { ], }, ], + [ + " osmosis.poolmanager.v1beta1Swaps", + { + file: "./proto/osmosis/poolmanager/v1beta1/tx.proto", + messages: ["MsgSwapExactAmountIn", "MsgSwapExactAmountOut"], + }, + ], [ " osmosis.poolmanager.v1beta1", { @@ -67,7 +74,7 @@ const project: CosmosProject = { dataSources: [ { kind: CosmosDatasourceKind.Runtime, - startBlock: 13338604, + startBlock: 13338625, mapping: { file: "./dist/index.js", handlers: [ @@ -78,6 +85,13 @@ const project: CosmosProject = { type: "/osmosis.gamm.v1beta1.MsgSwapExactAmountIn", }, }, + { + handler: "handleMsgSwapExactAmountIn", + kind: CosmosHandlerKind.Message, + filter: { + type: "/osmosis.poolmanager.v1beta1.MsgSwapExactAmountIn", + }, + }, { handler: "handleMsgSwapExactAmountOut", kind: CosmosHandlerKind.Message, @@ -85,6 +99,13 @@ const project: CosmosProject = { type: "/osmosis.gamm.v1beta1.MsgSwapExactAmountOut", }, }, + { + handler: "handleMsgSwapExactAmountOut", + kind: CosmosHandlerKind.Message, + filter: { + type: "/osmosis.poolmanager.v1beta1.MsgSwapExactAmountOut", + }, + }, { handler: "handleMsgJoinSwapShareAmountOut", kind: CosmosHandlerKind.Message, diff --git a/Osmosis/osmosis-dex-data/proto/osmosis/poolmanager/v1beta1/tx.proto b/Osmosis/osmosis-dex-data/proto/osmosis/poolmanager/v1beta1/tx.proto new file mode 100644 index 000000000..c6fbf5c95 --- /dev/null +++ b/Osmosis/osmosis-dex-data/proto/osmosis/poolmanager/v1beta1/tx.proto @@ -0,0 +1,152 @@ +syntax = "proto3"; +package osmosis.poolmanager.v1beta1; + +import "gogoproto/gogo.proto"; +import "amino/amino.proto"; +import "cosmos/base/v1beta1/coin.proto"; +import "osmosis/poolmanager/v1beta1/swap_route.proto"; + +option go_package = "github.com/osmosis-labs/osmosis/v22/x/poolmanager/types"; + +service Msg { + rpc SwapExactAmountIn(MsgSwapExactAmountIn) + returns (MsgSwapExactAmountInResponse); + rpc SwapExactAmountOut(MsgSwapExactAmountOut) + returns (MsgSwapExactAmountOutResponse); + rpc SplitRouteSwapExactAmountIn(MsgSplitRouteSwapExactAmountIn) + returns (MsgSplitRouteSwapExactAmountInResponse); + rpc SplitRouteSwapExactAmountOut(MsgSplitRouteSwapExactAmountOut) + returns (MsgSplitRouteSwapExactAmountOutResponse); + rpc SetDenomPairTakerFee(MsgSetDenomPairTakerFee) + returns (MsgSetDenomPairTakerFeeResponse); +} + +// ===================== MsgSwapExactAmountIn +message MsgSwapExactAmountIn { + option (amino.name) = "osmosis/poolmanager/swap-exact-amount-in"; + + string sender = 1 [ (gogoproto.moretags) = "yaml:\"sender\"" ]; + repeated SwapAmountInRoute routes = 2 [ (gogoproto.nullable) = false ]; + cosmos.base.v1beta1.Coin token_in = 3 [ + (gogoproto.moretags) = "yaml:\"token_in\"", + (gogoproto.nullable) = false + ]; + string token_out_min_amount = 4 [ + + (gogoproto.customtype) = "cosmossdk.io/math.Int", + (gogoproto.moretags) = "yaml:\"token_out_min_amount\"", + (gogoproto.nullable) = false + ]; +} + +message MsgSwapExactAmountInResponse { + string token_out_amount = 1 [ + + (gogoproto.customtype) = "cosmossdk.io/math.Int", + (gogoproto.moretags) = "yaml:\"token_out_amount\"", + (gogoproto.nullable) = false + ]; +} + +// ===================== MsgSplitRouteSwapExactAmountIn +message MsgSplitRouteSwapExactAmountIn { + option (amino.name) = "osmosis/poolmanager/split-amount-in"; + + string sender = 1 [ (gogoproto.moretags) = "yaml:\"sender\"" ]; + repeated SwapAmountInSplitRoute routes = 2 [ (gogoproto.nullable) = false ]; + string token_in_denom = 3 + [ (gogoproto.moretags) = "yaml:\"token_in_denom\"" ]; + string token_out_min_amount = 4 [ + + (gogoproto.customtype) = "cosmossdk.io/math.Int", + (gogoproto.moretags) = "yaml:\"token_out_min_amount\"", + (gogoproto.nullable) = false + ]; +} + +message MsgSplitRouteSwapExactAmountInResponse { + string token_out_amount = 1 [ + + (gogoproto.customtype) = "cosmossdk.io/math.Int", + (gogoproto.moretags) = "yaml:\"token_out_amount\"", + (gogoproto.nullable) = false + ]; +} + +// ===================== MsgSwapExactAmountOut +message MsgSwapExactAmountOut { + option (amino.name) = "osmosis/poolmanager/swap-exact-amount-out"; + + string sender = 1 [ (gogoproto.moretags) = "yaml:\"sender\"" ]; + repeated SwapAmountOutRoute routes = 2 [ (gogoproto.nullable) = false ]; + string token_in_max_amount = 3 [ + + (gogoproto.customtype) = "cosmossdk.io/math.Int", + (gogoproto.moretags) = "yaml:\"token_in_max_amount\"", + (gogoproto.nullable) = false + ]; + cosmos.base.v1beta1.Coin token_out = 4 [ + (gogoproto.moretags) = "yaml:\"token_out\"", + (gogoproto.nullable) = false + ]; +} + +message MsgSwapExactAmountOutResponse { + string token_in_amount = 1 [ + + (gogoproto.customtype) = "cosmossdk.io/math.Int", + (gogoproto.moretags) = "yaml:\"token_in_amount\"", + (gogoproto.nullable) = false + ]; +} + +// ===================== MsgSplitRouteSwapExactAmountOut +message MsgSplitRouteSwapExactAmountOut { + option (amino.name) = "osmosis/poolmanager/split-amount-out"; + + string sender = 1 [ (gogoproto.moretags) = "yaml:\"sender\"" ]; + repeated SwapAmountOutSplitRoute routes = 2 [ (gogoproto.nullable) = false ]; + string token_out_denom = 3 + [ (gogoproto.moretags) = "yaml:\"token_out_denom\"" ]; + string token_in_max_amount = 4 [ + + (gogoproto.customtype) = "cosmossdk.io/math.Int", + (gogoproto.moretags) = "yaml:\"token_in_max_amount\"", + (gogoproto.nullable) = false + ]; +} + +message MsgSplitRouteSwapExactAmountOutResponse { + string token_in_amount = 1 [ + + (gogoproto.customtype) = "cosmossdk.io/math.Int", + (gogoproto.moretags) = "yaml:\"token_in_amount\"", + (gogoproto.nullable) = false + ]; +} + +// ===================== MsgSetDenomPairTakerFee +message MsgSetDenomPairTakerFee { + option (amino.name) = "osmosis/poolmanager/set-denom-pair-taker-fee"; + + string sender = 1 [ (gogoproto.moretags) = "yaml:\"sender\"" ]; + repeated DenomPairTakerFee denom_pair_taker_fee = 2 [ + (gogoproto.moretags) = "yaml:\"denom_pair_taker_fee\"", + (gogoproto.nullable) = false + ]; +} + +message MsgSetDenomPairTakerFeeResponse { bool success = 1; } + +message DenomPairTakerFee { + // denom0 and denom1 get automatically lexigographically sorted + // when being stored, so the order of input here does not matter. + string denom0 = 1 [ (gogoproto.moretags) = "yaml:\"denom0\"" ]; + string denom1 = 2 [ (gogoproto.moretags) = "yaml:\"denom1\"" ]; + string taker_fee = 3 [ + + (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec", + (gogoproto.moretags) = "yaml:\"taker_fee\"", + (gogoproto.nullable) = false + ]; +} \ No newline at end of file diff --git a/Osmosis/osmosis-dex-data/schema.graphql b/Osmosis/osmosis-dex-data/schema.graphql index fe9471177..7619770eb 100644 --- a/Osmosis/osmosis-dex-data/schema.graphql +++ b/Osmosis/osmosis-dex-data/schema.graphql @@ -21,6 +21,7 @@ type Swap @entity { blockHeight: BigInt! date: Date! message: Message! + msgType: String! tokenInDenom: String tokenInAmount: BigInt tokenOutMin: BigInt diff --git a/Osmosis/osmosis-dex-data/src/mappings/mappingHandlers.ts b/Osmosis/osmosis-dex-data/src/mappings/mappingHandlers.ts index 42cc3ff8b..654089e8f 100644 --- a/Osmosis/osmosis-dex-data/src/mappings/mappingHandlers.ts +++ b/Osmosis/osmosis-dex-data/src/mappings/mappingHandlers.ts @@ -106,6 +106,7 @@ function createSwap( sender: msg.msg.decodedMsg.sender, direction, message, + msgType: msg.msg.typeUrl.toString(), date: new Date(msg.block.header.time.toISOString()), }); } @@ -157,7 +158,9 @@ async function createSwapRoutes(msg: CosmosMessage, swap: Swap) { } export async function handleMsgSwapExactAmountIn( - msg: messages.osmosis.gamm.v1beta1.tx.MsgSwapExactAmountInMessage + msg: + | messages.osmosis.gamm.v1beta1.tx.MsgSwapExactAmountInMessage + | messages.osmosis.poolmanager.v1beta1.tx.MsgSwapExactAmountInMessage ): Promise { logger.info( `Processing MsgSwapExactAmountIn at block ${msg.block.header.height.toString()}` @@ -173,7 +176,9 @@ export async function handleMsgSwapExactAmountIn( } export async function handleMsgSwapExactAmountOut( - msg: messages.osmosis.gamm.v1beta1.tx.MsgSwapExactAmountOutMessage + msg: + | messages.osmosis.gamm.v1beta1.tx.MsgSwapExactAmountOutMessage + | messages.osmosis.poolmanager.v1beta1.tx.MsgSwapExactAmountOutMessage ): Promise { logger.info( `Processing MsgSwapExactAmountOut at block ${msg.block.header.height.toString()}`