From 05e7ed9bc1def231d73b3e4c249b93667167c230 Mon Sep 17 00:00:00 2001 From: brightiron Date: Tue, 23 Jul 2024 12:12:58 -0500 Subject: [PATCH 1/3] fixes for price display --- src/views/Range/RangeChart.tsx | 19 ++++++++++++++----- src/views/Range/hooks.tsx | 17 ++++++++++++++--- src/views/Range/index.tsx | 6 ++++-- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/views/Range/RangeChart.tsx b/src/views/Range/RangeChart.tsx index 0782872df..40fe4fdd0 100644 --- a/src/views/Range/RangeChart.tsx +++ b/src/views/Range/RangeChart.tsx @@ -71,11 +71,20 @@ const RangeChart = (props: { /* We load an object at the front of the chartData array * with no price data to shift the chart line left and add an extra element with current market price */ - chartData.unshift({ - uv: [formattedWallHigh, formattedCushionHigh], - lv: [formattedWallLow, formattedCushionLow], - ma: targetPrice, - }); + chartData.unshift( + { + uv: [formattedWallHigh, formattedCushionHigh], + lv: [formattedWallLow, formattedCushionLow], + ma: targetPrice, + }, + { + price: currentPrice, + timestamp: "now", + uv: [formattedWallHigh, formattedCushionHigh], + lv: [formattedWallLow, formattedCushionLow], + ma: targetPrice, + }, + ); const CustomReferenceDot = (props: { cx: string | number | undefined; diff --git a/src/views/Range/hooks.tsx b/src/views/Range/hooks.tsx index e888226c0..31e3bd570 100644 --- a/src/views/Range/hooks.tsx +++ b/src/views/Range/hooks.tsx @@ -33,7 +33,7 @@ export const PriceHistory = () => { const contract = RANGE_PRICE_CONTRACT.getEthersContract(networks.MAINNET); const lastObservationIndex = await contract.nextObsIndex(); const secondsToSubtract = await contract.observationFrequency(); - const totalObservations = lastObservationIndex < 10 ? lastObservationIndex : 10; + const totalObservations = lastObservationIndex < 9 ? lastObservationIndex : 9; let currentDate = new Date(); // Start with the current date const resultsArray: { price: number; @@ -41,14 +41,15 @@ export const PriceHistory = () => { }[] = []; for (let i = 0; i < totalObservations; i++) { - const observation = lastObservationIndex - i; + const observation = lastObservationIndex - 1 - i; + console.log(observation, "observation"); if (i > 0) { currentDate = new Date(currentDate.getTime() - secondsToSubtract * 1000); } const datapoint = await contract.observations(observation); const resultObject = { price: parseFloat(formatEther(datapoint)), - timestamp: i === 0 ? "now" : currentDate.toLocaleString(), + timestamp: currentDate.toLocaleString(), }; resultsArray.push(resultObject); } @@ -72,6 +73,16 @@ export const OperatorPrice = () => { return { data, isFetched, isLoading }; }; +export const LastSnapshotPrice = () => { + const networks = useTestableNetworks(); + + const contract = RANGE_PRICE_CONTRACT.getEthersContract(networks.MAINNET); + const { data, isFetched, isLoading } = useQuery(["getLastSnapshotPrice", networks.MAINNET], async () => { + return parseBigNumber(await contract.getLastPrice(), 18); + }); + return { data, isFetched, isLoading }; +}; + /** * Returns the Target price of the Operator at the given address */ diff --git a/src/views/Range/index.tsx b/src/views/Range/index.tsx index 2c47ac358..3d490c143 100644 --- a/src/views/Range/index.tsx +++ b/src/views/Range/index.tsx @@ -23,6 +23,7 @@ import { usePathForNetwork } from "src/hooks/usePathForNetwork"; import { useTestableNetworks } from "src/hooks/useTestableNetworks"; import { DetermineRangePrice, + LastSnapshotPrice, OperatorPrice, OperatorReserveSymbol, RangeBondMaxPayout, @@ -61,6 +62,7 @@ export const Range = () => { const { data: ohmBalance = new DecimalBigNumber("0", 9) } = useBalance(OHM_ADDRESSES)[networks.MAINNET]; const { data: currentPrice } = OperatorPrice(); + const { data: lastPrice } = LastSnapshotPrice(); const { data: currentMarketPrices } = useGetDefillamaPrice({ addresses: [DAI_ADDRESSES[1], OHM_ADDRESSES[1]], }); @@ -172,7 +174,7 @@ export const Range = () => { } /> - {currentPrice ? ( + {currentPrice && lastPrice ? ( <> { title="Last Snapshot Price" balance={ - {formatNumber(currentPrice, 2)} {reserveSymbol} + {formatNumber(lastPrice, 2)} {reserveSymbol} Date: Tue, 23 Jul 2024 12:31:48 -0500 Subject: [PATCH 2/3] handle circular index --- src/views/Range/hooks.tsx | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/views/Range/hooks.tsx b/src/views/Range/hooks.tsx index 31e3bd570..b8d2e5728 100644 --- a/src/views/Range/hooks.tsx +++ b/src/views/Range/hooks.tsx @@ -33,16 +33,14 @@ export const PriceHistory = () => { const contract = RANGE_PRICE_CONTRACT.getEthersContract(networks.MAINNET); const lastObservationIndex = await contract.nextObsIndex(); const secondsToSubtract = await contract.observationFrequency(); - const totalObservations = lastObservationIndex < 9 ? lastObservationIndex : 9; let currentDate = new Date(); // Start with the current date const resultsArray: { price: number; timestamp: string; }[] = []; - for (let i = 0; i < totalObservations; i++) { - const observation = lastObservationIndex - 1 - i; - console.log(observation, "observation"); + for (let i = 1; i < 10; i++) { + const observation = (lastObservationIndex - i + 90) % 90; if (i > 0) { currentDate = new Date(currentDate.getTime() - secondsToSubtract * 1000); } @@ -60,6 +58,14 @@ export const PriceHistory = () => { return { data, isFetched, isLoading }; }; +function getLastTenIndices(currentIndex: number): number[] { + const result: number[] = []; + for (let i = 0; i < 10; i++) { + result.unshift((currentIndex - i + 100) % 100); + } + return result; +} + /** * Returns the current price of the Operator at the given address */ From a86d5c49c4a2c8ca06b876c370e11a8b1f706657 Mon Sep 17 00:00:00 2001 From: brightiron Date: Tue, 23 Jul 2024 12:32:04 -0500 Subject: [PATCH 3/3] cleanup --- src/views/Range/hooks.tsx | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/views/Range/hooks.tsx b/src/views/Range/hooks.tsx index b8d2e5728..6199771f8 100644 --- a/src/views/Range/hooks.tsx +++ b/src/views/Range/hooks.tsx @@ -58,14 +58,6 @@ export const PriceHistory = () => { return { data, isFetched, isLoading }; }; -function getLastTenIndices(currentIndex: number): number[] { - const result: number[] = []; - for (let i = 0; i < 10; i++) { - result.unshift((currentIndex - i + 100) % 100); - } - return result; -} - /** * Returns the current price of the Operator at the given address */