1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Ensure tx has value before it's added (#8486)

Previously a transaction would get assigned a default value during the
`addTxGasDefaults` function, after the transaction was added and sent
to the UI.

Instead the transaction is assigned a default value before it gets
added. This flow is simpler to follow, and it avoids the race condition
where the transaction is assigned a value from the UI before this
default is set. In that situation, the UI-assigned value would be
overridden, which is obviously not desired.
This commit is contained in:
Mark Stacey 2020-04-30 21:50:44 -03:00 committed by GitHub
parent c2b588975c
commit 92592fc905
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 4 deletions

View File

@ -233,6 +233,12 @@ export default class TransactionController extends EventEmitter {
const { transactionCategory, getCodeResponse } = await this._determineTransactionCategory(txParams) const { transactionCategory, getCodeResponse } = await this._determineTransactionCategory(txParams)
txMeta.transactionCategory = transactionCategory txMeta.transactionCategory = transactionCategory
// ensure value
txMeta.txParams.value = txMeta.txParams.value
? ethUtil.addHexPrefix(txMeta.txParams.value)
: '0x0'
this.addTx(txMeta) this.addTx(txMeta)
this.emit('newUnapprovedTx', txMeta) this.emit('newUnapprovedTx', txMeta)
@ -262,9 +268,6 @@ export default class TransactionController extends EventEmitter {
async addTxGasDefaults (txMeta, getCodeResponse) { async addTxGasDefaults (txMeta, getCodeResponse) {
const txParams = txMeta.txParams const txParams = txMeta.txParams
// ensure value
txParams.value = txParams.value ? ethUtil.addHexPrefix(txParams.value) : '0x0'
txMeta.gasPriceSpecified = Boolean(txParams.gasPrice) txMeta.gasPriceSpecified = Boolean(txParams.gasPrice)
let gasPrice = txParams.gasPrice let gasPrice = txParams.gasPrice
if (!gasPrice) { if (!gasPrice) {

View File

@ -182,6 +182,7 @@ describe('Transaction Controller', function () {
assert.ok('metamaskNetworkId' in txMeta, 'should have a metamaskNetworkId') assert.ok('metamaskNetworkId' in txMeta, 'should have a metamaskNetworkId')
assert.ok('txParams' in txMeta, 'should have a txParams') assert.ok('txParams' in txMeta, 'should have a txParams')
assert.ok('history' in txMeta, 'should have a history') assert.ok('history' in txMeta, 'should have a history')
assert.equal(txMeta.txParams.value, '0x0', 'should have added 0x0 as the value')
const memTxMeta = txController.txStateManager.getTx(txMeta.id) const memTxMeta = txController.txStateManager.getTx(txMeta.id)
assert.deepEqual(txMeta, memTxMeta) assert.deepEqual(txMeta, memTxMeta)
@ -241,7 +242,6 @@ describe('Transaction Controller', function () {
providerResultStub.eth_estimateGas = '5209' providerResultStub.eth_estimateGas = '5209'
const txMetaWithDefaults = await txController.addTxGasDefaults(txMeta) const txMetaWithDefaults = await txController.addTxGasDefaults(txMeta)
assert.equal(txMetaWithDefaults.txParams.value, '0x0', 'should have added 0x0 as the value')
assert.ok(txMetaWithDefaults.txParams.gasPrice, 'should have added the gas price') assert.ok(txMetaWithDefaults.txParams.gasPrice, 'should have added the gas price')
assert.ok(txMetaWithDefaults.txParams.gas, 'should have added the gas field') assert.ok(txMetaWithDefaults.txParams.gas, 'should have added the gas field')
}) })