Commit 116a7f38 authored by Ian Lapham's avatar Ian Lapham Committed by Noah Zinsmeister

fix bug with a try catch on approval (#516)

parent 04a918ed
......@@ -20,6 +20,7 @@ import TokenLogo from '../TokenLogo'
import SearchIcon from '../../assets/images/magnifying-glass.svg'
import { useTransactionAdder, usePendingApproval } from '../../contexts/Transactions'
import { useTokenDetails, useAllTokenDetails } from '../../contexts/Tokens'
import { useAddressBalance } from '../../contexts/Balances'
import { ReactComponent as Close } from '../../assets/images/x.svg'
import { transparentize } from 'polished'
import { Spinner } from '../../theme'
......@@ -296,6 +297,10 @@ export default function CurrencyInputPanel({
const allTokens = useAllTokenDetails()
const { account } = useWeb3Context()
const userTokenBalance = useAddressBalance(account, selectedTokenAddress)
function renderUnlockButton() {
if (disableUnlock || !showUnlock || selectedTokenAddress === 'ETH' || !selectedTokenAddress) {
return null
......@@ -304,14 +309,26 @@ export default function CurrencyInputPanel({
return (
<SubCurrencySelect
onClick={async () => {
const estimatedGas = await tokenContract.estimate.approve(
selectedTokenExchangeAddress,
ethers.constants.MaxUint256
)
tokenContract
.approve(selectedTokenExchangeAddress, ethers.constants.MaxUint256, {
gasLimit: calculateGasMargin(estimatedGas, GAS_MARGIN)
let estimatedGas
let useUserBalance = false
estimatedGas = await tokenContract.estimate
.approve(selectedTokenExchangeAddress, ethers.constants.MaxUint256)
.catch(e => {
console.log('Error setting max token approval.')
})
if (!estimatedGas) {
// general fallback for tokens who restrict approval amounts
estimatedGas = await tokenContract.estimate.approve(selectedTokenExchangeAddress, userTokenBalance)
useUserBalance = true
}
tokenContract
.approve(
selectedTokenExchangeAddress,
useUserBalance ? userTokenBalance : ethers.constants.MaxUint256,
{
gasLimit: calculateGasMargin(estimatedGas, GAS_MARGIN)
}
)
.then(response => {
addTransaction(response, { approval: selectedTokenAddress })
})
......
......@@ -551,6 +551,7 @@ export default function AddLiquidity({ params }) {
}, [inputValueParsed, inputBalance, outputValueMax, outputBalance, t])
const allowance = useAddressAllowance(account, outputCurrency, exchangeAddress)
const [showUnlock, setShowUnlock] = useState(false)
useEffect(() => {
if (outputValueParsed && allowance) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment