Skip to content

Commit

Permalink
fix: decouple wrap callback from balance (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzmp authored Dec 12, 2022
1 parent 0a43657 commit 318426b
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions src/hooks/swap/useWrapCallback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { Field, swapAtom } from 'state/swap'
import { TransactionType } from 'state/transactions'
import tryParseCurrencyAmount from 'utils/tryParseCurrencyAmount'

import useCurrencyBalance from '../useCurrencyBalance'

interface UseWrapCallbackReturns {
callback: () => Promise<ContractTransaction | void>
type?: TransactionType.WRAP | TransactionType.UNWRAP
Expand All @@ -36,7 +34,6 @@ export function useIsWrap(): boolean {
}

export default function useWrapCallback(): UseWrapCallbackReturns {
const { account } = useWeb3React()
const wrappedNativeCurrencyContract = useWETHContract()
const { amount, [Field.INPUT]: inputCurrency } = useAtomValue(swapAtom)
const wrapType = useWrapType()
Expand All @@ -45,24 +42,20 @@ export default function useWrapCallback(): UseWrapCallbackReturns {
() => tryParseCurrencyAmount(amount, inputCurrency ?? undefined),
[inputCurrency, amount]
)
const balanceIn = useCurrencyBalance(account, inputCurrency)

const callback = useMemo(() => {
if (!parsedAmountIn || !balanceIn || balanceIn.lessThan(parsedAmountIn) || !wrappedNativeCurrencyContract) {
return async () => undefined
}

return async () => {
if (!parsedAmountIn) return
switch (wrapType) {
case TransactionType.WRAP:
return wrappedNativeCurrencyContract.deposit({ value: `0x${parsedAmountIn.quotient.toString(16)}` })
return wrappedNativeCurrencyContract?.deposit({ value: `0x${parsedAmountIn.quotient.toString(16)}` })
case TransactionType.UNWRAP:
return wrappedNativeCurrencyContract.withdraw(`0x${parsedAmountIn.quotient.toString(16)}`)
return wrappedNativeCurrencyContract?.withdraw(`0x${parsedAmountIn.quotient.toString(16)}`)
case undefined:
return undefined
}
}
}, [parsedAmountIn, balanceIn, wrappedNativeCurrencyContract, wrapType])
}, [parsedAmountIn, wrappedNativeCurrencyContract, wrapType])

return useMemo(() => ({ callback, type: wrapType }), [callback, wrapType])
}

1 comment on commit 318426b

@vercel
Copy link

@vercel vercel bot commented on 318426b Dec 12, 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-uniswap.vercel.app
widgets-seven-tau.vercel.app
widgets-git-main-uniswap.vercel.app

Please sign in to comment.