|
|
@ -1,6 +1,5 @@ |
|
|
|
const EventEmitter = require('events') |
|
|
|
const EventEmitter = require('events') |
|
|
|
const extend = require('xtend') |
|
|
|
const extend = require('xtend') |
|
|
|
const clone = require('clone') |
|
|
|
|
|
|
|
const ObservableStore = require('obs-store') |
|
|
|
const ObservableStore = require('obs-store') |
|
|
|
const ethUtil = require('ethereumjs-util') |
|
|
|
const ethUtil = require('ethereumjs-util') |
|
|
|
const EthQuery = require('ethjs-query') |
|
|
|
const EthQuery = require('ethjs-query') |
|
|
@ -24,16 +23,18 @@ module.exports = class TransactionController extends EventEmitter { |
|
|
|
this.query = new EthQuery(this.provider) |
|
|
|
this.query = new EthQuery(this.provider) |
|
|
|
this.txProviderUtil = new TxProviderUtil(this.provider) |
|
|
|
this.txProviderUtil = new TxProviderUtil(this.provider) |
|
|
|
|
|
|
|
|
|
|
|
this.txStateManager = new TransactionStateManger(extend({ |
|
|
|
this.txStateManager = new TransactionStateManger({ |
|
|
|
|
|
|
|
initState: extend({ |
|
|
|
transactions: [], |
|
|
|
transactions: [], |
|
|
|
|
|
|
|
}, opts.initState), |
|
|
|
txHistoryLimit: opts.txHistoryLimit, |
|
|
|
txHistoryLimit: opts.txHistoryLimit, |
|
|
|
getNetwork: this.getNetwork.bind(this), |
|
|
|
getNetwork: this.getNetwork.bind(this), |
|
|
|
}, opts.initState)) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
this.nonceTracker = new NonceTracker({ |
|
|
|
this.nonceTracker = new NonceTracker({ |
|
|
|
provider: this.provider, |
|
|
|
provider: this.provider, |
|
|
|
getPendingTransactions: (address) => { |
|
|
|
getPendingTransactions: (address) => { |
|
|
|
return this.getFilteredTxList({ |
|
|
|
return this.txStateManager.getFilteredTxList({ |
|
|
|
from: address, |
|
|
|
from: address, |
|
|
|
status: 'submitted', |
|
|
|
status: 'submitted', |
|
|
|
err: undefined, |
|
|
|
err: undefined, |
|
|
@ -52,16 +53,16 @@ module.exports = class TransactionController extends EventEmitter { |
|
|
|
publishTransaction: this.txProviderUtil.publishTransaction.bind(this.txProviderUtil), |
|
|
|
publishTransaction: this.txProviderUtil.publishTransaction.bind(this.txProviderUtil), |
|
|
|
getPendingTransactions: () => { |
|
|
|
getPendingTransactions: () => { |
|
|
|
const network = this.getNetwork() |
|
|
|
const network = this.getNetwork() |
|
|
|
return this.getFilteredTxList({ |
|
|
|
return this.txStateManager.getFilteredTxList({ |
|
|
|
status: 'submitted', |
|
|
|
status: 'submitted', |
|
|
|
metamaskNetworkId: network, |
|
|
|
metamaskNetworkId: network, |
|
|
|
}) |
|
|
|
}) |
|
|
|
}, |
|
|
|
}, |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
this.pendingTxTracker.on('txWarning', this.updateTx.bind(this)) |
|
|
|
this.pendingTxTracker.on('txWarning', this.txStateManager.updateTx.bind(this.txStateManager)) |
|
|
|
this.pendingTxTracker.on('txFailed', this.setTxStatusFailed.bind(this)) |
|
|
|
this.pendingTxTracker.on('txFailed', this.txStateManager.setTxStatusFailed.bind(this.txStateManager)) |
|
|
|
this.pendingTxTracker.on('txConfirmed', this.setTxStatusConfirmed.bind(this)) |
|
|
|
this.pendingTxTracker.on('txConfirmed', this.txStateManager.setTxStatusConfirmed.bind(this.txStateManager)) |
|
|
|
|
|
|
|
|
|
|
|
this.blockTracker.on('rawBlock', this.pendingTxTracker.checkForTxInBlock.bind(this.pendingTxTracker)) |
|
|
|
this.blockTracker.on('rawBlock', this.pendingTxTracker.checkForTxInBlock.bind(this.pendingTxTracker)) |
|
|
|
// this is a little messy but until ethstore has been either
|
|
|
|
// this is a little messy but until ethstore has been either
|
|
|
@ -91,35 +92,17 @@ module.exports = class TransactionController extends EventEmitter { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
getUnapprovedTxCount () { |
|
|
|
getUnapprovedTxCount () { |
|
|
|
return Object.keys(this.getUnapprovedTxList()).length |
|
|
|
return Object.keys(this.txStateManager.getUnapprovedTxList()).length |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
getPendingTxCount () { |
|
|
|
getPendingTxCount () { |
|
|
|
return this.txStateManager.getTxsByMetaData('status', 'signed').length |
|
|
|
return this.txStateManager.getTxsByMetaData('status', 'signed').length |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Returns the tx list
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getUnapprovedTxList () { |
|
|
|
|
|
|
|
const txList = this.txStateManager.getTxsByMetaData('status', 'unapproved') |
|
|
|
|
|
|
|
return txList.reduce((result, tx) => { |
|
|
|
|
|
|
|
result[tx.id] = tx |
|
|
|
|
|
|
|
return result |
|
|
|
|
|
|
|
}, {}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Adds a tx to the txlist
|
|
|
|
// Adds a tx to the txlist
|
|
|
|
addTx (txMeta) { |
|
|
|
addTx (txMeta) { |
|
|
|
this.txStateManager.addTx(txMeta) |
|
|
|
this.txStateManager.addTx(txMeta) |
|
|
|
this.emit('update') |
|
|
|
this.emit('update') |
|
|
|
|
|
|
|
|
|
|
|
this.once(`${txMeta.id}:signed`, function (txId) { |
|
|
|
|
|
|
|
this.removeAllListeners(`${txMeta.id}:rejected`) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
this.once(`${txMeta.id}:rejected`, function (txId) { |
|
|
|
|
|
|
|
this.removeAllListeners(`${txMeta.id}:signed`) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.emit('updateBadge') |
|
|
|
this.emit('updateBadge') |
|
|
|
this.emit(`${txMeta.id}:unapproved`, txMeta) |
|
|
|
this.emit(`${txMeta.id}:unapproved`, txMeta) |
|
|
|
} |
|
|
|
} |
|
|
@ -130,7 +113,7 @@ module.exports = class TransactionController extends EventEmitter { |
|
|
|
this.emit('newUnaprovedTx', txMeta) |
|
|
|
this.emit('newUnaprovedTx', txMeta) |
|
|
|
// listen for tx completion (success, fail)
|
|
|
|
// listen for tx completion (success, fail)
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
this.once(`${txMeta.id}:finished`, (completedTx) => { |
|
|
|
this.txStateManager.once(`${txMeta.id}:finished`, (completedTx) => { |
|
|
|
switch (completedTx.status) { |
|
|
|
switch (completedTx.status) { |
|
|
|
case 'submitted': |
|
|
|
case 'submitted': |
|
|
|
return resolve(completedTx.hash) |
|
|
|
return resolve(completedTx.hash) |
|
|
@ -158,7 +141,7 @@ module.exports = class TransactionController extends EventEmitter { |
|
|
|
// add default tx params
|
|
|
|
// add default tx params
|
|
|
|
await this.addTxDefaults(txMeta) |
|
|
|
await this.addTxDefaults(txMeta) |
|
|
|
// save txMeta
|
|
|
|
// save txMeta
|
|
|
|
this.txStateManager.addTx(txMeta) |
|
|
|
this.addTx(txMeta) |
|
|
|
return txMeta |
|
|
|
return txMeta |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -175,7 +158,7 @@ module.exports = class TransactionController extends EventEmitter { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async updateAndApproveTransaction (txMeta) { |
|
|
|
async updateAndApproveTransaction (txMeta) { |
|
|
|
this.updateTx(txMeta) |
|
|
|
this.txStateManager.updateTx(txMeta) |
|
|
|
await this.approveTransaction(txMeta.id) |
|
|
|
await this.approveTransaction(txMeta.id) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -183,9 +166,9 @@ module.exports = class TransactionController extends EventEmitter { |
|
|
|
let nonceLock |
|
|
|
let nonceLock |
|
|
|
try { |
|
|
|
try { |
|
|
|
// approve
|
|
|
|
// approve
|
|
|
|
this.setTxStatusApproved(txId) |
|
|
|
this.txStateManager.setTxStatusApproved(txId) |
|
|
|
// get next nonce
|
|
|
|
// get next nonce
|
|
|
|
const txMeta = this.getTx(txId) |
|
|
|
const txMeta = this.txStateManager.getTx(txId) |
|
|
|
const fromAddress = txMeta.txParams.from |
|
|
|
const fromAddress = txMeta.txParams.from |
|
|
|
// wait for a nonce
|
|
|
|
// wait for a nonce
|
|
|
|
nonceLock = await this.nonceTracker.getNonceLock(fromAddress) |
|
|
|
nonceLock = await this.nonceTracker.getNonceLock(fromAddress) |
|
|
@ -193,14 +176,14 @@ module.exports = class TransactionController extends EventEmitter { |
|
|
|
txMeta.txParams.nonce = nonceLock.nextNonce |
|
|
|
txMeta.txParams.nonce = nonceLock.nextNonce |
|
|
|
// add nonce debugging information to txMeta
|
|
|
|
// add nonce debugging information to txMeta
|
|
|
|
txMeta.nonceDetails = nonceLock.nonceDetails |
|
|
|
txMeta.nonceDetails = nonceLock.nonceDetails |
|
|
|
this.updateTx(txMeta) |
|
|
|
this.txStateManager.updateTx(txMeta) |
|
|
|
// sign transaction
|
|
|
|
// sign transaction
|
|
|
|
const rawTx = await this.signTransaction(txId) |
|
|
|
const rawTx = await this.signTransaction(txId) |
|
|
|
await this.publishTransaction(txId, rawTx) |
|
|
|
await this.publishTransaction(txId, rawTx) |
|
|
|
// must set transaction to submitted/failed before releasing lock
|
|
|
|
// must set transaction to submitted/failed before releasing lock
|
|
|
|
nonceLock.releaseLock() |
|
|
|
nonceLock.releaseLock() |
|
|
|
} catch (err) { |
|
|
|
} catch (err) { |
|
|
|
this.setTxStatusFailed(txId, err) |
|
|
|
this.txStateManager.setTxStatusFailed(txId, err) |
|
|
|
// must set transaction to submitted/failed before releasing lock
|
|
|
|
// must set transaction to submitted/failed before releasing lock
|
|
|
|
if (nonceLock) nonceLock.releaseLock() |
|
|
|
if (nonceLock) nonceLock.releaseLock() |
|
|
|
// continue with error chain
|
|
|
|
// continue with error chain
|
|
|
@ -209,29 +192,29 @@ module.exports = class TransactionController extends EventEmitter { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async signTransaction (txId) { |
|
|
|
async signTransaction (txId) { |
|
|
|
const txMeta = this.getTx(txId) |
|
|
|
const txMeta = this.txStateManager.getTx(txId) |
|
|
|
const txParams = txMeta.txParams |
|
|
|
const txParams = txMeta.txParams |
|
|
|
const fromAddress = txParams.from |
|
|
|
const fromAddress = txParams.from |
|
|
|
// add network/chain id
|
|
|
|
// add network/chain id
|
|
|
|
txParams.chainId = this.getChainId() |
|
|
|
txParams.chainId = this.getChainId() |
|
|
|
const ethTx = this.txProviderUtil.buildEthTxFromParams(txParams) |
|
|
|
const ethTx = this.txProviderUtil.buildEthTxFromParams(txParams) |
|
|
|
await this.signEthTx(ethTx, fromAddress) |
|
|
|
await this.signEthTx(ethTx, fromAddress) |
|
|
|
this.setTxStatusSigned(txMeta.id) |
|
|
|
this.txStateManager.setTxStatusSigned(txMeta.id) |
|
|
|
const rawTx = ethUtil.bufferToHex(ethTx.serialize()) |
|
|
|
const rawTx = ethUtil.bufferToHex(ethTx.serialize()) |
|
|
|
return rawTx |
|
|
|
return rawTx |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async publishTransaction (txId, rawTx) { |
|
|
|
async publishTransaction (txId, rawTx) { |
|
|
|
const txMeta = this.getTx(txId) |
|
|
|
const txMeta = this.txStateManager.getTx(txId) |
|
|
|
txMeta.rawTx = rawTx |
|
|
|
txMeta.rawTx = rawTx |
|
|
|
this.updateTx(txMeta) |
|
|
|
this.txStateManager.updateTx(txMeta) |
|
|
|
const txHash = await this.txProviderUtil.publishTransaction(rawTx) |
|
|
|
const txHash = await this.txProviderUtil.publishTransaction(rawTx) |
|
|
|
this.setTxHash(txId, txHash) |
|
|
|
this.setTxHash(txId, txHash) |
|
|
|
this.setTxStatusSubmitted(txId) |
|
|
|
this.txStateManager.setTxStatusSubmitted(txId) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async cancelTransaction (txId) { |
|
|
|
async cancelTransaction (txId) { |
|
|
|
this.setTxStatusRejected(txId) |
|
|
|
this.txStateManager.setTxStatusRejected(txId) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -248,9 +231,9 @@ module.exports = class TransactionController extends EventEmitter { |
|
|
|
// receives a txHash records the tx as signed
|
|
|
|
// receives a txHash records the tx as signed
|
|
|
|
setTxHash (txId, txHash) { |
|
|
|
setTxHash (txId, txHash) { |
|
|
|
// Add the tx hash to the persisted meta-tx object
|
|
|
|
// Add the tx hash to the persisted meta-tx object
|
|
|
|
const txMeta = this.getTx(txId) |
|
|
|
const txMeta = this.txStateManager.getTx(txId) |
|
|
|
txMeta.hash = txHash |
|
|
|
txMeta.hash = txHash |
|
|
|
this.updateTx(txMeta) |
|
|
|
this.txStateManager.updateTx(txMeta) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* _____________________________________ |
|
|
|
/* _____________________________________ |
|
|
@ -259,8 +242,8 @@ module.exports = class TransactionController extends EventEmitter { |
|
|
|
|______________________________________*/ |
|
|
|
|______________________________________*/ |
|
|
|
|
|
|
|
|
|
|
|
_updateMemstore () { |
|
|
|
_updateMemstore () { |
|
|
|
const unapprovedTxs = this.getUnapprovedTxList() |
|
|
|
const unapprovedTxs = this.txStateManager.getUnapprovedTxList() |
|
|
|
const selectedAddressTxList = this.getFilteredTxList({ |
|
|
|
const selectedAddressTxList = this.txStateManager.getFilteredTxList({ |
|
|
|
from: this.getSelectedAddress(), |
|
|
|
from: this.getSelectedAddress(), |
|
|
|
metamaskNetworkId: this.getNetwork(), |
|
|
|
metamaskNetworkId: this.getNetwork(), |
|
|
|
}) |
|
|
|
}) |
|
|
|