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

Use Price from Price Contract #3251

Merged
merged 1 commit into from
Dec 10, 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
10 changes: 5 additions & 5 deletions src/components/Sidebar/NavContent.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Box, Link, Paper, SvgIcon, Typography, useTheme } from "@mui/material";
import { Box, Link, Paper, SvgIcon, Typography } from "@mui/material";
import { styled } from "@mui/material/styles";
import { Icon } from "@olympusdao/component-library";
import React from "react";
Expand All @@ -7,8 +7,9 @@ import OlympusIcon from "src/assets/icons/olympus-nav-header.svg?react";
import NavItem from "src/components/library/NavItem";
import { formatCurrency } from "src/helpers";
import { Environment } from "src/helpers/environment/Environment/Environment";
import { useGohmPriceDefiLlama, useOhmPriceDefillama } from "src/hooks/usePrices";
import { useGohmPriceContract } from "src/hooks/usePrices";
import { useTestableNetworks } from "src/hooks/useTestableNetworks";
import { usePriceContractPrice } from "src/views/Range/hooks";
import { useNetwork } from "wagmi";

const PREFIX = "NavContent";
Expand All @@ -24,11 +25,10 @@ const StyledBox = styled(Box)(({ theme }) => ({
}));

const NavContent: React.VFC = () => {
const theme = useTheme();
const { chain = { id: 1 } } = useNetwork();
const networks = useTestableNetworks();
const { data: ohmPrice } = useOhmPriceDefillama();
const { data: gohmPrice } = useGohmPriceDefiLlama();
const { data: ohmPrice } = usePriceContractPrice();
const { data: gohmPrice } = useGohmPriceContract();

const protocolMetricsEnabled = Boolean(Environment.getWundergraphNodeUrl());
const emissionsManagerEnabled = Environment.getEmissionsManagerEnabled();
Expand Down
2 changes: 2 additions & 0 deletions src/helpers/pricing/useGetDefillamaPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export const useGetDefillamaPrice = ({ addresses }: { addresses: string[] }) =>
const response = await axios.get<DefillamaPriceResponse>(
`https://coins.llama.fi/prices/current/${joinedAddresses}`,
);

console.log("coins", response.data.coins);
return response.data.coins;
} catch (error) {
return {};
Expand Down
23 changes: 3 additions & 20 deletions src/hooks/usePrices.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { useQuery } from "@tanstack/react-query";
import { NetworkId } from "src/constants";
import { OHM_ADDRESSES } from "src/constants/addresses";
import { OHM_TOKEN } from "src/constants/tokens";
import { DecimalBigNumber } from "src/helpers/DecimalBigNumber/DecimalBigNumber";
import { useGetDefillamaPrice } from "src/helpers/pricing/useGetDefillamaPrice";
import { queryAssertion } from "src/helpers/react-query/queryAssertion";
import { nonNullable } from "src/helpers/types/nonNullable";
import { useCurrentIndex } from "src/hooks/useCurrentIndex";
import { usePriceContractPrice } from "src/views/Range/hooks";

export const ohmPriceQueryKey = () => ["useOhmPrice"];

Expand All @@ -21,22 +20,6 @@ export const useOhmPrice = () => {
});
};

export const useOhmPriceDefillama = () => {
const { data: currentMarketPrices } = useGetDefillamaPrice({
addresses: [OHM_ADDRESSES[1]],
});

return useQuery(
["useOhmPriceDefillama"],
async () => {
const ohmPriceUSD = currentMarketPrices?.[`ethereum:${OHM_ADDRESSES[1]}`].price;

return ohmPriceUSD;
},
{ enabled: !!currentMarketPrices },
);
};

export const gohmPriceQueryKey = (marketPrice?: number, currentIndex?: DecimalBigNumber) =>
["useGOHMPrice", marketPrice, currentIndex].filter(nonNullable);

