aboutsummaryrefslogblamecommitdiffstats
path: root/ui/app/components/send/send-utils.js
blob: 4eb010173247ea0e596c555b0fcdb383e9bd0d64 (plain) (tree)
1
2
3
4
5
6
7
8
9








                                    
 


                              

                  



















                                                       
                                    





                            






















                                                    

                      
                           
 
const {
  addCurrencies,
  conversionGreaterThan,
  conversionUtil,
  conversionGTE,
} = require('../../conversion-util')
const {
  calcTokenAmount,
} = require('../../token-util')

function isBalanceSufficient({
  amount = '0x0',
  gasTotal = '0x0',
  balance,
  primaryCurrency,
  amountConversionRate,
  conversionRate,
}) {
  const totalAmount = addCurrencies(amount, gasTotal, {
    aBase: 16,
    bBase: 16,
    toNumericBase: 'hex',
  })

  const balanceIsSufficient = conversionGreaterThan(
    {
      value: balance,
      fromNumericBase: 'hex',
      fromCurrency: primaryCurrency,
      conversionRate,
    },
    {
      value: totalAmount,
      fromNumericBase: 'hex',
      conversionRate: amountConversionRate,
      fromCurrency: primaryCurrency,
    },
  )

  return balanceIsSufficient
}

function isTokenBalanceSufficient({
  amount = '0x0',
  tokenBalance,
  decimals,
}) {
  const amountInDec = conversionUtil(amount, {
    fromNumericBase: 'hex',
  })

  const tokenBalanceIsSufficient = conversionGTE(
    {
      value: tokenBalance,
      fromNumericBase: 'dec',
    },
    {
      value: calcTokenAmount(amountInDec, decimals),
      fromNumericBase: 'dec',
    },
  )

  return tokenBalanceIsSufficient
}

module.exports = {
  isBalanceSufficient,
  isTokenBalanceSufficient,
}