Skip to content

Commit

Permalink
Add back token price snapshot entity. (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfarid27 authored Jan 2, 2025
1 parent 5752016 commit 10fec48
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/PriceOracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
7 changes: 7 additions & 0 deletions test/PriceOracle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -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());
});
});
});
});

0 comments on commit 10fec48

Please sign in to comment.