Skip to content

Commit

Permalink
update for nft position manager
Browse files Browse the repository at this point in the history
  • Loading branch information
ianlapham committed May 31, 2021
1 parent d38bea7 commit 6b223ca
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 87 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-alt --ipfs https://api.thegraph.com/ipfs/ --node https://api.thegraph.com/deploy/ --debug",
"deploy": "graph deploy ianlapham/uniswap-v3-prod --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
4 changes: 2 additions & 2 deletions src/mappings/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Bundle, Pool, Token, Factory, Mint, Burn, Swap, Tick } from '../types/schema'
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 } from '../utils'
import { convertTokenToDecimal, loadTransaction, safeDiv } from '../utils'
import { FACTORY_ADDRESS, ONE_BI, ZERO_BD, ZERO_BI } from '../utils/constants'
import { findEthPerToken, getEthPriceInUSD, getTrackedAmountUSD, sqrtPriceX96ToTokenPrices } from '../utils/pricing'
import {
Expand Down Expand Up @@ -302,7 +302,7 @@ export function handleSwap(event: SwapEvent): void {
let amountTotalUSDTracked = getTrackedAmountUSD(amount0Abs, token0 as Token, amount1Abs, token1 as Token).div(
BigDecimal.fromString('2')
)
let amountTotalETHTracked = amountTotalUSDTracked.div(bundle.ethPriceUSD)
let amountTotalETHTracked = safeDiv(amountTotalUSDTracked, bundle.ethPriceUSD)
let amountTotalUSDUntracked = amount0USD.plus(amount1USD).div(BigDecimal.fromString('2'))

let feesETH = amountTotalETHTracked.times(pool.feeTier.toBigDecimal()).div(BigDecimal.fromString('1000000'))
Expand Down
68 changes: 26 additions & 42 deletions src/mappings/position-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ import {
Collect,
DecreaseLiquidity,
IncreaseLiquidity,
NonfungiblePositionManager, Transfer
NonfungiblePositionManager,
Transfer
} from '../types/NonfungiblePositionManager/NonfungiblePositionManager'
import { Position, Token } from '../types/schema'
import {
ADDRESS_ZERO,
factoryContract,
ZERO_BD,
ZERO_BI
} from '../utils/constants'
import { ADDRESS_ZERO, factoryContract, ZERO_BD, ZERO_BI } from '../utils/constants'
import { Address, BigInt, ethereum, log } from '@graphprotocol/graph-ts'
import { convertTokenToDecimal, loadTransaction } from '../utils'
import { fetchTokenDecimals } from '../utils/token'
Expand All @@ -21,11 +17,7 @@ function getPosition(event: ethereum.Event, tokenId: BigInt): Position {
let contract = NonfungiblePositionManager.bind(event.address)
let positionResult = contract.positions(tokenId)

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

position = new Position(tokenId.toString())
// The owner gets correctly updated in the Transfer handler
Expand All @@ -49,33 +41,27 @@ function getPosition(event: ethereum.Event, tokenId: BigInt): 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)
// 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()
}

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)
// 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 {
Expand All @@ -84,15 +70,13 @@ export function handleCollect(event: Collect): void {
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()
}

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.owner = event.params.to
// position.save()
}
13 changes: 11 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ export function exponentToBigDecimal(decimals: BigInt): BigDecimal {
return bd
}

// return 0 if denominator is 0 in division
export function safeDiv(amount0: BigDecimal, amount1: BigDecimal): BigDecimal {
if (amount1.equals(ZERO_BD)) {
return ZERO_BD
} else {
return amount0.div(amount1)
}
}

export function bigDecimalExponated(value: BigDecimal, power: BigInt): BigDecimal {
if (power.equals(ZERO_BI)) {
return ONE_BD
Expand All @@ -23,7 +32,7 @@ export function bigDecimalExponated(value: BigDecimal, power: BigInt): BigDecima
}

if (negativePower) {
result = ONE_BD.div(result)
result = safeDiv(ONE_BD, result)
}

return result
Expand All @@ -40,7 +49,7 @@ export function priceToDecimal(amount: BigDecimal, exchangeDecimals: BigInt): Bi
if (exchangeDecimals == ZERO_BI) {
return amount
}
return amount.div(exponentToBigDecimal(exchangeDecimals))
return safeDiv(amount, exponentToBigDecimal(exchangeDecimals))
}

