aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/util.js
blob: 70390e95c36e33de8da89a50824b7e4bc438f60d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const ethUtil = require('ethereumjs-util')
const BN = ethUtil.BN

module.exports = {
  getStack,
  sufficientBalance,
  hexToBn,
  bnToHex,
  BnMultiplyByFraction,
}

function getStack () {
  const stack = new Error('Stack trace generator - not an error').stack
  return stack
}

function sufficientBalance (txParams, hexBalance) {
  const balance = hexToBn(hexBalance)
  const value = hexToBn(txParams.value)
  const gasLimit = hexToBn(txParams.gas)
  const gasPrice = hexToBn(txParams.gasPrice)

  const maxCost = value.add(gasLimit.mul(gasPrice))
  return balance.gte(maxCost)
}

function bnToHex (inputBn) {
  return ethUtil.addHexPrefix(inputBn.toString(16))
}

function hexToBn (inputHex) {
  return new BN(ethUtil.stripHexPrefix(inputHex), 16)
}

function BnMultiplyByFraction (targetBN, numerator, denominator) {
  const numBN = new BN(numerator)
  const denomBN = new BN(denominator)
  return targetBN.mul(numBN).div(denomBN)
}