Skip to content

Commit

Permalink
step up
Browse files Browse the repository at this point in the history
  • Loading branch information
brucedonovan committed Dec 15, 2023
1 parent 61d3cc8 commit 63db42c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
31 changes: 18 additions & 13 deletions src/contexts/UserContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,6 @@ const UserProvider = ({ children }: { children: ReactNode }) => {
const { forkStartBlock } = useFork();

const {
// data: assetBalances,
// isLoading: assetsLoading,
// status: assetsStatus,
refetch: refetchAssetBalances,
} = useBalances(
Array.from(assetRootMap.values()), // asset list : assetRoot[]
Expand Down Expand Up @@ -261,7 +258,9 @@ const UserProvider = ({ children }: { children: ReactNode }) => {
updateState({ type: UserState.ASSETS_LOADING, payload: true });

/* refetch the asset balances */
const _assetBalances = (await refetchAssetBalances()).data as BalanceData[];
const _assetBalances = (await refetchAssetBalances()).data;

console.log( _assetBalances)

/**
* NOTE! this block Below is just a place holder for if EVER async updates of assets are required.
Expand All @@ -270,7 +269,7 @@ const UserProvider = ({ children }: { children: ReactNode }) => {
const updatedAssets = await Promise.all(
assetList.map(async (asset) => {
// get the balance of the asset from the assetsBalance array
const { balance, balance_ } = _assetBalances.find((a) => a.id.toLowerCase() === asset.id.toLowerCase()) || {
const { balance, balance_ } = _assetBalances!.find((a) => a.id.toLowerCase() === asset.id.toLowerCase()) || {
balance: ZERO_BN,
balance_: '0',
};
Expand Down Expand Up @@ -481,7 +480,7 @@ const UserProvider = ({ children }: { children: ReactNode }) => {
if (account) {
_accountData = await Promise.all(
_publicData.map(async (series): Promise<ISeries> => {
const [poolTokens, fyTokenBalance] = (await multicall({
const [ poolTokensResult, fyTokenBalanceResult ] = await multicall({
contracts: [
{
address: series.poolContract.address as `0x${string}`,
Expand All @@ -496,9 +495,12 @@ const UserProvider = ({ children }: { children: ReactNode }) => {
args: [account],
},
],
})) as unknown as BigNumber[];
});

const poolPercent = mulDecimal(divDecimal(poolTokens || ZERO_BN, series.totalSupply), '100');
const poolTokens = BigNumber.from(poolTokensResult.result);
const fyTokenBalance = BigNumber.from(fyTokenBalanceResult.result);

const poolPercent = '0'// mulDecimal(divDecimal(poolTokens || ZERO_BN, series.totalSupply), '100');

return {
...series,
Expand Down Expand Up @@ -576,7 +578,7 @@ const UserProvider = ({ children }: { children: ReactNode }) => {

/* Attatch the current series (if any) */
const currentSeries = _seriesList.find((s: ISeriesRoot) =>
fyToken ? s.address.toLowerCase() === (fyToken as String).toLowerCase() : undefined
fyToken ? s.address.toLowerCase() === (fyToken as String) : undefined
);

if (currentSeries) {
Expand Down Expand Up @@ -666,22 +668,25 @@ const UserProvider = ({ children }: { children: ReactNode }) => {
const _accountData = account
? await Promise.all(
_publicData.map(async (_strategy: IStrategy): Promise<IStrategy> => {
const [accountBalance, accountPoolBalance] = (await multicall({
const [accountBalanceResult, accountPoolBalanceResult] = await multicall({
contracts: [
{
address: _strategy.strategyContract.address as `0x${string}`,
abi: _strategy.strategyContract.interface as any,
abi: contractTypes.StrategyV2_1__factory.abi as any,
functionName: 'balanceOf',
args: [account],
},
{
address: _strategy.currentSeries?.poolContract.address as `0x${string}`,
abi: _strategy.currentSeries?.poolContract.interface as any,
abi: contractTypes.Pool__factory.abi as any,
functionName: 'balanceOf',
args: [account],
},
],
})) as unknown as BigNumber[];
});

const accountBalance = BigNumber.from(accountBalanceResult.result || 0);
const accountPoolBalance = BigNumber.from(accountPoolBalanceResult.result || 0);

// const stratConnected = _strategy.strategyContract.connect(signer!);
// const accountRewards =
Expand Down
19 changes: 11 additions & 8 deletions src/hooks/useBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,38 @@ const useBalances = (assetList: IAssetRoot[], enabled: boolean = false) => {
address: a.address as Address,
args: a.tokenIdentifier ? [account, a.tokenIdentifier] : [account], // handle erc1155 tokens with tokenIdentifier
functionName: 'balanceOf',
abi: a.tokenIdentifier ? erc1155ABI : erc20ABI, // : a!.assetContract!.interface!.format()! as const,
abi: a.tokenIdentifier ? erc1155ABI : erc20ABI as any, // : a!.assetContract!.interface!.format()! as const,
};
}): [],
[account, assetList]
);

const formatData = (_data: BigNumber[]) =>
const formatData = (_data: any[]) =>

_data ?
_data?.map((d: any, i: number) => {
return {
id: assetList[i].id,
balance: d || ethers.constants.Zero,
balance_: d ? formatUnits(d, assetList[i].decimals) : '0',
balance: BigNumber.from(d.result) || ethers.constants.Zero,
balance_: d.result ? formatUnits(BigNumber.from(d.result), assetList[i].decimals) : '0',
} as BalanceData;
});
}) :
[];

const {
data,
isLoading,
isFetched,
isError,
refetch,
} = useContractReads({
contracts,
enabled: false, // false so that the hook only runs on demand (when refetch() is called)
select: (data: any[]): BalanceData[] => formatData(data),
select: (data: any[]) => formatData(data),
});


isLoading && console.log('::: Refetching Asset balances :::');

if (isError) return { data: [], isLoading, refetch };
return { data, isLoading, refetch };
};

Expand Down

0 comments on commit 63db42c

Please sign in to comment.