Expand All @@ -62,8 +45,8 @@ export const useGohmPrice = () => {
/**
* Returns the calculated price of gOHM.
*/
export const useGohmPriceDefiLlama = () => {
const { data: ohmPrice } = useOhmPriceDefillama();
export const useGohmPriceContract = () => {
const { data: ohmPrice } = usePriceContractPrice();
const { data: currentIndex } = useCurrentIndex();

const key = gohmPriceQueryKey(ohmPrice, currentIndex);
Expand Down
4 changes: 2 additions & 2 deletions src/views/MyBalances/MyCoolerLoans.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { ethers } from "ethers";
import { Link as RouterLink } from "react-router-dom";
import lendAndBorrowIcon from "src/assets/icons/lendAndBorrow.svg?react";
import { formatCurrency } from "src/helpers";
import { useGohmPriceDefiLlama } from "src/hooks/usePrices";
import { useGohmPriceContract } from "src/hooks/usePrices";
import { useGetClearingHouse } from "src/views/Lending/Cooler/hooks/useGetClearingHouse";
import { useGetCoolerLoans } from "src/views/Lending/Cooler/hooks/useGetCoolerLoans";
import { useAccount } from "wagmi";

export const MyCoolerLoans = ({ balance, balanceUSD }: { balance: string; balanceUSD: string }) => {
const theme = useTheme();
const { address } = useAccount();
const { data: gOhmPrice = 0 } = useGohmPriceDefiLlama();
const { data: gOhmPrice = 0 } = useGohmPriceContract();

const { data: clearingHouseV1 } = useGetClearingHouse({ clearingHouse: "clearingHouseV1" });
const { data: clearingHouseV2 } = useGetClearingHouse({ clearingHouse: "clearingHouseV2" });
Expand Down
4 changes: 2 additions & 2 deletions src/views/MyBalances/MyGohmBalances.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import WalletIcon from "src/assets/icons/wallet.svg?react";
import { formatCurrency } from "src/helpers";
import { DecimalBigNumber } from "src/helpers/DecimalBigNumber/DecimalBigNumber";
import { useGohmBalance } from "src/hooks/useBalance";
import { useGohmPriceDefiLlama } from "src/hooks/usePrices";
import { useGohmPriceContract } from "src/hooks/usePrices";
import { useTestableNetworks } from "src/hooks/useTestableNetworks";
import { NetworkId } from "src/networkDetails";

export const MyGohmBalances = ({ walletBalance }: { walletBalance?: DecimalBigNumber }) => {
const gohmBalances = useGohmBalance();
const { data: gOhmPrice = 0 } = useGohmPriceDefiLlama();
const { data: gOhmPrice = 0 } = useGohmPriceContract();

const networks = useTestableNetworks();

Expand Down
4 changes: 2 additions & 2 deletions src/views/MyBalances/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
useWsohmBalance,
} from "src/hooks/useBalance";
import { useCurrentIndex } from "src/hooks/useCurrentIndex";
import { useOhmPriceDefillama } from "src/hooks/usePrices";
import { useTestableNetworks } from "src/hooks/useTestableNetworks";
import { NetworkId } from "src/networkDetails";
import { useGetClearingHouse } from "src/views/Lending/Cooler/hooks/useGetClearingHouse";
Expand All @@ -29,6 +28,7 @@ import { LearnAboutOhm } from "src/views/MyBalances/LearnAboutOhm";
import { MyCoolerLoans } from "src/views/MyBalances/MyCoolerLoans";
import { MyGohmBalances } from "src/views/MyBalances/MyGohmBalances";
import { MyOhmBalances } from "src/views/MyBalances/MyOhmBalances";
import { usePriceContractPrice } from "src/views/Range/hooks";
import { useAccount, useNetwork } from "wagmi";

/**
Expand All @@ -42,7 +42,7 @@ export const MyBalances: FC<OHMAssetsProps> = () => {
const { address } = useAccount();
const networks = useTestableNetworks();
const { chain = { id: 1 } } = useNetwork();
const { data: ohmPrice = 0 } = useOhmPriceDefillama();
const { data: ohmPrice = 0 } = usePriceContractPrice();
const { data: currentIndex = new DecimalBigNumber("0", 9) } = useCurrentIndex();
const { data: v1OhmBalance = new DecimalBigNumber("0", 9) } = useV1OhmBalance()[networks.MAINNET];
const { data: v1SohmBalance = new DecimalBigNumber("0", 9) } = useV1SohmBalance()[networks.MAINNET];
Expand Down
4 changes: 2 additions & 2 deletions src/views/Range/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ export const PriceHistory = () => {
/**
* Returns the current price of the Operator at the given address
*/
export const OperatorPrice = () => {
export const usePriceContractPrice = () => {
const networks = useTestableNetworks();

const contract = RANGE_PRICE_CONTRACT.getEthersContract(networks.MAINNET);
const { data, isFetched, isLoading } = useQuery(["getOperatorPrice", networks.MAINNET], async () => {
const { data, isFetched, isLoading } = useQuery(["getPriceContractPrice", networks.MAINNET], async () => {
return parseBigNumber(await contract.getCurrentPrice(), 18);
});
return { data, isFetched, isLoading };
Expand Down
4 changes: 2 additions & 2 deletions src/views/Range/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import { useTestableNetworks } from "src/hooks/useTestableNetworks";
import {
DetermineRangePrice,
LastSnapshotPrice,
OperatorPrice,
OperatorReserveSymbol,
RangeBondMaxPayout,
RangeData,
RangeNextBeat,
usePriceContractPrice,
useRangeCheckActive,
} from "src/views/Range/hooks";
import RangeChart from "src/views/Range/RangeChart";
Expand Down Expand Up @@ -63,7 +63,7 @@ export const Range = () => {
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: currentPrice } = usePriceContractPrice();
const { data: lastPrice } = LastSnapshotPrice();
const { data: currentMarketPrices } = useGetDefillamaPrice({
addresses: [USDS_ADDRESSES[1], OHM_ADDRESSES[1]],
Expand Down
Loading