1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-24 12:23:39 +02:00
metamask-extension/ui/app/components/send_/send-content/send-amount-row/send-amount-row.utils.js

62 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-04-26 18:38:38 +02:00
const {
conversionGreaterThan,
} = require('../../../../conversion-util')
const {
isBalanceSufficient,
isTokenBalanceSufficient,
} = require('../../send.utils')
2018-04-11 16:21:54 +02:00
function getAmountErrorObject ({
amount,
amountConversionRate,
balance,
2018-04-11 16:21:54 +02:00
conversionRate,
gasTotal,
2018-04-11 16:21:54 +02:00
primaryCurrency,
selectedToken,
tokenBalance,
}) {
let insufficientFunds = false
if (gasTotal && conversionRate) {
insufficientFunds = !isBalanceSufficient({
amount: selectedToken ? '0x0' : amount,
amountConversionRate,
balance,
2018-04-11 16:21:54 +02:00
conversionRate,
gasTotal,
primaryCurrency,
2018-04-11 16:21:54 +02:00
})
}
let inSufficientTokens = false
if (selectedToken && tokenBalance !== null) {
const { decimals } = selectedToken
inSufficientTokens = !isTokenBalanceSufficient({
tokenBalance,
amount,
decimals,
})
}
const amountLessThanZero = conversionGreaterThan(
{ value: 0, fromNumericBase: 'dec' },
{ value: amount, fromNumericBase: 'hex' },
)
let amountError = null
if (insufficientFunds) {
2018-04-26 18:38:38 +02:00
amountError = 'insufficientFunds'
} else if (inSufficientTokens) {
amountError = 'insufficientTokens'
2018-04-11 16:21:54 +02:00
} else if (amountLessThanZero) {
2018-04-26 18:38:38 +02:00
amountError = 'negativeETH'
2018-04-11 16:21:54 +02:00
}
return { amount: amountError }
}
module.exports = {
getAmountErrorObject,
2018-04-11 16:21:54 +02:00
}