diff options
author | kumavis <kumavis@users.noreply.github.com> | 2017-08-09 14:30:58 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-09 14:30:58 +0800 |
commit | 0188e7b94d85b45a783f9e3d5c182a8ffcaeac2e (patch) | |
tree | 428154973b91dfb2f8519777e67da7a6c75530b3 /app/scripts/lib/util.js | |
parent | 5e9926b0d035a5ba946080e94777ac0bd887d396 (diff) | |
parent | 57f6fce6b2524c4b36b591da5e600d0652f4077e (diff) | |
download | tangerine-wallet-browser-0188e7b94d85b45a783f9e3d5c182a8ffcaeac2e.tar tangerine-wallet-browser-0188e7b94d85b45a783f9e3d5c182a8ffcaeac2e.tar.gz tangerine-wallet-browser-0188e7b94d85b45a783f9e3d5c182a8ffcaeac2e.tar.bz2 tangerine-wallet-browser-0188e7b94d85b45a783f9e3d5c182a8ffcaeac2e.tar.lz tangerine-wallet-browser-0188e7b94d85b45a783f9e3d5c182a8ffcaeac2e.tar.xz tangerine-wallet-browser-0188e7b94d85b45a783f9e3d5c182a8ffcaeac2e.tar.zst tangerine-wallet-browser-0188e7b94d85b45a783f9e3d5c182a8ffcaeac2e.zip |
Merge branch 'master' into NewUI-flat
Diffstat (limited to 'app/scripts/lib/util.js')
-rw-r--r-- | app/scripts/lib/util.js | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/app/scripts/lib/util.js b/app/scripts/lib/util.js index bddd60ee8..6dee9edf0 100644 --- a/app/scripts/lib/util.js +++ b/app/scripts/lib/util.js @@ -1,8 +1,44 @@ +const ethUtil = require('ethereumjs-util') +const assert = require('assert') +const BN = require('bn.js') + 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) { + // 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') + + 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) +} |