Skip to content

Commit

Permalink
Fix tvl and prevent decimals error
Browse files Browse the repository at this point in the history
  • Loading branch information
0xjojoex committed Apr 2, 2024
1 parent 2b87e81 commit c5a6c98
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 41 deletions.
2 changes: 1 addition & 1 deletion subgraphs/exchange-v3/template/abis/ERC20.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"outputs": [
{
"name": "",
"type": "uint8"
"type": "uint32"
}
],
"payable": false,
Expand Down
53 changes: 30 additions & 23 deletions subgraphs/exchange-v3/template/mappings/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions subgraphs/exchange-v3/template/subgraph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -71,7 +71,7 @@ dataSources:
templates:
- kind: ethereum/contract
name: Pool
network: zksync-era
network: bsc
source:
abi: Pool
mapping:
Expand Down
2 changes: 1 addition & 1 deletion subgraphs/exchange-v3/template/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions subgraphs/exchange-v3/template/utils/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] {
Expand Down
7 changes: 4 additions & 3 deletions subgraphs/exchange-v3/template/utils/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -87,5 +88,5 @@ export function fetchTokenDecimals(tokenAddress: Address): BigInt {
}
}

return BigInt.fromI32(decimalValue as i32);
return null;
}

0 comments on commit c5a6c98

Please sign in to comment.