diff --git a/packages/dapp/components/redeem/lib/use-prices.ts b/packages/dapp/components/redeem/lib/use-prices.ts index cf4df037d..9b22d050e 100644 --- a/packages/dapp/components/redeem/lib/use-prices.ts +++ b/packages/dapp/components/redeem/lib/use-prices.ts @@ -1,28 +1,40 @@ -import useDeployedContracts from "@/components/lib/hooks/contracts/use-deployed-contracts"; -import useManagerManaged from "@/components/lib/hooks/contracts/use-manager-managed"; +import { getIMetaPoolContract } from "@/components/utils/contracts"; +import useProtocolContracts from "@/components/lib/hooks/contracts/use-protocol-contracts"; +import useWeb3 from "@/components/lib/hooks/use-web-3"; import { BigNumber, utils } from "ethers"; import { useEffect, useState } from "react"; const usePrices = (): [BigNumber | null, BigNumber | null, () => Promise] => { - const deployedContracts = useDeployedContracts(); - const managedContracts = useManagerManaged(); + const protocolContracts = useProtocolContracts(); + const { provider } = useWeb3(); const [twapPrice, setTwapPrice] = useState(null); const [spotPrice, setSpotPrice] = useState(null); async function refreshPrices() { - if (managedContracts && deployedContracts) { - const dollarTokenAddress = await deployedContracts.manager.dollarTokenAddress(); - const newTwapPrice = await managedContracts.dollarTwapOracle.consult(dollarTokenAddress); - const newSpotPrice = await managedContracts.dollarMetapool["get_dy(int128,int128,uint256)"](0, 1, utils.parseEther("1")); - setTwapPrice(newTwapPrice); - setSpotPrice(newSpotPrice); + try { + if(!protocolContracts || !provider) { + return; + } + + if(protocolContracts.managerFacet && protocolContracts.twapOracleDollar3poolFacet) { + const dollarTokenAddress = await protocolContracts.managerFacet.dollarTokenAddress(); + const newTwapPrice = await protocolContracts.twapOracleDollar3poolFacet.consult(dollarTokenAddress); + const dollar3poolMarket = await protocolContracts.managerFacet.stableSwapMetaPoolAddress(); + const dollarMetapool = getIMetaPoolContract(dollar3poolMarket, provider) + const newSpotPrice = await dollarMetapool["get_dy(int128,int128,uint256)"](0, 1, utils.parseEther("1")); + setTwapPrice(newTwapPrice); + setSpotPrice(newSpotPrice); + } + + } catch (error) { + console.log("Error in refreshPrices: ", error) } } useEffect(() => { refreshPrices(); - }, [managedContracts, deployedContracts]); + }, [protocolContracts, provider]); return [twapPrice, spotPrice, refreshPrices]; }; diff --git a/packages/dapp/pages/index.tsx b/packages/dapp/pages/index.tsx index 7dd1f428f..6003cdfbc 100644 --- a/packages/dapp/pages/index.tsx +++ b/packages/dapp/pages/index.tsx @@ -2,7 +2,7 @@ import { ethers } from "ethers"; import { FC, useState } from "react"; import "@uniswap/widgets/fonts.css"; -import useManagerManaged from "@/components/lib/hooks/contracts/use-manager-managed"; +import useProtocolContracts from "@/components/lib/hooks/contracts/use-protocol-contracts"; import useEffectAsync from "@/components/lib/hooks/use-effect-async"; import DollarPrice from "@/components/redeem/dollar-price"; import { fetchData } from "@/components/utils/local-data"; @@ -12,10 +12,10 @@ const WalletConnectionWall = dynamic(() => import("@/components/ui/wallet-connec const index: FC = (): JSX.Element => { const [twapPrice, setTwapPrice] = useState(null); - const managedContracts = useManagerManaged(); + const protocolContracts = useProtocolContracts(); useEffectAsync(async () => { - if (managedContracts != null) { + if (protocolContracts != null) { try { console.log(twapPrice, "priced in "); } catch (error) { @@ -23,10 +23,10 @@ const index: FC = (): JSX.Element => { setTwapPrice(null); } } else { - console.log("managedContracts is null"); + console.log("protocolContracts is null"); setTwapPrice(null); } - }, [managedContracts]); + }, [protocolContracts]); if (process.env.DEBUG === "true") { fetchData();