Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Release] - Range Price Improvements #3184

Merged
merged 4 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading