From b2ff545e7f667862c33bf3d75ce8d3e34bfbbfea Mon Sep 17 00:00:00 2001 From: benesjan Date: Sun, 23 May 2021 15:06:44 +0200 Subject: [PATCH] feat: NonfungiblePositionManager handlers --- schema.graphql | 34 +++++++++++++ src/mappings/position-manager.ts | 87 +++++++++++++++++++++++++++++++- subgraph.yaml | 6 ++- 3 files changed, 123 insertions(+), 4 deletions(-) diff --git a/schema.graphql b/schema.graphql index c528e0e6..dfac06db 100644 --- a/schema.graphql +++ b/schema.graphql @@ -173,6 +173,40 @@ type Tick @entity { # swaps: [Swap!]! @derivedFrom(field: "tick") } +type Position @entity { + # Positions created through NonfungiblePositionManager + # NFT token id + id: ID! + # the address that is approved for spending this token + operator: Bytes! + # pool position is within + pool: Pool! + # allow indexing by tokens + token0: Token! + # allow indexing by tokens + token1: Token! + # lower tick of the position + tickLower: BigInt! + # upper tick of the position + tickUpper: BigInt! + # total position liquidity + liquidity: BigInt! + # amount of token 0 ever deposited to position + depositedToken0: BigDecimal! + # amount of token 1 ever deposited to position + depositedToken1: BigDecimal! + # amount of token 0 ever withdrawn from position (without fees) + withdrawnToken0: BigDecimal! + # amount of token 1 ever withdrawn from position (without fees) + withdrawnToken1: BigDecimal! + # all time collected fees in token0 + collectedFeesToken0: BigDecimal! + # all time collected fees in token1 + collectedFeesToken1: BigDecimal! + # tx in which the position was initialized + transaction: Transaction! +} + type Transaction @entity { # txn hash id: ID! diff --git a/src/mappings/position-manager.ts b/src/mappings/position-manager.ts index 21459240..0c0dd0e8 100644 --- a/src/mappings/position-manager.ts +++ b/src/mappings/position-manager.ts @@ -1,10 +1,93 @@ -import { IncreaseLiquidity, DecreaseLiquidity, Collect } from '../types/NonfungiblePositionManager/NonfungiblePositionManager' +import { + Collect, + DecreaseLiquidity, + IncreaseLiquidity, + NonfungiblePositionManager +} from '../types/NonfungiblePositionManager/NonfungiblePositionManager' +import { Position, Token } from '../types/schema' +import { factoryContract, ZERO_BD, ZERO_BI } from '../utils/constants' +import { BigInt, ethereum, log } from '@graphprotocol/graph-ts' +import { convertTokenToDecimal, loadTransaction } from '../utils' + +function initializePosition(event: ethereum.Event, tokenId: BigInt): Position | null { + let contract = NonfungiblePositionManager.bind(event.address) + let positionResult = contract.try_positions(tokenId) + if (positionResult.reverted) { + log.error('fetching position failed', []) + return null + } + + let poolResult = factoryContract.try_getPool( + positionResult.value.value2, + positionResult.value.value3, + positionResult.value.value4 + ) + if (poolResult.reverted) { + log.error('fetching pool failed', []) + return null + } + + let position = new Position(tokenId.toString()) + position.operator = positionResult.value.value1 + position.pool = poolResult.value.toHexString() + position.token0 = positionResult.value.value2.toHexString() + position.token1 = positionResult.value.value3.toHexString() + position.tickLower = BigInt.fromI32(positionResult.value.value5) + position.tickUpper = BigInt.fromI32(positionResult.value.value6) + position.liquidity = ZERO_BI + position.depositedToken0 = ZERO_BD + position.depositedToken1 = ZERO_BD + position.withdrawnToken0 = ZERO_BD + position.withdrawnToken1 = ZERO_BD + position.collectedFeesToken0 = ZERO_BD + position.collectedFeesToken1 = ZERO_BD + position.transaction = loadTransaction(event).id + + return position +} export function handleIncreaseLiquidity(event: IncreaseLiquidity): void { + let position = Position.load(event.params.tokenId.toString()) + if (position === null) { + position = initializePosition(event, event.params.tokenId) + if (position === null) return + } + + let token0 = Token.load(position.token0) + let token1 = Token.load(position.token1) + let amount0 = convertTokenToDecimal(event.params.amount0, token0.decimals) + let amount1 = convertTokenToDecimal(event.params.amount1, token1.decimals) + + position.liquidity = position.liquidity.plus(event.params.liquidity) + position.depositedToken0 = position.depositedToken0.plus(amount0) + position.depositedToken1 = position.depositedToken1.plus(amount1) + + position.save() } export function handleDecreaseLiquidity(event: DecreaseLiquidity): void { + let position = Position.load(event.params.tokenId.toString()) + let token0 = Token.load(position.token0) + let token1 = Token.load(position.token1) + let amount0 = convertTokenToDecimal(event.params.amount0, token0.decimals) + let amount1 = convertTokenToDecimal(event.params.amount1, token1.decimals) + + position.liquidity = position.liquidity.plus(event.params.liquidity) + position.withdrawnToken0 = position.withdrawnToken0.plus(amount0) + position.withdrawnToken1 = position.withdrawnToken1.plus(amount1) + + position.save() } export function handleCollect(event: Collect): void { -} \ No newline at end of file + let position = Position.load(event.params.tokenId.toString()) + let token0 = Token.load(position.token0) + let token1 = Token.load(position.token1) + let amount0 = convertTokenToDecimal(event.params.amount0, token0.decimals) + let amount1 = convertTokenToDecimal(event.params.amount1, token1.decimals) + + position.collectedFeesToken0 = position.collectedFeesToken0.plus(amount0) + position.collectedFeesToken1 = position.collectedFeesToken1.plus(amount1) + + position.save() +} diff --git a/subgraph.yaml b/subgraph.yaml index 2667b7a2..0e04c72e 100644 --- a/subgraph.yaml +++ b/subgraph.yaml @@ -38,9 +38,9 @@ dataSources: name: NonfungiblePositionManager network: mainnet source: - address: '0xC62B9b27207ac749f72d65A98FF177cF5eF54099' + address: '0xC36442b4a4522E871399CD717aBDD847Ab11FE88' abi: NonfungiblePositionManager - startBlock: 12369621 + startBlock: 12369651 mapping: kind: ethereum/events apiVersion: 0.0.4 @@ -54,6 +54,8 @@ dataSources: file: ./abis/NonfungiblePositionManager.json - name: Pool file: ./abis/pool.json + - name: Factory + file: ./abis/factory.json eventHandlers: - event: IncreaseLiquidity(indexed uint256,uint128,uint256,uint256) handler: handleIncreaseLiquidity