Skip to content

Commit

Permalink
fix: update approval logic
Browse files Browse the repository at this point in the history
  • Loading branch information
denviljclarke committed Nov 6, 2024
1 parent 3031b61 commit 35afeac
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export const CreateLockProvider = ({
});
}, [address, lock, parsedAmount, resetAll, slope]);

const approve = useApprove({ onConfirmation: lockMento });
const approve = useApprove();

const needsApproval = React.useMemo(() => {
if (!allowance.data) return true;
Expand Down Expand Up @@ -128,7 +128,11 @@ export const CreateLockProvider = ({
if (!needsApproval) {
lockMento();
} else {
approve.approveMento(contracts.Locking.address, parsedAmount);
approve.approveMento({
target: contracts.Locking.address,
amount: parsedAmount,
onConfirmation: lockMento,
});
}
}, [
lock,
Expand Down
19 changes: 13 additions & 6 deletions src/components/lock-manage-lock/hooks/useManageLockLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export const useManageLockLogic = (lockToManage: LockWithExpiration) => {
},
});

const approve = useApprove({ onConfirmation: relock.relockMento });
const approve = useApprove();

const needsApproval = useMemo(() => {
if (parsedAdditionalAmountToLock === 0n) return false;
Expand Down Expand Up @@ -126,7 +126,8 @@ export const useManageLockLogic = (lockToManage: LockWithExpiration) => {
onError,
}: { onSuccess?: () => void; onError?: () => void } = {}) => {
setIsTxModalOpen(true);
if (!needsApproval) {

const submitRelock = () => {
relock.relockMento({
onSuccess: () => {
resetForm();
Expand All @@ -136,11 +137,17 @@ export const useManageLockLogic = (lockToManage: LockWithExpiration) => {
},
onError,
});
};

if (needsApproval) {
approve.approveMento({
target: contracts.Locking.address,
amount: parsedAdditionalAmountToLock,
onConfirmation: submitRelock,
onError,
});
} else {
approve.approveMento(
contracts.Locking.address,
parsedAdditionalAmountToLock,
);
submitRelock();
}
},
[
Expand Down
59 changes: 38 additions & 21 deletions src/lib/contracts/mento/useApprove.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import React, { useCallback } from "react";
import { useCallback } from "react";
import { useContracts } from "@/lib/contracts/useContracts";
import {
useAccount,
useConfig,
useWaitForTransactionReceipt,
useWriteContract,
} from "wagmi";
import { Address, erc20Abi } from "viem";
import { WriteContractErrorType } from "wagmi/actions";
import {
waitForTransactionReceipt,
WriteContractErrorType,
} from "wagmi/actions";
import * as Sentry from "@sentry/nextjs";

const useApprove = ({
onConfirmation,
}: {
interface ApproveParams {
target: Address;
amount: bigint;
onSuccess?: () => void;
onError?: (error?: WriteContractErrorType) => void;
onConfirmation?: () => void;
} = {}) => {
}

const useApprove = () => {
const contracts = useContracts();
const {
writeContract,
Expand All @@ -22,27 +30,17 @@ const useApprove = ({
...restWrite
} = useWriteContract();
const { address } = useAccount();
const config = useConfig();

const { isLoading: isConfirming, isSuccess: isConfirmed } =
useWaitForTransactionReceipt({
hash: data,
pollingInterval: 1000,
});

React.useEffect(() => {
if (isConfirmed && onConfirmation) {
onConfirmation();
restWrite.reset();
}
}, [isConfirmed, onConfirmation, restWrite]);

const approveMento = useCallback(
(
target: Address,
amount: bigint,
onSuccess?: () => void,
onError?: (error?: WriteContractErrorType) => void,
) => {
(params: ApproveParams) => {
const { target, amount, onConfirmation, onError } = params;

writeContract(
{
address: contracts.MentoToken.address,
Expand All @@ -51,7 +49,25 @@ const useApprove = ({
args: [target, amount],
},
{
onSuccess,
onSuccess: async (data) => {
try {
await waitForTransactionReceipt(config, {
hash: data,
pollingInterval: 1000,
confirmations: 2,
});
onConfirmation?.();
} catch (error) {
Sentry.captureException(error, {
data: {
function: "useApprove - waitForTransactionReceipt",
user: address,
contract: contracts.Locking.address,
contractArgs: JSON.stringify([target, amount]),
},
});
}
},
onError: (error: WriteContractErrorType) => {
Sentry.captureException(error, {
data: {
Expand All @@ -68,6 +84,7 @@ const useApprove = ({
},
[
address,
config,
contracts.Locking.address,
contracts.MentoToken.address,
writeContract,
Expand Down

0 comments on commit 35afeac

Please sign in to comment.