From 10fec48307cd94ddab71984a3aef45382d2b9735 Mon Sep 17 00:00:00 2001 From: "Jalil F." Date: Wed, 1 Jan 2025 19:33:15 -0500 Subject: [PATCH] Add back token price snapshot entity. (#242) --- src/PriceOracle.ts | 14 +++++++++++++- test/PriceOracle.test.ts | 7 +++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/PriceOracle.ts b/src/PriceOracle.ts index 5ac7af6..678139d 100644 --- a/src/PriceOracle.ts +++ b/src/PriceOracle.ts @@ -66,13 +66,25 @@ export async function refreshTokenPrice( } const tokenPriceData = await getTokenPriceData(token.address, blockNumber, chainId); + const currentPrice = tokenPriceData.pricePerUSDNew; const updatedToken: Token = { ...token, - pricePerUSDNew: tokenPriceData.pricePerUSDNew, + pricePerUSDNew: currentPrice, decimals: tokenPriceData.decimals, lastUpdatedTimestamp: new Date(blockTimestampMs) }; context.Token.set(updatedToken); + + // Create new TokenPrice entity + const tokenPrice: TokenPriceSnapshot = { + id: TokenIdByBlock(token.address, chainId, blockNumber), + address: toChecksumAddress(token.address), + pricePerUSDNew: currentPrice, + chainId: chainId, + lastUpdatedTimestamp: new Date(blockTimestampMs), + }; + + context.TokenPriceSnapshot.set(tokenPrice); return updatedToken; } diff --git a/test/PriceOracle.test.ts b/test/PriceOracle.test.ts index 64925eb..feb68fe 100644 --- a/test/PriceOracle.test.ts +++ b/test/PriceOracle.test.ts @@ -74,6 +74,8 @@ describe("PriceOracle", () => { }); it("should not update prices if the update interval hasn't passed", async () => { expect(mockContract.called).to.be.false; + expect(mockContext.Token.set.called).to.be.false; + expect(mockContext.TokenPriceSnapshot.set.called).to.be.false; }); }); describe("if the update interval has passed", () => { @@ -93,6 +95,11 @@ describe("PriceOracle", () => { expect(updatedToken.pricePerUSDNew).to.equal(mockTokenPriceData.pricePerUSDNew); expect(updatedToken.lastUpdatedTimestamp.getTime()).greaterThan(testLastUpdated.getTime()); }); + it("should create a new TokenPriceSnapshot entity", async () => { + const tokenPrice = mockContext.TokenPriceSnapshot.set.lastCall.args[0]; + expect(tokenPrice.pricePerUSDNew).to.equal(mockTokenPriceData.pricePerUSDNew); + expect(tokenPrice.lastUpdatedTimestamp.getTime()).greaterThan(testLastUpdated.getTime()); + }); }); }); });