-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Featured Osmosis Enhanced Starter (#57)
* Featured Osmosis Enhanced Starter * Update example Osmosis project --------- Co-authored-by: James Bayly <[email protected]>
- Loading branch information
1 parent
518eb48
commit 8ef5fc2
Showing
5 changed files
with
157 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 111 additions & 23 deletions
134
Osmosis/osmosis-starter/src/mappings/mappingHandlers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,142 @@ | ||
import { MsgSwapExactAmountInMessage } from "../types/CosmosMessageTypes"; | ||
import { Pool, Swap, SwapRoute } from "../types"; | ||
import { | ||
MsgSwapExactAmountInMessage, | ||
MsgSwapExactAmountOutMessage, | ||
MsgJoinSwapShareAmountOutMessage, | ||
} from "../types/CosmosMessageTypes"; | ||
import { Pool, Swap, SwapRoute, Direction, Message } from "../types"; | ||
import { CosmosBlock } from "@subql/types-cosmos"; | ||
|
||
async function checkGetPool(id: string): Promise<Pool> { | ||
async function checkGetPool(id: string, block: CosmosBlock): 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); | ||
pool = Pool.create({ | ||
id, | ||
createdBlockHeight: BigInt(block.header.height), | ||
created: new Date(block.header.time.toISOString()), | ||
}); | ||
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(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({ | ||
function createSwap( | ||
msg: | ||
| MsgSwapExactAmountInMessage | ||
| MsgSwapExactAmountOutMessage | ||
| MsgJoinSwapShareAmountOutMessage, | ||
direction: Direction, | ||
message: Message | ||
): Swap { | ||
return Swap.create({ | ||
id: `${msg.tx.hash}-${msg.idx}`, | ||
txHash: msg.tx.hash, | ||
blockHeight: BigInt(msg.block.block.header.height), | ||
blockHeight: BigInt(msg.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), | ||
direction, | ||
message, | ||
date: new Date(msg.block.header.time.toISOString()), | ||
}); | ||
} | ||
|
||
export async function handleMsgSwapExactAmountIn( | ||
msg: MsgSwapExactAmountInMessage | ||
): Promise<void> { | ||
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 lastTokenOutDenom = swap.tokenInDenom; | ||
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 = await checkGetPool(route.poolId.toString()); | ||
const pool = 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: lastTokenOutDenom, | ||
tokenInDenom: currentTokenInDenom, | ||
tokenOutDenom: route.tokenOutDenom, | ||
}); | ||
lastTokenOutDenom = route.tokenOutDenom; | ||
currentTokenInDenom = route.tokenOutDenom; | ||
await swapRoute.save(); | ||
} | ||
} | ||
|
||
export async function handleMsgSwapExactAmountOut( | ||
msg: MsgSwapExactAmountOutMessage | ||
): Promise<void> { | ||
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 = 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, | ||
}); | ||
currentTokenOutDenom = route.tokenInDenom; | ||
await swapRoute.save(); | ||
} | ||
} | ||
|
||
export async function handleMsgJoinSwapShareAmountOut( | ||
msg: MsgJoinSwapShareAmountOutMessage | ||
): Promise<void> { | ||
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 = 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, | ||
}); | ||
await swapRoute.save(); | ||
} |