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

usds handling RBS #3246

Merged
merged 3 commits into from
Dec 9, 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
7 changes: 6 additions & 1 deletion src/constants/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ export const DAI_ADDRESSES = {
[NetworkId.MAINNET]: "0x6b175474e89094c44da98b954eedeac495271d0f",
};

export const USDS_ADDRESSES = {
[NetworkId.MAINNET]: "0xdC035D45d973E3EC169d2276DDab16f1e407384F",
[NetworkId.TESTNET_GOERLI]: "",
};

export const WETH_ADDRESSES = {
[NetworkId.MAINNET]: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
};
Expand Down Expand Up @@ -142,7 +147,7 @@ export const BALANCER_VAULT_ADDRESSSES = {
};

export const RANGE_OPERATOR_ADDRESSES = {
[NetworkId.MAINNET]: "0x0AE561226896dA978EaDA0Bec4a7d3CfAE04f506",
[NetworkId.MAINNET]: "0x6417F206a0a6628Da136C0Faa39026d0134D2b52",
[NetworkId.TESTNET_GOERLI]: "0x6620592f9bdffAbadcea644a35946E7b93EaaF56",
};

Expand Down
4 changes: 3 additions & 1 deletion src/views/Range/RangeConfirmationModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ const RangeConfirmationModal = (props: {
<Box display="flex" flexDirection="row" justifyContent="space-between" alignItems="center" mb={"9px"}>
<Typography sx={{ fontSize: "15px", lineHeight: "21px" }}>Price of OHM</Typography>
<Box display="flex" flexDirection="column" textAlign="right">
<Typography sx={{ fontSize: "15px", lineHeight: "21px" }}>{props.swapPrice} DAI</Typography>
<Typography sx={{ fontSize: "15px", lineHeight: "21px" }}>
{props.swapPrice} {props.reserveSymbol}
</Typography>
</Box>
</Box>
<Box display="flex" flexDirection="row" justifyContent="space-between" alignItems="center" mb={"9px"}>
Expand Down
47 changes: 29 additions & 18 deletions src/views/Range/RangeInputForm.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Box } from "@mui/material";
import { Box, SvgIcon } from "@mui/material";
import { SwapCollection } from "@olympusdao/component-library";
import { OHMTokenProps, SwapCard } from "@olympusdao/component-library";
import React from "react";
import usdsIcon from "src/assets/tokens/usds.svg?react";
import { DecimalBigNumber } from "src/helpers/DecimalBigNumber/DecimalBigNumber";

/**
* Component for Displaying RangeInputForm
*/
Expand Down Expand Up @@ -36,22 +36,33 @@ const RangeInputForm = (props: {
const trimmedOhmBalance = ohmBalance.toString({ decimals: 2 });
const trimmedReserveBalance = reserveBalance.toString({ decimals: 2 });

const ReserveInput = () => (
<SwapCard
key="reserveAmount"
id="reserve-amount"
inputProps={{ "data-testid": "reserve-amount" }}
name="reserveAmount"
value={reserveAmount}
onChange={event => onChangeReserveAmount(event.currentTarget.value)}
endString={`Max`}
endStringOnClick={() => hasPrice && onChangeReserveAmount(reserveBalance.toString())}
token={reserveSymbol}
type="string"
info={`Balance: ${trimmedReserveBalance} ${reserveSymbol}`}
disabled={!hasPrice}
/>
);
const ReserveInput = () => {
const token =
reserveSymbol === ("USDS" as OHMTokenProps["name"]) ? (
<Box display="flex" gap="9px" alignItems="center">
<SvgIcon color="primary" sx={{ width: "20px", height: "20px" }} viewBox="0 0 50 50" component={usdsIcon} />
USDS
</Box>
) : (
reserveSymbol
);
return (
<SwapCard
key="reserveAmount"
id="reserve-amount"
inputProps={{ "data-testid": "reserve-amount" }}
name="reserveAmount"
value={reserveAmount}
onChange={event => onChangeReserveAmount(event.currentTarget.value)}
endString={`Max`}
endStringOnClick={() => hasPrice && onChangeReserveAmount(reserveBalance.toString())}
token={token}
type="string"
info={`Balance: ${trimmedReserveBalance} ${reserveSymbol}`}
disabled={!hasPrice}
/>
);
};

const OhmInput = () => (
<SwapCard
Expand Down
12 changes: 11 additions & 1 deletion src/views/Range/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export const OperatorMovingAverage = () => {
*/
export const OperatorReserveSymbol = () => {
const networks = useTestableNetworks();
const contract = RANGE_CONTRACT.getEthersContract(networks.MAINNET);
const contract = RANGE_OPERATOR_CONTRACT.getEthersContract(networks.MAINNET);
const {
data = { symbol: "", reserveAddress: "" },
isFetched,
Expand Down Expand Up @@ -445,3 +445,13 @@ export const RangeNextBeat = () => {
});
return { data, isFetched, isLoading };
};

export const useRangeCheckActive = () => {
const networks = useTestableNetworks();
const contract = RANGE_OPERATOR_CONTRACT.getEthersContract(networks.MAINNET);
const { data, isFetched, isLoading } = useQuery(["getRangeCheckActive", networks.MAINNET], async () => {
const active = await contract.active();
return active;
});
return { data, isFetched, isLoading };
};
38 changes: 25 additions & 13 deletions src/views/Range/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useNavigate } from "react-router-dom";
import { Link as RouterLink } from "react-router-dom";
import PageTitle from "src/components/PageTitle";
import { WalletConnectedGuard } from "src/components/WalletConnectedGuard";
import { DAI_ADDRESSES, OHM_ADDRESSES } from "src/constants/addresses";
import { OHM_ADDRESSES, USDS_ADDRESSES } from "src/constants/addresses";
import { formatNumber, parseBigNumber } from "src/helpers";
import CountdownTimer from "src/helpers/CountdownTimer";
import { DecimalBigNumber } from "src/helpers/DecimalBigNumber/DecimalBigNumber";
Expand All @@ -29,6 +29,7 @@ import {
RangeBondMaxPayout,
RangeData,
RangeNextBeat,
useRangeCheckActive,
} from "src/views/Range/hooks";
import RangeChart from "src/views/Range/RangeChart";
import RangeConfirmationModal from "src/views/Range/RangeConfirmationModal";
Expand All @@ -46,6 +47,7 @@ export const Range = () => {
const networks = useTestableNetworks();
const { chain = { id: 1 } } = useNetwork();
const { data: rangeData, isLoading: rangeDataLoading } = RangeData();
const { data: isActive } = useRangeCheckActive();
usePathForNetwork({ pathName: "range", networkID: chain.id, navigate });

const {
Expand All @@ -58,19 +60,19 @@ export const Range = () => {
const [reserveAmount, setReserveAmount] = useState("");
const [ohmAmount, setOhmAmount] = useState("");

const { data: reserveBalance = new DecimalBigNumber("0", 18) } = useBalance(DAI_ADDRESSES)[networks.MAINNET];
const { data: reserveBalance = new DecimalBigNumber("0", 18) } = useBalance(USDS_ADDRESSES)[networks.MAINNET];
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]],
addresses: [USDS_ADDRESSES[1], OHM_ADDRESSES[1]],
});
const { data: nextBeat } = RangeNextBeat();

const daiPriceUSD = currentMarketPrices?.[`ethereum:${DAI_ADDRESSES[1]}`].price;
const usdsPriceUSD = currentMarketPrices?.[`ethereum:${USDS_ADDRESSES[1]}`].price;
const ohmPriceUSD = currentMarketPrices?.[`ethereum:${OHM_ADDRESSES[1]}`].price;
const marketOhmPriceDAI = daiPriceUSD && ohmPriceUSD ? ohmPriceUSD / daiPriceUSD : undefined;
const marketOhmPriceUSDS = usdsPriceUSD && ohmPriceUSD ? ohmPriceUSD / usdsPriceUSD : undefined;

const maxString = sellActive ? `Max You Can Sell` : `Max You Can Buy`;

Expand Down Expand Up @@ -112,12 +114,12 @@ export const Range = () => {
// Set sell active if market price is defined and is below lower cushion OR if there is a active lower bond market.
useEffect(() => {
if (
(marketOhmPriceDAI && marketOhmPriceDAI < parseBigNumber(rangeData.low.cushion.price, 18)) ||
(marketOhmPriceUSDS && marketOhmPriceUSDS < parseBigNumber(rangeData.low.cushion.price, 18)) ||
bidPrice.activeBondMarket
) {
setSellActive(true);
}
}, [rangeData.low.cushion.price, marketOhmPriceDAI]);
}, [rangeData.low.cushion.price, marketOhmPriceUSDS]);

const maxBalanceString = `${formatNumber(maxCapacity, 2)} ${buyAsset} (${formatNumber(
sellActive ? maxCapacity / bidPrice.price : maxCapacity * askPrice.price,
Expand Down Expand Up @@ -146,7 +148,9 @@ export const Range = () => {
const swapPriceFormatted = formatNumber(swapPrice, 2);

const discount =
(marketOhmPriceDAI && (marketOhmPriceDAI - swapPrice) / (sellActive ? -marketOhmPriceDAI : marketOhmPriceDAI)) || 0;
(marketOhmPriceUSDS &&
(marketOhmPriceUSDS - swapPrice) / (sellActive ? -marketOhmPriceUSDS : marketOhmPriceUSDS)) ||
0;

const hasPrice = (sellActive && askPrice.price) || (!sellActive && bidPrice.price) ? true : false;

Expand All @@ -164,7 +168,7 @@ export const Range = () => {
name="Range Bound Stability"
subtitle={
<Box display="flex" flexDirection="row" alignItems="center" gap="4px">
Swap DAI or OHM directly with the treasury.{" "}
Swap ${reserveSymbol} or OHM directly with the treasury.{" "}
<Link
component={RouterLink}
to="https://docs.olympusdao.finance/main/overview/range-bound"
Expand All @@ -184,9 +188,9 @@ export const Range = () => {
<>
<Metric
label="Market Price"
metric={`${formatNumber(marketOhmPriceDAI || 0, 2)} DAI`}
metric={`${formatNumber(marketOhmPriceUSDS || 0, 2)} ${reserveSymbol}`}
tooltip="Market Price uses DefiLlama API, and is also used when calculating premium/discount on RBS"
isLoading={!marketOhmPriceDAI}
isLoading={!marketOhmPriceUSDS}
/>
<Grid container>
<Grid item xs={12} lg={6}>
Expand Down Expand Up @@ -232,6 +236,13 @@ export const Range = () => {
</Box>
</Box>
)}
{!isActive && (
<Box display="flex" flexDirection="row" width="100%" justifyContent="center" mt="24px">
<Box display="flex" flexDirection="column" width="100%" maxWidth="476px">
<InfoNotification>RBS Operator is currently inactive</InfoNotification>
</Box>
</Box>
)}
</Box>
<form onSubmit={handleSubmit}>
<RangeInputForm
Expand Down Expand Up @@ -263,7 +274,7 @@ export const Range = () => {
? "premium"
: "discount"}{" "}
of {formatNumber(Math.abs(discount) * 100, 2)}% relative to market price of{" "}
{formatNumber(marketOhmPriceDAI || 0, 2)} {reserveSymbol}
{formatNumber(marketOhmPriceUSDS || 0, 2)} {reserveSymbol}
</InfoNotification>
</Box>
<div data-testid="max-row">
Expand Down Expand Up @@ -308,7 +319,8 @@ export const Range = () => {
amountAboveCapacity ||
amountAboveBalance ||
(sellActive && !rangeData.low.active) ||
(!sellActive && !rangeData.high.active)
(!sellActive && !rangeData.high.active) ||
!isActive
}
>
{amountAboveCapacity
Expand Down
Loading