diff options
author | Alexander Tseung <alextsg@gmail.com> | 2018-01-04 03:06:08 +0800 |
---|---|---|
committer | Alexander Tseung <alextsg@gmail.com> | 2018-01-04 03:06:08 +0800 |
commit | a6f062a6865d3e1e5ceb98885ab4b38713e4293d (patch) | |
tree | ea379a341cc19f8942536b1800c309f7d79b3583 /ui/app/util.js | |
parent | 313b3c087a09bcc4462da15ff3caeac515967cf5 (diff) | |
parent | dfb22471087f040d8345a5a17321e1462842045c (diff) | |
download | tangerine-wallet-browser-a6f062a6865d3e1e5ceb98885ab4b38713e4293d.tar tangerine-wallet-browser-a6f062a6865d3e1e5ceb98885ab4b38713e4293d.tar.gz tangerine-wallet-browser-a6f062a6865d3e1e5ceb98885ab4b38713e4293d.tar.bz2 tangerine-wallet-browser-a6f062a6865d3e1e5ceb98885ab4b38713e4293d.tar.lz tangerine-wallet-browser-a6f062a6865d3e1e5ceb98885ab4b38713e4293d.tar.xz tangerine-wallet-browser-a6f062a6865d3e1e5ceb98885ab4b38713e4293d.tar.zst tangerine-wallet-browser-a6f062a6865d3e1e5ceb98885ab4b38713e4293d.zip |
Merge branch 'NewUI-flat' into NewUI-flat-4.0.5c
Diffstat (limited to 'ui/app/util.js')
-rw-r--r-- | ui/app/util.js | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/ui/app/util.js b/ui/app/util.js index 293f4228c..70c503550 100644 --- a/ui/app/util.js +++ b/ui/app/util.js @@ -1,4 +1,16 @@ +const abi = require('human-standard-token-abi') const ethUtil = require('ethereumjs-util') +const hexToBn = require('../../app/scripts/lib/hex-to-bn') +const vreme = new (require('vreme'))() + +const MIN_GAS_PRICE_GWEI_BN = new ethUtil.BN(1) +const GWEI_FACTOR = new ethUtil.BN(1e9) +const MIN_GAS_PRICE_BN = MIN_GAS_PRICE_GWEI_BN.mul(GWEI_FACTOR) + +// formatData :: ( date: <Unix Timestamp> ) -> String +function formatDate (date) { + return vreme.format(new Date(date), 'March 16 2014 14:30') +} var valueTable = { wei: '1000000000000000000', @@ -36,8 +48,14 @@ module.exports = { valueTable: valueTable, bnTable: bnTable, isHex: isHex, + formatDate, + bnMultiplyByFraction, + getTxFeeBn, + shortenBalance, + getContractAtAddress, exportAsFile: exportAsFile, isInvalidChecksumAddress, + allNull, } function valuesFor (obj) { @@ -227,6 +245,24 @@ function isHex (str) { return Boolean(str.match(/^(0x)?[0-9a-fA-F]+$/)) } +function bnMultiplyByFraction (targetBN, numerator, denominator) { + const numBN = new ethUtil.BN(numerator) + const denomBN = new ethUtil.BN(denominator) + return targetBN.mul(numBN).div(denomBN) +} + +function getTxFeeBn (gas, gasPrice = MIN_GAS_PRICE_BN.toString(16), blockGasLimit) { + const gasBn = hexToBn(gas) + const gasPriceBn = hexToBn(gasPrice) + const txFeeBn = gasBn.mul(gasPriceBn) + + return txFeeBn.toString(16) +} + +function getContractAtAddress (tokenAddress) { + return global.eth.contract(abi).at(tokenAddress) +} + function exportAsFile (filename, data) { // source: https://stackoverflow.com/a/33542499 by Ludovic Feltz const blob = new Blob([data], {type: 'text/csv'}) @@ -241,3 +277,7 @@ function exportAsFile (filename, data) { document.body.removeChild(elem) } } + +function allNull (obj) { + return Object.entries(obj).every(([key, value]) => value === null) +} |