From 875e74088d29760ededc7485f3ae1b8ba8adb565 Mon Sep 17 00:00:00 2001 From: Nikita Yutanov Date: Thu, 21 Nov 2024 01:10:23 +0300 Subject: [PATCH] remove allowance refetch --- .../swap/components/swap-form/swap-form.tsx | 2 +- .../swap/hooks/eth/use-handle-eth-submit.ts | 19 +++++-------------- .../swap/hooks/vara/use-handle-vara-submit.ts | 12 +++++------- frontend/src/features/swap/types/hooks.ts | 2 +- 4 files changed, 12 insertions(+), 23 deletions(-) diff --git a/frontend/src/features/swap/components/swap-form/swap-form.tsx b/frontend/src/features/swap/components/swap-form/swap-form.tsx index b0bc6945..4bc31b84 100644 --- a/frontend/src/features/swap/components/swap-form/swap-form.tsx +++ b/frontend/src/features/swap/components/swap-form/swap-form.tsx @@ -43,7 +43,7 @@ function SwapForm({ const accountBalance = useAccountBalance(); const ftBalance = useFTBalance(address, decimals); const allowance = useFTAllowance(address); - const [{ mutateAsync: onSubmit, ...submit }, approve] = useHandleSubmit(address, fee.value, allowance); + const [{ mutateAsync: onSubmit, ...submit }, approve] = useHandleSubmit(address, fee.value, allowance.data); const { form, amount, onValueChange, onExpectedValueChange, handleSubmit, setMaxBalance } = useSwapForm( isVaraNetwork, diff --git a/frontend/src/features/swap/hooks/eth/use-handle-eth-submit.ts b/frontend/src/features/swap/hooks/eth/use-handle-eth-submit.ts index c4362c9b..f0a1d9df 100644 --- a/frontend/src/features/swap/hooks/eth/use-handle-eth-submit.ts +++ b/frontend/src/features/swap/hooks/eth/use-handle-eth-submit.ts @@ -6,15 +6,11 @@ import { watchContractEvent } from 'wagmi/actions'; import { isUndefined } from '@/utils'; import { BRIDGING_PAYMENT_ABI, ETH_BRIDGING_PAYMENT_CONTRACT_ADDRESS } from '../../consts'; -import { FormattedValues, UseFTAllowance } from '../../types'; +import { FormattedValues } from '../../types'; import { useApprove } from './use-approve'; -function useHandleEthSubmit( - ftAddress: HexString | undefined, - fee: bigint | undefined, - allowance: ReturnType, -) { +function useHandleEthSubmit(ftAddress: HexString | undefined, fee: bigint | undefined, allowance: bigint | undefined) { const { writeContractAsync } = useWriteContract(); const approve = useApprove(ftAddress); const config = useConfig(); @@ -51,16 +47,11 @@ function useHandleEthSubmit( }); const onSubmit = async ({ amount, accountAddress }: FormattedValues) => { - if (isUndefined(allowance.data)) throw new Error('Allowance is not defined'); + if (isUndefined(allowance)) throw new Error('Allowance is not defined'); - if (amount > allowance.data) { - await approve.mutateAsync(amount); - await allowance.refetch(); // TODO: replace with queryClient.setQueryData after @gear-js/react-hooks update to return QueryKey - } + if (amount > allowance) await approve.mutateAsync(amount); - return requestBridging(amount, accountAddress) - .then(() => watch()) - .then(() => allowance.refetch()); + return requestBridging(amount, accountAddress).then(() => watch()); }; const submit = useMutation({ mutationFn: onSubmit }); diff --git a/frontend/src/features/swap/hooks/vara/use-handle-vara-submit.ts b/frontend/src/features/swap/hooks/vara/use-handle-vara-submit.ts index bae6159a..5ea36c30 100644 --- a/frontend/src/features/swap/hooks/vara/use-handle-vara-submit.ts +++ b/frontend/src/features/swap/hooks/vara/use-handle-vara-submit.ts @@ -6,7 +6,7 @@ import { BRIDGING_PAYMENT_CONTRACT_ADDRESS, BridgingPaymentProgram, VftProgram } import { isUndefined } from '@/utils'; import { FUNCTION_NAME, SERVICE_NAME } from '../../consts/vara'; -import { FormattedValues, UseFTAllowance } from '../../types'; +import { FormattedValues } from '../../types'; function useSendBridgingPaymentRequest() { const { data: program } = useProgram({ @@ -37,7 +37,7 @@ function useSendVftApprove(ftAddress: HexString | undefined) { function useHandleVaraSubmit( ftAddress: HexString | undefined, feeValue: bigint | undefined, - allowance: ReturnType, + allowance: bigint | undefined, ) { const bridgingPaymentRequest = useSendBridgingPaymentRequest(); const vftApprove = useSendVftApprove(ftAddress); @@ -54,14 +54,12 @@ function useHandleVaraSubmit( const onSubmit = async ({ amount, accountAddress }: FormattedValues) => { if (isUndefined(feeValue)) throw new Error('Fee is not found'); - if (isUndefined(allowance.data)) throw new Error('Allowance is not found'); + if (isUndefined(allowance)) throw new Error('Allowance is not found'); - if (amount > allowance.data) { + if (amount > allowance) await vftApprove.sendTransactionAsync({ args: [BRIDGING_PAYMENT_CONTRACT_ADDRESS, amount] }); - await allowance.refetch(); // TODO: replace with queryClient.setQueryData after @gear-js/react-hooks update to return QueryKey - } - return sendBridgingPaymentRequest(amount, accountAddress).then(() => allowance.refetch()); + return sendBridgingPaymentRequest(amount, accountAddress); }; const submit = useMutation({ mutationFn: onSubmit }); diff --git a/frontend/src/features/swap/types/hooks.ts b/frontend/src/features/swap/types/hooks.ts index c8dc34c5..813353c0 100644 --- a/frontend/src/features/swap/types/hooks.ts +++ b/frontend/src/features/swap/types/hooks.ts @@ -26,7 +26,7 @@ type UseFee = () => { type UseHandleSubmit = ( ftAddress: HexString | undefined, feeValue: bigint | undefined, - allowance: ReturnType, + allowance: bigint | undefined, ) => Readonly< [ {