From c4b42a77df8ef3de8fe66994061f68b1e89bc028 Mon Sep 17 00:00:00 2001 From: jaredvu Date: Mon, 5 Feb 2024 11:32:24 -0800 Subject: [PATCH] :construction: withdrwal-safety client calls --- src/hooks/index.ts | 2 + src/hooks/useDydxClient.tsx | 13 +++++++ src/hooks/useWithdrawalInfo.ts | 37 +++++++++++++++++++ .../AccountManagementForms/WithdrawForm.tsx | 5 +++ 4 files changed, 57 insertions(+) create mode 100644 src/hooks/useWithdrawalInfo.ts diff --git a/src/hooks/index.ts b/src/hooks/index.ts index fc985a8..a137d15 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -26,6 +26,7 @@ import { useStringGetter } from './useStringGetter'; import { useSubaccount } from './useSubaccount'; import { useTradeFormInputs } from './useTradeFormInputs'; import { useURLConfigs } from './useURLConfigs'; +import { useWithdrawalInfo } from './useWithdrawalInfo'; export { useApiState, @@ -56,4 +57,5 @@ export { useSubaccount, useTradeFormInputs, useURLConfigs, + useWithdrawalInfo, }; diff --git a/src/hooks/useDydxClient.tsx b/src/hooks/useDydxClient.tsx index 581dd58..f3b4c0a 100644 --- a/src/hooks/useDydxClient.tsx +++ b/src/hooks/useDydxClient.tsx @@ -249,6 +249,17 @@ const useDydxClientContext = () => { [compositeClient] ); + const getWithdrawalAndTransferGatingStatus = useCallback(async () => { + return await compositeClient?.validatorClient.get.GetWithdrawalAndTransferGatingStatus(); + }, [compositeClient]); + + const getWithdrawalCapacityByDenom = useCallback( + async ({ denom }: { denom: string }) => { + return await compositeClient?.validatorClient.get.getWithdrawalCapacityByDenom(denom); + }, + [compositeClient] + ); + return { // Client initialization connect: setNetworkConfig, @@ -265,5 +276,7 @@ const useDydxClientContext = () => { requestAllGovernanceProposals, getCandlesForDatafeed, screenAddresses, + getWithdrawalAndTransferGatingStatus, + getWithdrawalCapacityByDenom, }; }; diff --git a/src/hooks/useWithdrawalInfo.ts b/src/hooks/useWithdrawalInfo.ts new file mode 100644 index 0000000..94ec219 --- /dev/null +++ b/src/hooks/useWithdrawalInfo.ts @@ -0,0 +1,37 @@ +import { useQuery } from 'react-query'; + +import { useDydxClient } from './useDydxClient'; +import { useTokenConfigs } from './useTokenConfigs'; +import { encodeJson } from '@dydxprotocol/v4-client-js'; +import { ByteArrayEncoding } from '@dydxprotocol/v4-client-js/build/src/lib/helpers'; + +export const useWithdrawalInfo = () => { + const { getWithdrawalAndTransferGatingStatus, getWithdrawalCapacityByDenom } = useDydxClient(); + const { usdcDenom } = useTokenConfigs(); + + const { data: usdcWithdawalCapacity } = useQuery({ + queryKey: 'usdcWithdrawalCapacity', + queryFn: async () => { + try { + const response = await getWithdrawalCapacityByDenom({ denom: usdcDenom }); + return JSON.parse(encodeJson(response, ByteArrayEncoding.BIGINT)); + } catch (error) { + console.error('error'); + } + }, + refetchInterval: 60_000, + staleTime: 60_000, + }); + + const { data: withdrawalAndTransferGatingStatus } = useQuery({ + queryKey: 'withdrawalTransferGateStatus', + queryFn: getWithdrawalAndTransferGatingStatus, + refetchInterval: 60_000, + staleTime: 60_000, + }); + + return { + usdcWithdawalCapacity, + withdrawalAndTransferGatingStatus, + }; +}; diff --git a/src/views/forms/AccountManagementForms/WithdrawForm.tsx b/src/views/forms/AccountManagementForms/WithdrawForm.tsx index 33f26ef..571dc59 100644 --- a/src/views/forms/AccountManagementForms/WithdrawForm.tsx +++ b/src/views/forms/AccountManagementForms/WithdrawForm.tsx @@ -21,6 +21,7 @@ import { useSelectedNetwork, useStringGetter, useSubaccount, + useWithdrawalInfo, } from '@/hooks'; import { useLocalNotifications } from '@/hooks/useLocalNotifications'; @@ -75,6 +76,8 @@ export const WithdrawForm = () => { const [withdrawAmount, setWithdrawAmount] = useState(''); const [slippage, setSlippage] = useState(isCctp ? 0 : 0.01); // 0.1% slippage const debouncedAmount = useDebounce(withdrawAmount, 500); + const { usdcWithdawalCapacity, withdrawalAndTransferGatingStatus } = useWithdrawalInfo(); + console.log({ usdcWithdawalCapacity, withdrawalAndTransferGatingStatus }); const isValidAddress = toAddress && isAddress(toAddress); @@ -348,6 +351,8 @@ export const WithdrawForm = () => { sanctionedAddresses, stringGetter, summary, + withdrawalCapacity, + withdrawalTransferGateStatus, ]); const isDisabled =