From d91b53110d151acf9edb4eb2bfb00995b6bd057f Mon Sep 17 00:00:00 2001 From: uncoolzero <107518216+uncoolzero@users.noreply.github.com> Date: Mon, 9 Oct 2023 21:17:42 -0300 Subject: [PATCH 1/3] Add WETH/3CRV price --- .../hooks/beanstalk/useDataFeedTokenPrices.ts | 22 ++++++++++++++++--- projects/ui/src/hooks/ledger/useContract.ts | 13 +++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/projects/ui/src/hooks/beanstalk/useDataFeedTokenPrices.ts b/projects/ui/src/hooks/beanstalk/useDataFeedTokenPrices.ts index 1f03da2a56..3ea73776c8 100644 --- a/projects/ui/src/hooks/beanstalk/useDataFeedTokenPrices.ts +++ b/projects/ui/src/hooks/beanstalk/useDataFeedTokenPrices.ts @@ -4,14 +4,14 @@ 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, USDC_CHAINLINK_ADDRESSES, ETH_CHAINLINK_ADDRESS, } from '../../constants/addresses'; -import { useAggregatorV3Contract } from '~/hooks/ledger/useContract'; +import { use3CRVPoolContract, useAggregatorV3Contract } from '~/hooks/ledger/useContract'; import { AppState } from '../../state/index'; import { updateTokenPrices } from '~/state/beanstalk/tokenPrices/actions'; @@ -39,12 +39,13 @@ export default function useDataFeedTokenPrices() { const usdtPriceFeed = useAggregatorV3Contract(USDT_CHAINLINK_ADDRESSES); const usdcPriceFeed = useAggregatorV3Contract(USDC_CHAINLINK_ADDRESSES); const ethPriceFeed = useAggregatorV3Contract(ETH_CHAINLINK_ADDRESS); + const crv3Pool = use3CRVPoolContract(); 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'); @@ -58,6 +59,7 @@ export default function useDataFeedTokenPrices() { usdcPriceDecimals, ethPriceData, ethPriceDecimals, + crv3Price, ] = await Promise.all([ daiPriceFeed.latestRoundData(), daiPriceFeed.decimals(), @@ -67,12 +69,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 = {}; @@ -99,6 +104,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( @@ -112,6 +127,7 @@ export default function useDataFeedTokenPrices() { usdtPriceFeed, usdcPriceFeed, ethPriceFeed, + crv3Pool, getChainToken, ]); diff --git a/projects/ui/src/hooks/ledger/useContract.ts b/projects/ui/src/hooks/ledger/useContract.ts index fd970bef22..acc9cd1af7 100644 --- a/projects/ui/src/hooks/ledger/useContract.ts +++ b/projects/ui/src/hooks/ledger/useContract.ts @@ -11,6 +11,7 @@ import BEANFT_WINTER_ABI from '~/constants/abi/BeaNFT/BeaNFTWinter.json'; import BEANFT_BARNRAISE_ABI from '~/constants/abi/BeaNFT/BeaNFTBarnRaise.json'; import AGGREGATOR_V3_ABI from '~/constants/abi/Chainlink/AggregatorV3.json'; import GNOSIS_DELEGATE_REGISTRY_ABI from '~/constants/abi/Gnosis/DelegateRegistry.json'; +import CURVE3POOL_ABI from '~/constants/abi/Curve/Pool/Curve3Pool.json'; import useChainConstant from '../chain/useChainConstant'; import { SupportedChainId } from '~/constants/chains'; import { @@ -21,6 +22,7 @@ import { BEANSTALK_FERTILIZER_ADDRESSES, BEANSTALK_PRICE_ADDRESSES, DELEGATES_REGISTRY_ADDRESSES, + POOL3_ADDRESSES } from '~/constants/addresses'; import { ChainConstant } from '~/constants'; import { getChainConstant } from '~/util/Chain'; @@ -35,6 +37,7 @@ import { ERC20, AggregatorV3, DelegateRegistry, + Curve3Pool } from '~/generated/index'; export type AddressOrAddressMap = string | ChainConstant; @@ -221,6 +224,16 @@ export function useAggregatorV3Contract( }) as AggregatorV3; } +export function use3CRVPoolContract(signer?: ethers.Signer | null) { + const address = useChainConstant(POOL3_ADDRESSES); + const provider = useProvider(); + return useWagmiContract({ + address, + abi: CURVE3POOL_ABI, + signerOrProvider: signer || provider, + }) as Curve3Pool; +} + export function useDelegatesRegistryContract(signer?: ethers.Signer | null) { const address = useChainConstant(DELEGATES_REGISTRY_ADDRESSES); const provider = useProvider(); From d0ddab15d5642e40425a79f10f472d000df94338 Mon Sep 17 00:00:00 2001 From: uncoolzero <107518216+uncoolzero@users.noreply.github.com> Date: Wed, 11 Oct 2023 14:48:13 -0300 Subject: [PATCH 2/3] Sorting --- .../ui/src/hooks/farmer/useFarmerBalancesWithFiatValue.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/projects/ui/src/hooks/farmer/useFarmerBalancesWithFiatValue.ts b/projects/ui/src/hooks/farmer/useFarmerBalancesWithFiatValue.ts index 0818191f88..e338171301 100644 --- a/projects/ui/src/hooks/farmer/useFarmerBalancesWithFiatValue.ts +++ b/projects/ui/src/hooks/farmer/useFarmerBalancesWithFiatValue.ts @@ -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 = { From 402aad6af4d0c663e14cad2aa2a8797d37d7cd33 Mon Sep 17 00:00:00 2001 From: uncoolzero <107518216+uncoolzero@users.noreply.github.com> Date: Wed, 11 Oct 2023 15:25:24 -0300 Subject: [PATCH 3/3] Refactor --- .../src/hooks/beanstalk/useDataFeedTokenPrices.ts | 7 +++++-- projects/ui/src/hooks/ledger/useContract.ts | 13 ------------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/projects/ui/src/hooks/beanstalk/useDataFeedTokenPrices.ts b/projects/ui/src/hooks/beanstalk/useDataFeedTokenPrices.ts index 3ea73776c8..c34b66eb95 100644 --- a/projects/ui/src/hooks/beanstalk/useDataFeedTokenPrices.ts +++ b/projects/ui/src/hooks/beanstalk/useDataFeedTokenPrices.ts @@ -11,9 +11,10 @@ import { USDC_CHAINLINK_ADDRESSES, ETH_CHAINLINK_ADDRESS, } from '../../constants/addresses'; -import { use3CRVPoolContract, useAggregatorV3Contract } from '~/hooks/ledger/useContract'; +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); @@ -35,11 +36,13 @@ 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 = use3CRVPoolContract(); + const crv3Pool = sdk.contracts.curve.pools.pool3; const getChainToken = useGetChainToken(); const dispatch = useDispatch(); diff --git a/projects/ui/src/hooks/ledger/useContract.ts b/projects/ui/src/hooks/ledger/useContract.ts index acc9cd1af7..fd970bef22 100644 --- a/projects/ui/src/hooks/ledger/useContract.ts +++ b/projects/ui/src/hooks/ledger/useContract.ts @@ -11,7 +11,6 @@ import BEANFT_WINTER_ABI from '~/constants/abi/BeaNFT/BeaNFTWinter.json'; import BEANFT_BARNRAISE_ABI from '~/constants/abi/BeaNFT/BeaNFTBarnRaise.json'; import AGGREGATOR_V3_ABI from '~/constants/abi/Chainlink/AggregatorV3.json'; import GNOSIS_DELEGATE_REGISTRY_ABI from '~/constants/abi/Gnosis/DelegateRegistry.json'; -import CURVE3POOL_ABI from '~/constants/abi/Curve/Pool/Curve3Pool.json'; import useChainConstant from '../chain/useChainConstant'; import { SupportedChainId } from '~/constants/chains'; import { @@ -22,7 +21,6 @@ import { BEANSTALK_FERTILIZER_ADDRESSES, BEANSTALK_PRICE_ADDRESSES, DELEGATES_REGISTRY_ADDRESSES, - POOL3_ADDRESSES } from '~/constants/addresses'; import { ChainConstant } from '~/constants'; import { getChainConstant } from '~/util/Chain'; @@ -37,7 +35,6 @@ import { ERC20, AggregatorV3, DelegateRegistry, - Curve3Pool } from '~/generated/index'; export type AddressOrAddressMap = string | ChainConstant; @@ -224,16 +221,6 @@ export function useAggregatorV3Contract( }) as AggregatorV3; } -export function use3CRVPoolContract(signer?: ethers.Signer | null) { - const address = useChainConstant(POOL3_ADDRESSES); - const provider = useProvider(); - return useWagmiContract({ - address, - abi: CURVE3POOL_ABI, - signerOrProvider: signer || provider, - }) as Curve3Pool; -} - export function useDelegatesRegistryContract(signer?: ethers.Signer | null) { const address = useChainConstant(DELEGATES_REGISTRY_ADDRESSES); const provider = useProvider();