Skip to content

Commit

Permalink
fix: minichef hook continue re-render issue (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
SarjuHansaliya authored Nov 10, 2023
1 parent 4c35b95 commit 00edc94
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 31 deletions.
9 changes: 7 additions & 2 deletions src/state/pstake/hooks/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@ import { usePairBalanceHook } from 'src/state/pwallet/hooks';
import { unwrappedToken } from 'src/utils/wrappedCurrency';
import { DoubleSideStakingInfo, MinichefStakingInfo } from '../types';

const DEFIEDGE_TOKEN = '0xd947375F78df5B8FeEa6814eCd999ee64507a057';

export const useMinichefPools = (): { [key: string]: number } => {
const minichefContract = useMiniChefContract();
const lpTokens = useSingleCallResult(minichefContract, 'lpTokens', []).result;
const blocksPerFetch = useMemo(() => ({ blocksPerFetch: 100 }), []);
const lpTokens = useSingleCallResult(minichefContract, 'lpTokens', undefined, blocksPerFetch).result;
const lpTokensArr = lpTokens?.[0];

return useMemo(() => {
const poolMap: { [key: string]: number } = {};
if (lpTokensArr) {
lpTokensArr.forEach((address: string, index: number) => {
poolMap[address] = index;
if (address !== DEFIEDGE_TOKEN) {
poolMap[address] = index;
}
});
}
return poolMap;
Expand Down
65 changes: 36 additions & 29 deletions src/state/pstake/hooks/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { useUSDCPrice } from 'src/hooks/useUSDCPrice/evm';
import { useShouldUseSubgraph } from 'src/state/papplication/hooks';
import { useMiniChefContract } from '../../../hooks/useContract';
import {
NEVER_RELOAD,
useMultipleContractSingleData,
useSingleCallResult,
useSingleContractMultipleData,
Expand Down Expand Up @@ -62,17 +63,15 @@ const dummyApr: AprResult = {
swapFeeApr: 0,
};

const DEFIEDGE_TOKEN = '0xd947375F78df5B8FeEa6814eCd999ee64507a057';

export const useMinichefStakingInfos = (version = 2, pairToFilterBy?: Pair | null): MinichefStakingInfo[] => {
const { account } = usePangolinWeb3();
const chainId = useChainId();

const minichefContract = useMiniChefContract();
const poolMap = useMinichefPools();
const lpTokens = Object.keys(poolMap);
const lpTokens = useMemo(() => Object.keys(poolMap), [poolMap]);

const pids = Object.values(poolMap).map((pid) => pid.toString());
const pids = useMemo(() => Object.values(poolMap).map((pid) => pid.toString()), [poolMap]);

const { data: farmsAprs } = useMichefFarmsAprs(pids);

Expand All @@ -81,13 +80,22 @@ export const useMinichefStakingInfos = (version = 2, pairToFilterBy?: Pair | nul
lpTokens.shift();
}

if (chainId === ChainId.AVALANCHE) {
const index = lpTokens.indexOf(DEFIEDGE_TOKEN);
index >= 0 && delete lpTokens[index];
}
const emptyArr = useMemo(() => [], []);

const _tokens0Call = useMultipleContractSingleData(lpTokens, PANGOLIN_PAIR_INTERFACE, 'token0', []);
const _tokens1Call = useMultipleContractSingleData(lpTokens, PANGOLIN_PAIR_INTERFACE, 'token1', []);
const _tokens0Call = useMultipleContractSingleData(
lpTokens,
PANGOLIN_PAIR_INTERFACE,
'token0',
undefined,
NEVER_RELOAD,
);
const _tokens1Call = useMultipleContractSingleData(
lpTokens,
PANGOLIN_PAIR_INTERFACE,
'token1',
undefined,
NEVER_RELOAD,
);

const tokens0Adrr = useMemo(() => {
return _tokens0Call.map((result) => {
Expand Down Expand Up @@ -130,7 +138,7 @@ export const useMinichefStakingInfos = (version = 2, pairToFilterBy?: Pair | nul
return _infoTokens;
}, [chainId, minichefContract, tokens0, tokens1, pairToFilterBy, version]);

const _tokens = useMemo(() => (info ? info.map(({ tokens }) => tokens) : []), [info]);
const _tokens = useMemo(() => (info ? info.map(({ tokens }) => tokens) : emptyArr), [info]);
const pairs = usePairsContract(_tokens);
// @dev: If no farms load, you likely loaded an incorrect config from doubleSideConfig.js
// Enable this and look for an invalid pair
Expand All @@ -139,56 +147,55 @@ export const useMinichefStakingInfos = (version = 2, pairToFilterBy?: Pair | nul
return pairs.map(([, pair]) => pair?.liquidityToken.address);
}, [pairs]);

const minichefAddress = useMemo(() => [MINICHEF_ADDRESS[chainId]], [chainId]);
const pairTotalSupplies = useMultipleContractSingleData(pairAddresses, ERC20_INTERFACE, 'totalSupply');
const balances = useMultipleContractSingleData(pairAddresses, ERC20_INTERFACE, 'balanceOf', [
MINICHEF_ADDRESS[chainId],
]);
const balances = useMultipleContractSingleData(pairAddresses, ERC20_INTERFACE, 'balanceOf', minichefAddress);

const [avaxPngPairState, avaxPngPair] = usePair(WAVAX[chainId], PNG[chainId]);

const poolIdArray = useMemo(() => {
if (!pairAddresses || !poolMap) return [];
if (!pairAddresses || !poolMap) return emptyArr;

const NOT_FOUND = -1;
const results = pairAddresses.map((address) => poolMap[address ?? ''] ?? NOT_FOUND);
if (results.some((result) => result === NOT_FOUND)) return [];
if (results.some((result) => result === NOT_FOUND)) return emptyArr;
return results;
}, [poolMap, pairAddresses]);

const poolsIdInput = useMemo(() => {
if (!poolIdArray) return [];
if (!poolIdArray) return emptyArr;
return poolIdArray.map((pid) => [pid]);
}, [poolIdArray]);

const poolInfos = useSingleContractMultipleData(minichefContract, 'poolInfo', poolsIdInput ?? []);
const poolInfos = useSingleContractMultipleData(minichefContract, 'poolInfo', poolsIdInput ?? emptyArr);

const rewarders = useSingleContractMultipleData(minichefContract, 'rewarder', poolsIdInput ?? []);
const rewarders = useSingleContractMultipleData(minichefContract, 'rewarder', poolsIdInput ?? emptyArr);

const userInfoInput = useMemo(() => {
if (!poolIdArray || !account) return [];
if (!poolIdArray || !account) return emptyArr;
return poolIdArray.map((pid) => [pid, account]);
}, [poolIdArray, account]);

const userInfos = useSingleContractMultipleData(minichefContract, 'userInfo', userInfoInput ?? []);
const userInfos = useSingleContractMultipleData(minichefContract, 'userInfo', userInfoInput ?? emptyArr);

const pendingRewards = useSingleContractMultipleData(minichefContract, 'pendingReward', userInfoInput ?? []);
const pendingRewards = useSingleContractMultipleData(minichefContract, 'pendingReward', userInfoInput ?? emptyArr);

const rewardsAddresses = useMemo(() => {
if ((rewarders || []).length === 0) return [];
if (rewarders.some((item) => item.loading)) return [];
if ((rewarders || []).length === 0) return emptyArr;
if (rewarders.some((item) => item.loading)) return emptyArr;
return rewarders.map((reward) => reward?.result?.[0]);
}, [rewarders]);

const rewardTokensMultipliers = useMultipleContractSingleData(
rewardsAddresses,
REWARDER_VIA_MULTIPLIER_INTERFACE,
'getRewardMultipliers',
[],
undefined,
);

const rewardPerSecond = useSingleCallResult(minichefContract, 'rewardPerSecond', []).result;
const totalAllocPoint = useSingleCallResult(minichefContract, 'totalAllocPoint', []).result;
const rewardsExpiration = useSingleCallResult(minichefContract, 'rewardsExpiration', []).result;
const rewardPerSecond = useSingleCallResult(minichefContract, 'rewardPerSecond', undefined).result;
const totalAllocPoint = useSingleCallResult(minichefContract, 'totalAllocPoint', undefined).result;
const rewardsExpiration = useSingleCallResult(minichefContract, 'rewardsExpiration', undefined).result;
const usdPriceTmp = useUSDCPrice(WAVAX[chainId]);
const usdPrice = CHAINS[chainId]?.mainnet ? usdPriceTmp : undefined;

Expand Down Expand Up @@ -334,7 +341,7 @@ export const useMinichefStakingInfos = (version = 2, pairToFilterBy?: Pair | nul
: undefined;
} else {
// Contains no stablecoin, WAVAX, nor PNG
console.error(`Could not identify total staked value for pair ${pair.liquidityToken.address}`);
console.info(`Could not identify total staked value for pair ${pair.liquidityToken.address}`);
}

const getHypotheticalWeeklyRewardRate = (
Expand Down

0 comments on commit 00edc94

Please sign in to comment.