export function equalToZero(value: BigDecimal): boolean {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { ONE_BD, ZERO_BD, ZERO_BI } from './constants'
import { Bundle, Pool, Token } from './../types/schema'
import { BigDecimal, BigInt } from '@graphprotocol/graph-ts'
import { exponentToBigDecimal } from '../utils/index'
import { exponentToBigDecimal, safeDiv } from '../utils/index'

const WETH_ADDRESS = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'
const USDC_WETH_03_POOL = '0x8ad599c3a0ff1de082011efddc58f1908eb6e6d8'
Expand Down Expand Up @@ -44,7 +44,7 @@ export function sqrtPriceX96ToTokenPrices(sqrtPriceX96: BigInt, token0: Token, t
.times(exponentToBigDecimal(token0.decimals))
.div(exponentToBigDecimal(token1.decimals))

let price0 = BigDecimal.fromString('1').div(price1)
let price0 = safeDiv(BigDecimal.fromString('1'), price1)
return [price0, price1]
}

Expand Down
77 changes: 39 additions & 38 deletions src/utils/tick.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,53 @@
import { BigDecimal, BigInt } from '@graphprotocol/graph-ts';
import { bigDecimalExponated } from '.';
/* eslint-disable prefer-const */
import { BigDecimal, BigInt } from '@graphprotocol/graph-ts'
import { bigDecimalExponated, safeDiv } from '.'
import { Tick } from '../types/schema'
import { Mint as MintEvent } from '../types/templates/Pool/Pool'
import { ONE_BD, ZERO_BD, ZERO_BI } from './constants';
import { ONE_BD, ZERO_BD, ZERO_BI } from './constants'

export function createTick(tickId: string, tickIdx: i32, poolId: string, event: MintEvent): Tick {
let tick = new Tick(tickId);
tick.tickIdx = BigInt.fromI32(tickIdx)
tick.pool = poolId;
tick.poolAddress = poolId;

tick.createdAtTimestamp = event.block.timestamp
tick.createdAtBlockNumber = event.block.number
tick.liquidityGross = ZERO_BI
tick.liquidityNet = ZERO_BI
tick.liquidityProviderCount = ZERO_BI

tick.price0 = ONE_BD
tick.price1 = ONE_BD

// 1.0001^tick is token1/token0.
let price0 = bigDecimalExponated(BigDecimal.fromString('1.0001'), BigInt.fromI32(tickIdx))
tick.price0 = price0
tick.price1 = ONE_BD.div(price0)

tick.volumeToken0 = ZERO_BD
tick.volumeToken1 = ZERO_BD
tick.volumeUSD = ZERO_BD
tick.feesUSD = ZERO_BD
tick.untrackedVolumeUSD = ZERO_BD
tick.collectedFeesToken0 = ZERO_BD
tick.collectedFeesToken1 = ZERO_BD
tick.collectedFeesUSD = ZERO_BD
tick.liquidityProviderCount = ZERO_BI

return tick;
let tick = new Tick(tickId)
tick.tickIdx = BigInt.fromI32(tickIdx)
tick.pool = poolId
tick.poolAddress = poolId

tick.createdAtTimestamp = event.block.timestamp
tick.createdAtBlockNumber = event.block.number
tick.liquidityGross = ZERO_BI
tick.liquidityNet = ZERO_BI
tick.liquidityProviderCount = ZERO_BI

tick.price0 = ONE_BD
tick.price1 = ONE_BD

// 1.0001^tick is token1/token0.
let price0 = bigDecimalExponated(BigDecimal.fromString('1.0001'), BigInt.fromI32(tickIdx))
tick.price0 = price0
tick.price1 = safeDiv(ONE_BD, price0)

tick.volumeToken0 = ZERO_BD
tick.volumeToken1 = ZERO_BD
tick.volumeUSD = ZERO_BD
tick.feesUSD = ZERO_BD
tick.untrackedVolumeUSD = ZERO_BD
tick.collectedFeesToken0 = ZERO_BD
tick.collectedFeesToken1 = ZERO_BD
tick.collectedFeesUSD = ZERO_BD
tick.liquidityProviderCount = ZERO_BI

return tick
}

export function feeTierToTickSpacing(feeTier: BigInt): BigInt {
if (feeTier.equals(BigInt.fromI32(10000))) {
return BigInt.fromI32(200)
return BigInt.fromI32(200)
}
if (feeTier.equals(BigInt.fromI32(3000))) {
return BigInt.fromI32(60)
return BigInt.fromI32(60)
}
if (feeTier.equals(BigInt.fromI32(500))) {
return BigInt.fromI32(10)
return BigInt.fromI32(10)
}

throw Error('Unexpected fee tier');
}
throw Error('Unexpected fee tier')
}

0 comments on commit 6b223ca

Please sign in to comment.