aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/transaction-manager.js
diff options
context:
space:
mode:
authorFrances Pangilinan <frankie.diamond@gmail.com>2016-12-21 05:12:14 +0800
committerFrances Pangilinan <frankie.diamond@gmail.com>2016-12-22 05:41:49 +0800
commit1ebcbe296b060c9cf431d485d7bb84f696edbdf5 (patch)
tree0b9cf46894b54f61476b2ebc74d069cab6dc46cd /app/scripts/transaction-manager.js
parent6e78494846c9032fbf1264a0225c0df4df0867cb (diff)
downloadtangerine-wallet-browser-1ebcbe296b060c9cf431d485d7bb84f696edbdf5.tar
tangerine-wallet-browser-1ebcbe296b060c9cf431d485d7bb84f696edbdf5.tar.gz
tangerine-wallet-browser-1ebcbe296b060c9cf431d485d7bb84f696edbdf5.tar.bz2
tangerine-wallet-browser-1ebcbe296b060c9cf431d485d7bb84f696edbdf5.tar.lz
tangerine-wallet-browser-1ebcbe296b060c9cf431d485d7bb84f696edbdf5.tar.xz
tangerine-wallet-browser-1ebcbe296b060c9cf431d485d7bb84f696edbdf5.tar.zst
tangerine-wallet-browser-1ebcbe296b060c9cf431d485d7bb84f696edbdf5.zip
Migrate all tx mutation code out of keyring controller and
Fix up txManager to reflect code review requests
Diffstat (limited to 'app/scripts/transaction-manager.js')
-rw-r--r--app/scripts/transaction-manager.js84
1 files changed, 53 insertions, 31 deletions
diff --git a/app/scripts/transaction-manager.js b/app/scripts/transaction-manager.js
index 6b3d1806f..031993424 100644
--- a/app/scripts/transaction-manager.js
+++ b/app/scripts/transaction-manager.js
@@ -1,8 +1,11 @@
const EventEmitter = require('events')
const extend = require('xtend')
const ethUtil = require('ethereumjs-util')
+const Transaction = require('ethereumjs-tx')
+const BN = ethUtil.BN
const TxProviderUtil = require('./lib/tx-utils')
const createId = require('./lib/random-id')
+const normalize = require('./lib/sig-util').normalize
module.exports = class TransactionManager extends EventEmitter {
constructor (opts) {
@@ -75,7 +78,7 @@ module.exports = class TransactionManager extends EventEmitter {
this._saveTxList(txList)
}
- get unconfTxCount () {
+ get unapprovedTxCount () {
return Object.keys(this.getUnapprovedTxList()).length
}
@@ -128,19 +131,39 @@ module.exports = class TransactionManager extends EventEmitter {
}
}
- resolveSignedTransaction (txPromise) {
- const self = this
-
- txPromise.then(({tx, txParams, cb}) => {
- // Add the tx hash to the persisted meta-tx object
- var txHash = ethUtil.bufferToHex(tx.hash())
+ // formats txParams so the keyringController can sign it
+ formatTxForSigining (txParams, cb) {
+ var address = txParams.from
+ var metaTx = this.getTx(txParams.metamaskId)
+ var gasMultiplier = metaTx.gasMultiplier
+ var gasPrice = new BN(ethUtil.stripHexPrefix(txParams.gasPrice), 16)
+ 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)
+ const ethTx = new Transaction(txParams)
+ // this.updateTxParams(txParams.metamaskId, ethTx)
+
+ // listener is assigned in metamaskController
+ this.emit(`${txParams.metamaskId}:formated`, ethTx, address, txParams.metamaskId, cb)
+ }
- var metaTx = self.getTx(txParams.metamaskId)
- metaTx.hash = txHash
- // return raw serialized tx
- var rawTx = ethUtil.bufferToHex(tx.serialize())
- cb(null, rawTx)
- })
+ // receives a signed tx object and updates the tx hash
+ // and pass it to the cb to be sent off
+ resolveSignedTransaction ({tx, txId, cb}) {
+ // Add the tx hash to the persisted meta-tx object
+ var txHash = ethUtil.bufferToHex(tx.hash())
+ var metaTx = this.getTx(txId)
+ metaTx.hash = txHash
+ this.updateTx(metaTx)
+ var rawTx = ethUtil.bufferToHex(tx.serialize())
+ cb(null, rawTx)
}
/*
@@ -176,20 +199,23 @@ module.exports = class TransactionManager extends EventEmitter {
})
}
- // should return the status of the tx.
- getTxStatus (txId, cb) {
+ // STATUS METHODS
+ // get::set status
+
+ // should return the status of the tx.
+ getTxStatus (txId) {
const txMeta = this.getTx(txId)
- return cb ? cb(txMeta.staus) : txMeta.status
+ return txMeta.status
}
- // should update the status of the tx to 'signed'.
+ // should update the status of the tx to 'signed'.
setTxStatusSigned (txId) {
this._setTxStatus(txId, 'signed')
this.emit('update')
}
- // should update the status of the tx to 'rejected'.
+ // should update the status of the tx to 'rejected'.
setTxStatusRejected (txId) {
this._setTxStatus(txId, 'rejected')
this.emit('update')
@@ -203,7 +229,7 @@ module.exports = class TransactionManager extends EventEmitter {
// use extend to ensure that all fields are filled
updateTxParams (txId, txParams) {
var txMeta = this.getTx(txId)
- txMeta.txParams = extend(txMeta, txParams)
+ txMeta.txParams = extend(txMeta.txParams, txParams)
this.updateTx(txMeta)
}
@@ -218,7 +244,7 @@ module.exports = class TransactionManager extends EventEmitter {
if (!txHash) return
this.txProviderUtils.query.getTransactionByHash(txHash, (err, txMeta) => {
if (err || !txMeta) {
- tx.err = err || 'Tx could possibly have not submitted'
+ tx.err = err || 'Tx could possibly have not been submitted'
this.updateTx(tx)
return txMeta ? console.error(err) : console.debug(`txMeta is ${txMeta} for:`, tx)
}
@@ -229,16 +255,7 @@ module.exports = class TransactionManager extends EventEmitter {
})
}
- // Private functions
-
- // Saves the new/updated txList.
- // Function is intended only for internal use
- _saveTxList (txList) {
- this.txList = txList
- this._setTxList(txList)
- }
-
- // should return the tx
+ // PRIVATE METHODS
// Should find the tx in the tx list and
// update it.
@@ -255,7 +272,12 @@ module.exports = class TransactionManager extends EventEmitter {
this.updateTx(txMeta)
}
-
+ // Saves the new/updated txList.
+ // Function is intended only for internal use
+ _saveTxList (txList) {
+ this.txList = txList
+ this._setTxList(txList)
+ }
}