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

Adds createSpeedUpTransaction to txController

This commit is contained in:
Dan Miller 2018-10-26 02:12:59 -02:30
parent 3162a2747c
commit 9b9a2cc2e0
3 changed files with 97 additions and 0 deletions

View File

@ -290,6 +290,29 @@ class TransactionController extends EventEmitter {
return newTxMeta
}
async createSpeedUpTransaction (originalTxId, customGasPrice) {
const originalTxMeta = this.txStateManager.getTx(originalTxId)
const { txParams } = originalTxMeta
const { gasPrice: lastGasPrice } = txParams
const newGasPrice = customGasPrice || bnToHex(BnMultiplyByFraction(hexToBn(lastGasPrice), 11, 10))
const newTxMeta = this.txStateManager.generateTxMeta({
txParams: {
...txParams,
gasPrice: newGasPrice,
},
lastGasPrice,
loadingDefaults: false,
status: TRANSACTION_STATUS_APPROVED,
type: TRANSACTION_TYPE_RETRY,
})
this.addTx(newTxMeta)
await this.approveTransaction(newTxMeta.id)
return newTxMeta
}
/**
updates the txMeta in the txStateManager
@param txMeta {Object} - the updated txMeta

View File

@ -445,6 +445,7 @@ module.exports = class MetamaskController extends EventEmitter {
updateAndApproveTransaction: nodeify(txController.updateAndApproveTransaction, txController),
retryTransaction: nodeify(this.retryTransaction, this),
createCancelTransaction: nodeify(this.createCancelTransaction, this),
createSpeedUpTransaction: nodeify(this.createSpeedUpTransaction, this),
getFilteredTxList: nodeify(txController.getFilteredTxList, txController),
isNonceTaken: nodeify(txController.isNonceTaken, txController),
estimateGas: nodeify(this.estimateGas, this),
@ -1162,6 +1163,12 @@ module.exports = class MetamaskController extends EventEmitter {
return state
}
async createSpeedUpTransaction (originalTxId, customGasPrice, cb) {
await this.txController.createSpeedUpTransaction(originalTxId, customGasPrice)
const state = await this.getState()
return state
}
estimateGas (estimateGasParams) {
return new Promise((resolve, reject) => {
return this.txController.txGasUtil.query.estimateGas(estimateGasParams, (err, res) => {

View File

@ -5,6 +5,9 @@ const EthTx = require('ethereumjs-tx')
const ObservableStore = require('obs-store')
const sinon = require('sinon')
const TransactionController = require('../../../../../app/scripts/controllers/transactions')
const {
TRANSACTION_TYPE_RETRY,
} = require('../../../../../app/scripts/controllers/transactions/enums')
const { createTestProviderTools, getTestAccounts } = require('../../../../stub/provider')
const noop = () => true
@ -392,6 +395,70 @@ describe('Transaction Controller', function () {
})
describe('#createSpeedUpTransaction', () => {
let addTxSpy
let approveTransactionSpy
let txParams
let expectedTxParams
beforeEach(() => {
addTxSpy = sinon.spy(txController, 'addTx')
approveTransactionSpy = sinon.spy(txController, 'approveTransaction')
txParams = {
nonce: '0x00',
from: '0xB09d8505E1F4EF1CeA089D47094f5DD3464083d4',
to: '0xB09d8505E1F4EF1CeA089D47094f5DD3464083d4',
gas: '0x5209',
gasPrice: '0xa',
}
txController.txStateManager._saveTxList([
{ id: 1, status: 'submitted', metamaskNetworkId: currentNetworkId, txParams, history: [] },
])
expectedTxParams = Object.assign({}, txParams, { gasPrice: '0xb'})
})
afterEach(() => {
addTxSpy.restore()
approveTransactionSpy.restore()
})
it('should call this.addTx and this.approveTransaction with the expected args', async () => {
await txController.createSpeedUpTransaction(1)
assert.equal(addTxSpy.callCount, 1)
const addTxArgs = addTxSpy.getCall(0).args[0]
assert.deepEqual(addTxArgs.txParams, expectedTxParams)
const { lastGasPrice, type } = addTxArgs
assert.deepEqual({ lastGasPrice, type }, {
lastGasPrice: '0xa',
type: TRANSACTION_TYPE_RETRY,
})
})
it('should call this.approveTransaction with the id of the returned tx', async () => {
const result = await txController.createSpeedUpTransaction(1)
assert.equal(approveTransactionSpy.callCount, 1)
const approveTransactionArg = approveTransactionSpy.getCall(0).args[0]
assert.equal(result.id, approveTransactionArg)
})
it('should return the expected txMeta', async () => {
const result = await txController.createSpeedUpTransaction(1)
assert.deepEqual(result.txParams, expectedTxParams)
const { lastGasPrice, type } = result
assert.deepEqual({ lastGasPrice, type }, {
lastGasPrice: '0xa',
type: TRANSACTION_TYPE_RETRY,
})
})
})
describe('#publishTransaction', function () {
let hash, txMeta
beforeEach(function () {