fix(ElectrumX): get methods for balance, utxos, and tx history compelte

This commit is contained in:
Chris Troutner
2020-04-16 18:37:57 -07:00
parent d6925164e0
commit 7bb3741fa9
3 changed files with 125 additions and 6 deletions
+80 -2
View File
@@ -255,7 +255,7 @@ describe('#ElectrumX Router', () => {
})
it('should get balance for a single address', async () => {
const address = 'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf'
const address = 'bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7'
// Mock unit tests to prevent live network calls.
if (process.env.TEST === 'unit') {
@@ -304,7 +304,27 @@ describe('#ElectrumX Router', () => {
})
it('should get balance for a single address', async () => {
const address = 'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf'
const address = 'bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7'
// Mock unit tests to prevent live network calls.
if (process.env.TEST === 'unit') {
electrumxRoute.isReady = true // Force flag.
sandbox
.stub(electrumxRoute.electrumx, 'request')
.resolves(mockData.balance)
}
// Call the details API.
const result = await electrumxRoute._balanceFromElectrumx(address)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert.property(result, 'confirmed')
assert.property(result, 'unconfirmed')
})
it('should get balance for an address with no transaction history', async () => {
const address = 'bitcoincash:qp2ew6pvrs22jtsvtjyumjgas6jkvgn2hy3ad4wpw8'
// Mock unit tests to prevent live network calls.
if (process.env.TEST === 'unit') {
@@ -323,4 +343,62 @@ describe('#ElectrumX Router', () => {
assert.property(result, 'unconfirmed')
})
})
describe('#_transactionsFromElectrumx', () => {
it('should throw error for invalid address', async () => {
try {
// Address has invalid checksum.
const address = 'bitcoincash:qr69kyzha07dcecrsvjwsj4s6slnlq4r8c30lxnur2'
// Call the details API.
await electrumxRoute._transactionsFromElectrumx(address)
assert.equal(true, false, 'Unexpected code path')
} catch (err) {
// console.log('err2: ', err)
assert.include(err.message, 'Invalid checksum')
}
})
it('should get transaction history for a single address', async () => {
const address = 'bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7'
// Mock unit tests to prevent live network calls.
if (process.env.TEST === 'unit') {
electrumxRoute.isReady = true // Force flag.
sandbox
.stub(electrumxRoute.electrumx, 'request')
.resolves(mockData.txHistory)
}
// Call the details API.
const result = await electrumxRoute._transactionsFromElectrumx(address)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert.isArray(result)
assert.property(result[0], 'height')
assert.property(result[0], 'tx_hash')
})
it('should get history for an address with no transaction history', async () => {
const address = 'bitcoincash:qp2ew6pvrs22jtsvtjyumjgas6jkvgn2hy3ad4wpw8'
// Mock unit tests to prevent live network calls.
if (process.env.TEST === 'unit') {
electrumxRoute.isReady = true // Force flag.
sandbox
.stub(electrumxRoute.electrumx, 'request')
.resolves([])
}
// Call the details API.
const result = await electrumxRoute._transactionsFromElectrumx(address)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert.isArray(result)
assert.equal(result.length, 0)
})
})
})
+9 -1
View File
@@ -24,7 +24,15 @@ const balance = {
unconfirmed: 0
}
const txHistory = [
{
height: 601861,
tx_hash: '6181c669614fa18039a19b23eb06806bfece1f7514ab457c3bb82a40fe171a6d'
}
]
module.exports = {
utxos,
balance
balance,
txHistory
}