From b9024ca6df42bc9a76d38a3927f046bb568503ab Mon Sep 17 00:00:00 2001 From: jaredvu Date: Tue, 20 Feb 2024 10:41:33 -0800 Subject: [PATCH] Add useEnvFeatures to gate fetch when unsupported on env --- public/configs/env.json | 47 ++++++++++++------- public/configs/v1/env.json | 47 ++++++++++++------- src/hooks/index.ts | 2 + src/hooks/useEnvFeatures.ts | 15 ++++++ src/hooks/useWithdrawalInfo.ts | 26 ++++++---- .../AccountManagementForms/WithdrawForm.tsx | 8 ++-- 6 files changed, 101 insertions(+), 44 deletions(-) create mode 100644 src/hooks/useEnvFeatures.ts diff --git a/public/configs/env.json b/public/configs/env.json index 25a8291..db71304 100644 --- a/public/configs/env.json +++ b/public/configs/env.json @@ -127,7 +127,8 @@ } }, "featureFlags": { - "reduceOnlySupported": true + "reduceOnlySupported": true, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-dev-2": { @@ -212,7 +213,8 @@ } }, "featureFlags": { - "reduceOnlySupported": true + "reduceOnlySupported": true, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-dev-4": { @@ -298,7 +300,8 @@ } }, "featureFlags": { - "reduceOnlySupported": true + "reduceOnlySupported": true, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-dev-5": { @@ -383,7 +386,8 @@ } }, "featureFlags": { - "reduceOnlySupported": true + "reduceOnlySupported": true, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-staging": { @@ -470,7 +474,8 @@ } }, "featureFlags": { - "reduceOnlySupported": true + "reduceOnlySupported": true, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-staging-forced-update": { @@ -555,7 +560,8 @@ } }, "featureFlags": { - "reduceOnlySupported": true + "reduceOnlySupported": true, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-staging-west": { @@ -642,7 +648,8 @@ } }, "featureFlags": { - "reduceOnlySupported": true + "reduceOnlySupported": true, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-testnet": { @@ -733,7 +740,8 @@ } }, "featureFlags": { - "reduceOnlySupported": false + "reduceOnlySupported": false, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-testnet-dydx": { @@ -821,7 +829,8 @@ } }, "featureFlags": { - "reduceOnlySupported": false + "reduceOnlySupported": false, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-testnet-nodefleet": { @@ -909,7 +918,8 @@ } }, "featureFlags": { - "reduceOnlySupported": false + "reduceOnlySupported": false, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-testnet-kingnodes": { @@ -997,7 +1007,8 @@ } }, "featureFlags": { - "reduceOnlySupported": false + "reduceOnlySupported": false, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-testnet-liquify": { @@ -1085,7 +1096,8 @@ } }, "featureFlags": { - "reduceOnlySupported": false + "reduceOnlySupported": false, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-testnet-polkachu": { @@ -1164,7 +1176,8 @@ } }, "featureFlags": { - "reduceOnlySupported": false + "reduceOnlySupported": false, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-testnet-bware": { @@ -1252,7 +1265,8 @@ } }, "featureFlags": { - "reduceOnlySupported": false + "reduceOnlySupported": false, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-mainnet": { @@ -1340,8 +1354,9 @@ } }, "featureFlags": { - "reduceOnlySupported": false + "reduceOnlySupported": false, + "withdrawalSafetyEnabled": false } } } -} \ No newline at end of file +} diff --git a/public/configs/v1/env.json b/public/configs/v1/env.json index 924e94a..0e78655 100644 --- a/public/configs/v1/env.json +++ b/public/configs/v1/env.json @@ -258,7 +258,8 @@ "faucet": "https://faucet.v4dev.dydx.exchange" }, "featureFlags": { - "reduceOnlySupported": true + "reduceOnlySupported": true, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-dev-2": { @@ -283,7 +284,8 @@ "nobleValidator": "https://noble-testnet-rpc.polkachu.com/" }, "featureFlags": { - "reduceOnlySupported": true + "reduceOnlySupported": true, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-dev-4": { @@ -309,7 +311,8 @@ "faucet": "https://faucet.v4dev4.dydx.exchange" }, "featureFlags": { - "reduceOnlySupported": true + "reduceOnlySupported": true, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-dev-5": { @@ -334,7 +337,8 @@ "nobleValidator": "https://noble-testnet-rpc.polkachu.com/" }, "featureFlags": { - "reduceOnlySupported": true + "reduceOnlySupported": true, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-staging": { @@ -360,7 +364,8 @@ "nobleValidator": "https://noble-testnet-rpc.polkachu.com/" }, "featureFlags": { - "reduceOnlySupported": true + "reduceOnlySupported": true, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-staging-forced-update": { @@ -393,7 +398,8 @@ } }, "featureFlags": { - "reduceOnlySupported": true + "reduceOnlySupported": true, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-staging-west": { @@ -419,7 +425,8 @@ "nobleValidator": "https://noble-testnet-rpc.polkachu.com/" }, "featureFlags": { - "reduceOnlySupported": true + "reduceOnlySupported": true, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-testnet": { @@ -449,7 +456,8 @@ "faucet": "https://faucet.v4testnet.dydx.exchange" }, "featureFlags": { - "reduceOnlySupported": false + "reduceOnlySupported": false, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-testnet-dydx": { @@ -475,7 +483,8 @@ "faucet": "https://faucet.v4testnet.dydx.exchange" }, "featureFlags": { - "reduceOnlySupported": false + "reduceOnlySupported": false, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-testnet-nodefleet": { @@ -501,7 +510,8 @@ "faucet": "https://faucet.v4testnet.dydx.exchange" }, "featureFlags": { - "reduceOnlySupported": false + "reduceOnlySupported": false, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-testnet-kingnodes": { @@ -527,7 +537,8 @@ "faucet": "https://faucet.v4testnet.dydx.exchange" }, "featureFlags": { - "reduceOnlySupported": false + "reduceOnlySupported": false, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-testnet-liquify": { @@ -553,7 +564,8 @@ "faucet": "https://faucet.v4testnet.dydx.exchange" }, "featureFlags": { - "reduceOnlySupported": false + "reduceOnlySupported": false, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-testnet-polkachu": { @@ -579,7 +591,8 @@ "faucet": "https://faucet.v4testnet.dydx.exchange" }, "featureFlags": { - "reduceOnlySupported": false + "reduceOnlySupported": false, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-testnet-bware": { @@ -605,7 +618,8 @@ "faucet": "https://faucet.v4testnet.dydx.exchange" }, "featureFlags": { - "reduceOnlySupported": false + "reduceOnlySupported": false, + "withdrawalSafetyEnabled": true } }, "dydxprotocol-mainnet": { @@ -631,8 +645,9 @@ "nobleValidator": "[noble validator endpoint for mainnet]" }, "featureFlags": { - "reduceOnlySupported": false + "reduceOnlySupported": false, + "withdrawalSafetyEnabled": false } } } -} \ No newline at end of file +} diff --git a/src/hooks/index.ts b/src/hooks/index.ts index a137d15..8200dc3 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -7,6 +7,7 @@ import { useDebounce } from './useDebounce'; import { useInterval } from './useInterval'; import { useDocumentTitle } from './useDocumentTitle'; import { useDydxClient } from './useDydxClient'; +import { useEnvFeatures } from './useEnvFeatures'; import { useGovernanceVariables } from './useGovernanceVariables'; import { useAccountBalance } from './useAccountBalance'; import { useAccounts } from './useAccounts'; @@ -37,6 +38,7 @@ export { useDebounce, useDocumentTitle, useDydxClient, + useEnvFeatures, useGovernanceVariables, useAccountBalance, useAccounts, diff --git a/src/hooks/useEnvFeatures.ts b/src/hooks/useEnvFeatures.ts new file mode 100644 index 0000000..a6189eb --- /dev/null +++ b/src/hooks/useEnvFeatures.ts @@ -0,0 +1,15 @@ +import { useSelector } from 'react-redux'; + +import { ENVIRONMENT_CONFIG_MAP } from '@/constants/networks'; + +import { getSelectedNetwork } from '@/state/appSelectors'; + +export interface EnvironmentFeatures { + reduceOnlySupported: boolean; + withdrawalSafetyEnabled: boolean; +} + +export const useEnvFeatures = (): EnvironmentFeatures => { + const selectedNetwork = useSelector(getSelectedNetwork); + return ENVIRONMENT_CONFIG_MAP[selectedNetwork].featureFlags; +}; diff --git a/src/hooks/useWithdrawalInfo.ts b/src/hooks/useWithdrawalInfo.ts index db53b12..2f25d9e 100644 --- a/src/hooks/useWithdrawalInfo.ts +++ b/src/hooks/useWithdrawalInfo.ts @@ -7,6 +7,7 @@ import { shallowEqual, useDispatch, useSelector } from 'react-redux'; import { DialogTypes } from '@/constants/dialogs'; import { isMainnet } from '@/constants/networks'; +import { useEnvFeatures } from './useEnvFeatures'; import { getApiState } from '@/state/appSelectors'; import { closeDialog, openDialog } from '@/state/dialogs'; @@ -31,8 +32,10 @@ export const useWithdrawalInfo = ({ const { height } = apiState || {}; const selectedLocale = useSelector(getSelectedLocale); const dispatch = useDispatch(); + const { withdrawalSafetyEnabled } = useEnvFeatures(); - const { data: usdcWithdawalCapacity } = useQuery({ + const { data: usdcWithdrawalCapacity } = useQuery({ + enabled: withdrawalSafetyEnabled, queryKey: 'usdcWithdrawalCapacity', queryFn: async () => { try { @@ -47,6 +50,7 @@ export const useWithdrawalInfo = ({ }); const { data: withdrawalAndTransferGatingStatus } = useQuery({ + enabled: withdrawalSafetyEnabled, queryKey: 'withdrawalTransferGateStatus', queryFn: getWithdrawalAndTransferGatingStatus, refetchInterval: 60_000, @@ -54,8 +58,12 @@ export const useWithdrawalInfo = ({ }); const capacity = useMemo(() => { - const capacityList = usdcWithdawalCapacity?.limiterCapacityList; + const capacityList = usdcWithdrawalCapacity?.limiterCapacityList; if (!capacityList || capacityList.length < 2) { + if (!withdrawalSafetyEnabled) { + return BigNumber(Infinity); + } + return BIG_NUMBERS.ZERO; } @@ -63,14 +71,15 @@ export const useWithdrawalInfo = ({ const dailyBN = MustBigNumber(daily); const weeklyBN = MustBigNumber(weekly); return BigNumber.minimum(dailyBN, weeklyBN).div(10 ** usdcDecimals); - }, [usdcDecimals, usdcWithdawalCapacity]); + }, [usdcDecimals, usdcWithdrawalCapacity]); const withdrawalAndTransferGatingStatusValue = useMemo(() => { const { withdrawalsAndTransfersUnblockedAtBlock } = withdrawalAndTransferGatingStatus ?? {}; if ( height && withdrawalsAndTransfersUnblockedAtBlock && - height < withdrawalsAndTransfersUnblockedAtBlock + height < withdrawalsAndTransfersUnblockedAtBlock && + withdrawalSafetyEnabled ) { return { estimatedUnblockTime: formatRelativeTime( @@ -87,12 +96,13 @@ export const useWithdrawalInfo = ({ estimatedUnblockTime: null, isGated: false, }; - }, [height, withdrawalAndTransferGatingStatus]); + }, [height, withdrawalAndTransferGatingStatus, withdrawalSafetyEnabled]); useEffect(() => { if ( withdrawalAndTransferGatingStatusValue.isGated && - withdrawalAndTransferGatingStatusValue.estimatedUnblockTime + withdrawalAndTransferGatingStatusValue.estimatedUnblockTime && + withdrawalSafetyEnabled ) { dispatch(closeDialog()); dispatch( @@ -105,10 +115,10 @@ export const useWithdrawalInfo = ({ }) ); } - }, [withdrawalAndTransferGatingStatusValue.isGated]); + }, [withdrawalAndTransferGatingStatusValue.isGated, withdrawalSafetyEnabled]); return { - usdcWithdawalCapacity: capacity, + usdcWithdrawalCapacity: capacity, withdrawalAndTransferGatingStatus, }; }; diff --git a/src/views/forms/AccountManagementForms/WithdrawForm.tsx b/src/views/forms/AccountManagementForms/WithdrawForm.tsx index 0f8bbe0..9acb9b8 100644 --- a/src/views/forms/AccountManagementForms/WithdrawForm.tsx +++ b/src/views/forms/AccountManagementForms/WithdrawForm.tsx @@ -89,7 +89,7 @@ export const WithdrawForm = () => { const [slippage, setSlippage] = useState(isCctp ? 0 : 0.01); // 0.1% slippage const debouncedAmount = useDebounce(withdrawAmount, 500); const { usdcLabel } = useTokenConfigs(); - const { usdcWithdawalCapacity } = useWithdrawalInfo({ transferType: 'withdrawal' }); + const { usdcWithdrawalCapacity } = useWithdrawalInfo({ transferType: 'withdrawal' }); const isValidAddress = toAddress && isAddress(toAddress); @@ -402,7 +402,7 @@ export const WithdrawForm = () => { } // Withdrawal Safety - if (usdcWithdawalCapacity.gt(0) && debouncedAmountBN.gt(usdcWithdawalCapacity)) { + if (usdcWithdrawalCapacity.gt(0) && debouncedAmountBN.gt(usdcWithdrawalCapacity)) { return { alertType: AlertType.Warning, errorMessage: stringGetter({ @@ -410,7 +410,7 @@ export const WithdrawForm = () => { params: { USDC_LIMIT: ( - {usdcWithdawalCapacity.toFormat(TOKEN_DECIMALS)} + {usdcWithdrawalCapacity.toFormat(TOKEN_DECIMALS)} {usdcLabel} ), @@ -434,7 +434,7 @@ export const WithdrawForm = () => { sanctionedAddresses, stringGetter, summary, - usdcWithdawalCapacity, + usdcWithdrawalCapacity, ]); const isInvalidNobleAddress = Boolean(