tx manager - adjust new tx flow and txMeta decorations

feature/default_network_editable
kumavis 8 years ago
parent 81d3658343
commit 1495240969
  1. 32
      app/scripts/lib/tx-utils.js
  2. 41
      app/scripts/transaction-manager.js

@ -12,48 +12,49 @@ and used to do things like calculate gas of a tx.
*/ */
module.exports = class txProviderUtils { module.exports = class txProviderUtils {
constructor (provider) { constructor (provider) {
this.provider = provider this.provider = provider
this.query = new EthQuery(provider) this.query = new EthQuery(provider)
} }
analyzeGasUsage (txData, cb) { analyzeGasUsage (txMeta, cb) {
var self = this var self = this
this.query.getBlockByNumber('latest', true, (err, block) => { this.query.getBlockByNumber('latest', true, (err, block) => {
if (err) return cb(err) if (err) return cb(err)
async.waterfall([ async.waterfall([
self.estimateTxGas.bind(self, txData, block.gasLimit), self.estimateTxGas.bind(self, txMeta, block.gasLimit),
self.setTxGas.bind(self, txData, block.gasLimit), self.setTxGas.bind(self, txMeta, block.gasLimit),
], cb) ], cb)
}) })
} }
estimateTxGas (txData, blockGasLimitHex, cb) { estimateTxGas (txMeta, blockGasLimitHex, cb) {
const txParams = txData.txParams const txParams = txMeta.txParams
// check if gasLimit is already specified // check if gasLimit is already specified
txData.gasLimitSpecified = Boolean(txParams.gas) txMeta.gasLimitSpecified = Boolean(txParams.gas)
// if not, fallback to block gasLimit // if not, fallback to block gasLimit
if (!txData.gasLimitSpecified) { if (!txMeta.gasLimitSpecified) {
txParams.gas = blockGasLimitHex txParams.gas = blockGasLimitHex
} }
// run tx, see if it will OOG // run tx, see if it will OOG
this.query.estimateGas(txParams, cb) this.query.estimateGas(txParams, cb)
} }
setTxGas (txData, blockGasLimitHex, estimatedGasHex, cb) { setTxGas (txMeta, blockGasLimitHex, estimatedGasHex, cb) {
txData.estimatedGas = estimatedGasHex txMeta.estimatedGas = estimatedGasHex
const txParams = txData.txParams const txParams = txMeta.txParams
// if gasLimit was specified and doesnt OOG, // if gasLimit was specified and doesnt OOG,
// use original specified amount // use original specified amount
if (txData.gasLimitSpecified) { if (txMeta.gasLimitSpecified) {
txData.estimatedGas = txParams.gas txMeta.estimatedGas = txParams.gas
cb() cb()
return return
} }
// if gasLimit not originally specified, // if gasLimit not originally specified,
// try adding an additional gas buffer to our estimation for safety // try adding an additional gas buffer to our estimation for safety
const recommendedGasHex = this.addGasBuffer(txData.estimatedGas, blockGasLimitHex) const recommendedGasHex = this.addGasBuffer(txMeta.estimatedGas, blockGasLimitHex)
txParams.gas = recommendedGasHex txParams.gas = recommendedGasHex
cb() cb()
return return
@ -90,16 +91,13 @@ module.exports = class txProviderUtils {
// builds ethTx from txParams object // builds ethTx from txParams object
buildEthTxFromParams (txParams) { buildEthTxFromParams (txParams) {
// apply gas multiplyer
let gasPrice = hexToBn(txParams.gasPrice)
// multiply and divide by 100 so as to add percision to integer mul
txParams.gasPrice = ethUtil.intToHex(gasPrice.toNumber())
// normalize values // normalize values
txParams.to = normalize(txParams.to) txParams.to = normalize(txParams.to)
txParams.from = normalize(txParams.from) txParams.from = normalize(txParams.from)
txParams.value = normalize(txParams.value) txParams.value = normalize(txParams.value)
txParams.data = normalize(txParams.data) txParams.data = normalize(txParams.data)
txParams.gas = normalize(txParams.gas || txParams.gasLimit) txParams.gas = normalize(txParams.gas || txParams.gasLimit)
txParams.gasPrice = normalize(txParams.gasPrice)
txParams.nonce = normalize(txParams.nonce) txParams.nonce = normalize(txParams.nonce)
// build ethTx // build ethTx
log.info(`Prepared tx for signing: ${JSON.stringify(txParams)}`) log.info(`Prepared tx for signing: ${JSON.stringify(txParams)}`)

@ -4,7 +4,6 @@ const extend = require('xtend')
const Semaphore = require('semaphore') const Semaphore = require('semaphore')
const ObservableStore = require('obs-store') const ObservableStore = require('obs-store')
const ethUtil = require('ethereumjs-util') const ethUtil = require('ethereumjs-util')
const BN = require('ethereumjs-util').BN
const TxProviderUtil = require('./lib/tx-utils') const TxProviderUtil = require('./lib/tx-utils')
const createId = require('./lib/random-id') const createId = require('./lib/random-id')
@ -121,44 +120,38 @@ module.exports = class TransactionManager extends EventEmitter {
async.waterfall([ async.waterfall([
// validate // validate
(cb) => this.txProviderUtils.validateTxParams(txParams, cb), (cb) => this.txProviderUtils.validateTxParams(txParams, cb),
// prepare txMeta // construct txMeta
(cb) => { (cb) => {
// create txMeta obj with parameters and meta data
let time = (new Date()).getTime()
let txId = createId()
txParams.metamaskId = txId
txParams.metamaskNetworkId = this.getNetwork()
txMeta = { txMeta = {
id: txId, id: createId(),
time: time, time: (new Date()).getTime(),
status: 'unapproved', status: 'unapproved',
metamaskNetworkId: this.getNetwork(), metamaskNetworkId: this.getNetwork(),
txParams: txParams, txParams: txParams,
} }
// calculate metadata for tx cb()
this.txProviderUtils.analyzeGasUsage(txMeta, cb)
}, },
// add default tx params
(cb) => this.addTxDefaults(txMeta, cb),
// save txMeta // save txMeta
(cb) => { (cb) => {
this.addTx(txMeta) this.addTx(txMeta)
this.setMaxTxCostAndFee(txMeta)
cb(null, txMeta) cb(null, txMeta)
}, },
], done) ], done)
} }
setMaxTxCostAndFee (txMeta) { addTxDefaults (txMeta, cb) {
var txParams = txMeta.txParams const txParams = txMeta.txParams
var gasCost = new BN(ethUtil.stripHexPrefix(txParams.gas || txMeta.estimatedGas), 16) // ensure value
var gasPrice = new BN(ethUtil.stripHexPrefix(txParams.gasPrice || '0x4a817c800'), 16) txParams.value = txParams.value || '0x0'
var txFee = gasCost.mul(gasPrice) this.query.gasPrice((err, gasPrice) => {
var txValue = new BN(ethUtil.stripHexPrefix(txParams.value || '0x0'), 16) if (err) return cb(err)
var maxCost = txValue.add(txFee) // set gasPrice
txMeta.txFee = txFee txParams.gasPrice = gasPrice
txMeta.txValue = txValue // set gasLimit
txMeta.maxCost = maxCost this.txProviderUtils.analyzeGasUsage(txMeta, cb)
txMeta.gasPrice = gasPrice })
this.updateTx(txMeta)
} }
getUnapprovedTxList () { getUnapprovedTxList () {

Loading…
Cancel
Save