Skip to content

Commit

Permalink
Merge branch 'bip-38' of https://github.com/beanstalkfarms/beanstalk
Browse files Browse the repository at this point in the history
…into bip-38
  • Loading branch information
BrendanSanderson committed Oct 11, 2023
2 parents 2dfa201 + 258508b commit cd9de38
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
23 changes: 21 additions & 2 deletions projects/ui/src/hooks/beanstalk/useDataFeedTokenPrices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useSelector, useDispatch } from 'react-redux';
import { TokenMap } from '../../constants/index';
import { bigNumberResult } from '../../util/Ledger';
import useGetChainToken from '~/hooks/chain/useGetChainToken';
import { DAI, ETH, USDC, USDT } from '../../constants/tokens';
import { CRV3, DAI, ETH, USDC, USDT, WETH } from '../../constants/tokens';
import {
DAI_CHAINLINK_ADDRESSES,
USDT_CHAINLINK_ADDRESSES,
Expand All @@ -14,6 +14,7 @@ import {
import { useAggregatorV3Contract } from '~/hooks/ledger/useContract';
import { AppState } from '../../state/index';
import { updateTokenPrices } from '~/state/beanstalk/tokenPrices/actions';
import useSdk from '../sdk';

const getBNResult = (result: any, decimals: number) => {
const bnResult = bigNumberResult(result);
Expand All @@ -35,16 +36,19 @@ export default function useDataFeedTokenPrices() {
AppState['_beanstalk']['tokenPrices']
>((state) => state._beanstalk.tokenPrices);

const sdk = useSdk();

const daiPriceFeed = useAggregatorV3Contract(DAI_CHAINLINK_ADDRESSES);
const usdtPriceFeed = useAggregatorV3Contract(USDT_CHAINLINK_ADDRESSES);
const usdcPriceFeed = useAggregatorV3Contract(USDC_CHAINLINK_ADDRESSES);
const ethPriceFeed = useAggregatorV3Contract(ETH_CHAINLINK_ADDRESS);
const crv3Pool = sdk.contracts.curve.pools.pool3;
const getChainToken = useGetChainToken();
const dispatch = useDispatch();

const fetch = useCallback(async () => {
if (Object.values(tokenPriceMap).length) return;
if (!daiPriceFeed || !usdtPriceFeed || !usdcPriceFeed || !ethPriceFeed)
if (!daiPriceFeed || !usdtPriceFeed || !usdcPriceFeed || !ethPriceFeed || !crv3Pool)
return;

console.debug('[beanstalk/tokenPrices/useCrvUnderlylingPrices] FETCH');
Expand All @@ -58,6 +62,7 @@ export default function useDataFeedTokenPrices() {
usdcPriceDecimals,
ethPriceData,
ethPriceDecimals,
crv3Price,
] = await Promise.all([
daiPriceFeed.latestRoundData(),
daiPriceFeed.decimals(),
Expand All @@ -67,12 +72,15 @@ export default function useDataFeedTokenPrices() {
usdcPriceFeed.decimals(),
ethPriceFeed.latestRoundData(),
ethPriceFeed.decimals(),
crv3Pool.get_virtual_price(),
]);

const dai = getChainToken(DAI);
const usdc = getChainToken(USDC);
const usdt = getChainToken(USDT);
const eth = getChainToken(ETH);
const weth = getChainToken(WETH);
const crv3 = getChainToken(CRV3);

const priceDataCache: TokenMap<BigNumber> = {};

Expand All @@ -99,6 +107,16 @@ export default function useDataFeedTokenPrices() {
ethPriceData.answer,
ethPriceDecimals
);
priceDataCache[weth.address] = getBNResult(
ethPriceData.answer,
ethPriceDecimals
);
}
if (crv3Price) {
priceDataCache[crv3.address] = getBNResult(
crv3Price,
crv3.decimals
);
}

console.debug(
Expand All @@ -112,6 +130,7 @@ export default function useDataFeedTokenPrices() {
usdtPriceFeed,
usdcPriceFeed,
ethPriceFeed,
crv3Pool,
getChainToken,
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ const sortMap = {
urBEAN: 2,
urBEAN3CRV: 3,
ETH: 4,
DAI: 5,
USDC: 6,
USDT: 7,
WETH: 5,
"3CRV": 6,
DAI: 7,
USDC: 8,
USDT: 9,
} as const;

export type TokenBalanceWithFiatValue = {
Expand Down

0 comments on commit cd9de38

Please sign in to comment.