-
Notifications
You must be signed in to change notification settings - Fork 54
Warning claim too much eth #2256
Changes from all commits
c11c224
3a646ec
3d278f4
bff3f6d
5ee6163
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { parseUnits } from '@ethersproject/units' | ||
import { CurrencyAmount, Currency } from '@uniswap/sdk-core' | ||
import { BigNumber } from '@ethersproject/bignumber' | ||
// eslint-disable-next-line no-restricted-imports | ||
import { t } from '@lingui/macro' | ||
import { useGasPrices } from 'state/gas/hooks' | ||
|
||
export const MINIMUM_TXS = '10' | ||
export const AVG_APPROVE_COST_GWEI = '50000' | ||
|
@@ -18,11 +20,12 @@ export function _isLowBalanceCheck({ | |
nativeInput, | ||
balance, | ||
}: { | ||
threshold: CurrencyAmount<Currency> | ||
txCost: CurrencyAmount<Currency> | ||
threshold?: CurrencyAmount<Currency> | ||
txCost?: CurrencyAmount<Currency> | ||
nativeInput?: CurrencyAmount<Currency> | ||
balance?: CurrencyAmount<Currency> | ||
}) { | ||
if (!threshold || !txCost) return false | ||
if (!nativeInput || !balance || nativeInput.add(txCost).greaterThan(balance)) return true | ||
// OK if: users_balance - (amt_input + 1_tx_cost) > low_balance_threshold | ||
return balance.subtract(nativeInput.add(txCost)).lessThan(threshold) | ||
|
@@ -35,11 +38,29 @@ export const _getAvailableTransactions = ({ | |
}: { | ||
nativeBalance?: CurrencyAmount<Currency> | ||
nativeInput?: CurrencyAmount<Currency> | ||
singleTxCost: CurrencyAmount<Currency> | ||
singleTxCost?: CurrencyAmount<Currency> | ||
}) => { | ||
if (!nativeBalance || !nativeInput || nativeBalance.lessThan(nativeInput.add(singleTxCost))) return null | ||
if (!nativeBalance || !nativeInput || !singleTxCost || nativeBalance.lessThan(nativeInput.add(singleTxCost))) { | ||
return null | ||
} | ||
|
||
// USER_BALANCE - (USER_WRAP_AMT + 1_TX_CST) / 1_TX_COST = AVAILABLE_TXS | ||
const txsAvailable = nativeBalance.subtract(nativeInput.add(singleTxCost)).divide(singleTxCost) | ||
return txsAvailable.lessThan('1') ? null : txsAvailable.toSignificant(1) | ||
} | ||
|
||
export function _estimateTxCost(gasPrice: ReturnType<typeof useGasPrices>, native: Currency | undefined) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think is a bit strange to expose a function starting with an underscore. I assume this was a "private function" convention. If it's useful to expose, maybe should be without? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, not sure if this should live in a more generic place instead of inside swap -> wrapEth |
||
if (!native) { | ||
return {} | ||
} | ||
// TODO: should use DEFAULT_GAS_FEE from backup source | ||
// when/if implemented | ||
const gas = gasPrice?.standard || DEFAULT_GAS_FEE | ||
|
||
const amount = BigNumber.from(gas).mul(MINIMUM_TXS).mul(AVG_APPROVE_COST_GWEI) | ||
|
||
return { | ||
multiTxCost: CurrencyAmount.fromRawAmount(native, amount.toString()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I felt the name was a bit hard to understand. What about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm just moving this fn around. But ok... I'll refactor this next week when I'm more in the mood. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. its just a nit, the PR was approved :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i wrote this? 😂 |
||
singleTxCost: CurrencyAmount.fromFractionalAmount(native, amount.toString(), MINIMUM_TXS), | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,12 +20,17 @@ import { useErrorModal } from 'hooks/useErrorMessageAndModal' | |
import { tryParseAmount } from 'state/swap/hooks' | ||
import { calculateInvestmentAmounts, calculatePercentage } from 'state/claim/hooks/utils' | ||
import { AMOUNT_PRECISION, PERCENTAGE_PRECISION } from 'constants/index' | ||
import { useGasPrices } from 'state/gas/hooks' | ||
import { _estimateTxCost } from 'components/swap/EthWethWrap/helpers' | ||
import { useWalletInfo } from 'hooks/useWalletInfo' | ||
|
||
const ErrorMsgs = { | ||
InsufficientBalance: (symbol = '') => `Insufficient ${symbol} balance to cover investment amount`, | ||
OverMaxInvestment: `Your investment amount can't be above the maximum investment allowed`, | ||
InvestmentIsZero: `Your investment amount can't be zero`, | ||
NotApproved: (symbol = '') => `Please approve ${symbol} token`, | ||
InsufficientNativeBalance: (symbol = '', action = "won't") => | ||
`You ${action} have enough ${symbol} to pay the network transaction fee`, | ||
alfetopito marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
export default function InvestOption({ approveData, claim, optionIndex }: InvestOptionProps) { | ||
|
@@ -35,10 +40,12 @@ export default function InvestOption({ approveData, claim, optionIndex }: Invest | |
|
||
const { handleSetError, handleCloseError, ErrorModal } = useErrorModal() | ||
|
||
const { account } = useActiveWeb3React() | ||
const { account, chainId } = useActiveWeb3React() | ||
const { isSmartContractWallet } = useWalletInfo() | ||
|
||
const [percentage, setPercentage] = useState<string>('0') | ||
const [typedValue, setTypedValue] = useState<string>('0') | ||
const [inputWarning, setInputWarning] = useState<string>('') | ||
|
||
const investedAmount = investFlowData[optionIndex].investedAmount | ||
const inputError = investFlowData[optionIndex].error | ||
|
@@ -60,6 +67,12 @@ export default function InvestOption({ approveData, claim, optionIndex }: Invest | |
const token = currencyAmount?.currency | ||
const balance = useCurrencyBalance(account || undefined, token) | ||
|
||
const gasPrice = useGasPrices(chainId) | ||
const { singleTxCost } = useMemo( | ||
() => _estimateTxCost(gasPrice, token?.isNative ? token : undefined), | ||
[gasPrice, token] | ||
) | ||
|
||
const isSelfClaiming = account === activeClaimAccount | ||
const noBalance = !balance || balance.equalTo('0') | ||
|
||
|
@@ -132,6 +145,7 @@ export default function InvestOption({ approveData, claim, optionIndex }: Invest | |
// handle input value change | ||
useEffect(() => { | ||
let error = null | ||
let warning | ||
|
||
const parsedAmount = tryParseAmount(typedValue, token) | ||
|
||
|
@@ -150,7 +164,14 @@ export default function InvestOption({ approveData, claim, optionIndex }: Invest | |
error = ErrorMsgs.OverMaxInvestment | ||
} else if (parsedAmount.greaterThan(balance)) { | ||
error = ErrorMsgs.InsufficientBalance(token?.symbol) | ||
} else if (isNative && parsedAmount && singleTxCost?.add(parsedAmount).greaterThan(balance)) { | ||
if (isSmartContractWallet) { | ||
warning = ErrorMsgs.InsufficientNativeBalance(token?.symbol, 'might not') | ||
} else { | ||
error = ErrorMsgs.InsufficientNativeBalance(token?.symbol) | ||
} | ||
} | ||
setInputWarning(warning || '') | ||
|
||
if (error) { | ||
// if there is error set it in redux | ||
|
@@ -162,7 +183,7 @@ export default function InvestOption({ approveData, claim, optionIndex }: Invest | |
} | ||
// basically the magic happens in this block | ||
|
||
// update redux state to remove errro for this field | ||
// update redux state to remove error for this field | ||
resetInputError() | ||
|
||
// update redux state with new investAmount value | ||
|
@@ -182,6 +203,8 @@ export default function InvestOption({ approveData, claim, optionIndex }: Invest | |
setInputError, | ||
resetInputError, | ||
setInvestedAmount, | ||
isSmartContractWallet, | ||
singleTxCost, | ||
]) | ||
|
||
return ( | ||
|
@@ -284,7 +307,8 @@ export default function InvestOption({ approveData, claim, optionIndex }: Invest | |
</label> | ||
<i>Receive: {formatSmartLocaleAware(vCowAmount, AMOUNT_PRECISION) || 0} vCOW</i> | ||
{/* Insufficient balance validation error */} | ||
{inputError ? <small>{inputError}</small> : ''} | ||
{inputError && <small>{inputError}</small>} | ||
{inputWarning && <small className="warn">{inputWarning}</small>} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will probably refactor to pass a |
||
</div> | ||
</InvestInput> | ||
</span> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -935,6 +935,10 @@ export const InvestInput = styled.span` | |
color: red; | ||
margin: 12px 0; | ||
font-size: 15px; | ||
|
||
&.warn { | ||
color: orange; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @biocom might want to review colors for validations |
||
} | ||
} | ||
|
||
> div > i { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you cache the last nativeBalance check?