From 0770bab032307c69a8e65d1578efe1cdc4de1d1d Mon Sep 17 00:00:00 2001 From: Noah Zinsmeister Date: Wed, 5 May 2021 17:36:07 -0400 Subject: [PATCH] try manual approve if erc712 doesn't work (#1397) * initial impl * fix async logic --- src/components/earn/StakingModal.tsx | 13 ++++++++++++- src/hooks/useERC20Permit.ts | 2 +- src/pages/MigrateV2/MigrateV2Pair.tsx | 16 +++++++++++++++- src/pages/RemoveLiquidity/index.tsx | 13 ++++++++++++- src/pages/Swap/index.tsx | 16 +++++++++++++--- 5 files changed, 53 insertions(+), 7 deletions(-) diff --git a/src/components/earn/StakingModal.tsx b/src/components/earn/StakingModal.tsx index 9acb4e6ea9..6107af176f 100644 --- a/src/components/earn/StakingModal.tsx +++ b/src/components/earn/StakingModal.tsx @@ -128,7 +128,18 @@ export default function StakingModal({ isOpen, onDismiss, stakingInfo, userLiqui if (!pairContract || !library || !deadline) throw new Error('missing dependencies') if (!parsedAmount) throw new Error('missing liquidity amount') - return gatherPermitSignature ? gatherPermitSignature() : approveCallback() + if (gatherPermitSignature) { + try { + await gatherPermitSignature() + } catch (error) { + // try to approve if gatherPermitSignature failed for any reason other than the user rejecting it + if (error?.code !== 4001) { + await approveCallback() + } + } + } else { + await approveCallback() + } } return ( diff --git a/src/hooks/useERC20Permit.ts b/src/hooks/useERC20Permit.ts index be792ddff4..a1dfb7747b 100644 --- a/src/hooks/useERC20Permit.ts +++ b/src/hooks/useERC20Permit.ts @@ -219,7 +219,7 @@ export function useERC20Permit( message, }) - library + return library .send('eth_signTypedData_v4', [account, data]) .then(splitSignature) .then((signature) => { diff --git a/src/pages/MigrateV2/MigrateV2Pair.tsx b/src/pages/MigrateV2/MigrateV2Pair.tsx index 33f9423278..a2dbb63d1c 100644 --- a/src/pages/MigrateV2/MigrateV2Pair.tsx +++ b/src/pages/MigrateV2/MigrateV2Pair.tsx @@ -239,7 +239,21 @@ function V2PairMigration({ const { signatureData, gatherPermitSignature } = useV2LiquidityTokenPermit(pairBalance, migratorAddress) const approve = useCallback(async () => { - isNotUniswap ? approveManually() : gatherPermitSignature ? gatherPermitSignature() : approveManually() + if (isNotUniswap) { + // sushi has to be manually approved + await approveManually() + } else if (gatherPermitSignature) { + try { + await gatherPermitSignature() + } catch (error) { + // try to approve if gatherPermitSignature failed for any reason other than the user rejecting it + if (error?.code !== 4001) { + await approveManually() + } + } + } else { + await approveManually() + } }, [isNotUniswap, gatherPermitSignature, approveManually]) const addTransaction = useTransactionAdder() diff --git a/src/pages/RemoveLiquidity/index.tsx b/src/pages/RemoveLiquidity/index.tsx index e8026a3cd3..15c9495f77 100644 --- a/src/pages/RemoveLiquidity/index.tsx +++ b/src/pages/RemoveLiquidity/index.tsx @@ -109,7 +109,18 @@ export default function RemoveLiquidity({ const liquidityAmount = parsedAmounts[Field.LIQUIDITY] if (!liquidityAmount) throw new Error('missing liquidity amount') - return gatherPermitSignature ? gatherPermitSignature() : approveCallback() + if (gatherPermitSignature) { + try { + await gatherPermitSignature() + } catch (error) { + // try to approve if gatherPermitSignature failed for any reason other than the user rejecting it + if (error?.code !== 4001) { + await approveCallback() + } + } + } else { + await approveCallback() + } } // wrapped onUserInput to clear signatures diff --git a/src/pages/Swap/index.tsx b/src/pages/Swap/index.tsx index fcaffa0735..940c439961 100644 --- a/src/pages/Swap/index.tsx +++ b/src/pages/Swap/index.tsx @@ -205,9 +205,19 @@ export default function Swap({ history }: RouteComponentProps) { allowedSlippage ) - const handleApprove = useCallback(() => { - if (signatureState === UseERC20PermitState.NOT_SIGNED && gatherPermitSignature) gatherPermitSignature() - else approveCallback() + const handleApprove = useCallback(async () => { + if (signatureState === UseERC20PermitState.NOT_SIGNED && gatherPermitSignature) { + try { + await gatherPermitSignature() + } catch (error) { + // try to approve if gatherPermitSignature failed for any reason other than the user rejecting it + if (error?.code !== 4001) { + await approveCallback() + } + } + } else { + await approveCallback() + } }, [approveCallback, gatherPermitSignature, signatureState]) // check if user has gone through approval process, used to show two step buttons, reset on token change