From 901d23a02904a859901ca4f0bf5a6e42684567ec Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Tue, 19 Apr 2016 17:32:09 -0700 Subject: [PATCH] Add extra tx methods to configManager --- app/scripts/lib/config-manager.js | 28 +++++++++++++++++++++--- test/unit/config-manager-test.js | 36 +++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/app/scripts/lib/config-manager.js b/app/scripts/lib/config-manager.js index 356d53c22..bcf251496 100644 --- a/app/scripts/lib/config-manager.js +++ b/app/scripts/lib/config-manager.js @@ -161,6 +161,16 @@ ConfigManager.prototype.getTx = function(txId) { return matching.length > 0 ? matching[0] : null } +ConfigManager.prototype.getTxWithParams = function(params) { + var transactions = this.getTxList() + var matching = transactions.filter((tx) => { + return Object.keys(tx.txParams).reduce((result, key) => { + return tx.params[key] === params[key] && result + }, true) + }) + return matching.length > 0 ? matching[0] : null +} + ConfigManager.prototype.confirmTx = function(txId) { this._setTxStatus(txId, 'confirmed') } @@ -170,14 +180,26 @@ ConfigManager.prototype.rejectTx = function(txId) { } ConfigManager.prototype._setTxStatus = function(txId, status) { + var tx = this.getTx(txId) + tx.status = status + this.updateTx(tx) +} + +ConfigManager.prototype.updateTx = function(tx) { var transactions = this.getTxList() - transactions.forEach((tx) => { - if (tx.id === txId) { - tx.status = status + var found, index + transactions.forEach((otherTx, i) => { + if (otherTx.id === tx.id) { + found = true + index = i } }) + if (found) { + transactions[index] = tx + } this._saveTxList(transactions) } + ConfigManager.prototype.unconfirmedTxs = function() { var transactions = this.getTxList() return transactions.filter(tx => tx.status === 'unconfirmed') diff --git a/test/unit/config-manager-test.js b/test/unit/config-manager-test.js index 84632b0ea..a5ddf78a4 100644 --- a/test/unit/config-manager-test.js +++ b/test/unit/config-manager-test.js @@ -126,6 +126,16 @@ describe('config-manager', function() { }) }) + describe('#updateTx', function() { + it('replaces the tx with the same id', function() { + configManager.addTx({ id: '1', status: 'unconfirmed' }) + configManager.addTx({ id: '2', status: 'confirmed' }) + configManager.updateTx({ id: '1', status: 'blah', hash: 'foo' }) + var result = configManager.getTx('1') + assert.equal(result.hash, 'foo') + }) + }) + describe('#unconfirmedTxs', function() { it('returns unconfirmed txs in a hash', function() { configManager.addTx({ id: '1', status: 'unconfirmed' }) @@ -146,5 +156,31 @@ describe('config-manager', function() { assert.equal(configManager.getTx('2').status, 'confirmed') }) }) + + describe('#getTxWithParams', function() { + it('returns a tx with the matching params', function() { + configManager.addTx({ id: '1', status: 'unconfirmed', txParams: { + from: 'from', + to: 'to', + data: 'data', + value: 'value', + } + }) + configManager.addTx({ id: '2', status: 'unconfirmed', txParams: { + from: 'from1', + to: 'to', + data: 'data', + value: 'value', + } + }) + var result = configManager.getTxWithParams({ + from: 'from', + to: 'to', + data: 'data', + value: 'value', + }) + assert.equal(result.id, '1') + }) + }) }) })