Skip to content

Commit

Permalink
fix for token id, fix for null return on positions cal
Browse files Browse the repository at this point in the history
  • Loading branch information
ianlapham committed Jun 10, 2021
2 parents 6b223ca + afbfe5a commit ce95e9a
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 47 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"build": "graph build",
"create-local": "graph create ianlapham/uniswap-v3 --node http://127.0.0.1:8020",
"deploy-local": "graph deploy ianlapham/uniswap-v3 --debug --ipfs http://localhost:5001 --node http://127.0.0.1:8020",
"deploy": "graph deploy ianlapham/uniswap-v3-prod --ipfs https://api.thegraph.com/ipfs/ --node https://api.thegraph.com/deploy/ --debug",
"deploy": "graph deploy ianlapham/uniswap-v3-alt --ipfs https://api.thegraph.com/ipfs/ --node https://api.thegraph.com/deploy/ --debug",
"deploy-dev": "graph deploy sommelier/uniswap-v3 --ipfs http://35.197.14.14:5000/ --node http://35.197.14.14:8020/ --debug",
"deploy-staging": "graph deploy $THE_GRAPH_GITHUB_USER/$THE_GRAPH_SUBGRAPH_NAME /Uniswap --ipfs https://api.staging.thegraph.com/ipfs/ --node https://api.staging.thegraph.com/deploy/",
"watch-local": "graph deploy ianlapham/uniswap-v3 --watch --debug --node http://127.0.0.1:8020/ --ipfs http://localhost:5001"
Expand Down
12 changes: 12 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ type Pool @entity {
liquidity: BigInt!
# current price tracker
sqrtPrice: BigInt!
# tracker for global fee growth
feeGrowthGlobal0X128: BigInt!
# tracker for global fee growth
feeGrowthGlobal1X128: BigInt!
# token0 per token1
token0Price: BigDecimal!
# token1 per token0
Expand Down Expand Up @@ -425,6 +429,10 @@ type PoolDayData @entity {
token1Price: BigDecimal!
# current tick at end of period
tick: BigInt
# tracker for global fee growth
feeGrowthGlobal0X128: BigInt!
# tracker for global fee growth
feeGrowthGlobal1X128: BigInt!
# tvl derived in USD at end of period
tvlUSD: BigDecimal!
# volume in token0
Expand Down Expand Up @@ -465,6 +473,10 @@ type PoolHourData @entity {
token1Price: BigDecimal!
# current tick at end of period
tick: BigInt
# tracker for global fee growth
feeGrowthGlobal0X128: BigInt!
# tracker for global fee growth
feeGrowthGlobal1X128: BigInt!
# tvl derived in USD at end of period
tvlUSD: BigDecimal!
# volume in token0
Expand Down
8 changes: 8 additions & 0 deletions src/mappings/core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable prefer-const */
import { Bundle, Pool, Token, Factory, Mint, Burn, Swap, Tick } from '../types/schema'
import { Pool as PoolABI } from '../types/Factory/Pool'
import { BigDecimal, BigInt, store } from '@graphprotocol/graph-ts'
import { Mint as MintEvent, Burn as BurnEvent, Swap as SwapEvent, Initialize } from '../types/templates/Pool/Pool'
import { convertTokenToDecimal, loadTransaction, safeDiv } from '../utils'
Expand Down Expand Up @@ -395,6 +396,13 @@ export function handleSwap(event: SwapEvent): void {
swap.sqrtPriceX96 = event.params.sqrtPriceX96
swap.logIndex = event.logIndex

// update fee growth
let poolContract = PoolABI.bind(event.address)
let feeGrowthGlobal0X128 = poolContract.feeGrowthGlobal0X128()
let feeGrowthGlobal1X128 = poolContract.feeGrowthGlobal1X128()
pool.feeGrowthGlobal0X128 = feeGrowthGlobal0X128 as BigInt
pool.feeGrowthGlobal1X128 = feeGrowthGlobal1X128 as BigInt

// interval data
let uniswapDayData = updateUniswapDayData(event)
let poolDayData = updatePoolDayData(event)
Expand Down
2 changes: 2 additions & 0 deletions src/mappings/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ export function handlePoolCreated(event: PoolCreated): void {
pool.txCount = ZERO_BI
pool.liquidity = ZERO_BI
pool.sqrtPrice = ZERO_BI
pool.feeGrowthGlobal0X128 = ZERO_BI
pool.feeGrowthGlobal1X128 = ZERO_BI
pool.token0Price = ZERO_BD
pool.token1Price = ZERO_BD
pool.observationIndex = ZERO_BI
Expand Down
135 changes: 91 additions & 44 deletions src/mappings/position-manager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable prefer-const */
import {
Collect,
DecreaseLiquidity,
Expand All @@ -7,65 +8,105 @@ import {
} from '../types/NonfungiblePositionManager/NonfungiblePositionManager'
import { Position, Token } from '../types/schema'
import { ADDRESS_ZERO, factoryContract, ZERO_BD, ZERO_BI } from '../utils/constants'
import { Address, BigInt, ethereum, log } from '@graphprotocol/graph-ts'
import { Address, BigInt, ethereum } from '@graphprotocol/graph-ts'
import { convertTokenToDecimal, loadTransaction } from '../utils'
import { fetchTokenDecimals } from '../utils/token'

function getPosition(event: ethereum.Event, tokenId: BigInt): Position {
function getPosition(event: ethereum.Event, tokenId: BigInt): Position | null {
let position = Position.load(tokenId.toString())
if (position === null) {
let contract = NonfungiblePositionManager.bind(event.address)
let positionResult = contract.positions(tokenId)

let poolAddress = factoryContract.getPool(positionResult.value2, positionResult.value3, positionResult.value4)

position = new Position(tokenId.toString())
// The owner gets correctly updated in the Transfer handler
position.owner = Address.fromString(ADDRESS_ZERO)
position.pool = poolAddress.toHexString()
position.token0 = positionResult.value2.toHexString()
position.token1 = positionResult.value3.toHexString()
position.tickLower = BigInt.fromI32(positionResult.value5)
position.tickUpper = BigInt.fromI32(positionResult.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
let positionCall = contract.try_positions(tokenId)

if (!positionCall.reverted) {
let positionResult = positionCall.value
let poolAddress = factoryContract.getPool(positionResult.value2, positionResult.value3, positionResult.value4)

position = new Position(tokenId.toString())
// The owner gets correctly updated in the Transfer handler
position.owner = Address.fromString(ADDRESS_ZERO)
position.pool = poolAddress.toHexString()
position.token0 = positionResult.value2.toHexString()
position.token1 = positionResult.value3.toHexString()
position.tickLower = BigInt.fromI32(positionResult.value5)
position.tickUpper = BigInt.fromI32(positionResult.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 = getPosition(event, event.params.tokenId)
// let decimals0 = fetchTokenDecimals(Address.fromString(position.token0))
// let decimals1 = fetchTokenDecimals(Address.fromString(position.token1))
// let amount0 = convertTokenToDecimal(event.params.amount0, decimals0)
// let amount1 = convertTokenToDecimal(event.params.amount1, decimals1)
// position.liquidity = position.liquidity.plus(event.params.liquidity)
// position.depositedToken0 = position.depositedToken0.plus(amount0)
// position.depositedToken1 = position.depositedToken1.plus(amount1)
// position.save()
let position = getPosition(event, event.params.tokenId)

// position was not able to be fetched
if (position == null) {
return
}

// temp fix
if (Address.fromString(position.pool).equals(Address.fromHexString('0x8fe8d9bb8eeba3ed688069c3d6b556c9ca258248'))) {
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 = getPosition(event, event.params.tokenId)
// 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()
let position = getPosition(event, event.params.tokenId)

// position was not able to be fetched
if (position == null) {
return
}

// temp fix
if (Address.fromString(position.pool).equals(Address.fromHexString('0x8fe8d9bb8eeba3ed688069c3d6b556c9ca258248'))) {
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.minus(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 = getPosition(event, event.params.tokenId)

// position was not able to be fetched
if (position == null) {
return
}

// temp fix
if (Address.fromString(position.pool).equals(Address.fromHexString('0x8fe8d9bb8eeba3ed688069c3d6b556c9ca258248'))) {
return
}

let token0 = Token.load(position.token0)
let token1 = Token.load(position.token1)
let amount0 = convertTokenToDecimal(event.params.amount0, token0.decimals)
Expand All @@ -76,7 +117,13 @@ export function handleCollect(event: Collect): void {
}

export function handleTransfer(event: Transfer): void {
// let position = getPosition(event, event.params.tokenId)
// position.owner = event.params.to
// position.save()
let position = getPosition(event, event.params.tokenId)

// position was not able to be fetched
if (position == null) {
return
}

position.owner = event.params.to
position.save()
}
12 changes: 10 additions & 2 deletions src/utils/intervalUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export function updatePoolDayData(event: ethereum.Event): PoolDayData {
poolDayData.volumeUSD = ZERO_BD
poolDayData.feesUSD = ZERO_BD
poolDayData.txCount = ZERO_BI
poolDayData.feeGrowthGlobal0X128 = ZERO_BI
poolDayData.feeGrowthGlobal1X128 = ZERO_BI
poolDayData.open = pool.token0Price
poolDayData.high = pool.token0Price
poolDayData.low = pool.token0Price
Expand All @@ -73,6 +75,8 @@ export function updatePoolDayData(event: ethereum.Event): PoolDayData {

poolDayData.liquidity = pool.liquidity
poolDayData.sqrtPrice = pool.sqrtPrice
poolDayData.feeGrowthGlobal0X128 = pool.feeGrowthGlobal0X128
poolDayData.feeGrowthGlobal1X128 = pool.feeGrowthGlobal1X128
poolDayData.token0Price = pool.token0Price
poolDayData.token1Price = pool.token1Price
poolDayData.tick = pool.tick
Expand Down Expand Up @@ -103,6 +107,8 @@ export function updatePoolHourData(event: ethereum.Event): PoolHourData {
poolHourData.volumeUSD = ZERO_BD
poolHourData.txCount = ZERO_BI
poolHourData.feesUSD = ZERO_BD
poolHourData.feeGrowthGlobal0X128 = ZERO_BI
poolHourData.feeGrowthGlobal1X128 = ZERO_BI
poolHourData.open = pool.token0Price
poolHourData.high = pool.token0Price
poolHourData.low = pool.token0Price
Expand All @@ -120,6 +126,8 @@ export function updatePoolHourData(event: ethereum.Event): PoolHourData {
poolHourData.sqrtPrice = pool.sqrtPrice
poolHourData.token0Price = pool.token0Price
poolHourData.token1Price = pool.token1Price
poolHourData.feeGrowthGlobal0X128 = pool.feeGrowthGlobal0X128
poolHourData.feeGrowthGlobal1X128 = pool.feeGrowthGlobal1X128
poolHourData.close = pool.token0Price
poolHourData.tick = pool.tick
poolHourData.tvlUSD = pool.totalValueLockedUSD
Expand Down Expand Up @@ -178,8 +186,8 @@ export function updateTokenHourData(token: Token, event: ethereum.Event): TokenH
let timestamp = event.block.timestamp.toI32()
let hourIndex = timestamp / 3600 // get unique hour within unix history
let hourStartUnix = hourIndex * 3600 // want the rounded effect
let tokenHourID = event.address
.toHexString()
let tokenHourID = token.id
.toString()
.concat('-')
.concat(hourIndex.toString())
let tokenHourData = TokenHourData.load(tokenHourID)
Expand Down
2 changes: 2 additions & 0 deletions subgraph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ dataSources:
file: ./abis/ERC20SymbolBytes.json
- name: ERC20NameBytes
file: ./abis/ERC20NameBytes.json
- name: Pool
file: ./abis/pool.json
eventHandlers:
- event: PoolCreated(indexed address,indexed address,indexed uint24,int24,address)
handler: handlePoolCreated
Expand Down

0 comments on commit ce95e9a

Please sign in to comment.