mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
transactions - add a nonce check utility for ui use
This commit is contained in:
parent
62bf76db53
commit
a45cb75435
@ -111,6 +111,21 @@ class TransactionController extends EventEmitter {
|
|||||||
this.txStateManager.wipeTransactions(address)
|
this.txStateManager.wipeTransactions(address)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Check if a txMeta in the list with the same nonce has been confirmed in a block
|
||||||
|
if the txParams dont have a nonce will return false
|
||||||
|
@returns {boolean} weather the nonce has been used in a transaction confirmed in a block
|
||||||
|
@param {object} txMeta - the txMeta object
|
||||||
|
*/
|
||||||
|
async isNonceTaken (txMeta) {
|
||||||
|
const { from, nonce } = txMeta.txParams
|
||||||
|
if ('nonce' in txMeta.txParams) {
|
||||||
|
const sameNonceTxList = this.txStateManager.getFilteredTxList({from, nonce, status: 'confirmed'})
|
||||||
|
return (sameNonceTxList.length >= 1)
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
add a new unapproved transaction to the pipeline
|
add a new unapproved transaction to the pipeline
|
||||||
|
|
||||||
|
@ -382,6 +382,7 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
updateTransaction: nodeify(txController.updateTransaction, txController),
|
updateTransaction: nodeify(txController.updateTransaction, txController),
|
||||||
updateAndApproveTransaction: nodeify(txController.updateAndApproveTransaction, txController),
|
updateAndApproveTransaction: nodeify(txController.updateAndApproveTransaction, txController),
|
||||||
retryTransaction: nodeify(this.retryTransaction, this),
|
retryTransaction: nodeify(this.retryTransaction, this),
|
||||||
|
isNonceTaken: nodeify(txController.isNonceTaken, txController),
|
||||||
|
|
||||||
// messageManager
|
// messageManager
|
||||||
signMessage: nodeify(this.signMessage, this),
|
signMessage: nodeify(this.signMessage, this),
|
||||||
|
@ -40,6 +40,36 @@ describe('Transaction Controller', function () {
|
|||||||
txController.nonceTracker.getNonceLock = () => Promise.resolve({ nextNonce: 0, releaseLock: noop })
|
txController.nonceTracker.getNonceLock = () => Promise.resolve({ nextNonce: 0, releaseLock: noop })
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('#isNonceTaken', function () {
|
||||||
|
it('should return true', function (done) {
|
||||||
|
txController.txStateManager._saveTxList([
|
||||||
|
{ id: 1, status: 'submitted', metamaskNetworkId: currentNetworkId, txParams: {nonce: 0, from: '0x8ACCE2391C0d510a6C5E5D8f819A678F79B7E675'} },
|
||||||
|
{ id: 2, status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {nonce: 0, from: '0x8ACCE2391C0d510a6C5E5D8f819A678F79B7E675'} },
|
||||||
|
{ id: 3, status: 'submitted', metamaskNetworkId: currentNetworkId, txParams: {nonce: 0, from: '0x8ACCE2391C0d510a6C5E5D8f819A678F79B7E675'} },
|
||||||
|
])
|
||||||
|
txController.isNonceTaken({txParams: {nonce:0, from:'0x8ACCE2391C0d510a6C5E5D8f819A678F79B7E675'}})
|
||||||
|
.then((isNonceTaken) => {
|
||||||
|
assert(isNonceTaken)
|
||||||
|
done()
|
||||||
|
}).catch(done)
|
||||||
|
|
||||||
|
})
|
||||||
|
it('should return false', function (done) {
|
||||||
|
txController.txStateManager._saveTxList([
|
||||||
|
{ id: 1, status: 'submitted', metamaskNetworkId: currentNetworkId, txParams: {nonce: 0, from: '0x8ACCE2391C0d510a6C5E5D8f819A678F79B7E675'} },
|
||||||
|
{ id: 2, status: 'submitted', metamaskNetworkId: currentNetworkId, txParams: {nonce: 0, from: '0x8ACCE2391C0d510a6C5E5D8f819A678F79B7E675'} },
|
||||||
|
{ id: 3, status: 'submitted', metamaskNetworkId: currentNetworkId, txParams: {nonce: 0, from: '0x8ACCE2391C0d510a6C5E5D8f819A678F79B7E675'} },
|
||||||
|
])
|
||||||
|
|
||||||
|
txController.isNonceTaken({txParams: {nonce:0, from:'0x8ACCE2391C0d510a6C5E5D8f819A678F79B7E675'}})
|
||||||
|
.then((isNonceTaken) => {
|
||||||
|
assert(!isNonceTaken)
|
||||||
|
done()
|
||||||
|
}).catch(done)
|
||||||
|
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('#getState', function () {
|
describe('#getState', function () {
|
||||||
it('should return a state object with the right keys and datat types', function () {
|
it('should return a state object with the right keys and datat types', function () {
|
||||||
const exposedState = txController.getState()
|
const exposedState = txController.getState()
|
||||||
|
Loading…
Reference in New Issue
Block a user