aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/controllers
diff options
context:
space:
mode:
authorfrankiebee <frankie.diamond@gmail.com>2017-07-27 02:56:52 +0800
committerfrankiebee <frankie.diamond@gmail.com>2017-08-02 22:26:25 +0800
commit432f516ab005dd2b4eb4b2e8766ed30216386d98 (patch)
tree1e150e48c39ca55d14f9b2b6f1838dbd84b34732 /app/scripts/controllers
parent5af753a597d565e4654899ea37349ba7e839bb00 (diff)
downloadtangerine-wallet-browser-432f516ab005dd2b4eb4b2e8766ed30216386d98.tar.gz
tangerine-wallet-browser-432f516ab005dd2b4eb4b2e8766ed30216386d98.tar.zst
tangerine-wallet-browser-432f516ab005dd2b4eb4b2e8766ed30216386d98.zip
make addUnapprovedTransaction async function and use promise based ethQuery
Diffstat (limited to 'app/scripts/controllers')
-rw-r--r--app/scripts/controllers/transactions.js296
1 files changed, 138 insertions, 158 deletions
diff --git a/app/scripts/controllers/transactions.js b/app/scripts/controllers/transactions.js
index 5f2d75b47..d3e852ef9 100644
--- a/app/scripts/controllers/transactions.js
+++ b/app/scripts/controllers/transactions.js
@@ -1,10 +1,11 @@
const EventEmitter = require('events')
const async = require('async')
const extend = require('xtend')
+const pify = require('pify')
const clone = require('clone')
const ObservableStore = require('obs-store')
const ethUtil = require('ethereumjs-util')
-const pify = require('pify')
+const EthQuery = require('ethjs-query');
const TxProviderUtil = require('../lib/tx-utils')
const getStack = require('../lib/util').getStack
const createId = require('../lib/random-id')
@@ -33,7 +34,7 @@ module.exports = class TransactionController extends EventEmitter {
})
},
})
- this.query = opts.ethQuery
+ this.query = new EthQuery(this.provider)
this.txProviderUtils = new TxProviderUtil(this.query)
this.blockTracker.on('rawBlock', this.checkForTxInBlock.bind(this))
// this is a little messy but until ethstore has been either
@@ -62,13 +63,6 @@ module.exports = class TransactionController extends EventEmitter {
return this.preferencesStore.getState().selectedAddress
}
- // Returns the tx list
- getTxList () {
- const network = this.getNetwork()
- const fullTxList = this.getFullTxList()
- return fullTxList.filter(txMeta => txMeta.metamaskNetworkId === network)
- }
-
// Returns the number of txs for the current network.
getTxCount () {
return this.getTxList().length
@@ -79,6 +73,50 @@ module.exports = class TransactionController extends EventEmitter {
return this.store.getState().transactions
}
+ get unapprovedTxCount () {
+ return Object.keys(this.getUnapprovedTxList()).length
+ }
+
+ get pendingTxCount () {
+ return this.getTxsByMetaData('status', 'signed').length
+ }
+
+ // Returns the tx list
+ getTxList () {
+ const network = this.getNetwork()
+ const fullTxList = this.getFullTxList()
+ return this.getTxsByMetaData('metamaskNetworkId', network, fullTxList)
+ }
+
+ // gets tx by Id and returns it
+ getTx (txId) {
+ const txList = this.getTxList()
+ const txMeta = txList.find(txData => txData.id === txId)
+ return txMeta
+ }
+ getUnapprovedTxList () {
+ let txList = this.getTxList()
+ return txList.filter((txMeta) => txMeta.status === 'unapproved')
+ .reduce((result, tx) => {
+ result[tx.id] = tx
+ return result
+ }, {})
+ }
+
+ updateTx (txMeta) {
+ const txMetaForHistory = clone(txMeta)
+ txMetaForHistory.stack = getStack()
+ const txId = txMeta.id
+ const txList = this.getFullTxList()
+ const index = txList.findIndex(txData => txData.id === txId)
+ if (!txMeta.history) txMeta.history = []
+ txMeta.history.push(txMetaForHistory)
+
+ txList[index] = txMeta
+ this._saveTxList(txList)
+ this.emit('update')
+ }
+
// Adds a tx to the txlist
addTx (txMeta) {
const txCount = this.getTxCount()
@@ -92,7 +130,7 @@ module.exports = class TransactionController extends EventEmitter {
// or rejected tx's.
// not tx's that are pending or unapproved
if (txCount > txHistoryLimit - 1) {
- var index = fullTxList.findIndex((metaTx) => ((metaTx.status === 'confirmed' || metaTx.status === 'rejected') && network === txMeta.metamaskNetworkId))
+ let index = fullTxList.findIndex((metaTx) => ((metaTx.status === 'confirmed' || metaTx.status === 'rejected') && network === txMeta.metamaskNetworkId))
fullTxList.splice(index, 1)
}
fullTxList.push(txMeta)
@@ -110,86 +148,40 @@ module.exports = class TransactionController extends EventEmitter {
this.emit(`${txMeta.id}:unapproved`, txMeta)
}
- // gets tx by Id and returns it
- getTx (txId, cb) {
- var txList = this.getTxList()
- var txMeta = txList.find(txData => txData.id === txId)
- return cb ? cb(txMeta) : txMeta
- }
-
- //
- updateTx (txMeta) {
- const txMetaForHistory = clone(txMeta)
- txMetaForHistory.stack = getStack()
- var txId = txMeta.id
- var txList = this.getFullTxList()
- var index = txList.findIndex(txData => txData.id === txId)
- if (!txMeta.history) txMeta.history = []
- txMeta.history.push(txMetaForHistory)
-
- txList[index] = txMeta
- this._saveTxList(txList)
- this.emit('update')
- }
-
- get unapprovedTxCount () {
- return Object.keys(this.getUnapprovedTxList()).length
- }
-
- get pendingTxCount () {
- return this.getTxsByMetaData('status', 'signed').length
- }
-
- addUnapprovedTransaction (txParams, done) {
- let txMeta = {}
- async.waterfall([
- // validate
- (cb) => this.txProviderUtils.validateTxParams(txParams, cb),
- // construct txMeta
- (cb) => {
- txMeta = {
- id: createId(),
- time: (new Date()).getTime(),
- status: 'unapproved',
- metamaskNetworkId: this.getNetwork(),
- txParams: txParams,
- history: [],
- }
- cb()
- },
- // add default tx params
- (cb) => this.addTxDefaults(txMeta, cb),
- // save txMeta
- (cb) => {
- this.addTx(txMeta)
- cb(null, txMeta)
- },
- ], done)
+ async addUnapprovedTransaction (txParams) {
+ // validate
+ await this.txProviderUtils.validateTxParams(txParams)
+ // construct txMeta
+ const txMeta = {
+ id: createId(),
+ time: (new Date()).getTime(),
+ status: 'unapproved',
+ metamaskNetworkId: this.getNetwork(),
+ txParams: txParams,
+ history: [],
+ }
+ // add default tx params
+ await this.addTxDefaults(txMeta),
+ // save txMeta
+ this.addTx(txMeta)
+ return txMeta
}
- addTxDefaults (txMeta, cb) {
+ async addTxDefaults (txMeta) {
const txParams = txMeta.txParams
// ensure value
txParams.value = txParams.value || '0x0'
if (!txParams.gasPrice) {
- this.query.gasPrice((err, gasPrice) => {
-
- if (err) return cb(err)
- // set gasPrice
- txParams.gasPrice = gasPrice
- })
+ gassPrice = await this.query.gasPrice()
+ txParams.gasPrice = gasPrice
}
// set gasLimit
- this.txProviderUtils.analyzeGasUsage(txMeta, cb)
+ return await this.txProviderUtils.analyzeGasUsage(txMeta)
}
- getUnapprovedTxList () {
- var txList = this.getTxList()
- return txList.filter((txMeta) => txMeta.status === 'unapproved')
- .reduce((result, tx) => {
- result[tx.id] = tx
- return result
- }, {})
+ async updateAndApproveTransaction (txMeta) {
+ this.updateTx(txMeta)
+ await this.approveTransaction(txMeta.id)
}
async approveTransaction (txId) {
@@ -221,26 +213,6 @@ module.exports = class TransactionController extends EventEmitter {
}
}
- cancelTransaction (txId, cb = warn) {
- this.setTxStatusRejected(txId)
- cb()
- }
-
- async updateAndApproveTransaction (txMeta) {
- this.updateTx(txMeta)
- await this.approveTransaction(txMeta.id)
- }
-
- getChainId () {
- const networkState = this.networkStore.getState()
- const getChainId = parseInt(networkState)
- if (Number.isNaN(getChainId)) {
- return 0
- } else {
- return getChainId
- }
- }
-
async signTransaction (txId) {
const txMeta = this.getTx(txId)
const txParams = txMeta.txParams
@@ -265,6 +237,22 @@ module.exports = class TransactionController extends EventEmitter {
})
}
+ cancelTransaction (txId) {
+ this.setTxStatusRejected(txId)
+ return Promise.resolve()
+ }
+
+
+ getChainId () {
+ const networkState = this.networkStore.getState()
+ const getChainId = parseInt(networkState)
+ if (Number.isNaN(getChainId)) {
+ return 0
+ } else {
+ return getChainId
+ }
+ }
+
// receives a txHash records the tx as signed
setTxHash (txId, txHash) {
// Add the tx hash to the persisted meta-tx object
@@ -275,7 +263,7 @@ module.exports = class TransactionController extends EventEmitter {
/*
Takes an object of fields to search for eg:
- var thingsToLookFor = {
+ let thingsToLookFor = {
to: '0x0..',
from: '0x0..',
status: 'signed',
@@ -298,7 +286,7 @@ module.exports = class TransactionController extends EventEmitter {
and that have been 'confirmed'
*/
getFilteredTxList (opts) {
- var filteredTxList
+ let filteredTxList
Object.keys(opts).forEach((key) => {
filteredTxList = this.getTxsByMetaData(key, opts[key], filteredTxList)
})
@@ -359,7 +347,7 @@ module.exports = class TransactionController extends EventEmitter {
// merges txParams obj onto txData.txParams
// use extend to ensure that all fields are filled
updateTxParams (txId, txParams) {
- var txMeta = this.getTx(txId)
+ const txMeta = this.getTx(txId)
txMeta.txParams = extend(txMeta.txParams, txParams)
this.updateTx(txMeta)
}
@@ -367,20 +355,19 @@ module.exports = class TransactionController extends EventEmitter {
// checks if a signed tx is in a block and
// if included sets the tx status as 'confirmed'
checkForTxInBlock (block) {
- var signedTxList = this.getFilteredTxList({status: 'submitted'})
+ const signedTxList = this.getFilteredTxList({status: 'submitted'})
if (!signedTxList.length) return
signedTxList.forEach((txMeta) => {
- var txHash = txMeta.hash
- var txId = txMeta.id
+ const txHash = txMeta.hash
+ const txId = txMeta.id
if (!txHash) {
- return this.setTxStatusFailed(txId, {
- stack: 'checkForTxInBlock: custom tx-controller error message',
- errCode: 'No hash was provided',
- message: 'We had an error while submitting this transaction, please try again.',
- })
+ const noTxHash = new Error('We had an error while submitting this transaction, please try again.')
+ noTxHash.name = 'NoTxHashError'
+ this.setTxStatusFailed(noTxHash)
}
+
block.transactions.forEach((tx) => {
if (tx.hash === txHash) this.setTxStatusConfirmed(txId)
})
@@ -398,6 +385,39 @@ module.exports = class TransactionController extends EventEmitter {
if (diff > 1) this._checkPendingTxs()
}
+ resubmitPendingTxs () {
+ const pending = this.getTxsByMetaData('status', 'submitted')
+ // only try resubmitting if their are transactions to resubmit
+ if (!pending.length) return
+ pending.forEach((txMeta) => this._resubmitTx(txMeta).catch((err) => {
+ /*
+ Dont marked as failed if the error is a "known" transaction warning
+ "there is already a transaction with the same sender-nonce
+ but higher/same gas price"
+ */
+ const errorMessage = err.message.toLowerCase()
+ const isKnownTx = (
+ // geth
+ errorMessage.includes('replacement transaction underpriced')
+ || errorMessage.includes('known transaction')
+ // parity
+ || errorMessage.includes('gas price too low to replace')
+ || errorMessage.includes('transaction with the same hash was already imported')
+ // other
+ || errorMessage.includes('gateway timeout')
+ || errorMessage.includes('nonce too low')
+ )
+ // ignore resubmit warnings, return early
+ if (isKnownTx) return
+ // encountered real error - transition to error state
+ this.setTxStatusFailed(txMeta.id, {
+ stack: err.stack || err.message,
+ errCode: err.errCode || err,
+ message: err.message,
+ })
+ }))
+ }
+
// PRIVATE METHODS
// Should find the tx in the tx list and
@@ -411,7 +431,7 @@ module.exports = class TransactionController extends EventEmitter {
// - `'confirmed'` the tx has been included in a block.
// - `'failed'` the tx failed for some reason, included on tx data.
_setTxStatus (txId, status) {
- var txMeta = this.getTx(txId)
+ const txMeta = this.getTx(txId)
txMeta.status = status
this.emit(`${txMeta.id}:${status}`, txId)
if (status === 'submitted' || status === 'rejected') {
@@ -436,39 +456,6 @@ module.exports = class TransactionController extends EventEmitter {
this.memStore.updateState({ unapprovedTxs, selectedAddressTxList })
}
- resubmitPendingTxs () {
- const pending = this.getTxsByMetaData('status', 'submitted')
- // only try resubmitting if their are transactions to resubmit
- if (!pending.length) return
- pending.forEach((txMeta) => this._resubmitTx(txMeta).catch((err) => {
- /*
- Dont marked as failed if the error is a "known" transaction warning
- "there is already a transaction with the same sender-nonce
- but higher/same gas price"
- */
- const errorMessage = err.message.toLowerCase()
- const isKnownTx = (
- // geth
- errorMessage.includes('replacement transaction underpriced')
- || errorMessage.includes('known transaction')
- // parity
- || errorMessage.includes('gas price too low to replace')
- || errorMessage.includes('transaction with the same hash was already imported')
- // other
- || errorMessage.includes('gateway timeout')
- || errorMessage.includes('nonce too low')
- )
- // ignore resubmit warnings, return early
- if (isKnownTx) return
- // encountered real error - transition to error state
- this.setTxStatusFailed(txMeta.id, {
- stack: err.stack || err.message,
- errCode: err.errCode || err,
- message: err.message,
- })
- }))
- }
-
async _resubmitTx (txMeta) {
const address = txMeta.txParams.from
const balance = this.ethStore.getState().accounts[address].balance
@@ -515,17 +502,14 @@ module.exports = class TransactionController extends EventEmitter {
// extra check in case there was an uncaught error during the
// signature and submission process
if (!txHash) {
- this.setTxStatusFailed(txId, {
- stack: '_checkPendingTxs: custom tx-controller error message',
- errCode: 'No hash was provided',
- message: 'We had an error while submitting this transaction, please try again.',
- })
- return
+ const noTxHash = new Error('We had an error while submitting this transaction, please try again.')
+ noTxHash.name = 'NoTxHashError'
+ this.setTxStatusFailed(noTxHash)
}
// get latest transaction status
let txParams
try {
- txParams = await pify((cb) => this.query.getTransactionByHash(txHash, cb))()
+ txParams = await this.query.getTransactionByHash(txHash)
if (!txParams) return
if (txParams.blockNumber) {
this.setTxStatusConfirmed(txId)
@@ -538,12 +522,8 @@ module.exports = class TransactionController extends EventEmitter {
message: 'There was a problem loading this transaction.',
}
this.updateTx(txMeta)
- log.error(err)
+ throw err
}
}
}
-
-}
-
-
-const warn = () => log.warn('warn was used no cb provided')
+} \ No newline at end of file