Skip to content

Commit

Permalink
remove allowance refetch
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitayutanov committed Nov 20, 2024
1 parent 5b61c5b commit 875e740
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 5 additions & 14 deletions frontend/src/features/swap/hooks/eth/use-handle-eth-submit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<UseFTAllowance>,
) {
function useHandleEthSubmit(ftAddress: HexString | undefined, fee: bigint | undefined, allowance: bigint | undefined) {
const { writeContractAsync } = useWriteContract();
const approve = useApprove(ftAddress);
const config = useConfig();
Expand Down Expand Up @@ -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 });
Expand Down
12 changes: 5 additions & 7 deletions frontend/src/features/swap/hooks/vara/use-handle-vara-submit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -37,7 +37,7 @@ function useSendVftApprove(ftAddress: HexString | undefined) {
function useHandleVaraSubmit(
ftAddress: HexString | undefined,
feeValue: bigint | undefined,
allowance: ReturnType<UseFTAllowance>,
allowance: bigint | undefined,
) {
const bridgingPaymentRequest = useSendBridgingPaymentRequest();
const vftApprove = useSendVftApprove(ftAddress);
Expand All @@ -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 });
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/features/swap/types/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type UseFee = () => {
type UseHandleSubmit = (
ftAddress: HexString | undefined,
feeValue: bigint | undefined,
allowance: ReturnType<UseFTAllowance>,
allowance: bigint | undefined,
) => Readonly<
[
{
Expand Down

0 comments on commit 875e740

Please sign in to comment.