aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/util.js
diff options
context:
space:
mode:
authorfrankiebee <frankie.diamond@gmail.com>2017-08-05 01:55:00 +0800
committerfrankiebee <frankie.diamond@gmail.com>2017-08-05 01:55:00 +0800
commitcaee2a9e35c0e80efed9da0798cb75044db6c920 (patch)
tree56184228fcb8dd9cb74027ca9ea4a1c5be12ddf1 /app/scripts/lib/util.js
parent3dcc199845f4026cc4d02f9f760332ac9752ff69 (diff)
downloadtangerine-wallet-browser-caee2a9e35c0e80efed9da0798cb75044db6c920.tar
tangerine-wallet-browser-caee2a9e35c0e80efed9da0798cb75044db6c920.tar.gz
tangerine-wallet-browser-caee2a9e35c0e80efed9da0798cb75044db6c920.tar.bz2
tangerine-wallet-browser-caee2a9e35c0e80efed9da0798cb75044db6c920.tar.lz
tangerine-wallet-browser-caee2a9e35c0e80efed9da0798cb75044db6c920.tar.xz
tangerine-wallet-browser-caee2a9e35c0e80efed9da0798cb75044db6c920.tar.zst
tangerine-wallet-browser-caee2a9e35c0e80efed9da0798cb75044db6c920.zip
move util functions to util.js
Diffstat (limited to 'app/scripts/lib/util.js')
-rw-r--r--app/scripts/lib/util.js31
1 files changed, 31 insertions, 0 deletions
diff --git a/app/scripts/lib/util.js b/app/scripts/lib/util.js
index bddd60ee8..70390e95c 100644
--- a/app/scripts/lib/util.js
+++ b/app/scripts/lib/util.js
@@ -1,8 +1,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)
+}