From c5a6c988bc6b48842e5ee2a677ec634aefb50eb8 Mon Sep 17 00:00:00 2001 From: ChefJoJo <94336009+chef-jojo@users.noreply.github.com> Date: Tue, 2 Apr 2024 11:40:49 +0800 Subject: [PATCH] Fix tvl and prevent decimals error --- .../exchange-v3/template/abis/ERC20.json | 2 +- .../exchange-v3/template/mappings/core.ts | 53 +++++++++++-------- subgraphs/exchange-v3/template/subgraph.yaml | 14 ++--- .../exchange-v3/template/utils/constants.ts | 2 +- .../exchange-v3/template/utils/pricing.ts | 12 ++--- subgraphs/exchange-v3/template/utils/token.ts | 7 +-- 6 files changed, 49 insertions(+), 41 deletions(-) diff --git a/subgraphs/exchange-v3/template/abis/ERC20.json b/subgraphs/exchange-v3/template/abis/ERC20.json index 405d6b36..327c0fad 100644 --- a/subgraphs/exchange-v3/template/abis/ERC20.json +++ b/subgraphs/exchange-v3/template/abis/ERC20.json @@ -84,7 +84,7 @@ "outputs": [ { "name": "", - "type": "uint8" + "type": "uint32" } ], "payable": false, diff --git a/subgraphs/exchange-v3/template/mappings/core.ts b/subgraphs/exchange-v3/template/mappings/core.ts index 8a78782b..0d0945a4 100644 --- a/subgraphs/exchange-v3/template/mappings/core.ts +++ b/subgraphs/exchange-v3/template/mappings/core.ts @@ -192,20 +192,21 @@ export function handleBurn(event: BurnEvent): void { pool.txCount = pool.txCount.plus(ONE_BI); // Update TVL values. - let oldPoolTotalValueLockedETH = pool.totalValueLockedETH; - let oldPoolTVLETHUntracked = pool.totalValueLockedETHUntracked; - token0.totalValueLocked = token0.totalValueLocked.minus(amount0); - token1.totalValueLocked = token1.totalValueLocked.minus(amount1); - pool.totalValueLockedToken0 = pool.totalValueLockedToken0.minus(amount0); - pool.totalValueLockedToken1 = pool.totalValueLockedToken1.minus(amount1); - updateDerivedTVLAmounts( - pool as Pool, - factory as Factory, - token0 as Token, - token1 as Token, - oldPoolTotalValueLockedETH, - oldPoolTVLETHUntracked - ); + // commented to avoid double counting as burn event is emitted with same amount + // let oldPoolTotalValueLockedETH = pool.totalValueLockedETH; + // let oldPoolTVLETHUntracked = pool.totalValueLockedETHUntracked; + // token0.totalValueLocked = token0.totalValueLocked.minus(amount0); + // token1.totalValueLocked = token1.totalValueLocked.minus(amount1); + // pool.totalValueLockedToken0 = pool.totalValueLockedToken0.minus(amount0); + // pool.totalValueLockedToken1 = pool.totalValueLockedToken1.minus(amount1); + // updateDerivedTVLAmounts( + // pool as Pool, + // factory as Factory, + // token0 as Token, + // token1 as Token, + // oldPoolTotalValueLockedETH, + // oldPoolTVLETHUntracked + // ); // Pools liquidity tracks the currently active liquidity given pools current tick. // We only want to update it on burn if the position being burnt includes the current tick. @@ -555,15 +556,21 @@ export function handleCollect(event: CollectEvent): void { token1 as Token ); - // commented to avoid double counting as burn event is emitted with same amount - // // Adjust pool TVL based on amount collected. - // let oldPoolTVLETH = pool.totalValueLockedETH; - // let oldPoolTVLETHUntracked = pool.totalValueLockedETHUntracked; - // pool.totalValueLockedToken0 = pool.totalValueLockedToken0.minus(amount0); - // pool.totalValueLockedToken1 = pool.totalValueLockedToken1.minus(amount1); - // token0.totalValueLocked = token0.totalValueLocked.minus(amount0); - // token1.totalValueLocked = token1.totalValueLocked.minus(amount1); - // updateDerivedTVLAmounts(pool as Pool, factory as Factory, oldPoolTVLETH, oldPoolTVLETHUntracked); + // Adjust pool TVL based on amount collected. + let oldPoolTotalValueLockedETH = pool.totalValueLockedETH; + let oldPoolTVLETHUntracked = pool.totalValueLockedETHUntracked; + pool.totalValueLockedToken0 = pool.totalValueLockedToken0.minus(amount0); + pool.totalValueLockedToken1 = pool.totalValueLockedToken1.minus(amount1); + token0.totalValueLocked = token0.totalValueLocked.minus(amount0); + token1.totalValueLocked = token1.totalValueLocked.minus(amount1); + updateDerivedTVLAmounts( + pool as Pool, + factory as Factory, + token0 as Token, + token1 as Token, + oldPoolTotalValueLockedETH, + oldPoolTVLETHUntracked + ); // Update aggregate fee collection values. pool.collectedFeesToken0 = pool.collectedFeesToken0.plus(amount0); diff --git a/subgraphs/exchange-v3/template/subgraph.yaml b/subgraphs/exchange-v3/template/subgraph.yaml index 2633629a..0ba8f2a8 100644 --- a/subgraphs/exchange-v3/template/subgraph.yaml +++ b/subgraphs/exchange-v3/template/subgraph.yaml @@ -8,11 +8,11 @@ features: dataSources: - kind: ethereum/contract name: Factory - network: zksync-era + network: bsc source: - address: "0x1bb72e0cbbea93c08f535fc7856e0338d7f7a8ab" + address: "0x0bfbcf9fa4f9c56b0f40a671ad40e0805a091865" abi: Factory - startBlock: 8639214 + startBlock: 26956207 mapping: kind: ethereum/events apiVersion: 0.0.4 @@ -37,11 +37,11 @@ dataSources: handler: handlePoolCreated - kind: ethereum/contract name: NonfungiblePositionManager - network: zksync-era + network: bsc source: - address: "0xa815e2ed7f7d5b0c49fda367f249232a1b9d2883" + address: "0x46a15b0b27311cedf172ab29e4f4766fbe7f4364" abi: NonfungiblePositionManager - startBlock: 8640657 + startBlock: 26931961 mapping: kind: ethereum/events apiVersion: 0.0.4 @@ -71,7 +71,7 @@ dataSources: templates: - kind: ethereum/contract name: Pool - network: zksync-era + network: bsc source: abi: Pool mapping: diff --git a/subgraphs/exchange-v3/template/utils/constants.ts b/subgraphs/exchange-v3/template/utils/constants.ts index b2c2835e..f5f5e80e 100644 --- a/subgraphs/exchange-v3/template/utils/constants.ts +++ b/subgraphs/exchange-v3/template/utils/constants.ts @@ -4,7 +4,7 @@ import { Factory as FactoryContract } from "../generated/templates/Pool/Factory" export const ADDRESS_ZERO = "0x0000000000000000000000000000000000000000"; // prettier-ignore -export const FACTORY_ADDRESS = "0x1bb72e0cbbea93c08f535fc7856e0338d7f7a8ab"; +export const FACTORY_ADDRESS = "0x0bfbcf9fa4f9c56b0f40a671ad40e0805a091865"; export let ZERO_BI = BigInt.fromI32(0); export let ONE_BI = BigInt.fromI32(1); diff --git a/subgraphs/exchange-v3/template/utils/pricing.ts b/subgraphs/exchange-v3/template/utils/pricing.ts index 9bd625b4..c76e766b 100644 --- a/subgraphs/exchange-v3/template/utils/pricing.ts +++ b/subgraphs/exchange-v3/template/utils/pricing.ts @@ -5,21 +5,21 @@ import { BigDecimal, BigInt } from "@graphprotocol/graph-ts"; import { exponentToBigDecimal, safeDiv } from "./index"; // prettier-ignore -const WETH_ADDRESS = "0x5aea5775959fbc2557cc8789bc1bf90a239d9a91"; +const WETH_ADDRESS = "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c"; // prettier-ignore -const USDC_WETH_03_POOL = "0x291d9f9764c72c9ba6ff47b451a9f7885ebf9977"; +const USDC_WETH_03_POOL = "0x36696169c63e42cd08ce11f5deebbcebae652050"; -const STABLE_IS_TOKEN0 = "false" as string; +const STABLE_IS_TOKEN0 = "true" as string; // token where amounts should contribute to tracked volume and liquidity // usually tokens that many tokens are paired with s // prettier-ignore -export let WHITELIST_TOKENS: string[] = "0x5aea5775959fbc2557cc8789bc1bf90a239d9a91,0x493257fd37edb34451f62edf8d2a0c418852ba4c,0x2039bb4116b4efc145ec4f0e2ea75012d6c0f181,0x3355df6d4c9c3035724fd0e3914de96a5a83aaf4,0xbbeb516fb02a01611cbbe0453fe3c580d7281011,0x32fd44bb869620c0ef993754c8a00be67c464806,0x703b52f2b28febcb60e1372858af5b18849fe867,0x3a287a06c66f9e95a56327185ca2bdf5f031cecd,0x4b9eb6c0b6ea15176bbf62841c6b2a8a398cb656,0x8e86e46278518efc1c5ced245cba2c7e3ef11557".split(","); +export let WHITELIST_TOKENS: string[] = "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c,0x55d398326f99059ff775485246999027b3197955,0xe9e7cea3dedca5984780bafc599bd69add087d56,0x8ac76a51cc950d9822d68b83fe1ad97b32cd580d,0x7130d2a12b9bcbfae4f2634d864a1ee1ce3ead9c,0x2170ed0880ac9a755fd29b2688956bd959f933f8,0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82".split(","); // prettier-ignore -let STABLE_COINS: string[] = "0x493257fd37edb34451f62edf8d2a0c418852ba4c,0x3355df6d4c9c3035724fd0e3914de96a5a83aaf4,0x2039bb4116b4efc145ec4f0e2ea75012d6c0f181,0x4b9eb6c0b6ea15176bbf62841c6b2a8a398cb656,0x8e86e46278518efc1c5ced245cba2c7e3ef11557".split(","); +let STABLE_COINS: string[] = "0x55d398326f99059ff775485246999027b3197955,0xe9e7cea3dedca5984780bafc599bd69add087d56,0x8ac76a51cc950d9822d68b83fe1ad97b32cd580d".split(","); -let MINIMUM_ETH_LOCKED = BigDecimal.fromString("5"); +let MINIMUM_ETH_LOCKED = BigDecimal.fromString("10"); let Q192 = 2 ** 192; export function sqrtPriceX96ToTokenPrices(sqrtPriceX96: BigInt, token0: Token, token1: Token): BigDecimal[] { diff --git a/subgraphs/exchange-v3/template/utils/token.ts b/subgraphs/exchange-v3/template/utils/token.ts index d8d5e6e7..ca30dfe2 100644 --- a/subgraphs/exchange-v3/template/utils/token.ts +++ b/subgraphs/exchange-v3/template/utils/token.ts @@ -75,10 +75,11 @@ export function fetchTokenTotalSupply(tokenAddress: Address): BigInt { export function fetchTokenDecimals(tokenAddress: Address): BigInt { let contract = ERC20.bind(tokenAddress); // try types uint8 for decimals - let decimalValue = null; let decimalResult = contract.try_decimals(); if (!decimalResult.reverted) { - decimalValue = decimalResult.value; + if (decimalResult.value.lt(BigInt.fromI32(255))) { + return decimalResult.value; + } } else { // try with the static definition let staticTokenDefinition = StaticTokenDefinition.fromAddress(tokenAddress); @@ -87,5 +88,5 @@ export function fetchTokenDecimals(tokenAddress: Address): BigInt { } } - return BigInt.fromI32(decimalValue as i32); + return null; }