Skip to content

Commit

Permalink
feat: used protocol-contracts in use-balances and use-named-contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
bojan07 committed Oct 3, 2023
1 parent 510e7a2 commit 8da767d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -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<string, any> = 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<typeof connectedContracts> | null;
export function connectedContracts(provider: NonNullable<PossibleProviders>) {
Expand All @@ -20,7 +11,6 @@ export function connectedContracts(provider: NonNullable<PossibleProviders>) {
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),
};
}
Expand Down
30 changes: 19 additions & 11 deletions packages/dapp/components/lib/hooks/use-balances.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -26,19 +28,25 @@ export const BalancesContext = createContext<[Balances | null, RefreshBalances]>
export const BalancesContextProvider: React.FC<ChildrenShim> = ({ children }) => {
const [balances, setBalances] = useState<Balances | null>(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),
Expand All @@ -61,7 +69,7 @@ export const BalancesContextProvider: React.FC<ChildrenShim> = ({ children }) =>

useEffect(() => {
refreshBalances();
}, [walletAddress, managedContracts]);
}, [walletAddress, protocolContracts]);

return <BalancesContext.Provider value={[balances, refreshBalances]}>{children}</BalancesContext.Provider>;
};
Expand Down
6 changes: 3 additions & 3 deletions packages/dapp/components/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ export async function performTransaction(transaction: Promise<ContractTransactio
// return false;
}

export async function erc1155BalanceOf(addr: string, erc1155UbiquityCtr: Contract): Promise<BigNumber> {
const treasuryIds = await erc1155UbiquityCtr.holderTokens(addr);
export async function erc1155BalanceOf(addr: string, erc1155UbiquityCtr: Contract | null): Promise<BigNumber> {
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);
Expand Down

0 comments on commit 8da767d

Please sign in to comment.