Skip to content

Commit

Permalink
feat: NonfungiblePositionManager handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed May 24, 2021
1 parent 3286d8c commit b2ff545
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 4 deletions.
34 changes: 34 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
87 changes: 85 additions & 2 deletions src/mappings/position-manager.ts
Original file line number Diff line number Diff line change
@@ -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 {
}
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()
}
6 changes: 4 additions & 2 deletions subgraph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit b2ff545

Please sign in to comment.