This repository has been archived by the owner on Jun 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Claim] Approve flow - GNO approve (#2130)
* useTransactionConfirmationModal: make hooks to simplify * useREmainingAllowanceToApprove hook * useApproveCallbackFromClaim hook * index update draft 1 * style in approve * remove test code
- Loading branch information
Showing
4 changed files
with
140 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { useMemo } from 'react' | ||
import { Currency, CurrencyAmount } from '@uniswap/sdk-core' | ||
import { useTokenAllowance } from 'hooks/useTokenAllowance' | ||
import { useActiveWeb3React } from 'hooks/web3' | ||
|
||
interface Params { | ||
amountToApprove: CurrencyAmount<Currency> | undefined | ||
spender: string | undefined | ||
} | ||
|
||
/** | ||
* useRemainingAllowanceToApprove | ||
* ====================== | ||
* @description returns allowance of a token against a spender | ||
* and the remaining allowance IF allowance > remaining. Else is null | ||
*/ | ||
export default function useRemainingAllowanceToApprove({ amountToApprove, spender }: Params) { | ||
const { account } = useActiveWeb3React() | ||
const allowance = useTokenAllowance(amountToApprove?.wrapped.currency, account ?? undefined, spender) | ||
|
||
return useMemo(() => { | ||
// syntactic sugar - useful in UI to show to users for granularity | ||
// Remaining allowance starts off as undefined - aka 0 | ||
let remainingAllowanceToApprove: CurrencyAmount<Currency> | undefined = undefined | ||
// If amountToApprove is > current allowance, let's return what the difference is | ||
// e.g amountToApprove<100>.minus(currentAllowance<50>) = 50 | ||
// user now only needs to approve 50 | ||
if (allowance && amountToApprove?.greaterThan(allowance)) { | ||
remainingAllowanceToApprove = amountToApprove.subtract(allowance) | ||
} | ||
|
||
return { | ||
allowance, | ||
needsApproval: !allowance || amountToApprove?.greaterThan(allowance), | ||
remainingAllowanceToApprove, | ||
} | ||
}, [allowance, amountToApprove]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useState, useCallback } from 'react' | ||
import TransactionConfirmationModal from 'components/TransactionConfirmationModal' | ||
import { OperationType } from 'components/TransactionConfirmationModal' | ||
import { useOpenModal, useCloseModals, useModalOpen } from 'state/application/hooks' | ||
import { ApplicationModal } from 'state/application/reducer' | ||
|
||
export default function useTransactionConfirmationModal( | ||
defaultOperationType: OperationType = OperationType.WRAP_ETHER | ||
) { | ||
const [operationType, setOperationType] = useState<OperationType>(defaultOperationType) | ||
const [transactionConfirmationModalMsg, setTransactionConfirmationModalMsg] = useState<string>() | ||
const openTransactionConfirmationModalAux = useOpenModal(ApplicationModal.TRANSACTION_CONFIRMATION) | ||
const closeModal = useCloseModals() | ||
const showTransactionConfirmationModal = useModalOpen(ApplicationModal.TRANSACTION_CONFIRMATION) | ||
const openModal = useCallback( | ||
(message: string, operationType: OperationType) => { | ||
setTransactionConfirmationModalMsg(message) | ||
setOperationType(operationType) | ||
openTransactionConfirmationModalAux() | ||
}, | ||
[setTransactionConfirmationModalMsg, openTransactionConfirmationModalAux] | ||
) | ||
|
||
return { | ||
openModal, | ||
closeModal, | ||
TransactionConfirmationModal: useCallback( | ||
() => ( | ||
<TransactionConfirmationModal | ||
attemptingTxn={true} | ||
isOpen={showTransactionConfirmationModal} | ||
pendingText={transactionConfirmationModalMsg} | ||
onDismiss={closeModal} | ||
operationType={operationType} | ||
/> | ||
), | ||
[closeModal, operationType, showTransactionConfirmationModal, transactionConfirmationModalMsg] | ||
), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters