2017-08-04 19:55:00 +02:00
|
|
|
const ethUtil = require('ethereumjs-util')
|
2017-08-09 06:05:59 +02:00
|
|
|
const assert = require('assert')
|
|
|
|
const BN = require('bn.js')
|
2017-08-04 19:55:00 +02:00
|
|
|
|
2017-07-25 22:08:31 +02:00
|
|
|
module.exports = {
|
|
|
|
getStack,
|
2017-08-04 19:55:00 +02:00
|
|
|
sufficientBalance,
|
|
|
|
hexToBn,
|
|
|
|
bnToHex,
|
|
|
|
BnMultiplyByFraction,
|
2017-07-25 22:08:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getStack () {
|
|
|
|
const stack = new Error('Stack trace generator - not an error').stack
|
|
|
|
return stack
|
|
|
|
}
|
2017-08-04 19:55:00 +02:00
|
|
|
|
|
|
|
function sufficientBalance (txParams, hexBalance) {
|
2017-08-09 06:05:59 +02:00
|
|
|
// validate hexBalance is a hex string
|
|
|
|
assert.equal(typeof hexBalance, 'string', 'sufficientBalance - hexBalance is not a hex string')
|
|
|
|
assert.equal(hexBalance.slice(0, 2), '0x', 'sufficientBalance - hexBalance is not a hex string')
|
|
|
|
|
2017-08-04 19:55:00 +02:00
|
|
|
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)
|
|
|
|
}
|