From c107dee60d94ed2ebeeb4eb90a3f77968892098c Mon Sep 17 00:00:00 2001 From: "Jalil F." Date: Sat, 9 Nov 2024 13:02:49 -0500 Subject: [PATCH 1/4] Remove token normalization in general counts. (#180) --- src/EventHandlers/CLPool.ts | 64 ++++++++------ src/EventHandlers/Pool.ts | 36 ++++---- test/EventHandlers/CLPool/CLPool.test.ts | 34 +++----- test/EventHandlers/Pool/Swap.test.ts | 104 +++++++++++++---------- test/EventHandlers/Pool/Sync.test.ts | 12 +-- test/EventHandlers/Pool/common.ts | 10 +-- 6 files changed, 141 insertions(+), 119 deletions(-) diff --git a/src/EventHandlers/CLPool.ts b/src/EventHandlers/CLPool.ts index 23a345f..f5faad2 100644 --- a/src/EventHandlers/CLPool.ts +++ b/src/EventHandlers/CLPool.ts @@ -42,32 +42,34 @@ function updateCLPoolFees( token0Instance: Token | undefined, token1Instance: Token | undefined ) { + let tokenUpdateData = { totalFees0: liquidityPoolAggregator.totalFees0, totalFees1: liquidityPoolAggregator.totalFees1, totalFeesUSD: liquidityPoolAggregator.totalFeesUSD, }; + tokenUpdateData.totalFees0 += event.params.amount0; + tokenUpdateData.totalFees1 += event.params.amount1; + if (token0Instance) { - const incomingFees0 = normalizeTokenAmountTo1e18( + const normalizedFees0 = normalizeTokenAmountTo1e18( event.params.amount0, Number(token0Instance.decimals) ); - tokenUpdateData.totalFees0 += incomingFees0; tokenUpdateData.totalFeesUSD += multiplyBase1e18( - incomingFees0, + normalizedFees0, token0Instance.pricePerUSDNew ); } if (token1Instance) { - const incomingFees1 = normalizeTokenAmountTo1e18( + const normalizedFees1 = normalizeTokenAmountTo1e18( event.params.amount1, Number(token1Instance.decimals) ); - tokenUpdateData.totalFees1 += incomingFees1; tokenUpdateData.totalFeesUSD += multiplyBase1e18( - incomingFees1, + normalizedFees1, token1Instance.pricePerUSDNew ); } @@ -81,24 +83,26 @@ function updateCLPoolLiquidity( token0Instance: Token | undefined, token1Instance: Token | undefined ) { + let tokenUpdateData = { totalLiquidityUSD: 0n, + reserve0: 0n, + reserve1: 0n, normalizedReserve0: 0n, normalizedReserve1: 0n, }; - // Update normalized reserves regardles of whether the token is priced - tokenUpdateData.normalizedReserve0 += normalizeTokenAmountTo1e18( - event.params.amount0, - Number(token0Instance?.decimals || 18) - ); + // Update reserves regardles of whether the token is priced + tokenUpdateData.reserve0 += event.params.amount0; - tokenUpdateData.normalizedReserve1 += normalizeTokenAmountTo1e18( - event.params.amount1, - Number(token1Instance?.decimals || 18) - ); + tokenUpdateData.reserve1 += event.params.amount1; if (token0Instance) { + tokenUpdateData.normalizedReserve0 += normalizeTokenAmountTo1e18( + event.params.amount0, + Number(token0Instance.decimals || 18) + ); + tokenUpdateData.totalLiquidityUSD += multiplyBase1e18( tokenUpdateData.normalizedReserve0, liquidityPoolAggregator.token0Price @@ -106,6 +110,10 @@ function updateCLPoolLiquidity( } if (token1Instance) { + tokenUpdateData.normalizedReserve1 += normalizeTokenAmountTo1e18( + event.params.amount1, + Number(token1Instance.decimals || 18) + ); tokenUpdateData.totalLiquidityUSD += multiplyBase1e18( tokenUpdateData.normalizedReserve1, liquidityPoolAggregator.token1Price @@ -162,9 +170,9 @@ CLPool.Burn.handlerWithLoader({ const liquidityPoolDiff = { reserve0: - liquidityPoolAggregator.reserve0 + tokenUpdateData.normalizedReserve0, + liquidityPoolAggregator.reserve0 + tokenUpdateData.reserve0, reserve1: - liquidityPoolAggregator.reserve1 + tokenUpdateData.normalizedReserve1, + liquidityPoolAggregator.reserve1 + tokenUpdateData.reserve1, totalLiquidityUSD: liquidityPoolAggregator.totalLiquidityUSD + tokenUpdateData.totalLiquidityUSD, @@ -385,9 +393,9 @@ CLPool.Mint.handlerWithLoader({ const liquidityPoolDiff = { reserve0: - liquidityPoolAggregator.reserve0 + tokenUpdateData.normalizedReserve0, + liquidityPoolAggregator.reserve0 + tokenUpdateData.reserve0, reserve1: - liquidityPoolAggregator.reserve1 + tokenUpdateData.normalizedReserve1, + liquidityPoolAggregator.reserve1 + tokenUpdateData.reserve1, totalLiquidityUSD: liquidityPoolAggregator.totalLiquidityUSD + tokenUpdateData.totalLiquidityUSD, @@ -460,6 +468,7 @@ CLPool.Swap.handlerWithLoader({ if (loaderReturn && loaderReturn.liquidityPoolAggregator) { const { liquidityPoolAggregator, token0Instance, token1Instance } = loaderReturn; + // Delta that will be added to the liquidity pool aggregator let tokenUpdateData = { netAmount0: 0n, netAmount1: 0n, @@ -468,26 +477,27 @@ CLPool.Swap.handlerWithLoader({ volumeInUSD: 0n, }; + tokenUpdateData.netAmount0 = abs(event.params.amount0); + tokenUpdateData.netAmount1 = abs(event.params.amount1); + if (token0Instance) { - tokenUpdateData.netAmount0 = normalizeTokenAmountTo1e18( - event.params.amount0, + const normalizedAmount0 = normalizeTokenAmountTo1e18( + abs(event.params.amount0), Number(token0Instance.decimals) ); - tokenUpdateData.netAmount0 = abs(tokenUpdateData.netAmount0); tokenUpdateData.netVolumeToken0USD = multiplyBase1e18( - tokenUpdateData.netAmount0, + normalizedAmount0, token0Instance.pricePerUSDNew ); } if (token1Instance) { - tokenUpdateData.netAmount1 = normalizeTokenAmountTo1e18( - event.params.amount1, + const normalizedAmount1 = normalizeTokenAmountTo1e18( + abs(event.params.amount1), Number(token1Instance.decimals) ); - tokenUpdateData.netAmount1 = abs(tokenUpdateData.netAmount1); tokenUpdateData.netVolumeToken1USD = multiplyBase1e18( - tokenUpdateData.netAmount1, + normalizedAmount1, token1Instance.pricePerUSDNew ); } diff --git a/src/EventHandlers/Pool.ts b/src/EventHandlers/Pool.ts index 635f229..c3f38b2 100644 --- a/src/EventHandlers/Pool.ts +++ b/src/EventHandlers/Pool.ts @@ -69,6 +69,7 @@ Pool.Fees.handlerWithLoader({ totalFeesUSD: 0n, }; + tokenUpdateData.totalFees0 = event.params.amount0 if (token0Instance) { tokenUpdateData.totalFees0 = normalizeTokenAmountTo1e18( event.params.amount0, @@ -80,6 +81,7 @@ Pool.Fees.handlerWithLoader({ ); } + tokenUpdateData.totalFees1 = event.params.amount1 if (token1Instance) { tokenUpdateData.totalFees1 = normalizeTokenAmountTo1e18( event.params.amount1, @@ -159,26 +161,26 @@ Pool.Swap.handlerWithLoader({ volumeInUSD: 0n, }; + tokenUpdateData.netAmount0 = event.params.amount0In + event.params.amount0Out; if (token0Instance) { - tokenUpdateData.netAmount0 = normalizeTokenAmountTo1e18( + const normalizedAmount0 = normalizeTokenAmountTo1e18( event.params.amount0In + event.params.amount0Out, Number(token0Instance.decimals) ); - tokenUpdateData.netAmount0 = abs(tokenUpdateData.netAmount0); tokenUpdateData.netVolumeToken0USD = multiplyBase1e18( - tokenUpdateData.netAmount0, + normalizedAmount0, token0Instance.pricePerUSDNew ); } + tokenUpdateData.netAmount1 = event.params.amount1In + event.params.amount1Out; if (token1Instance) { - tokenUpdateData.netAmount1 = normalizeTokenAmountTo1e18( + const normalizedAmount1 = normalizeTokenAmountTo1e18( event.params.amount1In + event.params.amount1Out, Number(token1Instance.decimals) ); - tokenUpdateData.netAmount1 = abs(tokenUpdateData.netAmount1); tokenUpdateData.netVolumeToken1USD = multiplyBase1e18( - tokenUpdateData.netAmount1, + normalizedAmount1, token1Instance.pricePerUSDNew ); } @@ -295,18 +297,13 @@ Pool.Sync.handlerWithLoader({ token1PricePerUSDNew: token1Instance?.pricePerUSDNew ?? liquidityPool.token1Price, }; - tokenUpdateData.normalizedReserve0 += normalizeTokenAmountTo1e18( - event.params.reserve0, - Number(token0Instance?.decimals || 18) - ); - - tokenUpdateData.normalizedReserve1 += normalizeTokenAmountTo1e18( - event.params.reserve1, - Number(token1Instance?.decimals || 18) - ); // Update price and liquidity if the token is priced if (token0Instance) { + tokenUpdateData.normalizedReserve0 += normalizeTokenAmountTo1e18( + event.params.reserve0, + Number(token0Instance?.decimals || 18) + ); tokenUpdateData.token0PricePerUSDNew = token0Instance.pricePerUSDNew; tokenUpdateData.totalLiquidityUSD += multiplyBase1e18( tokenUpdateData.normalizedReserve0, @@ -315,6 +312,11 @@ Pool.Sync.handlerWithLoader({ } if (token1Instance) { + tokenUpdateData.normalizedReserve1 += normalizeTokenAmountTo1e18( + event.params.reserve1, + Number(token1Instance?.decimals || 18) + ); + tokenUpdateData.token1PricePerUSDNew = token1Instance.pricePerUSDNew; tokenUpdateData.totalLiquidityUSD += multiplyBase1e18( tokenUpdateData.normalizedReserve1, @@ -323,8 +325,8 @@ Pool.Sync.handlerWithLoader({ } const liquidityPoolDiff = { - reserve0: tokenUpdateData.normalizedReserve0, - reserve1: tokenUpdateData.normalizedReserve1, + reserve0: event.params.reserve0, + reserve1: event.params.reserve1, totalLiquidityUSD: tokenUpdateData.totalLiquidityUSD || liquidityPool.totalLiquidityUSD, token0Price: tokenUpdateData.token0PricePerUSDNew, token1Price: tokenUpdateData.token1PricePerUSDNew, diff --git a/test/EventHandlers/CLPool/CLPool.test.ts b/test/EventHandlers/CLPool/CLPool.test.ts index ee4191d..fa1c77e 100644 --- a/test/EventHandlers/CLPool/CLPool.test.ts +++ b/test/EventHandlers/CLPool/CLPool.test.ts @@ -107,11 +107,11 @@ describe("CLPool Event Handlers", () => { expect(diff.reserve0).to.equal( mockLiquidityPoolData.reserve0 + expectations.amount0In, - "Reserve 0 should be appropriately updated and normalized"); + "Reserve 0 should be appropriately updated"); expect(diff.reserve1).to.equal( mockLiquidityPoolData.reserve1 + - expectations.amount1In * (10n ** 18n) / (10n ** 6n), - "Reserve 1 should be appropriately updated and normalized"); + expectations.amount1In, + "Reserve 1 should be appropriately updated"); }); it("should update the total liquidity in USD correctly", () => { expect(diff.totalLiquidityUSD).to.equal( @@ -188,11 +188,11 @@ describe("CLPool Event Handlers", () => { expect(diff.reserve0).to.equal( mockLiquidityPoolData.reserve0 + expectations.amount0In, - "Reserve 0 should be appropriately updated and normalized"); + "Reserve 0 should be appropriately updated"); expect(diff.reserve1).to.equal( mockLiquidityPoolData.reserve1 + - expectations.amount1In * (10n ** 18n) / (10n ** 6n), - "Reserve 1 should be appropriately updated and normalized"); + expectations.amount1In, + "Reserve 1 should be appropriately updated"); }); it("should update the total liquidity in USD correctly", () => { expect(diff.totalLiquidityUSD).to.equal( @@ -270,9 +270,7 @@ describe("CLPool Event Handlers", () => { mockLiquidityPoolData.totalFees0 + eventFees.amount0 ); expect(diff.totalFees1).to.equal( - mockLiquidityPoolData.totalFees1 + - (eventFees.amount1 * 10n ** 18n) / 10n ** 6n, - "It should normalize fees here" + mockLiquidityPoolData.totalFees1 + eventFees.amount1 ); }); }); @@ -351,9 +349,7 @@ describe("CLPool Event Handlers", () => { mockLiquidityPoolData.totalFees0 + eventFees.amount0 ); expect(diff.totalFees1).to.equal( - mockLiquidityPoolData.totalFees1 + - (eventFees.amount1 * 10n ** 18n) / 10n ** 6n, - "It should normalize fees here" + mockLiquidityPoolData.totalFees1 + eventFees.amount1 ); }); @@ -444,10 +440,7 @@ describe("CLPool Event Handlers", () => { mockLiquidityPoolData.totalVolume0 + abs(mockEvent.params.amount0) ); expect(diff.totalVolume1).to.equal( - mockLiquidityPoolData.totalVolume1 + - (abs(mockEvent.params.amount1) * 10n ** 18n) / - 10n ** mockToken1Data.decimals, - "It should normalize the volume 18 decimals. Note here the incoming decimals are 6." + mockLiquidityPoolData.totalVolume1 + abs(mockEvent.params.amount1) ); }); @@ -505,12 +498,11 @@ describe("CLPool Event Handlers", () => { expect(updateLiquidityPoolAggregatorStub.calledOnce).to.be.true; const [diff] = updateLiquidityPoolAggregatorStub.firstCall.args; - expect(diff.totalVolume0).to.equal(mockLiquidityPoolData.totalVolume0); // Unchanged + expect(diff.totalVolume0).to.equal( + mockLiquidityPoolData.totalVolume0 + abs(mockEvent.params.amount0) + ); expect(diff.totalVolume1).to.equal( - mockLiquidityPoolData.totalVolume1 + - (abs(mockEvent.params.amount1) * 10n ** 18n) / - 10n ** mockToken1Data.decimals, - "It should normalize the volume 18 decimals. Note here the incoming decimals are 6." + mockLiquidityPoolData.totalVolume1 + abs(mockEvent.params.amount1) ); expect(diff.totalVolumeUSD).to.equal( mockLiquidityPoolData.totalVolumeUSD + diff --git a/test/EventHandlers/Pool/Swap.test.ts b/test/EventHandlers/Pool/Swap.test.ts index cf227bc..c6a16bf 100644 --- a/test/EventHandlers/Pool/Swap.test.ts +++ b/test/EventHandlers/Pool/Swap.test.ts @@ -34,30 +34,25 @@ describe("Pool Swap Event", () => { expectations.swapAmount0In = 100n * 10n ** mockToken0Data.decimals; expectations.swapAmount1Out = 99n * 10n ** mockToken1Data.decimals; - // The code expects net abounts to be normalized to 1e18 - expectations.expectedNetAmount0 = - expectations.swapAmount0In * (TEN_TO_THE_18_BI / (10n ** mockToken0Data.decimals)); + expectations.expectedNetAmount0 = expectations.swapAmount0In; - expectations.expectedNetAmount1 = - expectations.swapAmount1Out * (TEN_TO_THE_18_BI / (10n ** mockToken1Data.decimals)); + expectations.expectedNetAmount1 = expectations.swapAmount1Out; expectations.totalVolume0 = - mockLiquidityPoolData.totalVolume0 + - expectations.swapAmount0In * (TEN_TO_THE_18_BI / (10n ** mockToken0Data.decimals)); + mockLiquidityPoolData.totalVolume0 + expectations.swapAmount0In; expectations.totalVolume1 = - mockLiquidityPoolData.totalVolume1 + - expectations.swapAmount1Out * (TEN_TO_THE_18_BI / (10n ** mockToken1Data.decimals)); + mockLiquidityPoolData.totalVolume1 + expectations.swapAmount1Out; // The code expects pricePerUSDNew to be normalized to 1e18 expectations.expectedLPVolumeUSD0 = mockLiquidityPoolData.totalVolumeUSD + - expectations.expectedNetAmount0 * + expectations.expectedNetAmount0 * (TEN_TO_THE_18_BI / 10n ** mockToken0Data.decimals) * (mockToken0Data.pricePerUSDNew / TEN_TO_THE_18_BI); expectations.expectedLPVolumeUSD1 = mockLiquidityPoolData.totalVolumeUSD + - expectations.expectedNetAmount1 * + expectations.expectedNetAmount1 * (TEN_TO_THE_18_BI / 10n ** mockToken1Data.decimals) * (mockToken1Data.pricePerUSDNew / TEN_TO_THE_18_BI); mockPriceOracle = sinon @@ -90,8 +85,11 @@ describe("Pool Swap Event", () => { }); describe("when both tokens are missing", () => { - it("should update the liquidity pool with swap count only", async () => { - const modifiedMockLiquidityPoolData = { + let updatedPool: any; + let modifiedMockLiquidityPoolData: any; + beforeEach(async () => { + + modifiedMockLiquidityPoolData = { ...mockLiquidityPoolData, token0_id: TokenIdByChain( "0x9999999999999999999999999999999999999990", @@ -113,30 +111,43 @@ describe("Pool Swap Event", () => { mockDb: updatedDB1, }); - const updatedPool = postEventDB.entities.LiquidityPoolAggregator.get( + updatedPool = postEventDB.entities.LiquidityPoolAggregator.get( toChecksumAddress(eventData.mockEventData.srcAddress) ); + + }); + it("should update the liquidity pool and timestamp", () => { expect(updatedPool).to.not.be.undefined; + expect(updatedPool?.lastUpdatedTimestamp).to.deep.equal( + new Date(eventData.mockEventData.block.timestamp * 1000) + ); + }); + it("should update the liquidity pool with swap count", async () => { + expect(updatedPool?.numberOfSwaps).to + .equal(mockLiquidityPoolData.numberOfSwaps + 1n, "Swap count should be updated"); + }); + it("should update the liquidity pool with swap volume", () => { expect(updatedPool?.totalVolume0).to.equal( - modifiedMockLiquidityPoolData.totalVolume0 + modifiedMockLiquidityPoolData.totalVolume0 + expectations.expectedNetAmount0 ); expect(updatedPool?.totalVolume1).to.equal( - modifiedMockLiquidityPoolData.totalVolume1 + modifiedMockLiquidityPoolData.totalVolume1 + expectations.expectedNetAmount1 ); + + }); + it("shouldn't update the liquidity pool volume in USD since it has no prices", () => { expect(updatedPool?.totalVolumeUSD).to.equal( modifiedMockLiquidityPoolData.totalVolumeUSD ); - expect(updatedPool?.numberOfSwaps).to - .equal(mockLiquidityPoolData.numberOfSwaps + 1n, "Swap count should be updated"); - expect(updatedPool?.lastUpdatedTimestamp).to.deep.equal( - new Date(eventData.mockEventData.block.timestamp * 1000) - ); + }); + it("should call set_whitelisted_prices", () => { expect(mockPriceOracle.calledOnce).to.be.true; }); }); describe("when token0 is missing", () => { let postEventDB: ReturnType; + let updatedPool: any; beforeEach(async () => { // Set token0 to a different address not in the db tokens mockLiquidityPoolData.token0_id = TokenIdByChain( @@ -156,23 +167,23 @@ describe("Pool Swap Event", () => { event: mockEvent, mockDb: updatedDB3, }); - }); - - it("should update the liquidity pool with token1 data only", async () => { - const updatedPool = postEventDB.entities.LiquidityPoolAggregator.get( + updatedPool = postEventDB.entities.LiquidityPoolAggregator.get( toChecksumAddress(eventData.mockEventData.srcAddress) ); - expect(updatedPool).to.not.be.undefined; + }); + it('should update nominal swap volumes', () => { expect(updatedPool?.totalVolume0).to.equal( - mockLiquidityPoolData.totalVolume0, - "Token0 nominal swap volume should not be updated" + expectations.totalVolume0, + "Token0 nominal swap volume should be updated" ); expect(updatedPool?.totalVolume1).to.equal( expectations.totalVolume1, "Token1 nominal swap volume should be updated" ); + }); + it("should update the liquidity pool with token1 data only", async () => { expect(updatedPool?.totalVolumeUSD).to.equal( expectations.expectedLPVolumeUSD1, "Swap volume in USD should be updated for token 1" @@ -191,6 +202,7 @@ describe("Pool Swap Event", () => { describe("when token1 is missing", () => { let postEventDB: ReturnType; + let updatedPool: any; beforeEach(async () => { mockLiquidityPoolData.token1_id = TokenIdByChain( @@ -210,37 +222,43 @@ describe("Pool Swap Event", () => { event: mockEvent, mockDb: updatedDB3, }); - }); - - it("should update the liquidity pool with token0 data only", async () => { - const updatedPool = postEventDB.entities.LiquidityPoolAggregator.get( + updatedPool = postEventDB.entities.LiquidityPoolAggregator.get( toChecksumAddress(eventData.mockEventData.srcAddress) ); - expect(updatedPool).to.not.be.undefined; + }); + it('should update nominal swap volumes', () => { expect(updatedPool?.totalVolume0).to.equal( expectations.totalVolume0, "Token0 nominal swap volume should be updated" ); expect(updatedPool?.totalVolume1).to.equal( - mockLiquidityPoolData.totalVolume1, - "Token1 nominal swap volume in USD should not be updated" + expectations.totalVolume1, + "Token1 nominal swap volume should be updated" ); - expect(updatedPool?.totalVolumeUSD).to.equal( - expectations.expectedLPVolumeUSD0, - "Total volume USD should be updated." - ); + }); + it('should update the liquidity pool token prices', () => { + expect( + mockPriceOracle.calledOnce, + "set_whitelisted_prices should be called" + ).to.be.true; + }); + + it('should update swap count', () => { expect(updatedPool?.numberOfSwaps).to.equal( mockLiquidityPoolData.numberOfSwaps + 1n, "Swap count should be updated" ); + }); + + it("should update the liquidity pool USD volume with token0 data only", async () => { + expect(updatedPool?.totalVolumeUSD).to.equal( + expectations.expectedLPVolumeUSD0, + "Total volume USD should be updated." + ); - expect( - mockPriceOracle.calledOnce, - "set_whitelisted_prices should be called" - ).to.be.true; }); }); diff --git a/test/EventHandlers/Pool/Sync.test.ts b/test/EventHandlers/Pool/Sync.test.ts index beb4a3a..fb640b1 100644 --- a/test/EventHandlers/Pool/Sync.test.ts +++ b/test/EventHandlers/Pool/Sync.test.ts @@ -24,18 +24,18 @@ describe("Pool Sync Event", () => { expectations.reserveAmount1In = 200n * (10n ** mockToken1Data.decimals); - expectations.expectedReserve0 = (expectations.reserveAmount0In * TEN_TO_THE_18_BI) / - 10n ** mockToken0Data.decimals; - expectations.expectedReserve1 =(expectations.reserveAmount1In * TEN_TO_THE_18_BI) / - 10n ** mockToken1Data.decimals; + expectations.expectedReserve0 = expectations.reserveAmount0In; + expectations.expectedReserve1 =expectations.reserveAmount1In; expectations.expectedReserve0InMissing = expectations.reserveAmount0In; expectations.expectedReserve1InMissing = expectations.reserveAmount1In; expectations.expectedLiquidity0USD = - BigInt(expectations.expectedReserve0 * mockToken0Data.pricePerUSDNew) / TEN_TO_THE_18_BI; + expectations.expectedReserve0 * (10n ** (18n - mockToken0Data.decimals)) * + mockToken0Data.pricePerUSDNew / TEN_TO_THE_18_BI; expectations.expectedLiquidity1USD = - BigInt(expectations.expectedReserve1 * mockToken1Data.pricePerUSDNew) / TEN_TO_THE_18_BI; + expectations.expectedReserve1 * (10n ** (18n - mockToken1Data.decimals)) * + mockToken1Data.pricePerUSDNew / TEN_TO_THE_18_BI; eventData = { reserve0: expectations.reserveAmount0In, diff --git a/test/EventHandlers/Pool/common.ts b/test/EventHandlers/Pool/common.ts index dc541ca..91be786 100644 --- a/test/EventHandlers/Pool/common.ts +++ b/test/EventHandlers/Pool/common.ts @@ -32,14 +32,14 @@ export function setupCommon() { token1_address: mockToken1Data.address, isStable: false, reserve0: 100n * TEN_TO_THE_18_BI, - reserve1: 100n * TEN_TO_THE_18_BI, + reserve1: 100n * TEN_TO_THE_6_BI, totalLiquidityUSD: 200n * TEN_TO_THE_18_BI, totalVolume0: 1n * TEN_TO_THE_18_BI, - totalVolume1: 1n * TEN_TO_THE_18_BI, + totalVolume1: 1n * TEN_TO_THE_6_BI, totalVolumeUSD: 10n * TEN_TO_THE_18_BI, - totalFees0: 100n * 10n ** 18n, - totalFees1: 200n * 10n ** 18n, - totalFeesUSD: 300n * 10n ** 18n, + totalFees0: 100n * TEN_TO_THE_18_BI, + totalFees1: 200n * TEN_TO_THE_6_BI, + totalFeesUSD: 300n * TEN_TO_THE_18_BI, numberOfSwaps: 1n, token0Price: 1n * TEN_TO_THE_18_BI, token1Price: 1n * TEN_TO_THE_18_BI, From 7a42a2e8acd3259e5e3e08edc345c18b258be62d Mon Sep 17 00:00:00 2001 From: "Jalil F." Date: Sun, 10 Nov 2024 22:41:49 -0500 Subject: [PATCH 2/4] Fixing starter prices for Mode price oracle. (#182) --- scripts/generate_whitelist.ts | 5 +++++ src/constants/Mode-Whitelisted.csv | 2 +- src/constants/modeWhitelistedTokens.json | 12 ++++++------ 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/scripts/generate_whitelist.ts b/scripts/generate_whitelist.ts index b7e595f..9c22135 100644 --- a/scripts/generate_whitelist.ts +++ b/scripts/generate_whitelist.ts @@ -13,6 +13,11 @@ const CHAIN_ARGS = { RPC_URL: Deno.env.get("OPTIMISM_RPC_URL"), TOKENS_FILE: "src/constants/Velodrome-Whitelisted.csv", OUTPUT_FILE: "src/constants/optimismWhitelistedTokens.json" + }, + "mode": { + RPC_URL: Deno.env.get("MODE_RPC_URL"), + TOKENS_FILE: "src/constants/Mode-Whitelisted.csv", + OUTPUT_FILE: "src/constants/modeWhitelistedTokens.json" } } diff --git a/src/constants/Mode-Whitelisted.csv b/src/constants/Mode-Whitelisted.csv index 526d028..3f9f190 100644 --- a/src/constants/Mode-Whitelisted.csv +++ b/src/constants/Mode-Whitelisted.csv @@ -1,6 +1,6 @@ address,blocktime -0x4200000000000000000000000000000000000006,15405187 0xd988097fb8612cc24eeC14542bC03424c656005f,15405187 +0x4200000000000000000000000000000000000006,15405187 0xDfc7C877a950e49D2610114102175A06C2e3167a,15405187 0x82af49447d8a07e3bd255e69ecc69b783092f3b8,15405187 0x8b2EeA0999876AAB1E7955fe01A5D261b570452C,15405187 diff --git a/src/constants/modeWhitelistedTokens.json b/src/constants/modeWhitelistedTokens.json index 636b383..d0d5559 100644 --- a/src/constants/modeWhitelistedTokens.json +++ b/src/constants/modeWhitelistedTokens.json @@ -35,16 +35,16 @@ "decimals": 18, "createdBlock": 15405187 }, - { - "address": "0xd988097fb8612cc24eeC14542bC03424c656005f", - "symbol": "USDC", - "decimals": 6, - "createdBlock": 15405187 - }, { "address": "0x4200000000000000000000000000000000000006", "symbol": "WETH", "decimals": 18, "createdBlock": 15405187 + }, + { + "address": "0xd988097fb8612cc24eeC14542bC03424c656005f", + "symbol": "USDC", + "decimals": 6, + "createdBlock": 15405187 } ] \ No newline at end of file From fc6c7311831acca2af1fdd376ccfea99047b72d9 Mon Sep 17 00:00:00 2001 From: "Jalil F." Date: Mon, 11 Nov 2024 19:54:38 -0500 Subject: [PATCH 3/4] Bugfix: Price oracle mode (#184) * Add correct chain logic for price oracle token selection. * Adding newly deployed price oracle. --- src/Constants.ts | 4 ++-- src/PriceOracle.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Constants.ts b/src/Constants.ts index f5efc7f..1b292c1 100644 --- a/src/Constants.ts +++ b/src/Constants.ts @@ -169,9 +169,9 @@ const MODE_CONSTANTS: chainConstants = { usdc: findToken(MODE_WHITELISTED_TOKENS, "USDC"), oracle: { getAddress: (blockNumber: number) => { - return "0xC7d87726753d2e7a8823db6c96a4f44e4D502a21"; + return "0xE50621a0527A43534D565B67D64be7C79807F269"; }, - startBlock: 15407790, + startBlock: 15591759, updateDelta: 60 * 60, // 1 hour }, rewardToken: { diff --git a/src/PriceOracle.ts b/src/PriceOracle.ts index 6715199..ed25635 100644 --- a/src/PriceOracle.ts +++ b/src/PriceOracle.ts @@ -92,7 +92,7 @@ export async function set_whitelisted_prices( ): Promise { // Skip if not yet available let startBlock = - CHAIN_CONSTANTS[chainId].oracle.startBlock || Number.MAX_SAFE_INTEGER; + CHAIN_CONSTANTS[chainId].oracle.startBlock; if (blockNumber < startBlock) return; @@ -105,8 +105,7 @@ export async function set_whitelisted_prices( if (!tokensNeedUpdate) return; // Get token data for chain - const tokenData = - chainId === 10 ? OPTIMISM_WHITELISTED_TOKENS : BASE_WHITELISTED_TOKENS; + const tokenData = CHAIN_CONSTANTS[chainId].whitelistedTokens; // Get prices from oracle and filter if token is not created yet const addresses = tokenData @@ -177,6 +176,7 @@ export async function set_whitelisted_prices( chainId: chainId, lastUpdatedTimestamp: blockDatetime, }; + context.TokenPriceSnapshot.set(tokenPrice); } From 2a32924b9fb8895b4dce2e2e9f9aa2f0f0c4ad0d Mon Sep 17 00:00:00 2001 From: "Jalil F." Date: Wed, 13 Nov 2024 17:23:48 -0500 Subject: [PATCH 4/4] Add fixes for CL pool burns. (#186) --- src/EventHandlers/CLPool.ts | 6 +++--- test/EventHandlers/CLPool/CLPool.test.ts | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/EventHandlers/CLPool.ts b/src/EventHandlers/CLPool.ts index f5faad2..0be5711 100644 --- a/src/EventHandlers/CLPool.ts +++ b/src/EventHandlers/CLPool.ts @@ -170,11 +170,11 @@ CLPool.Burn.handlerWithLoader({ const liquidityPoolDiff = { reserve0: - liquidityPoolAggregator.reserve0 + tokenUpdateData.reserve0, + liquidityPoolAggregator.reserve0 - tokenUpdateData.reserve0, reserve1: - liquidityPoolAggregator.reserve1 + tokenUpdateData.reserve1, + liquidityPoolAggregator.reserve1 - tokenUpdateData.reserve1, totalLiquidityUSD: - liquidityPoolAggregator.totalLiquidityUSD + + liquidityPoolAggregator.totalLiquidityUSD - tokenUpdateData.totalLiquidityUSD, lastUpdatedTimestamp: new Date(event.block.timestamp * 1000), }; diff --git a/test/EventHandlers/CLPool/CLPool.test.ts b/test/EventHandlers/CLPool/CLPool.test.ts index fa1c77e..31d4ecb 100644 --- a/test/EventHandlers/CLPool/CLPool.test.ts +++ b/test/EventHandlers/CLPool/CLPool.test.ts @@ -126,8 +126,8 @@ describe("CLPool Event Handlers", () => { let eventData: any; let expectations: any = { - amount0In: -100n * 10n ** 18n, - amount1In: -100n * 10n ** 6n, + amount0In: 100n * 10n ** 18n, + amount1In: 100n * 10n ** 6n, totalLiquidity: 0n, }; @@ -186,11 +186,11 @@ describe("CLPool Event Handlers", () => { it("should update the reserves", () => { expect(diff.reserve0).to.equal( - mockLiquidityPoolData.reserve0 + + mockLiquidityPoolData.reserve0 - expectations.amount0In, "Reserve 0 should be appropriately updated"); expect(diff.reserve1).to.equal( - mockLiquidityPoolData.reserve1 + + mockLiquidityPoolData.reserve1 - expectations.amount1In, "Reserve 1 should be appropriately updated"); });