From f90bbf0ca0f14107f5b13b0822fd3cc9c22d6bf4 Mon Sep 17 00:00:00 2001 From: bojan07 Date: Mon, 2 Oct 2023 15:38:12 -0400 Subject: [PATCH] feat: apply protocol-contracts to use-balances and use-named-contracts for staking page --- .../hooks/contracts/use-named-contracts.tsx | 12 +------- .../components/lib/hooks/use-balances.tsx | 30 ++++++++++++------- packages/dapp/components/lib/utils.ts | 6 ++-- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/packages/dapp/components/lib/hooks/contracts/use-named-contracts.tsx b/packages/dapp/components/lib/hooks/contracts/use-named-contracts.tsx index 61723bd0f..d8ce7d7dd 100644 --- a/packages/dapp/components/lib/hooks/contracts/use-named-contracts.tsx +++ b/packages/dapp/components/lib/hooks/contracts/use-named-contracts.tsx @@ -1,16 +1,7 @@ import useWeb3, { PossibleProviders } from "../use-web-3"; -import Deployed_Contracts from "@ubiquity/contracts/deployments.json"; import NAMED_ACCOUNTS from "../../../config/named-accounts.json"; -import { getCurveFactoryContract, getDebtCouponManagerContract, getERC20Contract, getIJarContract, getYieldProxyContract } from "@/components/utils/contracts"; - -const getDebtCouponManagerAddress = () => { - const contractDeployments: Record = Deployed_Contracts; - const record = contractDeployments["1"] ?? {}; - const contract = record?.contracts ? record?.contracts["DebtCouponManager"] : undefined; - return contract ? contract.address : undefined; -}; -export const DEBT_COUPON_MANAGER_ADDRESS = getDebtCouponManagerAddress(); +import { getCurveFactoryContract, getERC20Contract, getIJarContract, getYieldProxyContract } from "@/components/utils/contracts"; export type NamedContracts = ReturnType | null; export function connectedContracts(provider: NonNullable) { @@ -20,7 +11,6 @@ export function connectedContracts(provider: NonNullable) { usdc: getERC20Contract(NAMED_ACCOUNTS.USDC, provider), dai: getERC20Contract(NAMED_ACCOUNTS.DAI, provider), usdt: getERC20Contract(NAMED_ACCOUNTS.USDT, provider), - debtCouponManager: getDebtCouponManagerContract(DEBT_COUPON_MANAGER_ADDRESS, provider), jarUsdc: getIJarContract(NAMED_ACCOUNTS.jarUSDCAddr, provider), }; } diff --git a/packages/dapp/components/lib/hooks/use-balances.tsx b/packages/dapp/components/lib/hooks/use-balances.tsx index 6eaf6c53a..6fbb3c07e 100644 --- a/packages/dapp/components/lib/hooks/use-balances.tsx +++ b/packages/dapp/components/lib/hooks/use-balances.tsx @@ -1,10 +1,12 @@ import { erc1155BalanceOf } from "@/lib/utils"; import { BigNumber, Contract } from "ethers"; import { createContext, useContext, useEffect, useState } from "react"; -import useManagerManaged from "./contracts/use-manager-managed"; import useNamedContracts from "./contracts/use-named-contracts"; import useWalletAddress from "./use-wallet-address"; import { ChildrenShim } from "./children-shim-d"; +import useProtocolContracts from "@/components/lib/hooks/contracts/use-protocol-contracts"; +import { getERC20Contract, getIMetaPoolContract } from "@/components/utils/contracts"; +import useWeb3 from "@/components/lib/hooks/use-web-3"; export interface Balances { uad: BigNumber; @@ -26,19 +28,25 @@ export const BalancesContext = createContext<[Balances | null, RefreshBalances]> export const BalancesContextProvider: React.FC = ({ children }) => { const [balances, setBalances] = useState(null); const [walletAddress] = useWalletAddress(); - const managedContracts = useManagerManaged(); const namedContracts = useNamedContracts(); + const protocolContracts = useProtocolContracts(); + const { provider } = useWeb3(); async function refreshBalances() { - if (walletAddress && managedContracts && namedContracts) { + if (walletAddress && namedContracts && protocolContracts && provider) { + const _3crvToken = protocolContracts.managerFacet && await protocolContracts.managerFacet.curve3PoolTokenAddress(); + const dollar3poolMarket = protocolContracts.managerFacet && await protocolContracts.managerFacet.stableSwapMetaPoolAddress(); + const _3crvTokenContract = getERC20Contract(_3crvToken, provider); + const dollarMetapool = getIMetaPoolContract(dollar3poolMarket, provider); + const [uad, _3crv, uad3crv, ucr, ubq, ucrNft, stakingShares, usdc, dai, usdt] = await Promise.all([ - managedContracts.dollarToken.balanceOf(walletAddress), - managedContracts._3crvToken.balanceOf(walletAddress), - managedContracts.dollarMetapool.balanceOf(walletAddress), - managedContracts.creditToken.balanceOf(walletAddress), - managedContracts.governanceToken.balanceOf(walletAddress), - erc1155BalanceOf(walletAddress, managedContracts.creditNft), - erc1155BalanceOf(walletAddress, managedContracts.stakingToken), + protocolContracts.dollarToken && protocolContracts.dollarToken.balanceOf(walletAddress), + _3crvTokenContract.balanceOf(walletAddress), + dollarMetapool.balanceOf(walletAddress), + protocolContracts.creditToken && protocolContracts.creditToken.balanceOf(walletAddress), + protocolContracts.governanceToken && protocolContracts.governanceToken.balanceOf(walletAddress), + erc1155BalanceOf(walletAddress, protocolContracts.creditNft), + erc1155BalanceOf(walletAddress, protocolContracts.stakingShare), namedContracts.usdc.balanceOf(walletAddress), namedContracts.dai.balanceOf(walletAddress), namedContracts.usdt.balanceOf(walletAddress), @@ -61,7 +69,7 @@ export const BalancesContextProvider: React.FC = ({ children }) => useEffect(() => { refreshBalances(); - }, [walletAddress, managedContracts]); + }, [walletAddress, protocolContracts]); return {children}; }; diff --git a/packages/dapp/components/lib/utils.ts b/packages/dapp/components/lib/utils.ts index 2caf22cb1..65854ba7f 100644 --- a/packages/dapp/components/lib/utils.ts +++ b/packages/dapp/components/lib/utils.ts @@ -30,11 +30,11 @@ export async function performTransaction(transaction: Promise { - const treasuryIds = await erc1155UbiquityCtr.holderTokens(addr); +export async function erc1155BalanceOf(addr: string, erc1155UbiquityCtr: Contract | null): Promise { + const treasuryIds = erc1155UbiquityCtr && await erc1155UbiquityCtr.holderTokens(addr); const balanceOfs = treasuryIds.map((id: string) => { - return erc1155UbiquityCtr.balanceOf(addr, id); + return erc1155UbiquityCtr && erc1155UbiquityCtr.balanceOf(addr, id); }); const balances = await Promise.all(balanceOfs); let fullBalance = BigNumber.from(0);