From ff07a11ec5aa9112585f72ff052c3b2fce87c14c Mon Sep 17 00:00:00 2001 From: ianlapham Date: Thu, 28 Oct 2021 10:26:22 -0700 Subject: [PATCH] pricing hot fix for stablecoins --- package.json | 2 +- schema.graphql | 4 --- src/mappings/core.ts | 2 +- src/mappings/position-manager.ts | 20 ----------- src/utils/pricing.ts | 62 ++++++++++++++++++++------------ subgraph.yaml | 5 ++- 6 files changed, 45 insertions(+), 50 deletions(-) diff --git a/package.json b/package.json index 6e8ad49e..6c30ecac 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/schema.graphql b/schema.graphql index 9e1d6c42..6b10e59b 100644 --- a/schema.graphql +++ b/schema.graphql @@ -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 diff --git a/src/mappings/core.ts b/src/mappings/core.ts index 37687f54..fea22609 100644 --- a/src/mappings/core.ts +++ b/src/mappings/core.ts @@ -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, diff --git a/src/mappings/position-manager.ts b/src/mappings/position-manager.ts index 8eae854f..9bbef1c6 100644 --- a/src/mappings/position-manager.ts +++ b/src/mappings/position-manager.ts @@ -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 @@ -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) } diff --git a/src/utils/pricing.ts b/src/utils/pricing.ts index bb05f3aa..d94fb13d 100644 --- a/src/utils/pricing.ts +++ b/src/utils/pricing.ts @@ -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 @@ -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 @@ -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) + } } } } diff --git a/subgraph.yaml b/subgraph.yaml index bce44e6d..681821be 100644 --- a/subgraph.yaml +++ b/subgraph.yaml @@ -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 @@ -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 \ No newline at end of file + handler: handleFlash