Skip to content

Commit

Permalink
feat: refactor some hooks and show expired farms with pending rewards (
Browse files Browse the repository at this point in the history
…#476)

* fix: typo in comment

* feat: added usemultiplecontractmultipledata hook

* fix: address is "false" in state

* feat: added extrapendingpewards for super farms rewards

* refactor: useextrapendingrewards hook

* feat: show expired farms with pending rewards

* fix: code smells

* fix: tsc and lint

* fix: sonarcloud bug

* fix: stakingInfo is undefined

* fix: approve not is enabled
  • Loading branch information
prodesert22 committed Jul 22, 2023
1 parent 3757d93 commit 3f94746
Show file tree
Hide file tree
Showing 17 changed files with 365 additions and 216 deletions.
2 changes: 1 addition & 1 deletion src/apollo/pangochef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export const GET_FARMS_STAKED = gql`
`;

/**
* this hook is useful to get information for pangochef famrs from subgraph
* this hook is useful to get information for pangochef farms from subgraph
* @param
* @returns list farms
*/
Expand Down
2 changes: 1 addition & 1 deletion src/components/Pools/DetailModal/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const Header: React.FC<Props> = ({ stakingInfo, onClose }) => {
const swapFeeAPR = stakingInfo?.swapFeeApr || 0;
// for rest we get the data from contract calls if exist, else put 0 for this data
if (cheftType === ChefType.PANGO_CHEF) {
const userApr = (stakingInfo as PangoChefInfo).userApr;
const userApr = (stakingInfo as PangoChefInfo)?.userApr ?? 0;

return {
totalApr: stakingAPR + swapFeeAPR + extraFarmAPR,
Expand Down
2 changes: 1 addition & 1 deletion src/components/Pools/PangoChef/ClaimReward/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const ClaimRewardV3 = ({ stakingInfo, onClose, redirectToCompound }: ClaimProps)
hederaAssociated: isHederaTokenAssociated,
} = useHederaTokenAssociated(notAssociateTokens?.[0]?.address, notAssociateTokens?.[0]?.symbol);

const { callback: claimRewardCallback } = useClaimRewardCallback(stakingInfo.pid, stakingInfo.poolType);
const { callback: claimRewardCallback } = useClaimRewardCallback(stakingInfo.pid);

function wrappedOnDismiss() {
setHash(undefined);
Expand Down
14 changes: 10 additions & 4 deletions src/components/Pools/PangoChef/Stake/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import React, { useCallback, useContext, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { ThemeContext } from 'styled-components';
import { Box, Button, DoubleCurrencyLogo, NumberOptions, Stat, Text, TextInput, Tooltip } from 'src/components';
import { FARM_TYPE } from 'src/constants';
import { BIG_INT_ZERO, FARM_TYPE } from 'src/constants';
import { PNG } from 'src/constants/tokens';
import { usePair } from 'src/data/Reserves';
import { useChainId, usePangolinWeb3 } from 'src/hooks';
Expand Down Expand Up @@ -280,15 +280,19 @@ const Stake = ({ onComplete, type, stakingInfo, combinedApr }: StakeProps) => {
</>
);
} else {
console.log({ approval });
return (
<Buttons>
<Button
variant={approval === ApprovalState.APPROVED ? 'confirm' : 'primary'}
onClick={onAttemptToApprove}
isDisabled={
approval !== ApprovalState.NOT_APPROVED || !JSBI.greaterThan(stakingInfo.multiplier, JSBI.BigInt(0))
approval !== ApprovalState.NOT_APPROVED ||
(JSBI.equal(stakingInfo.multiplier, BIG_INT_ZERO) &&
stakingInfo.extraPendingRewards.length > 0 &&
stakingInfo.extraPendingRewards.some((pendigRewards) => JSBI.equal(pendigRewards, BIG_INT_ZERO)))
}
loading={attempting && !hash}
loading={approval === ApprovalState.PENDING}
loadingText={t('migratePage.loading')}
>
{t('earn.approve')}
Expand All @@ -301,7 +305,9 @@ const Stake = ({ onComplete, type, stakingInfo, combinedApr }: StakeProps) => {
approval !== ApprovalState.APPROVED ||
!!stakeCallbackError ||
shouldCreateStorage ||
!JSBI.greaterThan(stakingInfo.multiplier, JSBI.BigInt(0))
(JSBI.equal(stakingInfo.multiplier, BIG_INT_ZERO) &&
stakingInfo.extraPendingRewards.length > 0 &&
stakingInfo.extraPendingRewards.some((pendigRewards) => JSBI.equal(pendigRewards, BIG_INT_ZERO)))
}
onClick={onConfirm}
loading={attempting && !hash}
Expand Down
8 changes: 6 additions & 2 deletions src/components/Pools/Pool/PoolV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ const PoolV2: React.FC<Props> = ({ type, setMenu, activeMenu, menuItems, miniChe
JSBI.greaterThan(stakingInfo.multiplier, BIG_INT_ZERO),
);
case PoolType.own:
// return all farms with staked amount greater than 0
// return all farms with staked amount greater than 0 or user have pending rewards on farm
return (miniChefStakingInfo || []).filter((stakingInfo) => {
return Boolean(stakingInfo.stakedAmount.greaterThan('0'));
return Boolean(
stakingInfo.stakedAmount.greaterThan('0') ||
stakingInfo.earnedAmount.greaterThan('0') ||
stakingInfo.extraPendingRewards.some((pendingRewards) => JSBI.greaterThan(pendingRewards, BIG_INT_ZERO)),
);
});
case PoolType.superFarms:
// return all farms with reward tokens address greater than 1 and with weight (multipler) greater than 0
Expand Down
8 changes: 6 additions & 2 deletions src/components/Pools/Pool/PoolV3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ const PoolV3: React.FC<Props> = ({ type, setMenu, activeMenu, menuItems, pangoCh
JSBI.greaterThan(stakingInfo.multiplier, BIG_INT_ZERO),
);
case PoolType.own:
// return all farms with staked amount greater than 0
// return all farms with staked amount greater than 0 or user have pending rewards on farm
return (pangoChefStakingInfos || []).filter((stakingInfo) => {
return Boolean(stakingInfo.stakedAmount.greaterThan('0'));
return Boolean(
stakingInfo.stakedAmount.greaterThan('0') ||
stakingInfo.earnedAmount.greaterThan('0') ||
stakingInfo.extraPendingRewards.some((pendingRewards) => JSBI.greaterThan(pendingRewards, BIG_INT_ZERO)),
);
});
case PoolType.superFarms:
// return all farms with reward tokens address greater than 0 and with weight (multipler) greater than 0
Expand Down
7 changes: 5 additions & 2 deletions src/components/Pools/Pool/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BigNumber } from 'ethers';
import { BIG_INT_ZERO } from 'src/constants';
import { PNG } from 'src/constants/tokens';
import { useChainId } from 'src/hooks';
import { PangoChefInfo, PoolType } from 'src/state/ppangoChef/types';
import { PangoChefInfo } from 'src/state/ppangoChef/types';
import { MinichefStakingInfo } from 'src/state/pstake/types';

// get data for all farms
Expand Down Expand Up @@ -47,6 +47,7 @@ export const useGetMinichefStakingInfos = (): MinichefStakingInfo => {
getHypotheticalWeeklyRewardRate: () => {
return new TokenAmount(png, JSBI.BigInt(0));
},
extraPendingRewards: [],
} as MinichefStakingInfo;
};

Expand All @@ -65,6 +66,8 @@ export const useGetPangoChefInfos = (): PangoChefInfo => {
},
userRewardRate: new Fraction('1000000000000000'),
pairPrice: new Fraction('1', '20'),
poolType: PoolType.ERC20_POOL,
poolRewardRate: new Fraction('1000', '1'),
userApr: 0,
lockCount: undefined,
} as PangoChefInfo;
};
16 changes: 12 additions & 4 deletions src/components/Pools/Stake/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
TextInput,
TransactionCompleted,
} from 'src/components';
import { FARM_TYPE } from 'src/constants';
import { BIG_INT_ZERO, FARM_TYPE } from 'src/constants';
import { PNG } from 'src/constants/tokens';
import { usePair } from 'src/data/Reserves';
import { useChainId, useLibrary, usePangolinWeb3, useRefetchMinichefSubgraph } from 'src/hooks';
Expand All @@ -31,7 +31,7 @@ import {
useGetPoolDollerWorth,
useMinichefPools,
} from 'src/state/pstake/hooks/common';
import { DoubleSideStakingInfo, SpaceType } from 'src/state/pstake/types';
import { DoubleSideStakingInfo, MinichefStakingInfo, SpaceType } from 'src/state/pstake/types';
import { useTransactionAdder } from 'src/state/ptransactions/hooks';
import { useTokenBalance } from 'src/state/pwallet/hooks/evm';
import { waitForTransaction } from 'src/utils';
Expand Down Expand Up @@ -514,7 +514,11 @@ const Stake = ({ version, onComplete, type, stakingInfo, combinedApr }: StakePro
isDisabled={
approval !== ApprovalState.NOT_APPROVED ||
signatureData !== null ||
!JSBI.greaterThan(stakingInfo.multiplier, JSBI.BigInt(0))
(JSBI.equal(stakingInfo?.multiplier, BIG_INT_ZERO) &&
(stakingInfo as MinichefStakingInfo)?.extraPendingRewards.length > 0 &&
(stakingInfo as MinichefStakingInfo)?.extraPendingRewards.some((pendigRewards) =>
JSBI.equal(pendigRewards, BIG_INT_ZERO),
))
}
loading={attempting && !hash}
loadingText={t('migratePage.loading')}
Expand All @@ -527,7 +531,11 @@ const Stake = ({ version, onComplete, type, stakingInfo, combinedApr }: StakePro
isDisabled={
!!error ||
(signatureData === null && approval !== ApprovalState.APPROVED) ||
!JSBI.greaterThan(stakingInfo.multiplier, JSBI.BigInt(0))
(JSBI.equal(stakingInfo?.multiplier, BIG_INT_ZERO) &&
(stakingInfo as MinichefStakingInfo)?.extraPendingRewards.length > 0 &&
(stakingInfo as MinichefStakingInfo)?.extraPendingRewards.some((pendigRewards) =>
JSBI.equal(pendigRewards, BIG_INT_ZERO),
))
}
onClick={onStake}
loading={attempting && !hash}
Expand Down
9 changes: 7 additions & 2 deletions src/hooks/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getAddress } from '@ethersproject/address';
import { ExternalProvider, Web3Provider as Web3ProviderEthers } from '@ethersproject/providers';
import { ALL_CHAINS, CHAINS, ChainId } from '@pangolindex/sdk';
import { useWeb3React } from '@web3-react/core';
Expand Down Expand Up @@ -38,9 +39,13 @@ export const PangolinWeb3Provider: FC<Web3ProviderProps> = ({
account,
}: Web3ProviderProps) => {
const state = useMemo(() => {
let normalizedAccount;
let normalizedAccount: typeof account;
if (chainId) {
normalizedAccount = isEvmChain(chainId) ? isAddress(account) : account;
if (isEvmChain(chainId) && isAddress(account)) {
normalizedAccount = getAddress(account as string);
} else {
normalizedAccount = account;
}
}

return {
Expand Down
43 changes: 43 additions & 0 deletions src/state/pmulticall/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,49 @@ export function useMultipleContractSingleData(
}, [fragment, results, contractInterface, latestBlockNumber]);
}

/**
* This hook return the result of calls to multiple contracts with multiples inputs
* @param addresses Contracts Addresses
* @param contractInterface Contract interface from ethers
* @param methodName Contract method name to query
* @param callInputs Input of each call
* @param options Options, eg blocks per fetch
* @returns Array of callstate with result, isloading and error
*/
export function useMultipleContractMultipleData(
addresses: (string | undefined)[],
contractInterface: Interface,
methodName: string,
callInputs?: OptionalMethodInputs[],
options?: ListenerOptions,
): CallState[] {
const fragment = useMemo(() => contractInterface.getFunction(methodName), [contractInterface, methodName]);

const calls = useMemo(
() =>
fragment && callInputs && callInputs.length > 0 && addresses && addresses.length > 0
? callInputs.map<Call | undefined>((input, index) => {
const address = addresses[index];
return address && input
? {
address: address,
callData: contractInterface.encodeFunctionData(fragment, input),
}
: undefined;
})
: [],
[callInputs, contractInterface, fragment],
);

const results = useCallsData(calls, options);

const latestBlockNumber = useBlockNumber();

return useMemo(() => {
return results.map((result) => toCallState(result, contractInterface, fragment, latestBlockNumber));
}, [fragment, results, contractInterface, latestBlockNumber]);
}

export function useSingleCallResult(
contract: Contract | null | undefined,
methodName: string,
Expand Down
2 changes: 1 addition & 1 deletion src/state/ppangoChef/hooks/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function usePangoChefExtraFarmApr(

const multipliers = stakingInfo?.rewardTokensMultiplier;

const pairPrice: Fraction | undefined = stakingInfo.pairPrice;
const pairPrice: Fraction | undefined = stakingInfo?.pairPrice;

const png = PNG[chainId];
const tokensPrices = useTokensCurrencyPrice(_rewardTokens);
Expand Down
Loading

0 comments on commit 3f94746

Please sign in to comment.