diff options
author | Dan Finlay <flyswatter@users.noreply.github.com> | 2017-01-18 04:08:47 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-18 04:08:47 +0800 |
commit | c53932a19a77516110a45d7b201b85dc6ea71a47 (patch) | |
tree | 4b53230af166215f932b0d5c3ac17888946c85ca /app/scripts/lib/tx-utils.js | |
parent | 79040c2e681012c98869b7e4d49a9e0ed406c52f (diff) | |
parent | a245fb7d22a5fe08c4fc8c2c1c64d406805018a8 (diff) | |
download | tangerine-wallet-browser-c53932a19a77516110a45d7b201b85dc6ea71a47.tar tangerine-wallet-browser-c53932a19a77516110a45d7b201b85dc6ea71a47.tar.gz tangerine-wallet-browser-c53932a19a77516110a45d7b201b85dc6ea71a47.tar.bz2 tangerine-wallet-browser-c53932a19a77516110a45d7b201b85dc6ea71a47.tar.lz tangerine-wallet-browser-c53932a19a77516110a45d7b201b85dc6ea71a47.tar.xz tangerine-wallet-browser-c53932a19a77516110a45d7b201b85dc6ea71a47.tar.zst tangerine-wallet-browser-c53932a19a77516110a45d7b201b85dc6ea71a47.zip |
Merge branch 'dev' into conversion-api-err
Diffstat (limited to 'app/scripts/lib/tx-utils.js')
-rw-r--r-- | app/scripts/lib/tx-utils.js | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/app/scripts/lib/tx-utils.js b/app/scripts/lib/tx-utils.js index d1fb98f42..5116cb93b 100644 --- a/app/scripts/lib/tx-utils.js +++ b/app/scripts/lib/tx-utils.js @@ -1,6 +1,8 @@ const async = require('async') const EthQuery = require('eth-query') const ethUtil = require('ethereumjs-util') +const Transaction = require('ethereumjs-tx') +const normalize = require('./sig-util').normalize const BN = ethUtil.BN /* @@ -14,6 +16,7 @@ module.exports = class txProviderUtils { this.provider = provider this.query = new EthQuery(provider) } + analyzeGasUsage (txData, cb) { var self = this this.query.getBlockByNumber('latest', true, (err, block) => { @@ -71,4 +74,59 @@ module.exports = class txProviderUtils { const correct = bnGas.add(gasBuffer) return ethUtil.addHexPrefix(correct.toString(16)) } + + fillInTxParams (txParams, cb) { + let fromAddress = txParams.from + let reqs = {} + + if (isUndef(txParams.gas)) reqs.gas = (cb) => this.query.estimateGas(txParams, cb) + if (isUndef(txParams.gasPrice)) reqs.gasPrice = (cb) => this.query.gasPrice(cb) + if (isUndef(txParams.nonce)) reqs.nonce = (cb) => this.query.getTransactionCount(fromAddress, 'pending', cb) + + async.parallel(reqs, function(err, result) { + if (err) return cb(err) + // write results to txParams obj + Object.assign(txParams, result) + cb() + }) + } + + // builds ethTx from txParams object + buildEthTxFromParams (txParams, gasMultiplier = 1) { + // apply gas multiplyer + let gasPrice = new BN(ethUtil.stripHexPrefix(txParams.gasPrice), 16) + // multiply and divide by 100 so as to add percision to integer mul + gasPrice = gasPrice.mul(new BN(gasMultiplier * 100, 10)).div(new BN(100, 10)) + txParams.gasPrice = ethUtil.intToHex(gasPrice.toNumber()) + // normalize values + txParams.to = normalize(txParams.to) + txParams.from = normalize(txParams.from) + txParams.value = normalize(txParams.value) + txParams.data = normalize(txParams.data) + txParams.gasLimit = normalize(txParams.gasLimit || txParams.gas) + txParams.nonce = normalize(txParams.nonce) + // build ethTx + const ethTx = new Transaction(txParams) + return ethTx + } + + publishTransaction (rawTx, cb) { + this.query.sendRawTransaction(rawTx, cb) + } + + validateTxParams (txParams, cb) { + if (('value' in txParams) && txParams.value.indexOf('-') === 0) { + cb(new Error(`Invalid transaction value of ${txParams.value} not a positive number.`)) + } else { + cb() + } + } + + +} + +// util + +function isUndef(value) { + return value === undefined } |