Skip to content

Commit

Permalink
fix: mark permit not syncing if not permitted (#340)
Browse files Browse the repository at this point in the history
* fix: mark permit not syncing if not permitted

* fix: clarify naming
  • Loading branch information
zzmp authored Dec 15, 2022
1 parent b762666 commit 514be2e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
15 changes: 5 additions & 10 deletions src/components/Swap/SwapActionButton/Permit2Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PERMIT2_ADDRESS } from '@uniswap/permit2-sdk'
import ActionButton from 'components/ActionButton'
import EtherscanLink from 'components/EtherscanLink'
import { usePendingApproval } from 'hooks/transactions'
import { Permit } from 'hooks/usePermit2'
import { Permit, PermitState } from 'hooks/usePermit2'
import { Spinner } from 'icons'
import { useCallback, useEffect, useMemo, useState } from 'react'
import { ApprovalTransactionInfo } from 'state/transactions'
Expand All @@ -19,13 +19,8 @@ interface PermitButtonProps extends Permit {
* An approving PermitButton.
* Should only be rendered if a valid trade exists that is not yet permitted.
*/
export default function PermitButton({
token,
isSyncing: isApprovalPending,
callback,
color,
onSubmit,
}: PermitButtonProps) {
export default function PermitButton({ token, state, callback, color, onSubmit }: PermitButtonProps) {
const isApprovalLoading = state === PermitState.APPROVAL_LOADING
const [isPending, setIsPending] = useState(false)
const [isFailed, setIsFailed] = useState(false)
const pendingApproval = usePendingApproval(token, PERMIT2_ADDRESS)
Expand Down Expand Up @@ -54,7 +49,7 @@ export default function PermitButton({
icon: Spinner,
message: t`Approve in your wallet`,
}
} else if (isApprovalPending) {
} else if (isApprovalLoading) {
return {
icon: Spinner,
message: pendingApproval ? (
Expand All @@ -77,7 +72,7 @@ export default function PermitButton({
onClick,
}
}
}, [isApprovalPending, isFailed, isPending, onClick, pendingApproval, token])
}, [isApprovalLoading, isFailed, isPending, onClick, pendingApproval, token])

return (
<ActionButton color={color} disabled={!action?.onClick} action={action}>
Expand Down
5 changes: 4 additions & 1 deletion src/components/Swap/SwapActionButton/SwapButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ export default function SwapButton({
}, [onReviewSwapClick])

if (permit2Enabled) {
if (!disabled && permit.state === PermitState.PERMIT_NEEDED) {
if (
!disabled &&
(permit.state === PermitState.APPROVAL_OR_PERMIT_NEEDED || permit.state === PermitState.APPROVAL_LOADING)
) {
return <PermitButton color={color} onSubmit={onSubmit} {...permit} />
}
} else {
Expand Down
31 changes: 15 additions & 16 deletions src/hooks/usePermit2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ enum SyncState {
export enum PermitState {
INVALID,
LOADING,
PERMIT_NEEDED,
PERMITTED,
APPROVAL_OR_PERMIT_NEEDED,
APPROVAL_LOADING,
APPROVED_AND_PERMITTED,
}

export interface Permit {
token?: Token
state: PermitState
isSyncing?: boolean
signature?: PermitSignature
callback?: () => Promise<ApprovalTransactionInfo | void>
}
Expand Down Expand Up @@ -74,9 +74,8 @@ export default function usePermit(amount?: CurrencyAmount<Token>, spender?: stri

// Permit2 should be marked syncing from the time approval is submitted (pending) until it is
// synced in tokenAllowance, to avoid re-prompting the user for an already-submitted approval.
// It should *not* be marked syncing if not permitted, because the user must still take action.
const [syncState, setSyncState] = useState(SyncState.SYNCED)
const isSyncing = isPermitted || isSigned ? false : syncState !== SyncState.SYNCED
const isApprovalLoading = syncState !== SyncState.SYNCED
const hasPendingApproval = Boolean(usePendingApproval(token, PERMIT2_ADDRESS))
useEffect(() => {
if (hasPendingApproval) {
Expand Down Expand Up @@ -108,18 +107,18 @@ export default function usePermit(amount?: CurrencyAmount<Token>, spender?: stri
return useMemo(() => {
if (!token) {
return { state: PermitState.INVALID }
}

if (!tokenAllowance || !permitAllowance) {
} else if (!tokenAllowance || !permitAllowance) {
return { token, state: PermitState.LOADING }
} else if (isAllowed) {
if (isPermitted) {
return { token, state: PermitState.PERMITTED }
} else if (isSigned) {
return { token, state: PermitState.PERMITTED, signature }
} else if (!(isPermitted || isSigned)) {
return { token, state: PermitState.APPROVAL_OR_PERMIT_NEEDED, callback }
} else if (!isAllowed) {
return {
token,
state: isApprovalLoading ? PermitState.APPROVAL_LOADING : PermitState.APPROVAL_OR_PERMIT_NEEDED,
callback,
}
} else {
return { token, state: PermitState.APPROVED_AND_PERMITTED, signature: isPermitted ? undefined : signature }
}

return { token, state: PermitState.PERMIT_NEEDED, isSyncing, callback }
}, [callback, isAllowed, isPermitted, isSigned, isSyncing, permitAllowance, signature, token, tokenAllowance])
}, [callback, isAllowed, isApprovalLoading, isPermitted, isSigned, permitAllowance, signature, token, tokenAllowance])
}

1 comment on commit 514be2e

@vercel
Copy link

@vercel vercel bot commented on 514be2e Dec 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

widgets – ./

widgets-seven-tau.vercel.app
widgets-git-main-uniswap.vercel.app
widgets-uniswap.vercel.app

Please sign in to comment.