mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-21 16:52:04 -07:00
feat(txs): Created v5 Fulcrum /transactions endpoints in bch-api
This commit is contained in:
@@ -1102,6 +1102,251 @@ describe('#Electrumx', () => {
|
||||
assert.property(result.headers[0], 'headers')
|
||||
})
|
||||
})
|
||||
describe('#getTransactions', () => {
|
||||
it('should throw 400 if address is empty', async () => {
|
||||
const result = await electrumxRoute.getTransactions(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.equal(res.statusCode, 422, 'Expect 422 status code')
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'Unsupported address format')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should throw 400 on array input', async () => {
|
||||
req.params.address = ['qzs02v05l7qs5s24srqju498qu55dwuj0cx5ehjm2c']
|
||||
|
||||
const result = await electrumxRoute.getTransactions(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.equal(res.statusCode, 400, 'Expect 400 status code')
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'address can not be an array')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should throw an error for an invalid address', async () => {
|
||||
req.params.address = '02v05l7qs5s24srqju498qu55dwuj0cx5ehjm2c'
|
||||
|
||||
const result = await electrumxRoute.getTransactions(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.equal(res.statusCode, 422, 'Expect 422 status code')
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'Unsupported address format')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should detect a network mismatch', async () => {
|
||||
req.params.address = 'bchtest:qq89kjkeqz9mngp8kl3dpmu43y2wztdjqu500gn4c4'
|
||||
|
||||
const result = await electrumxRoute.getTransactions(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.equal(res.statusCode, 400, 'Expect 400 status code')
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'Invalid network', 'Proper error message')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should pass errors from electrum-cash to user', async () => {
|
||||
// Address has invalid checksum.
|
||||
req.params.address =
|
||||
'bitcoincash:qr69kyzha07dcecrsvjwsj4s6slnlq4r8c30lxnur2'
|
||||
|
||||
// Call the details API.
|
||||
const result = await electrumxRoute.getTransactions(req, res)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'Unsupported address format')
|
||||
})
|
||||
|
||||
it('should get transaction for a single address', async () => {
|
||||
req.params.address =
|
||||
'bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7'
|
||||
|
||||
// Mock unit tests to prevent live network calls.
|
||||
if (process.env.TEST === 'unit') {
|
||||
electrumxRoute.isReady = true // Force flag.
|
||||
|
||||
sandbox.stub(electrumxRoute.axios, 'get').resolves({
|
||||
data: { success: true, transactions: mockData.transactions }
|
||||
})
|
||||
}
|
||||
|
||||
// Call the details API.
|
||||
const result = await electrumxRoute.getTransactions(req, res)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, true)
|
||||
|
||||
assert.property(result, 'transactions')
|
||||
assert.isArray(result.transactions)
|
||||
|
||||
assert.property(result.transactions[0], 'height')
|
||||
assert.property(result.transactions[0], 'tx_hash')
|
||||
})
|
||||
})
|
||||
describe('#transactionsBulk', () => {
|
||||
it('should throw 400 if addresses is empty', async () => {
|
||||
const result = await electrumxRoute.transactionsBulk(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.equal(res.statusCode, 400, 'Expect 400 status code')
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'addresses needs to be an array')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should throw 400 if input provided is not array', async () => {
|
||||
req.body.addresses = 'qzs02v05l7qs5s24srqju498qu55dwuj0cx5ehjm2c'
|
||||
|
||||
const result = await electrumxRoute.transactionsBulk(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.equal(res.statusCode, 400, 'Expect 400 status code')
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'addresses needs to be an array')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should throw 400 error if addresses array is too large', async () => {
|
||||
const testArray = []
|
||||
for (var i = 0; i < 25; i++) testArray.push('')
|
||||
|
||||
req.body.addresses = testArray
|
||||
|
||||
const result = await electrumxRoute.transactionsBulk(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'Array too large')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should throw an error for an invalid address', async () => {
|
||||
req.body.addresses = ['02v05l7qs5s24srqju498qu55dwuj0cx5ehjm2c']
|
||||
|
||||
const result = await electrumxRoute.transactionsBulk(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.equal(res.statusCode, 400, 'Expect 400 status code')
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'Invalid BCH address')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should detect a network mismatch', async () => {
|
||||
req.body.addresses = [
|
||||
'bchtest:qq89kjkeqz9mngp8kl3dpmu43y2wztdjqu500gn4c4'
|
||||
]
|
||||
|
||||
const result = await electrumxRoute.transactionsBulk(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.equal(res.statusCode, 400, 'Expect 400 status code')
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'Invalid network', 'Proper error message')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should pass errors from electrum-cash to user', async () => {
|
||||
// Address has invalid checksum.
|
||||
req.body.addresses = [
|
||||
'bitcoincash:qr69kyzha07dcecrsvjwsj4s6slnlq4r8c30lxnur2'
|
||||
]
|
||||
|
||||
// Call the details API.
|
||||
const result = await electrumxRoute.transactionsBulk(req, res)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'Invalid BCH address')
|
||||
})
|
||||
it('should handle error', async () => {
|
||||
req.body.addresses = [
|
||||
'bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7',
|
||||
'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf'
|
||||
]
|
||||
// Force error
|
||||
sandbox.stub(electrumxRoute.axios, 'post').throws(new Error('Test error'))
|
||||
|
||||
// Call the details API.
|
||||
const result = await electrumxRoute.transactionsBulk(req, res)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'Test error')
|
||||
})
|
||||
it('should get transaction for an array of addresses', async () => {
|
||||
req.body.addresses = [
|
||||
'bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7',
|
||||
'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf'
|
||||
]
|
||||
|
||||
// Mock unit tests to prevent live network calls.
|
||||
if (process.env.TEST === 'unit') {
|
||||
electrumxRoute.isReady = true // Force flag.
|
||||
|
||||
sandbox
|
||||
.stub(electrumxRoute.axios, 'post')
|
||||
.resolves({ data: mockData.transactionsBulk })
|
||||
}
|
||||
|
||||
// Call the details API.
|
||||
const result = await electrumxRoute.transactionsBulk(req, res)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, true)
|
||||
|
||||
assert.property(result, 'transactions')
|
||||
assert.isArray(result.transactions)
|
||||
|
||||
assert.property(result.transactions[0], 'transactions')
|
||||
assert.isArray(result.transactions[0].transactions)
|
||||
|
||||
assert.property(result.transactions[0].transactions[0], 'height')
|
||||
assert.property(result.transactions[0].transactions[0], 'tx_hash')
|
||||
})
|
||||
})
|
||||
|
||||
// describe('#_utxosFromElectrumx', () => {
|
||||
// it('should throw error for invalid address', async () => {
|
||||
|
||||
@@ -180,6 +180,31 @@ const blockHeadersBulk = {
|
||||
success: true,
|
||||
headers: [{ headers: blockHeaders }, { headers: blockHeaders }]
|
||||
}
|
||||
|
||||
const transactions = [
|
||||
{
|
||||
height: 603416,
|
||||
tx_hash: 'eef683d236d88e978bd406419f144057af3fe1b62ef59162941c1a9f05ded62c'
|
||||
},
|
||||
{
|
||||
height: 646894,
|
||||
tx_hash: '4c695fae636f3e8e2edc571d11756b880ccaae744390f3950d798ce7b5e25754'
|
||||
}
|
||||
]
|
||||
|
||||
const transactionsBulk = {
|
||||
success: true,
|
||||
transactions: [
|
||||
{
|
||||
transactions,
|
||||
address: 'bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7'
|
||||
},
|
||||
{
|
||||
transactions,
|
||||
address: 'bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7'
|
||||
}
|
||||
]
|
||||
}
|
||||
module.exports = {
|
||||
utxos,
|
||||
utxosArray,
|
||||
@@ -190,5 +215,7 @@ module.exports = {
|
||||
txDetailsBulk,
|
||||
blockHeaders,
|
||||
blockHeadersBulk,
|
||||
balances
|
||||
balances,
|
||||
transactions,
|
||||
transactionsBulk
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user