From 569c557e96bd064eb8ca17b8872f520225529e97 Mon Sep 17 00:00:00 2001 From: Chef Huan Date: Thu, 1 Jun 2023 12:16:34 +0700 Subject: [PATCH] fix: Positions update in position manager --- subgraphs/user-position-v3/template/mappings/pool.ts | 8 ++++++-- .../template/mappings/position-manager.ts | 4 +++- subgraphs/user-position-v3/template/schema.graphql | 3 ++- subgraphs/user-position-v3/template/utils/schema.ts | 4 ++-- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/subgraphs/user-position-v3/template/mappings/pool.ts b/subgraphs/user-position-v3/template/mappings/pool.ts index 4995ec2d..3bded71e 100644 --- a/subgraphs/user-position-v3/template/mappings/pool.ts +++ b/subgraphs/user-position-v3/template/mappings/pool.ts @@ -39,9 +39,11 @@ export function handleMint(event: MintEvent): void { transaction.tickLower = BigInt.fromI32(event.params.tickLower); transaction.tickUpper = BigInt.fromI32(event.params.tickUpper); transaction.increaseLiquidityAmount = event.params.amount; + transaction.pool = pool.id; + transaction.save(); - updateUserPosition(event, transaction, pool.id); + updateUserPosition(event, transaction); } export function handleBurn(event: BurnEvent): void { @@ -76,7 +78,9 @@ export function handleBurn(event: BurnEvent): void { transaction.tickLower = BigInt.fromI32(event.params.tickLower); transaction.tickUpper = BigInt.fromI32(event.params.tickUpper); transaction.decreaseLiquidityAmount = event.params.amount; + transaction.pool = pool.id; + transaction.save(); - updateUserPosition(event, transaction, pool.id); + updateUserPosition(event, transaction); } diff --git a/subgraphs/user-position-v3/template/mappings/position-manager.ts b/subgraphs/user-position-v3/template/mappings/position-manager.ts index a53cc130..ceaccf02 100644 --- a/subgraphs/user-position-v3/template/mappings/position-manager.ts +++ b/subgraphs/user-position-v3/template/mappings/position-manager.ts @@ -5,7 +5,7 @@ import { IncreaseLiquidity, Transfer, } from "../generated/NonfungiblePositionManager/NonfungiblePositionManager"; -import { loadTransaction } from "../utils/schema"; +import { loadTransaction, updateUserPosition } from "../utils/schema"; import { ADDRESS_ZERO } from "../utils/constants"; import { UserPosition } from "../generated/schema"; @@ -13,12 +13,14 @@ export function handleIncreaseLiquidity(event: IncreaseLiquidity): void { let transaction = loadTransaction(event); transaction.tokenId = event.params.tokenId; transaction.save(); + updateUserPosition(event, transaction); } export function handleDecreaseLiquidity(event: DecreaseLiquidity): void { let transaction = loadTransaction(event); transaction.tokenId = event.params.tokenId; transaction.save(); + updateUserPosition(event, transaction); } export function handleTransfer(event: Transfer): void { diff --git a/subgraphs/user-position-v3/template/schema.graphql b/subgraphs/user-position-v3/template/schema.graphql index 3aaf13d5..ab2d3cfe 100644 --- a/subgraphs/user-position-v3/template/schema.graphql +++ b/subgraphs/user-position-v3/template/schema.graphql @@ -30,6 +30,7 @@ type Pool @entity { type Transaction @entity { # txn hash id: ID! + pool: Pool # block txn was included in blockNumber: BigInt! # timestamp txn was confirmed @@ -122,7 +123,7 @@ type UserPosition @entity { id: ID! owner: Bytes! # pool position is within - pool: Pool! + pool: Pool liquidity: BigInt! tickLower: BigInt! diff --git a/subgraphs/user-position-v3/template/utils/schema.ts b/subgraphs/user-position-v3/template/utils/schema.ts index 4e256640..a974264a 100644 --- a/subgraphs/user-position-v3/template/utils/schema.ts +++ b/subgraphs/user-position-v3/template/utils/schema.ts @@ -16,7 +16,7 @@ export function loadTransaction(event: ethereum.Event): Transaction { return transaction as Transaction; } -export function updateUserPosition(event: ethereum.Event, tx: Transaction, poolId: string): void { +export function updateUserPosition(event: ethereum.Event, tx: Transaction): void { if ( tx.isPositionUpdated === false && tx.tickLower !== null && @@ -29,7 +29,7 @@ export function updateUserPosition(event: ethereum.Event, tx: Transaction, poolI if (userPosition === null) { userPosition = new UserPosition(tx.tokenId.toString()); userPosition.liquidity = ZERO_BI; - userPosition.pool = poolId; + userPosition.pool = tx.pool; userPosition.createdAtBlockNumber = event.block.number; userPosition.createdAtTimestamp = event.block.timestamp; }