Skip to content

Commit

Permalink
pricing hot fix for stablecoins
Browse files Browse the repository at this point in the history
  • Loading branch information
ianlapham committed Oct 28, 2021
1 parent 5c08092 commit ff07a11
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 50 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-subgraph --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: 0 additions & 4 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,6 @@ type Position @entity {
withdrawnToken0: BigDecimal!
# amount of token 1 ever withdrawn from position (without fees)
withdrawnToken1: BigDecimal!
# all time collected token0 (withdrawnToken0 + collectedFeesToken0)
collectedToken0: BigDecimal!
# all time collected token1 (withdrawnToken1 + collectedFeesToken1)
collectedToken1: BigDecimal!
# all time collected fees in token0
collectedFeesToken0: BigDecimal!
# all time collected fees in token1
Expand Down
2 changes: 1 addition & 1 deletion src/mappings/core.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable prefer-const */
import { Bundle, Burn, Factory, Mint, Pool, Swap, Tick, Token } from '../types/schema'
import { Pool as PoolABI } from '../types/Factory/Pool'
import { BigDecimal, BigInt, ethereum, store } from '@graphprotocol/graph-ts'
import { BigDecimal, BigInt, ethereum } from '@graphprotocol/graph-ts'
import {
Burn as BurnEvent,
Flash as FlashEvent,
Expand Down
20 changes: 0 additions & 20 deletions src/mappings/position-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ function getPosition(event: ethereum.Event, tokenId: BigInt): Position | null {
position.depositedToken1 = ZERO_BD
position.withdrawnToken0 = ZERO_BD
position.withdrawnToken1 = ZERO_BD
position.collectedToken0 = ZERO_BD
position.collectedToken1 = ZERO_BD
position.collectedFeesToken0 = ZERO_BD
position.collectedFeesToken1 = ZERO_BD
position.transaction = loadTransaction(event).id
Expand Down Expand Up @@ -134,39 +132,21 @@ export function handleDecreaseLiquidity(event: DecreaseLiquidity): void {
position.withdrawnToken1 = position.withdrawnToken1.plus(amount1)

position = updateFeeVars(position!, event, event.params.tokenId)

position.save()

savePositionSnapshot(position!, event)
}

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)
let amount1 = convertTokenToDecimal(event.params.amount1, token1.decimals)
position.collectedToken0 = position.collectedToken0.plus(amount0)
position.collectedToken1 = position.collectedToken1.plus(amount1)

position.collectedFeesToken0 = position.collectedToken0.minus(position.withdrawnToken0)
position.collectedFeesToken1 = position.collectedToken1.minus(position.withdrawnToken1)

position = updateFeeVars(position!, event, event.params.tokenId)

position.save()

savePositionSnapshot(position!, event)
}

Expand Down
62 changes: 39 additions & 23 deletions src/utils/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const USDC_WETH_03_POOL = '0x8ad599c3a0ff1de082011efddc58f1908eb6e6d8'
// usually tokens that many tokens are paired with s
export let WHITELIST_TOKENS: string[] = [
WETH_ADDRESS, // WETH
'0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', // WETH
'0x6b175474e89094c44da98b954eedeac495271d0f', // DAI
'0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48', // USDC
'0xdac17f958d2ee523a2206206994597c13d831ec7', // USDT
Expand All @@ -33,6 +32,14 @@ export let WHITELIST_TOKENS: string[] = [
'0x7fc66500c84a76ad7e9c93437bfc5ac33e2ddae9' // AAVE
]

let STABLE_COINS: string[] = [
'0x6b175474e89094c44da98b954eedeac495271d0f',
'0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
'0xdac17f958d2ee523a2206206994597c13d831ec7',
'0x0000000000085d4780b73119b644ae5ecd22b376',
'0x956f47f50a910163d8bf957cf5846d573e7f87ca'
]

let MINIMUM_ETH_LOCKED = BigDecimal.fromString('52')

let Q192 = 2 ** 192
Expand Down Expand Up @@ -71,29 +78,38 @@ export function findEthPerToken(token: Token): BigDecimal {
// need to update this to actually detect best rate based on liquidity distribution
let largestLiquidityETH = ZERO_BD
let priceSoFar = ZERO_BD
for (let i = 0; i < whiteList.length; ++i) {
let poolAddress = whiteList[i]
let pool = Pool.load(poolAddress)
if (pool.liquidity.gt(ZERO_BI)) {
if (pool.token0 == token.id) {
// whitelist token is token1
let token1 = Token.load(pool.token1)
// get the derived ETH in pool
let ethLocked = pool.totalValueLockedToken1.times(token1.derivedETH)
if (ethLocked.gt(largestLiquidityETH) && ethLocked.gt(MINIMUM_ETH_LOCKED)) {
largestLiquidityETH = ethLocked
// token1 per our token * Eth per token1
priceSoFar = pool.token1Price.times(token1.derivedETH as BigDecimal)
let bundle = Bundle.load('1')

// hardcoded fix for incorrect rates
// if whitelist includes token - get the safe price
if (STABLE_COINS.includes(token.id)) {
priceSoFar = safeDiv(ONE_BD, bundle.ethPriceUSD)
} else {
for (let i = 0; i < whiteList.length; ++i) {
let poolAddress = whiteList[i]
let pool = Pool.load(poolAddress)

if (pool.liquidity.gt(ZERO_BI)) {
if (pool.token0 == token.id) {
// whitelist token is token1
let token1 = Token.load(pool.token1)
// get the derived ETH in pool
let ethLocked = pool.totalValueLockedToken1.times(token1.derivedETH)
if (ethLocked.gt(largestLiquidityETH) && ethLocked.gt(MINIMUM_ETH_LOCKED)) {
largestLiquidityETH = ethLocked
// token1 per our token * Eth per token1
priceSoFar = pool.token1Price.times(token1.derivedETH as BigDecimal)
}
}
}
if (pool.token1 == token.id) {
let token0 = Token.load(pool.token0)
// get the derived ETH in pool
let ethLocked = pool.totalValueLockedToken0.times(token0.derivedETH)
if (ethLocked.gt(largestLiquidityETH) && ethLocked.gt(MINIMUM_ETH_LOCKED)) {
largestLiquidityETH = ethLocked
// token0 per our token * ETH per token0
priceSoFar = pool.token0Price.times(token0.derivedETH as BigDecimal)
if (pool.token1 == token.id) {
let token0 = Token.load(pool.token0)
// get the derived ETH in pool
let ethLocked = pool.totalValueLockedToken0.times(token0.derivedETH)
if (ethLocked.gt(largestLiquidityETH) && ethLocked.gt(MINIMUM_ETH_LOCKED)) {
largestLiquidityETH = ethLocked
// token0 per our token * ETH per token0
priceSoFar = pool.token0Price.times(token0.derivedETH as BigDecimal)
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion subgraph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ description: Uniswap is a decentralized protocol for automated token exchange on
repository: https://github.com/Uniswap/uniswap-v3-subgraph
schema:
file: ./schema.graphql
graft:
base: QmRvEgUpbjVV326W2zPvxWimzQwcDREfjejUdFn2L92qry
block: 13498328
dataSources:
- kind: ethereum/contract
name: Factory
Expand Down Expand Up @@ -97,4 +100,4 @@ templates:
- event: Burn(indexed address,indexed int24,indexed int24,uint128,uint256,uint256)
handler: handleBurn
- event: Flash(indexed address,indexed address,uint256,uint256,uint256,uint256)
handler: handleFlash
handler: handleFlash

0 comments on commit ff07a11

Please sign in to comment.