From 7369062e9d14570c4e26562bd83b0f99eb0f63f0 Mon Sep 17 00:00:00 2001 From: Chris Steege <56660047+steegecs@users.noreply.github.com> Date: Mon, 24 Apr 2023 09:32:54 -0500 Subject: [PATCH] fix(#patch); Uniswap Forks; Upgrade Pricing Mechanism (#2040) --- deployment/deployment.json | 58 +++--- .../configurations/configure.mustache | 2 +- .../uniswap-forks/configurations/configure.ts | 2 +- .../apeswap-bsc/deploymentJsonContext.json | 2 +- .../src/common/handlers/handleReward.ts | 4 +- .../src/common/handlers/handleRewardMini.ts | 4 +- .../honeyswap/src/common/creators.ts | 12 +- .../honeyswap/src/mappings/honeyfarm.ts | 5 +- .../src/common/handlers/handleReward.ts | 4 +- .../deploymentJsonContext.json | 16 +- .../src/common/handlers/handleRewardMini.ts | 4 +- .../src/common/handlers/handleReward.ts | 4 +- .../src/common/handlers/handleReward.ts | 4 +- .../src/common/handlers/handleReward.ts | 4 +- .../src/common/handlers/handleRewardV2.ts | 2 +- .../src/common/handlers/handleReward.ts | 4 +- .../src/common/handlers/handleRewardMini.ts | 4 +- .../src/common/handlers/handleRewardV2.ts | 4 +- .../src/common/handlers/handleRewardV2.ts | 4 +- .../src/common/handlers/handleRewardV3.ts | 4 +- .../src/common/handlers/handleRewarder.ts | 33 ++-- .../src/mappings/masterchef/rewardV2.ts | 4 +- .../src/mappings/masterchef/rewardV3.ts | 4 +- .../src/common/handlers/handleReward.ts | 4 +- .../src/common/handlers/handleRewardV2.ts | 4 +- .../protocols/ubeswap/src/common/helpers.ts | 4 +- .../src/common/handlers/handleReward.ts | 4 +- subgraphs/uniswap-forks/schema.graphql | 10 ++ .../uniswap-forks/src/common/constants.ts | 7 + .../uniswap-forks/src/common/creators.ts | 61 ++++++- subgraphs/uniswap-forks/src/common/getters.ts | 61 +++++-- .../src/common/masterchef/helpers.ts | 4 +- .../uniswap-forks/src/common/updateMetrics.ts | 30 ++-- .../uniswap-forks/src/common/utils/utils.ts | 8 + subgraphs/uniswap-forks/src/mappings/pool.ts | 6 +- subgraphs/uniswap-forks/src/price/price.ts | 166 +++++++++++++----- subgraphs/uniswap-forks/src/versions.ts | 4 +- 37 files changed, 370 insertions(+), 191 deletions(-) diff --git a/deployment/deployment.json b/deployment/deployment.json index c51909e05f..95f24e8227 100644 --- a/deployment/deployment.json +++ b/deployment/deployment.json @@ -1308,7 +1308,7 @@ "status": "prod", "versions": { "schema": "1.3.0", - "subgraph": "1.0.2", + "subgraph": "1.0.3", "methodology": "1.0.0" }, "files": { @@ -4508,7 +4508,7 @@ "status": "prod", "versions": { "schema": "1.3.0", - "subgraph": "1.1.10", + "subgraph": "1.1.11", "methodology": "1.0.0" }, "files": { @@ -4530,7 +4530,7 @@ "status": "prod", "versions": { "schema": "1.3.0", - "subgraph": "1.1.10", + "subgraph": "1.1.11", "methodology": "1.0.0" }, "files": { @@ -4560,7 +4560,7 @@ "status": "prod", "versions": { "schema": "1.3.0", - "subgraph": "1.0.5", + "subgraph": "1.0.6", "methodology": "1.0.0" }, "files": { @@ -4582,7 +4582,7 @@ "status": "prod", "versions": { "schema": "1.3.0", - "subgraph": "1.0.2", + "subgraph": "1.0.3", "methodology": "1.0.0" }, "files": { @@ -4612,7 +4612,7 @@ "status": "dev", "versions": { "schema": "1.3.0", - "subgraph": "1.0.6", + "subgraph": "1.0.7", "methodology": "1.0.0" }, "files": { @@ -4642,7 +4642,7 @@ "status": "prod", "versions": { "schema": "1.3.0", - "subgraph": "1.1.8", + "subgraph": "1.1.9", "methodology": "1.0.0" }, "files": { @@ -4672,7 +4672,7 @@ "status": "prod", "versions": { "schema": "1.3.0", - "subgraph": "1.1.6", + "subgraph": "1.1.7", "methodology": "1.0.0" }, "files": { @@ -4702,7 +4702,7 @@ "status": "dev", "versions": { "schema": "1.3.0", - "subgraph": "1.1.8", + "subgraph": "1.1.9", "methodology": "1.0.0" }, "files": { @@ -4732,7 +4732,7 @@ "status": "prod", "versions": { "schema": "1.3.0", - "subgraph": "1.1.10", + "subgraph": "1.1.11", "methodology": "1.0.0" }, "files": { @@ -4754,7 +4754,7 @@ "status": "prod", "versions": { "schema": "1.3.0", - "subgraph": "1.1.10", + "subgraph": "1.1.11", "methodology": "1.0.0" }, "files": { @@ -4776,7 +4776,7 @@ "status": "prod", "versions": { "schema": "1.3.0", - "subgraph": "1.1.10", + "subgraph": "1.1.11", "methodology": "1.0.0" }, "files": { @@ -4798,7 +4798,7 @@ "status": "prod", "versions": { "schema": "1.3.0", - "subgraph": "1.1.10", + "subgraph": "1.1.11", "methodology": "1.0.0" }, "files": { @@ -4824,7 +4824,7 @@ "status": "prod", "versions": { "schema": "1.3.0", - "subgraph": "1.1.10", + "subgraph": "1.1.11", "methodology": "1.0.0" }, "files": { @@ -4846,7 +4846,7 @@ "status": "prod", "versions": { "schema": "1.3.0", - "subgraph": "1.1.10", + "subgraph": "1.1.11", "methodology": "1.0.0" }, "files": { @@ -4868,7 +4868,7 @@ "status": "prod", "versions": { "schema": "1.3.0", - "subgraph": "1.2.0", + "subgraph": "1.2.1", "methodology": "1.0.0" }, "files": { @@ -4894,7 +4894,7 @@ "status": "prod", "versions": { "schema": "1.3.0", - "subgraph": "1.1.11", + "subgraph": "1.1.12", "methodology": "1.0.0" }, "files": { @@ -4916,7 +4916,7 @@ "status": "prod", "versions": { "schema": "1.3.0", - "subgraph": "1.1.11", + "subgraph": "1.1.12", "methodology": "1.0.0" }, "files": { @@ -4938,7 +4938,7 @@ "status": "prod", "versions": { "schema": "1.3.0", - "subgraph": "1.1.10", + "subgraph": "1.1.11", "methodology": "1.0.0" }, "files": { @@ -4960,7 +4960,7 @@ "status": "prod", "versions": { "schema": "1.3.0", - "subgraph": "1.1.10", + "subgraph": "1.1.11", "methodology": "1.0.0" }, "files": { @@ -4982,7 +4982,7 @@ "status": "prod", "versions": { "schema": "1.3.1", - "subgraph": "1.1.11", + "subgraph": "1.1.12", "methodology": "1.0.0" }, "files": { @@ -5016,7 +5016,7 @@ "status": "prod", "versions": { "schema": "1.3.0", - "subgraph": "1.1.10", + "subgraph": "1.1.11", "methodology": "1.0.0" }, "files": { @@ -5046,7 +5046,7 @@ "status": "dev", "versions": { "schema": "1.3.0", - "subgraph": "1.0.5", + "subgraph": "1.0.6", "methodology": "1.0.0" }, "files": { @@ -5076,7 +5076,7 @@ "status": "prod", "versions": { "schema": "1.3.0", - "subgraph": "1.1.10", + "subgraph": "1.1.11", "methodology": "1.0.0" }, "files": { @@ -5110,7 +5110,7 @@ "status": "dev", "versions": { "schema": "1.3.0", - "subgraph": "1.1.6", + "subgraph": "1.1.7", "methodology": "1.0.0" }, "files": { @@ -5140,7 +5140,7 @@ "status": "prod", "versions": { "schema": "1.3.0", - "subgraph": "1.1.10", + "subgraph": "1.1.11", "methodology": "1.0.0" }, "files": { @@ -5170,7 +5170,7 @@ "status": "prod", "versions": { "schema": "1.3.1", - "subgraph": "1.0.8", + "subgraph": "1.0.9", "methodology": "1.0.1" }, "files": { @@ -5196,7 +5196,7 @@ "status": "prod", "versions": { "schema": "1.3.0", - "subgraph": "1.0.6", + "subgraph": "1.0.7", "methodology": "1.0.1" }, "files": { @@ -6936,7 +6936,7 @@ "status": "prod", "versions": { "schema": "1.3.0", - "subgraph": "1.0.4", + "subgraph": "1.0.5", "methodology": "1.0.0" }, "services": { diff --git a/subgraphs/uniswap-forks/configurations/configure.mustache b/subgraphs/uniswap-forks/configurations/configure.mustache index bb4dfa90df..b5fb0b99f3 100644 --- a/subgraphs/uniswap-forks/configurations/configure.mustache +++ b/subgraphs/uniswap-forks/configurations/configure.mustache @@ -3,7 +3,7 @@ import { getNetworkConfigurations } from "./configurations/configurations"; import { Deploy } from "./configurations/deploy"; // Select the deployment protocol and network -let deployment = Deploy.{{ deployment }}; +const deployment = Deploy.{{ deployment }}; // export const NetworkConfigs = configurationsMap.get(deployment)! export const NetworkConfigs = getNetworkConfigurations(deployment); \ No newline at end of file diff --git a/subgraphs/uniswap-forks/configurations/configure.ts b/subgraphs/uniswap-forks/configurations/configure.ts index 3ca115adf1..ba9f43b93f 100644 --- a/subgraphs/uniswap-forks/configurations/configure.ts +++ b/subgraphs/uniswap-forks/configurations/configure.ts @@ -3,7 +3,7 @@ import { getNetworkConfigurations } from "./configurations/configurations"; import { Deploy } from "./configurations/deploy"; // Select the deployment protocol and network -const deployment = Deploy.PANGOLIN_AVALANCHE; +const deployment = Deploy.TRADER_JOE_AVALANCHE; // export const NetworkConfigs = configurationsMap.get(deployment)! export const NetworkConfigs = getNetworkConfigurations(deployment); diff --git a/subgraphs/uniswap-forks/protocols/apeswap/config/deployments/apeswap-bsc/deploymentJsonContext.json b/subgraphs/uniswap-forks/protocols/apeswap/config/deployments/apeswap-bsc/deploymentJsonContext.json index 8a0523599d..6d9d2f608a 100644 --- a/subgraphs/uniswap-forks/protocols/apeswap/config/deployments/apeswap-bsc/deploymentJsonContext.json +++ b/subgraphs/uniswap-forks/protocols/apeswap/config/deployments/apeswap-bsc/deploymentJsonContext.json @@ -3,7 +3,7 @@ "status": "prod", "versions": { "schema": "1.3.0", - "subgraph": "1.1.7", + "subgraph": "1.1.11", "methodology": "1.0.0" }, "files": { diff --git a/subgraphs/uniswap-forks/protocols/apeswap/src/common/handlers/handleReward.ts b/subgraphs/uniswap-forks/protocols/apeswap/src/common/handlers/handleReward.ts index 7220d60661..e75a06a494 100644 --- a/subgraphs/uniswap-forks/protocols/apeswap/src/common/handlers/handleReward.ts +++ b/subgraphs/uniswap-forks/protocols/apeswap/src/common/handlers/handleReward.ts @@ -61,9 +61,9 @@ export function handleReward( return; } - const rewardToken = getOrCreateToken(NetworkConfigs.getRewardToken()); + const rewardToken = getOrCreateToken(event, NetworkConfigs.getRewardToken()); pool.rewardTokens = [ - getOrCreateRewardToken(NetworkConfigs.getRewardToken()).id, + getOrCreateRewardToken(event, NetworkConfigs.getRewardToken()).id, ]; // Update staked amounts diff --git a/subgraphs/uniswap-forks/protocols/apeswap/src/common/handlers/handleRewardMini.ts b/subgraphs/uniswap-forks/protocols/apeswap/src/common/handlers/handleRewardMini.ts index ae0a6804ff..6ea8663f4a 100644 --- a/subgraphs/uniswap-forks/protocols/apeswap/src/common/handlers/handleRewardMini.ts +++ b/subgraphs/uniswap-forks/protocols/apeswap/src/common/handlers/handleRewardMini.ts @@ -34,9 +34,9 @@ export function updateMasterChef( return; } - const rewardToken = getOrCreateToken(NetworkConfigs.getRewardToken()); + const rewardToken = getOrCreateToken(event, NetworkConfigs.getRewardToken()); pool.rewardTokens = [ - getOrCreateRewardToken(NetworkConfigs.getRewardToken()).id, + getOrCreateRewardToken(event, NetworkConfigs.getRewardToken()).id, ]; // Get the amount of Banana tokens emitted for all pools per second. diff --git a/subgraphs/uniswap-forks/protocols/honeyswap/src/common/creators.ts b/subgraphs/uniswap-forks/protocols/honeyswap/src/common/creators.ts index 96a8913e6f..9b242c386a 100644 --- a/subgraphs/uniswap-forks/protocols/honeyswap/src/common/creators.ts +++ b/subgraphs/uniswap-forks/protocols/honeyswap/src/common/creators.ts @@ -75,8 +75,8 @@ export function createLiquidityPool( const protocol = getOrCreateProtocol(); // create the tokens and tokentracker - const token0 = getOrCreateToken(token0Address); - const token1 = getOrCreateToken(token1Address); + const token0 = getOrCreateToken(event, token0Address); + const token1 = getOrCreateToken(event, token1Address); const LPtoken = getOrCreateLPToken(poolAddress, token0, token1); updateTokenWhitelists(token0, token1, poolAddress); @@ -136,13 +136,13 @@ export function createLiquidityPool( // Add reward token to liquidity pool from HoneyFarm add contract call (PoolAdded event) export function createPoolRewardToken( - poolAddress: string, - blockNumber: BigInt + event: ethereum.Event, + poolAddress: string ): void { - const pool = getLiquidityPool(poolAddress, blockNumber); + const pool = getLiquidityPool(poolAddress, event.block.number); pool.rewardTokens = [ - getOrCreateRewardToken(NetworkConfigs.getRewardToken()).id, + getOrCreateRewardToken(event, NetworkConfigs.getRewardToken()).id, ]; pool.save(); diff --git a/subgraphs/uniswap-forks/protocols/honeyswap/src/mappings/honeyfarm.ts b/subgraphs/uniswap-forks/protocols/honeyswap/src/mappings/honeyfarm.ts index 321e641905..77541da75a 100644 --- a/subgraphs/uniswap-forks/protocols/honeyswap/src/mappings/honeyfarm.ts +++ b/subgraphs/uniswap-forks/protocols/honeyswap/src/mappings/honeyfarm.ts @@ -14,10 +14,7 @@ import { handleReward } from "../common/handlers"; // WIP: HoneyFarm subgraph handlers currently not used in Honeyswap subgraph deployment export function handlePoolAdded(event: PoolAddedEvent): void { log.debug("poolToken added: {}", [event.params.poolToken.toHexString()]); - createPoolRewardToken( - event.params.poolToken.toHexString(), - event.block.number - ); + createPoolRewardToken(event, event.params.poolToken.toHexString()); } // WIP: HoneyFarm subgraph handlers currently not used in Honeyswap subgraph deployment diff --git a/subgraphs/uniswap-forks/protocols/mm-finance/src/common/handlers/handleReward.ts b/subgraphs/uniswap-forks/protocols/mm-finance/src/common/handlers/handleReward.ts index 2d23b03cb5..d5ee56e185 100644 --- a/subgraphs/uniswap-forks/protocols/mm-finance/src/common/handlers/handleReward.ts +++ b/subgraphs/uniswap-forks/protocols/mm-finance/src/common/handlers/handleReward.ts @@ -61,9 +61,9 @@ export function handleReward( return; } - const rewardToken = getOrCreateToken(NetworkConfigs.getRewardToken()); + const rewardToken = getOrCreateToken(event, NetworkConfigs.getRewardToken()); pool.rewardTokens = [ - getOrCreateRewardToken(NetworkConfigs.getRewardToken()).id, + getOrCreateRewardToken(event, NetworkConfigs.getRewardToken()).id, ]; // Update staked amounts diff --git a/subgraphs/uniswap-forks/protocols/pangolin/config/deployments/pangolin-avalanche/deploymentJsonContext.json b/subgraphs/uniswap-forks/protocols/pangolin/config/deployments/pangolin-avalanche/deploymentJsonContext.json index fb5d018c9b..5af37e6769 100644 --- a/subgraphs/uniswap-forks/protocols/pangolin/config/deployments/pangolin-avalanche/deploymentJsonContext.json +++ b/subgraphs/uniswap-forks/protocols/pangolin/config/deployments/pangolin-avalanche/deploymentJsonContext.json @@ -1,11 +1,17 @@ { "network": "avalanche", - "status": "dev", + "status": "prod", "versions": { "schema": "1.3.0", - "subgraph": "1.0.0", + "subgraph": "1.0.5", "methodology": "1.0.0" }, + "services": { + "hosted-service": { + "slug": "pangolin-avalanche", + "query-id": "pangolin-avalanche" + } + }, "files": { "template": "pangolin.template.yaml" }, @@ -13,12 +19,6 @@ "prepare:yaml": true, "prepare:constants": true }, - "services": { - "hosted-service": { - "slug": "pangolin-avalanche", - "query-id": "pangolin-avalanche" - } - }, "protocol": "pangolin", "base": "uniswap-forks", "schema": "dex-amm", diff --git a/subgraphs/uniswap-forks/protocols/pangolin/src/common/handlers/handleRewardMini.ts b/subgraphs/uniswap-forks/protocols/pangolin/src/common/handlers/handleRewardMini.ts index fee823d815..fc9b41e985 100644 --- a/subgraphs/uniswap-forks/protocols/pangolin/src/common/handlers/handleRewardMini.ts +++ b/subgraphs/uniswap-forks/protocols/pangolin/src/common/handlers/handleRewardMini.ts @@ -32,9 +32,9 @@ export function updateMasterChef( return; } - const rewardToken = getOrCreateToken(NetworkConfigs.getRewardToken()); + const rewardToken = getOrCreateToken(event, NetworkConfigs.getRewardToken()); pool.rewardTokens = [ - getOrCreateRewardToken(NetworkConfigs.getRewardToken()).id, + getOrCreateRewardToken(event, NetworkConfigs.getRewardToken()).id, ]; // Calculate Reward Emission per second to a specific pool diff --git a/subgraphs/uniswap-forks/protocols/solarbeam/src/common/handlers/handleReward.ts b/subgraphs/uniswap-forks/protocols/solarbeam/src/common/handlers/handleReward.ts index 01dd790fe4..2b82ef0130 100644 --- a/subgraphs/uniswap-forks/protocols/solarbeam/src/common/handlers/handleReward.ts +++ b/subgraphs/uniswap-forks/protocols/solarbeam/src/common/handlers/handleReward.ts @@ -92,9 +92,11 @@ export function handleReward( getPoolRewardsPerSecond.value.value3; rewardTokenIds.push( - getOrCreateRewardToken(rewardTokenAddresses[index].toHexString()).id + getOrCreateRewardToken(event, rewardTokenAddresses[index].toHexString()) + .id ); const rewardToken = getOrCreateToken( + event, rewardTokenAddresses[index].toHexString() ); const rewardTokenRateBigDecimal = diff --git a/subgraphs/uniswap-forks/protocols/spiritswap/src/common/handlers/handleReward.ts b/subgraphs/uniswap-forks/protocols/spiritswap/src/common/handlers/handleReward.ts index fb595cc6f7..12561271da 100644 --- a/subgraphs/uniswap-forks/protocols/spiritswap/src/common/handlers/handleReward.ts +++ b/subgraphs/uniswap-forks/protocols/spiritswap/src/common/handlers/handleReward.ts @@ -61,9 +61,9 @@ export function handleReward( return; } - const rewardToken = getOrCreateToken(NetworkConfigs.getRewardToken()); + const rewardToken = getOrCreateToken(event, NetworkConfigs.getRewardToken()); pool.rewardTokens = [ - getOrCreateRewardToken(NetworkConfigs.getRewardToken()).id, + getOrCreateRewardToken(event, NetworkConfigs.getRewardToken()).id, ]; // Update staked amounts diff --git a/subgraphs/uniswap-forks/protocols/spookyswap/src/common/handlers/handleReward.ts b/subgraphs/uniswap-forks/protocols/spookyswap/src/common/handlers/handleReward.ts index 87e41c5d70..f4367dfd54 100644 --- a/subgraphs/uniswap-forks/protocols/spookyswap/src/common/handlers/handleReward.ts +++ b/subgraphs/uniswap-forks/protocols/spookyswap/src/common/handlers/handleReward.ts @@ -61,9 +61,9 @@ export function handleReward( return; } - const rewardToken = getOrCreateToken(NetworkConfigs.getRewardToken()); + const rewardToken = getOrCreateToken(event, NetworkConfigs.getRewardToken()); pool.rewardTokens = [ - getOrCreateRewardToken(NetworkConfigs.getRewardToken()).id, + getOrCreateRewardToken(event, NetworkConfigs.getRewardToken()).id, ]; // Update staked amounts diff --git a/subgraphs/uniswap-forks/protocols/spookyswap/src/common/handlers/handleRewardV2.ts b/subgraphs/uniswap-forks/protocols/spookyswap/src/common/handlers/handleRewardV2.ts index 6b6d2005b4..20d7ef259b 100644 --- a/subgraphs/uniswap-forks/protocols/spookyswap/src/common/handlers/handleRewardV2.ts +++ b/subgraphs/uniswap-forks/protocols/spookyswap/src/common/handlers/handleRewardV2.ts @@ -52,7 +52,7 @@ export function updateMasterChef( return; } - const rewardToken = getOrCreateToken(NetworkConfigs.getRewardToken()); + const rewardToken = getOrCreateToken(event, NetworkConfigs.getRewardToken()); pool.rewardTokens = [rewardToken.id]; // Get the amount of reward tokens emitted per block at this point in time. diff --git a/subgraphs/uniswap-forks/protocols/sushiswap/src/common/handlers/handleReward.ts b/subgraphs/uniswap-forks/protocols/sushiswap/src/common/handlers/handleReward.ts index e83f343986..ed0e4d5cd5 100644 --- a/subgraphs/uniswap-forks/protocols/sushiswap/src/common/handlers/handleReward.ts +++ b/subgraphs/uniswap-forks/protocols/sushiswap/src/common/handlers/handleReward.ts @@ -64,9 +64,9 @@ export function handleReward( return; } - const rewardToken = getOrCreateToken(NetworkConfigs.getRewardToken()); + const rewardToken = getOrCreateToken(event, NetworkConfigs.getRewardToken()); pool.rewardTokens = [ - getOrCreateRewardToken(NetworkConfigs.getRewardToken()).id, + getOrCreateRewardToken(event, NetworkConfigs.getRewardToken()).id, ]; // Update staked amounts diff --git a/subgraphs/uniswap-forks/protocols/sushiswap/src/common/handlers/handleRewardMini.ts b/subgraphs/uniswap-forks/protocols/sushiswap/src/common/handlers/handleRewardMini.ts index fee823d815..fc9b41e985 100644 --- a/subgraphs/uniswap-forks/protocols/sushiswap/src/common/handlers/handleRewardMini.ts +++ b/subgraphs/uniswap-forks/protocols/sushiswap/src/common/handlers/handleRewardMini.ts @@ -32,9 +32,9 @@ export function updateMasterChef( return; } - const rewardToken = getOrCreateToken(NetworkConfigs.getRewardToken()); + const rewardToken = getOrCreateToken(event, NetworkConfigs.getRewardToken()); pool.rewardTokens = [ - getOrCreateRewardToken(NetworkConfigs.getRewardToken()).id, + getOrCreateRewardToken(event, NetworkConfigs.getRewardToken()).id, ]; // Calculate Reward Emission per second to a specific pool diff --git a/subgraphs/uniswap-forks/protocols/sushiswap/src/common/handlers/handleRewardV2.ts b/subgraphs/uniswap-forks/protocols/sushiswap/src/common/handlers/handleRewardV2.ts index 09e9458446..cc404a1a44 100644 --- a/subgraphs/uniswap-forks/protocols/sushiswap/src/common/handlers/handleRewardV2.ts +++ b/subgraphs/uniswap-forks/protocols/sushiswap/src/common/handlers/handleRewardV2.ts @@ -37,9 +37,9 @@ export function updateMasterChef( return; } - const rewardToken = getOrCreateToken(NetworkConfigs.getRewardToken()); + const rewardToken = getOrCreateToken(event, NetworkConfigs.getRewardToken()); pool.rewardTokens = [ - getOrCreateRewardToken(NetworkConfigs.getRewardToken()).id, + getOrCreateRewardToken(event, NetworkConfigs.getRewardToken()).id, ]; // Calculate Reward Emission per second to a specific pool diff --git a/subgraphs/uniswap-forks/protocols/trader-joe/src/common/handlers/handleRewardV2.ts b/subgraphs/uniswap-forks/protocols/trader-joe/src/common/handlers/handleRewardV2.ts index 040e6e055b..5e275e9c30 100644 --- a/subgraphs/uniswap-forks/protocols/trader-joe/src/common/handlers/handleRewardV2.ts +++ b/subgraphs/uniswap-forks/protocols/trader-joe/src/common/handlers/handleRewardV2.ts @@ -39,9 +39,9 @@ export function updateMasterChef( return; } - const rewardToken = getOrCreateToken(NetworkConfigs.getRewardToken()); + const rewardToken = getOrCreateToken(event, NetworkConfigs.getRewardToken()); pool.rewardTokens = [ - getOrCreateRewardToken(NetworkConfigs.getRewardToken()).id, + getOrCreateRewardToken(event, NetworkConfigs.getRewardToken()).id, ]; // Calculate Reward Emission per second to a specific pool diff --git a/subgraphs/uniswap-forks/protocols/trader-joe/src/common/handlers/handleRewardV3.ts b/subgraphs/uniswap-forks/protocols/trader-joe/src/common/handlers/handleRewardV3.ts index 06e6f5b640..7b48e327fb 100644 --- a/subgraphs/uniswap-forks/protocols/trader-joe/src/common/handlers/handleRewardV3.ts +++ b/subgraphs/uniswap-forks/protocols/trader-joe/src/common/handlers/handleRewardV3.ts @@ -41,9 +41,9 @@ export function updateMasterChef( return; } - const rewardToken = getOrCreateToken(NetworkConfigs.getRewardToken()); + const rewardToken = getOrCreateToken(event, NetworkConfigs.getRewardToken()); pool.rewardTokens = [ - getOrCreateRewardToken(NetworkConfigs.getRewardToken()).id, + getOrCreateRewardToken(event, NetworkConfigs.getRewardToken()).id, ]; // Get the amount of Joe tokens emitted for all pools per second. diff --git a/subgraphs/uniswap-forks/protocols/trader-joe/src/common/handlers/handleRewarder.ts b/subgraphs/uniswap-forks/protocols/trader-joe/src/common/handlers/handleRewarder.ts index 90d10c37b6..105c9de30f 100644 --- a/subgraphs/uniswap-forks/protocols/trader-joe/src/common/handlers/handleRewarder.ts +++ b/subgraphs/uniswap-forks/protocols/trader-joe/src/common/handlers/handleRewarder.ts @@ -49,6 +49,7 @@ export class PoolRewardData { // when a masterchef staking pool is added or updated. It will not update // pool reward tokens, since that will happen on every deposit/withdrawal from the pool. export function setPoolRewarder( + event: ethereum.Event, addr: Address, pool: _MasterChefStakingPool ): void { @@ -57,14 +58,18 @@ export function setPoolRewarder( return; } - replaceRewarder(addr, pool); + replaceRewarder(event, addr, pool); } // replaceRewarder will remove the current rewarder of a staking pool and add a new one. // Token emissions will be updated on the next deposit/withdrawal after the replacement. -function replaceRewarder(addr: Address, pool: _MasterChefStakingPool): void { +function replaceRewarder( + event: ethereum.Event, + addr: Address, + pool: _MasterChefStakingPool +): void { removeRewarder(pool); - addRewarder(addr, pool); + addRewarder(event, addr, pool); } // removeRewarder will remove the rewarder assigned to some staking pool (if any). @@ -75,8 +80,12 @@ function removeRewarder(sPool: _MasterChefStakingPool): void { // addRewarder adds a rewarder to a given staking pool and updates the pool // reward token and emission values. -function addRewarder(addr: Address, sPool: _MasterChefStakingPool): void { - const rewarder = getOrCreateRewarder(addr, sPool); +function addRewarder( + event: ethereum.Event, + addr: Address, + sPool: _MasterChefStakingPool +): void { + const rewarder = getOrCreateRewarder(event, addr, sPool); sPool.rewarder = rewarder.id; } @@ -95,7 +104,7 @@ export function getPoolRewardsWithBonus( } const rewarderAddr = Address.fromString(sPool.rewarder!); - const rewarder = getOrCreateRewarder(rewarderAddr, sPool); + const rewarder = getOrCreateRewarder(event, rewarderAddr, sPool); const calc = new RewarderCalculator(mc, rewarder); calc.calculateRewarderRate(event, user, isDeposit); @@ -144,8 +153,11 @@ function buildRewards( rewarder: _MasterChefRewarder, rewarderHasFunds: boolean ): PoolRewardData { - const rewardToken = getOrCreateRewardToken(NetworkConfigs.getRewardToken()); - const bonusToken = getOrCreateRewardToken(rewarder.rewardToken); + const rewardToken = getOrCreateRewardToken( + event, + NetworkConfigs.getRewardToken() + ); + const bonusToken = getOrCreateRewardToken(event, rewarder.rewardToken); const bonus = calculateBonusRewardAmounts(event, rewarder, rewarderHasFunds); @@ -202,7 +214,7 @@ function calculateBonusRewardAmounts( RewardIntervalType.TIMESTAMP ); - const bonusToken = getOrCreateToken(rewarder.rewardToken); + const bonusToken = getOrCreateToken(event, rewarder.rewardToken); const bonusAmount = BigInt.fromString( roundToWholeNumber(bonusRewards).toString() @@ -219,6 +231,7 @@ function calculateBonusRewardAmounts( } function getOrCreateRewarder( + event: ethereum.Event, addr: Address, pool: _MasterChefStakingPool | null ): _MasterChefRewarder { @@ -232,7 +245,7 @@ function getOrCreateRewarder( } const c = Rewarder.bind(addr); - const token = getOrCreateRewardToken(c.rewardToken().toHexString()); + const token = getOrCreateRewardToken(event, c.rewardToken().toHexString()); rewarder = new _MasterChefRewarder(addr.toHexString()); rewarder.pool = pool!.id; diff --git a/subgraphs/uniswap-forks/protocols/trader-joe/src/mappings/masterchef/rewardV2.ts b/subgraphs/uniswap-forks/protocols/trader-joe/src/mappings/masterchef/rewardV2.ts index ddd5d6732d..18296967b9 100644 --- a/subgraphs/uniswap-forks/protocols/trader-joe/src/mappings/masterchef/rewardV2.ts +++ b/subgraphs/uniswap-forks/protocols/trader-joe/src/mappings/masterchef/rewardV2.ts @@ -68,7 +68,7 @@ export function handleAdd(event: Add): void { masterChefV2Pool.poolAllocPoint = event.params.allocPoint; if (event.params.rewarder.toHexString() != ZERO_ADDRESS) { - setPoolRewarder(event.params.rewarder, masterChefV2Pool); + setPoolRewarder(event, event.params.rewarder, masterChefV2Pool); } masterChefV2Pool.save(); @@ -88,7 +88,7 @@ export function handleSet(event: Set): void { masterChefV2Pool.poolAllocPoint = event.params.allocPoint; if (event.params.overwrite) { - setPoolRewarder(event.params.rewarder, masterChefV2Pool); + setPoolRewarder(event, event.params.rewarder, masterChefV2Pool); } masterChefV2Pool.save(); diff --git a/subgraphs/uniswap-forks/protocols/trader-joe/src/mappings/masterchef/rewardV3.ts b/subgraphs/uniswap-forks/protocols/trader-joe/src/mappings/masterchef/rewardV3.ts index 55ec144afd..6a3eea33b1 100644 --- a/subgraphs/uniswap-forks/protocols/trader-joe/src/mappings/masterchef/rewardV3.ts +++ b/subgraphs/uniswap-forks/protocols/trader-joe/src/mappings/masterchef/rewardV3.ts @@ -66,7 +66,7 @@ export function handleAdd(event: Add): void { masterChefV3Pool.poolAllocPoint = event.params.allocPoint; if (event.params.rewarder.toHexString() != ZERO_ADDRESS) { - setPoolRewarder(event.params.rewarder, masterChefV3Pool); + setPoolRewarder(event, event.params.rewarder, masterChefV3Pool); } masterChefV3Pool.save(); } @@ -85,7 +85,7 @@ export function handleSet(event: Set): void { masterChefV3Pool.poolAllocPoint = event.params.allocPoint; if (event.params.overwrite) { - setPoolRewarder(event.params.rewarder, masterChefV3Pool); + setPoolRewarder(event, event.params.rewarder, masterChefV3Pool); } masterChefV3Pool.save(); diff --git a/subgraphs/uniswap-forks/protocols/trisolaris/src/common/handlers/handleReward.ts b/subgraphs/uniswap-forks/protocols/trisolaris/src/common/handlers/handleReward.ts index 989649de46..8aa22ecf14 100644 --- a/subgraphs/uniswap-forks/protocols/trisolaris/src/common/handlers/handleReward.ts +++ b/subgraphs/uniswap-forks/protocols/trisolaris/src/common/handlers/handleReward.ts @@ -64,9 +64,9 @@ export function handleReward( return; } - const rewardToken = getOrCreateToken(NetworkConfigs.getRewardToken()); + const rewardToken = getOrCreateToken(event, NetworkConfigs.getRewardToken()); pool.rewardTokens = [ - getOrCreateRewardToken(NetworkConfigs.getRewardToken()).id, + getOrCreateRewardToken(event, NetworkConfigs.getRewardToken()).id, ]; // Update staked amounts diff --git a/subgraphs/uniswap-forks/protocols/trisolaris/src/common/handlers/handleRewardV2.ts b/subgraphs/uniswap-forks/protocols/trisolaris/src/common/handlers/handleRewardV2.ts index 09e9458446..cc404a1a44 100644 --- a/subgraphs/uniswap-forks/protocols/trisolaris/src/common/handlers/handleRewardV2.ts +++ b/subgraphs/uniswap-forks/protocols/trisolaris/src/common/handlers/handleRewardV2.ts @@ -37,9 +37,9 @@ export function updateMasterChef( return; } - const rewardToken = getOrCreateToken(NetworkConfigs.getRewardToken()); + const rewardToken = getOrCreateToken(event, NetworkConfigs.getRewardToken()); pool.rewardTokens = [ - getOrCreateRewardToken(NetworkConfigs.getRewardToken()).id, + getOrCreateRewardToken(event, NetworkConfigs.getRewardToken()).id, ]; // Calculate Reward Emission per second to a specific pool diff --git a/subgraphs/uniswap-forks/protocols/ubeswap/src/common/helpers.ts b/subgraphs/uniswap-forks/protocols/ubeswap/src/common/helpers.ts index 04f403b3dc..572da6b85f 100644 --- a/subgraphs/uniswap-forks/protocols/ubeswap/src/common/helpers.ts +++ b/subgraphs/uniswap-forks/protocols/ubeswap/src/common/helpers.ts @@ -76,7 +76,7 @@ export function getOrCreateMasterChefStakingPool( const pool = LiquidityPool.load(masterChefPool.poolAddress!); if (pool) { pool.rewardTokens = [ - getOrCreateRewardToken(NetworkConfigs.getRewardToken()).id, + getOrCreateRewardToken(event, NetworkConfigs.getRewardToken()).id, ]; pool.save(); } @@ -141,7 +141,7 @@ export function updateRewardEmissions(event: ethereum.Event): void { // Divide the rewards for this period by 7 because each period lasts one week. const poolRewardDailyAverage = poolRewardForPeriod.div(ONE_WEEK_IN_DAYS); - const rewardToken = getOrCreateToken(NetworkConfigs.getRewardToken()); + const rewardToken = getOrCreateToken(event, NetworkConfigs.getRewardToken()); // Update the emissions amount in quantity and USD value. pool.rewardTokenEmissionsAmount = [poolRewardDailyAverage]; diff --git a/subgraphs/uniswap-forks/protocols/vvs-finance/src/common/handlers/handleReward.ts b/subgraphs/uniswap-forks/protocols/vvs-finance/src/common/handlers/handleReward.ts index 2f57c1dd46..4a7a2afb7b 100644 --- a/subgraphs/uniswap-forks/protocols/vvs-finance/src/common/handlers/handleReward.ts +++ b/subgraphs/uniswap-forks/protocols/vvs-finance/src/common/handlers/handleReward.ts @@ -61,9 +61,9 @@ export function handleReward( return; } - const rewardToken = getOrCreateToken(NetworkConfigs.getRewardToken()); + const rewardToken = getOrCreateToken(event, NetworkConfigs.getRewardToken()); pool.rewardTokens = [ - getOrCreateRewardToken(NetworkConfigs.getRewardToken()).id, + getOrCreateRewardToken(event, NetworkConfigs.getRewardToken()).id, ]; // Update staked amounts diff --git a/subgraphs/uniswap-forks/schema.graphql b/subgraphs/uniswap-forks/schema.graphql index 2d538630ab..3441918d5e 100644 --- a/subgraphs/uniswap-forks/schema.graphql +++ b/subgraphs/uniswap-forks/schema.graphql @@ -53,6 +53,16 @@ type Token @entity { " Optional field to track the block number of the last token price " lastPriceBlockNumber: BigInt + + " amount of tokens in the protocol " + _totalSupply: BigInt! + + " Total value locked in the protocol " + _totalValueLockedUSD: BigDecimal! + + _largePriceChangeBuffer: Int! + + _largeTVLImpactBuffer: Int! } enum RewardTokenType { diff --git a/subgraphs/uniswap-forks/src/common/constants.ts b/subgraphs/uniswap-forks/src/common/constants.ts index 886670e72f..c75f3dc996 100644 --- a/subgraphs/uniswap-forks/src/common/constants.ts +++ b/subgraphs/uniswap-forks/src/common/constants.ts @@ -139,6 +139,7 @@ export const INT_ONE = 1 as i32; export const INT_TWO = 2 as i32; export const INT_FOUR = 4 as i32; +export const BIGDECIMAL_NEG_ONE = new BigDecimal(BIGINT_NEG_ONE); export const BIGDECIMAL_ZERO = new BigDecimal(BIGINT_ZERO); export const BIGDECIMAL_ONE = new BigDecimal(BIGINT_ONE); export const BIGDECIMAL_TWO = new BigDecimal(BIGINT_TWO); @@ -182,3 +183,9 @@ export const MINIMUM_LIQUIDITY_FOUR_HUNDRED_THOUSAND = new BigDecimal( ); export const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"; + +export const PRICE_CHANGE_BUFFER_LIMIT = 5 as i32; +export const BIGDECIMAL_TEN_BILLION = new BigDecimal( + BigInt.fromString("10000000000") +); +export const BIGDECIMAL_FIVE_PERCENT = BigDecimal.fromString("0.05"); diff --git a/subgraphs/uniswap-forks/src/common/creators.ts b/subgraphs/uniswap-forks/src/common/creators.ts index 4e0e6b54c5..b95bb142db 100644 --- a/subgraphs/uniswap-forks/src/common/creators.ts +++ b/subgraphs/uniswap-forks/src/common/creators.ts @@ -106,8 +106,8 @@ export function createLiquidityPool( const protocol = getOrCreateProtocol(); // create the tokens and tokentracker - const token0 = getOrCreateToken(token0Address); - const token1 = getOrCreateToken(token1Address); + const token0 = getOrCreateToken(event, token0Address); + const token1 = getOrCreateToken(event, token1Address); const LPtoken = getOrCreateLPToken(poolAddress, token0, token1); updateTokenWhitelists(token0, token1, poolAddress); @@ -181,8 +181,23 @@ export function createDeposit( event.block.number ); - const token0 = getOrCreateToken(pool.inputTokens[INT_ZERO]); - const token1 = getOrCreateToken(pool.inputTokens[INT_ONE]); + const token0 = getOrCreateToken(event, pool.inputTokens[INT_ZERO]); + const token1 = getOrCreateToken(event, pool.inputTokens[INT_ONE]); + + token0._totalSupply = token0._totalSupply.plus(amount0); + token1._totalSupply = token1._totalSupply.plus(amount1); + + token0._totalValueLockedUSD = convertTokenToDecimal( + token0._totalSupply, + token0.decimals + ).times(token0.lastPriceUSD!); + token1._totalValueLockedUSD = convertTokenToDecimal( + token1._totalSupply, + token1.decimals + ).times(token1.lastPriceUSD!); + + token0.save(); + token1.save(); // update exchange info (except balances, sync will cover that) const token0Amount = convertTokenToDecimal(amount0, token0.decimals); @@ -228,8 +243,23 @@ export function createWithdraw( event.block.number ); - const token0 = getOrCreateToken(pool.inputTokens[INT_ZERO]); - const token1 = getOrCreateToken(pool.inputTokens[INT_ONE]); + const token0 = getOrCreateToken(event, pool.inputTokens[INT_ZERO]); + const token1 = getOrCreateToken(event, pool.inputTokens[INT_ONE]); + + token0._totalSupply = token0._totalSupply.minus(amount0); + token1._totalSupply = token1._totalSupply.minus(amount1); + + token0._totalValueLockedUSD = convertTokenToDecimal( + token0._totalSupply, + token0.decimals + ).times(token0.lastPriceUSD!); + token1._totalValueLockedUSD = convertTokenToDecimal( + token1._totalSupply, + token1.decimals + ).times(token1.lastPriceUSD!); + + token0.save(); + token1.save(); // update exchange info (except balances, sync will cover that) const token0Amount = convertTokenToDecimal(amount0, token0.decimals); @@ -291,13 +321,28 @@ export function createSwapHandleVolumeAndFees( ); const poolAmounts = getLiquidityPoolAmounts(event.address.toHexString()); - const token0 = getOrCreateToken(pool.inputTokens[0]); - const token1 = getOrCreateToken(pool.inputTokens[1]); + const token0 = getOrCreateToken(event, pool.inputTokens[0]); + const token1 = getOrCreateToken(event, pool.inputTokens[1]); // totals for volume updates const amount0 = amount0In.minus(amount0Out); const amount1 = amount1In.minus(amount1Out); + token0._totalSupply = token0._totalSupply.plus(amount0); + token1._totalSupply = token1._totalSupply.plus(amount1); + + token0._totalValueLockedUSD = convertTokenToDecimal( + token0._totalSupply, + token0.decimals + ).times(token0.lastPriceUSD!); + token1._totalValueLockedUSD = convertTokenToDecimal( + token1._totalSupply, + token1.decimals + ).times(token1.lastPriceUSD!); + + token0.save(); + token1.save(); + // Gets the tokenIn and tokenOut payload based on the amounts const swapTokens = getSwapTokens( token0, diff --git a/subgraphs/uniswap-forks/src/common/getters.ts b/subgraphs/uniswap-forks/src/common/getters.ts index fde2504324..d994e939fe 100644 --- a/subgraphs/uniswap-forks/src/common/getters.ts +++ b/subgraphs/uniswap-forks/src/common/getters.ts @@ -1,7 +1,7 @@ // import { log } from "@graphprotocol/graph-ts"; import { Address, BigInt, ethereum } from "@graphprotocol/graph-ts"; import { NetworkConfigs } from "../../configurations/configure"; -import { TokenABI } from "../../generated/Factory/TokenABI"; +import { TokenABI as ERC20 } from "../../generated/Factory/TokenABI"; import { DexAmmProtocol, LiquidityPool, @@ -27,8 +27,10 @@ import { RewardTokenType, BIGINT_ZERO, SECONDS_PER_HOUR, + BIGINT_TEN, } from "./constants"; import { createPoolFees } from "./creators"; +import { findUSDPricePerToken } from "../price/price"; export function getOrCreateProtocol(): DexAmmProtocol { let protocol = DexAmmProtocol.load(NetworkConfigs.getFactoryAddress()); @@ -272,22 +274,15 @@ export function getOrCreateFinancialsDailySnapshot( return financialMetrics; } -export function getOrCreateToken(address: string): Token { +export function getOrCreateToken( + event: ethereum.Event, + address: string, + getNewPrice: boolean = true +): Token { let token = Token.load(address); if (!token) { token = new Token(address); - - token.lastPriceUSD = BIGDECIMAL_ZERO; - token.lastPriceBlockNumber = BIGINT_ZERO; - if (NetworkConfigs.getBrokenERC20Tokens().includes(address)) { - token.name = ""; - token.symbol = ""; - token.decimals = DEFAULT_DECIMALS; - token.save(); - - return token as Token; - } - const erc20Contract = TokenABI.bind(Address.fromString(address)); + const erc20Contract = ERC20.bind(Address.fromString(address)); const decimals = erc20Contract.try_decimals(); // Using try_cause some values might be missing const name = erc20Contract.try_name(); @@ -296,9 +291,36 @@ export function getOrCreateToken(address: string): Token { token.decimals = decimals.reverted ? DEFAULT_DECIMALS : decimals.value; token.name = name.reverted ? "" : name.value; token.symbol = symbol.reverted ? "" : symbol.value; + if (NetworkConfigs.getBrokenERC20Tokens().includes(address)) { + token.name = ""; + token.symbol = ""; + token.decimals = DEFAULT_DECIMALS; + token.save(); + return token as Token; + } + token.lastPriceUSD = BIGDECIMAL_ZERO; + token.lastPriceBlockNumber = BIGINT_ZERO; + token._totalSupply = BIGINT_ZERO; + token._totalValueLockedUSD = BIGDECIMAL_ZERO; + token._largeTVLImpactBuffer = 0; + token._largePriceChangeBuffer = 0; + + token.save(); + } + + if ( + token.lastPriceBlockNumber! && + event.block.number.minus(token.lastPriceBlockNumber!).gt(BIGINT_TEN) && + getNewPrice + ) { + const newPrice = findUSDPricePerToken(event, token); + + token.lastPriceUSD = newPrice; + token.lastPriceBlockNumber = event.block.number; token.save(); } + return token as Token; } @@ -316,15 +338,22 @@ export function getOrCreateLPToken( token.decimals = DEFAULT_DECIMALS; token.lastPriceUSD = BIGDECIMAL_ZERO; token.lastPriceBlockNumber = BIGINT_ZERO; + token._totalSupply = BIGINT_ZERO; + token._totalValueLockedUSD = BIGDECIMAL_ZERO; + token._largeTVLImpactBuffer = 0; + token._largePriceChangeBuffer = 0; token.save(); } return token; } -export function getOrCreateRewardToken(address: string): RewardToken { +export function getOrCreateRewardToken( + event: ethereum.Event, + address: string +): RewardToken { let rewardToken = RewardToken.load(address); if (rewardToken == null) { - const token = getOrCreateToken(address); + const token = getOrCreateToken(event, address); rewardToken = new RewardToken(RewardTokenType.DEPOSIT + "-" + address); rewardToken.token = token.id; rewardToken.type = RewardTokenType.DEPOSIT; diff --git a/subgraphs/uniswap-forks/src/common/masterchef/helpers.ts b/subgraphs/uniswap-forks/src/common/masterchef/helpers.ts index e8926782c9..f45b8859d9 100644 --- a/subgraphs/uniswap-forks/src/common/masterchef/helpers.ts +++ b/subgraphs/uniswap-forks/src/common/masterchef/helpers.ts @@ -26,7 +26,9 @@ export function createMasterChefStakingPool( const pool = LiquidityPool.load(masterChefPool.poolAddress!); if (pool) { - pool.rewardTokens = [getOrCreateToken(NetworkConfigs.getRewardToken()).id]; + pool.rewardTokens = [ + getOrCreateToken(event, NetworkConfigs.getRewardToken()).id, + ]; pool.save(); } diff --git a/subgraphs/uniswap-forks/src/common/updateMetrics.ts b/subgraphs/uniswap-forks/src/common/updateMetrics.ts index 467f95f895..bf2c8c42da 100644 --- a/subgraphs/uniswap-forks/src/common/updateMetrics.ts +++ b/subgraphs/uniswap-forks/src/common/updateMetrics.ts @@ -32,10 +32,6 @@ import { UsageType, } from "./constants"; import { convertTokenToDecimal, percToDec } from "./utils/utils"; -import { - findUSDPricePerToken, - updateNativeTokenPriceInUSD, -} from "../price/price"; import { NetworkConfigs } from "../../configurations/configure"; // Update FinancialsDailySnapshots entity @@ -206,16 +202,16 @@ export function updateTokenWhitelists( // Upate token balances based on reserves emitted from the Sync event. export function updateInputTokenBalances( + event: ethereum.Event, poolAddress: string, reserve0: BigInt, - reserve1: BigInt, - blockNumber: BigInt + reserve1: BigInt ): void { - const pool = getLiquidityPool(poolAddress, blockNumber); + const pool = getLiquidityPool(poolAddress, event.block.number); const poolAmounts = getLiquidityPoolAmounts(poolAddress); - const token0 = getOrCreateToken(pool.inputTokens[INT_ZERO]); - const token1 = getOrCreateToken(pool.inputTokens[INT_ONE]); + const token0 = getOrCreateToken(event, pool.inputTokens[INT_ZERO]); + const token1 = getOrCreateToken(event, pool.inputTokens[INT_ONE]); const tokenDecimal0 = convertTokenToDecimal(reserve0, token0.decimals); const tokenDecimal1 = convertTokenToDecimal(reserve1, token1.decimals); @@ -229,20 +225,15 @@ export function updateInputTokenBalances( // Update tvl an token prices in the Sync event. export function updateTvlAndTokenPrices( - poolAddress: string, - blockNumber: BigInt + event: ethereum.Event, + poolAddress: string ): void { - const pool = getLiquidityPool(poolAddress, blockNumber); + const pool = getLiquidityPool(poolAddress, event.block.number); const protocol = getOrCreateProtocol(); - const token0 = getOrCreateToken(pool.inputTokens[0]); - const token1 = getOrCreateToken(pool.inputTokens[1]); - - const nativeToken = updateNativeTokenPriceInUSD(); - - token0.lastPriceUSD = findUSDPricePerToken(token0, nativeToken, blockNumber); - token1.lastPriceUSD = findUSDPricePerToken(token1, nativeToken, blockNumber); + const token0 = getOrCreateToken(event, pool.inputTokens[0]); + const token1 = getOrCreateToken(event, pool.inputTokens[1]); // Subtract the old pool tvl protocol.totalValueLockedUSD = protocol.totalValueLockedUSD.minus( @@ -283,7 +274,6 @@ export function updateTvlAndTokenPrices( protocol.save(); token0.save(); token1.save(); - nativeToken.save(); } // Update the volume and fees from financial metrics snapshot, pool metrics snapshot, protocol, and pool entities. diff --git a/subgraphs/uniswap-forks/src/common/utils/utils.ts b/subgraphs/uniswap-forks/src/common/utils/utils.ts index 7232ed750a..8f9e052991 100644 --- a/subgraphs/uniswap-forks/src/common/utils/utils.ts +++ b/subgraphs/uniswap-forks/src/common/utils/utils.ts @@ -1,6 +1,7 @@ import { BigInt, BigDecimal, Bytes, ethereum } from "@graphprotocol/graph-ts"; import { BIGDECIMAL_HUNDRED, + BIGDECIMAL_NEG_ONE, BIGDECIMAL_ONE, BIGDECIMAL_TEN, BIGDECIMAL_ZERO, @@ -91,3 +92,10 @@ export function isSameSign(a: BigInt, b: BigInt): boolean { } return false; } + +export function absBigDecimal(value: BigDecimal): BigDecimal { + if (value.lt(BIGDECIMAL_ZERO)) { + return value.times(BIGDECIMAL_NEG_ONE); + } + return value; +} diff --git a/subgraphs/uniswap-forks/src/mappings/pool.ts b/subgraphs/uniswap-forks/src/mappings/pool.ts index 1a5ad46daf..1d512b02c7 100644 --- a/subgraphs/uniswap-forks/src/mappings/pool.ts +++ b/subgraphs/uniswap-forks/src/mappings/pool.ts @@ -82,12 +82,12 @@ export function handleTransfer(event: Transfer): void { // Gives information about the rebalancing of tokens used to update tvl, balances, and token pricing export function handleSync(event: Sync): void { updateInputTokenBalances( + event, event.address.toHexString(), event.params.reserve0, - event.params.reserve1, - event.block.number + event.params.reserve1 ); - updateTvlAndTokenPrices(event.address.toHexString(), event.block.number); + updateTvlAndTokenPrices(event, event.address.toHexString()); } // Handle a mint event emitted from a pool contract. Considered a deposit into the given liquidity pool. diff --git a/subgraphs/uniswap-forks/src/price/price.ts b/subgraphs/uniswap-forks/src/price/price.ts index 90dfc3118f..48f573c896 100644 --- a/subgraphs/uniswap-forks/src/price/price.ts +++ b/subgraphs/uniswap-forks/src/price/price.ts @@ -1,11 +1,13 @@ -import { BigDecimal, BigInt } from "@graphprotocol/graph-ts/index"; +import { BigDecimal, ethereum, log } from "@graphprotocol/graph-ts/index"; import { getLiquidityPool, getLiquidityPoolAmounts, + getOrCreateProtocol, getOrCreateToken, getOrCreateTokenWhitelist, } from "../common/getters"; import { + LiquidityPool, Token, _HelperStore, _LiquidityPoolAmount, @@ -15,17 +17,23 @@ import { BIGDECIMAL_ONE, BIGDECIMAL_TWO, BIGINT_ZERO, + PRICE_CHANGE_BUFFER_LIMIT, + BIGDECIMAL_TEN_BILLION, + BIGDECIMAL_FIVE_PERCENT, } from "../common/constants"; -import { safeDiv } from "../common/utils/utils"; +import { + absBigDecimal, + convertTokenToDecimal, + safeDiv, +} from "../common/utils/utils"; import { NetworkConfigs } from "../../configurations/configure"; // Update the token price of the native token for the specific protocol/network (see network specific configs) // Update the token by referencing the native token against pools with the reference token and a stable coin // Estimate the price against the pool with the highest liquidity -export function updateNativeTokenPriceInUSD(): Token { +export function getNativeTokenPriceInUSD(nativeToken: Token): BigDecimal { let nativeAmount = BIGDECIMAL_ZERO; let stableAmount = BIGDECIMAL_ZERO; - const nativeToken = getOrCreateToken(NetworkConfigs.getReferenceToken()); // fetch average price of NATIVE_TOKEN_ADDRESS from STABLE_ORACLES for (let i = 0; i < NetworkConfigs.getStableOraclePools().length; i++) { const pool = _LiquidityPoolAmount.load( @@ -45,7 +53,8 @@ export function updateNativeTokenPriceInUSD(): Token { } } nativeToken.lastPriceUSD = safeDiv(stableAmount, nativeAmount); - return nativeToken; + nativeToken.save(); + return nativeToken.lastPriceUSD!; } /** @@ -53,12 +62,11 @@ export function updateNativeTokenPriceInUSD(): Token { * You can find the possible whitelisted tokens used for comparision in the network configuration typescript file. **/ export function findUSDPricePerToken( - token: Token, - nativeToken: Token, - blockNumber: BigInt + event: ethereum.Event, + token: Token ): BigDecimal { if (token.id == NetworkConfigs.getReferenceToken()) { - return nativeToken.lastPriceUSD!; + return getNativeTokenPriceInUSD(token); } const tokenWhitelist = getOrCreateTokenWhitelist(token.id); const whiteList = tokenWhitelist.whitelistPools; @@ -77,54 +85,122 @@ export function findUSDPricePerToken( for (let i = 0; i < whiteList.length; ++i) { const poolAddress = whiteList[i]; const poolAmounts = getLiquidityPoolAmounts(poolAddress); - const pool = getLiquidityPool(poolAddress, blockNumber); + const pool = getLiquidityPool(poolAddress, event.block.number); if (pool.outputTokenSupply!.gt(BIGINT_ZERO)) { - if ( - pool.inputTokens[0] == token.id && - pool.totalValueLockedUSD.gt( - NetworkConfigs.getMinimumLiquidityThresholdTrackPrice() - ) - ) { - // whitelist token is token1 - const whitelistToken = getOrCreateToken(pool.inputTokens[1]); - // get the derived NativeToken in pool - const whitelistTokenLocked = poolAmounts.inputTokenBalances[1].times( - whitelistToken.lastPriceUSD! - ); - if (whitelistTokenLocked.gt(largestLiquidityWhitelistTokens)) { - largestLiquidityWhitelistTokens = whitelistTokenLocked; - // token1 per our token * nativeToken per token1 - priceSoFar = safeDiv( - poolAmounts.inputTokenBalances[1], - poolAmounts.inputTokenBalances[0] - ).times(whitelistToken.lastPriceUSD! as BigDecimal); - } + const priceTokenIndex: i32 = get_token_index(pool, token); + if (priceTokenIndex == -1) { + continue; } + const whitelistTokenIndex = 0 == priceTokenIndex ? 1 : 0; + + const whitelistToken = getOrCreateToken( + event, + pool.inputTokens[whitelistTokenIndex], + false + ); + const whitelistTokenLocked = poolAmounts.inputTokenBalances[ + whitelistTokenIndex + ].times(whitelistToken.lastPriceUSD!); if ( - pool.inputTokens[1] == token.id && - pool.totalValueLockedUSD.gt( + whitelistTokenLocked.gt(largestLiquidityWhitelistTokens) && + whitelistTokenLocked.gt( NetworkConfigs.getMinimumLiquidityThresholdTrackPrice() ) ) { - const whitelistToken = getOrCreateToken(pool.inputTokens[0]); - // get the derived nativeToken in pool - const whitelistTokenLocked = poolAmounts.inputTokenBalances[0].times( - whitelistToken.lastPriceUSD! - ); - if (whitelistTokenLocked.gt(largestLiquidityWhitelistTokens)) { - largestLiquidityWhitelistTokens = whitelistTokenLocked; - // token0 per our token * NativeToken per token0 - priceSoFar = safeDiv( - poolAmounts.inputTokenBalances[0], - poolAmounts.inputTokenBalances[1] - ).times(whitelistToken.lastPriceUSD! as BigDecimal); + largestLiquidityWhitelistTokens = whitelistTokenLocked; + // token1 per our token * nativeToken per token1 + + const newPrice = safeDiv( + poolAmounts.inputTokenBalances[whitelistTokenIndex], + poolAmounts.inputTokenBalances[priceTokenIndex] + ).times(whitelistToken.lastPriceUSD! as BigDecimal); + + if (isValidTVL(pool, token, whitelistToken.lastPriceUSD!)) { + priceSoFar = newPrice; } } } } } - return priceSoFar; // nothing was found return 0 + + // Buffer token pricings that would cause large spikes on the protocol level + const protocol = getOrCreateProtocol(); + const tokenTVLDelta = absBigDecimal( + priceSoFar + .times(convertTokenToDecimal(token._totalSupply, token.decimals)) + .minus(token._totalValueLockedUSD) + ); + const protocolTVLPercentageDelta = absBigDecimal( + safeDiv(tokenTVLDelta, protocol.totalValueLockedUSD) + ); + if (protocolTVLPercentageDelta.gt(BIGDECIMAL_FIVE_PERCENT)) { + log.warning("Price too high for token: {} from pool: {}", [ + token.id, + priceSoFar.toString(), + ]); + if (token._largeTVLImpactBuffer < PRICE_CHANGE_BUFFER_LIMIT) { + token._largeTVLImpactBuffer += 1; + token.save(); + return token.lastPriceUSD!; + } + } + + if (!token.lastPriceUSD || token.lastPriceUSD!.equals(BIGDECIMAL_ZERO)) { + token.save(); + return priceSoFar; + } + + // If priceSoFar 10x greater or less than token.lastPriceUSD, use token.lastPriceUSD + // Increment buffer so that it allows large price jumps if seen repeatedly + if ( + priceSoFar.gt(token.lastPriceUSD!.times(BIGDECIMAL_TWO)) || + priceSoFar.lt(token.lastPriceUSD!.div(BIGDECIMAL_TWO)) + ) { + if (token._largePriceChangeBuffer < PRICE_CHANGE_BUFFER_LIMIT) { + token._largePriceChangeBuffer += 1; + token.save(); + return token.lastPriceUSD!; + } + } + + token._largePriceChangeBuffer = 0; + token._largeTVLImpactBuffer = 0; + + token.save(); + return priceSoFar; +} + +// Tried to return null from here and it did not +function get_token_index(pool: LiquidityPool, token: Token): i32 { + if (pool.inputTokens[0] == token.id) { + return 0; + } + if (pool.inputTokens[1] == token.id) { + return 1; + } + return -1; +} + +function isValidTVL( + pool: LiquidityPool, + token: Token, + price: BigDecimal +): bool { + const newTokenTotalValueLocked = convertTokenToDecimal( + token._totalSupply, + token.decimals + ).times(price); + + // If price is too high, skip this pool + if (newTokenTotalValueLocked.gt(BIGDECIMAL_TEN_BILLION)) { + log.warning("Price too high for token: {} from pool: {}", [ + token.id, + pool.id, + ]); + return false; + } + return true; } /** diff --git a/subgraphs/uniswap-forks/src/versions.ts b/subgraphs/uniswap-forks/src/versions.ts index f955e28520..801fde1ec6 100644 --- a/subgraphs/uniswap-forks/src/versions.ts +++ b/subgraphs/uniswap-forks/src/versions.ts @@ -6,11 +6,11 @@ export class VersionsClass implements VersionsInterface { } getSubgraphVersion(): string { - return "1.0.5"; + return "1.1.11"; } getMethodologyVersion(): string { - return "1.0.1"; + return "1.0.0"; } }