Add extra tx methods to configManager

This commit is contained in:
Dan Finlay 2016-04-19 17:32:09 -07:00
parent 0a0a631af2
commit 901d23a029
2 changed files with 61 additions and 3 deletions

View File

@ -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')

View File

@ -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')
})
})
})
})