1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-30 08:09:15 +01:00

add better comments

This commit is contained in:
frankiebee 2017-09-05 21:50:36 -07:00
parent 00bd5b143f
commit 15c12ca4bb
3 changed files with 43 additions and 34 deletions

View File

@ -2,13 +2,29 @@ const EventEmitter = require('events')
const extend = require('xtend') const extend = require('xtend')
const ObservableStore = require('obs-store') const ObservableStore = require('obs-store')
const ethUtil = require('ethereumjs-util') const ethUtil = require('ethereumjs-util')
const Transaction = require('ethereumjs-tx')
const EthQuery = require('ethjs-query') const EthQuery = require('ethjs-query')
const TransactionStateManger = require('../lib/tx-state-manager') const TransactionStateManger = require('../lib/tx-state-manager')
const TxProviderUtil = require('../lib/tx-utils') const TxGasUtil = require('../lib/tx-gas-utils')
const PendingTransactionTracker = require('../lib/pending-tx-tracker') const PendingTransactionTracker = require('../lib/pending-tx-tracker')
const createId = require('../lib/random-id') const createId = require('../lib/random-id')
const NonceTracker = require('../lib/nonce-tracker') const NonceTracker = require('../lib/nonce-tracker')
/*
Transaction Controller is an aggregate of sub-controllers and trackers
composing them in a way to be exposed to the metamask controller
- txStateManager
responsible for the state of a transaction and
storing the transaction
- pendingTxTracker
watching blocks for transactions to be include
and emitting confirmed events
- txGasUtil
gas calculations and safety buffering
- nonceTracker
calculating nonces
*/
module.exports = class TransactionController extends EventEmitter { module.exports = class TransactionController extends EventEmitter {
constructor (opts) { constructor (opts) {
super() super()
@ -21,7 +37,7 @@ module.exports = class TransactionController extends EventEmitter {
this.memStore = new ObservableStore({}) this.memStore = new ObservableStore({})
this.query = new EthQuery(this.provider) this.query = new EthQuery(this.provider)
this.txProviderUtil = new TxProviderUtil(this.provider) this.txGasUtil = new TxGasUtil(this.provider)
this.txStateManager = new TransactionStateManger({ this.txStateManager = new TransactionStateManger({
initState: extend({ initState: extend({
@ -37,7 +53,6 @@ module.exports = class TransactionController extends EventEmitter {
return this.txStateManager.getFilteredTxList({ return this.txStateManager.getFilteredTxList({
from: address, from: address,
status: 'submitted', status: 'submitted',
err: undefined,
}) })
}, },
}) })
@ -50,16 +65,17 @@ module.exports = class TransactionController extends EventEmitter {
if (!account) return if (!account) return
return account.balance return account.balance
}, },
publishTransaction: this.txProviderUtil.publishTransaction.bind(this.txProviderUtil), publishTransaction: this.query.sendRawTransaction,
getPendingTransactions: () => { getPendingTransactions: () => {
const network = this.getNetwork() return this.txStateManager.getFilteredTxList({ status: 'submitted' })
return this.txStateManager.getFilteredTxList({
status: 'submitted',
metamaskNetworkId: network,
})
}, },
}) })
this.txStateManager.subscribe(() => {
this.emit('update')
this.emit('updateBadge')
})
this.pendingTxTracker.on('txWarning', this.txStateManager.updateTx.bind(this.txStateManager)) this.pendingTxTracker.on('txWarning', this.txStateManager.updateTx.bind(this.txStateManager))
this.pendingTxTracker.on('txFailed', this.txStateManager.setTxStatusFailed.bind(this.txStateManager)) this.pendingTxTracker.on('txFailed', this.txStateManager.setTxStatusFailed.bind(this.txStateManager))
this.pendingTxTracker.on('txConfirmed', this.txStateManager.setTxStatusConfirmed.bind(this.txStateManager)) this.pendingTxTracker.on('txConfirmed', this.txStateManager.setTxStatusConfirmed.bind(this.txStateManager))
@ -99,11 +115,19 @@ module.exports = class TransactionController extends EventEmitter {
return this.txStateManager.getTxsByMetaData('status', 'signed').length return this.txStateManager.getTxsByMetaData('status', 'signed').length
} }
getChainId () {
const networkState = this.networkStore.getState()
const getChainId = parseInt(networkState)
if (Number.isNaN(getChainId)) {
return 0
} else {
return getChainId
}
}
// 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('updateBadge')
this.emit(`${txMeta.id}:unapproved`, txMeta) this.emit(`${txMeta.id}:unapproved`, txMeta)
} }
@ -114,7 +138,6 @@ module.exports = class TransactionController extends EventEmitter {
// listen for tx completion (success, fail) // listen for tx completion (success, fail)
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.txStateManager.once(`${txMeta.id}:finished`, (completedTx) => { this.txStateManager.once(`${txMeta.id}:finished`, (completedTx) => {
this.emit('updateBadge')
switch (completedTx.status) { switch (completedTx.status) {
case 'submitted': case 'submitted':
return resolve(completedTx.hash) return resolve(completedTx.hash)
@ -129,7 +152,7 @@ module.exports = class TransactionController extends EventEmitter {
async addUnapprovedTransaction (txParams) { async addUnapprovedTransaction (txParams) {
// validate // validate
await this.txProviderUtil.validateTxParams(txParams) await this.txGasUtil.validateTxParams(txParams)
// construct txMeta // construct txMeta
const txMeta = { const txMeta = {
id: createId(), id: createId(),
@ -148,13 +171,11 @@ module.exports = class TransactionController extends EventEmitter {
async addTxDefaults (txMeta) { async addTxDefaults (txMeta) {
const txParams = txMeta.txParams const txParams = txMeta.txParams
// ensure value // ensure value
const gasPrice = txParams.gasPrice || await this.query.gasPrice()
txParams.value = txParams.value || '0x0' txParams.value = txParams.value || '0x0'
if (!txParams.gasPrice) { txParams.gasPrice = ethUtil.addHexPrefix(gasPrice.toString(16))
const gasPrice = await this.query.gasPrice()
txParams.gasPrice = gasPrice
}
// set gasLimit // set gasLimit
return await this.txProviderUtil.analyzeGasUsage(txMeta) return await this.txGasUtil.analyzeGasUsage(txMeta)
} }
async updateAndApproveTransaction (txMeta) { async updateAndApproveTransaction (txMeta) {
@ -197,7 +218,7 @@ module.exports = class TransactionController extends EventEmitter {
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 = new Transaction(txParams)
await this.signEthTx(ethTx, fromAddress) await this.signEthTx(ethTx, fromAddress)
this.txStateManager.setTxStatusSigned(txMeta.id) this.txStateManager.setTxStatusSigned(txMeta.id)
const rawTx = ethUtil.bufferToHex(ethTx.serialize()) const rawTx = ethUtil.bufferToHex(ethTx.serialize())
@ -208,7 +229,7 @@ module.exports = class TransactionController extends EventEmitter {
const txMeta = this.txStateManager.getTx(txId) const txMeta = this.txStateManager.getTx(txId)
txMeta.rawTx = rawTx txMeta.rawTx = rawTx
this.txStateManager.updateTx(txMeta) this.txStateManager.updateTx(txMeta)
const txHash = await this.txProviderUtil.publishTransaction(rawTx) const txHash = await this.query.sendRawTransaction(rawTx)
this.setTxHash(txId, txHash) this.setTxHash(txId, txHash)
this.txStateManager.setTxStatusSubmitted(txId) this.txStateManager.setTxStatusSubmitted(txId)
} }
@ -217,17 +238,6 @@ module.exports = class TransactionController extends EventEmitter {
this.txStateManager.setTxStatusRejected(txId) this.txStateManager.setTxStatusRejected(txId)
} }
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 // 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

View File

@ -33,7 +33,6 @@ module.exports = class TransactionStateManger extends ObservableStore {
}, {}) }, {})
} }
addTx (txMeta) { addTx (txMeta) {
this.once(`${txMeta.id}:signed`, function (txId) { this.once(`${txMeta.id}:signed`, function (txId) {
this.removeAllListeners(`${txMeta.id}:rejected`) this.removeAllListeners(`${txMeta.id}:rejected`)

View File

@ -5,7 +5,7 @@ const ObservableStore = require('obs-store')
const clone = require('clone') const clone = require('clone')
const sinon = require('sinon') const sinon = require('sinon')
const TransactionController = require('../../app/scripts/controllers/transactions') const TransactionController = require('../../app/scripts/controllers/transactions')
const TxProvideUtils = require('../../app/scripts/lib/tx-utils') const TxGasUtils = require('../../app/scripts/lib/tx-gas-utils')
const txStateHistoryHelper = require('../../app/scripts/lib/tx-state-history-helper') const txStateHistoryHelper = require('../../app/scripts/lib/tx-state-history-helper')
const noop = () => true const noop = () => true
@ -34,7 +34,7 @@ describe('Transaction Controller', function () {
}), }),
}) })
txController.nonceTracker.getNonceLock = () => Promise.resolve({ nextNonce: 0, releaseLock: noop }) txController.nonceTracker.getNonceLock = () => Promise.resolve({ nextNonce: 0, releaseLock: noop })
txController.txProviderUtils = new TxProvideUtils(txController.provider) txController.txProviderUtils = new TxGasUtils(txController.provider)
}) })
describe('#newUnapprovedTransaction', function () { describe('#newUnapprovedTransaction', function () {