Skip to content

Commit

Permalink
Merge pull request #3183 from OlympusDAO/rangePriceFixes
Browse files Browse the repository at this point in the history
Range price fixes
  • Loading branch information
brightiron authored Jul 23, 2024
2 parents 2c32d99 + a86d5c4 commit 6cc3e6e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
19 changes: 14 additions & 5 deletions src/views/Range/RangeChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 13 additions & 4 deletions src/views/Range/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,21 @@ 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;
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 - i;
for (let i = 1; i < 10; i++) {
const observation = (lastObservationIndex - i + 90) % 90;
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);
}
Expand All @@ -72,6 +71,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
*/
Expand Down
6 changes: 4 additions & 2 deletions src/views/Range/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { usePathForNetwork } from "src/hooks/usePathForNetwork";
import { useTestableNetworks } from "src/hooks/useTestableNetworks";
import {
DetermineRangePrice,
LastSnapshotPrice,
OperatorPrice,
OperatorReserveSymbol,
RangeBondMaxPayout,
Expand Down Expand Up @@ -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]],
});
Expand Down Expand Up @@ -172,7 +174,7 @@ export const Range = () => {
}
/>
<Paper sx={{ width: "98%" }}>
{currentPrice ? (
{currentPrice && lastPrice ? (
<>
<Metric
label="Market Price"
Expand All @@ -199,7 +201,7 @@ export const Range = () => {
title="Last Snapshot Price"
balance={
<Box display="flex" alignItems="center">
{formatNumber(currentPrice, 2)} {reserveSymbol}
{formatNumber(lastPrice, 2)} {reserveSymbol}
<Box display="flex" fontSize="12px" alignItems="center">
<InfoTooltip
message={`Snapshot Price is returned from price feed connected to RBS Operator. The current price feed is Chainlink, which updates price if there's a 2% deviation or 24 hours, whichever comes first. The Snapshot Price is used by RBS Operator to turn on/off bond markets.`}
Expand Down

0 comments on commit 6cc3e6e

Please sign in to comment.