mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-21 16:52:04 -07:00
Added unit and integration tests for balance endpoints with blockbook
This commit is contained in:
@@ -24,6 +24,9 @@ const BITBOX = new BITBOXJS()
|
||||
// Connect the route endpoints to their handler functions.
|
||||
router.get("/", root)
|
||||
router.get("/balance/:address", balanceSingle)
|
||||
router.post("/balance", balanceBulk)
|
||||
router.get("/utxos/:address", utxosSingle)
|
||||
router.post("/utxos", utxosBulk)
|
||||
|
||||
// Root API endpoint. Simply acknowledges that it exists.
|
||||
function root(req, res, next) {
|
||||
|
||||
+18
-17
@@ -19,11 +19,14 @@ util.inspect.defaultOptions = { depth: 1 }
|
||||
const BITBOXJS = require("@chris.troutner/bitbox-js")
|
||||
const BITBOX = new BITBOXJS()
|
||||
|
||||
//const BITCORE_URL = process.env.BITCORE_URL
|
||||
//const BLOCKBOOK_URL = process.env.BLOCKBOOK_URL
|
||||
|
||||
// Connect the route endpoints to their handler functions.
|
||||
router.get("/", root)
|
||||
router.get("/balance/:address", balanceSingle)
|
||||
router.post("/balance", balanceBulk)
|
||||
//router.get("/utxos/:address", utxosSingle)
|
||||
//router.post("/utxos", utxosBulk)
|
||||
|
||||
// Root API endpoint. Simply acknowledges that it exists.
|
||||
function root(req, res, next) {
|
||||
@@ -34,7 +37,7 @@ function root(req, res, next) {
|
||||
// Returns a Promise.
|
||||
async function balanceFromBlockbook(thisAddress) {
|
||||
try {
|
||||
//console.log(`BITCORE_URL: ${BITCORE_URL}`)
|
||||
//console.log(`BLOCKBOOK_URL: ${BLOCKBOOK_URL}`)
|
||||
|
||||
// Convert the address to a cashaddr without a prefix.
|
||||
const addr = BITBOX.Address.toCashAddress(thisAddress)
|
||||
@@ -107,8 +110,6 @@ async function balanceSingle(req, res, next) {
|
||||
res.status(200)
|
||||
return res.json(retData)
|
||||
} catch (err) {
|
||||
console.log(`err: ${JSON.stringify(err, null, 2)}`)
|
||||
|
||||
// Attempt to decode the error message.
|
||||
const { msg, status } = routeUtils.decodeError(err)
|
||||
if (msg) {
|
||||
@@ -123,7 +124,7 @@ async function balanceSingle(req, res, next) {
|
||||
return res.json({ error: util.inspect(err) })
|
||||
}
|
||||
}
|
||||
/*
|
||||
|
||||
// POST handler for bulk queries on address details
|
||||
async function balanceBulk(req, res, next) {
|
||||
try {
|
||||
@@ -147,7 +148,7 @@ async function balanceBulk(req, res, next) {
|
||||
}
|
||||
|
||||
wlogger.debug(
|
||||
`Executing bitcore.js/balanceBulk with these addresses: `,
|
||||
`Executing blockbook.js/balanceBulk with these addresses: `,
|
||||
addresses
|
||||
)
|
||||
|
||||
@@ -178,7 +179,7 @@ async function balanceBulk(req, res, next) {
|
||||
// Loops through each address and creates an array of Promises, querying
|
||||
// Insight API in parallel.
|
||||
addresses = addresses.map(async (address, index) =>
|
||||
balanceFromBitcore(address)
|
||||
balanceFromBlockbook(address)
|
||||
)
|
||||
|
||||
// Wait for all parallel Insight requests to return.
|
||||
@@ -195,18 +196,18 @@ async function balanceBulk(req, res, next) {
|
||||
return res.json({ error: msg })
|
||||
}
|
||||
|
||||
wlogger.error(`Error in bitcore.js/balanceBulk().`, err)
|
||||
wlogger.error(`Error in blockbook.js/balanceBulk().`, err)
|
||||
|
||||
res.status(500)
|
||||
return res.json({ error: util.inspect(err) })
|
||||
}
|
||||
}
|
||||
|
||||
// Query the Bitcore Node API for utxos associated with a BCH address.
|
||||
/*
|
||||
// Query the Blockbook API for utxos associated with a BCH address.
|
||||
// Returns a Promise.
|
||||
async function utxosFromBitcore(thisAddress) {
|
||||
async function utxosFromBlockbook(thisAddress) {
|
||||
try {
|
||||
//console.log(`BITCORE_URL: ${BITCORE_URL}`)
|
||||
//console.log(`BLOCKBOOK_URL: ${BLOCKBOOK_URL}`)
|
||||
|
||||
// Convert the address to a cashaddr without a prefix.
|
||||
const addr = BITBOX.Address.toCashAddress(thisAddress, false)
|
||||
@@ -215,9 +216,9 @@ async function utxosFromBitcore(thisAddress) {
|
||||
let network = "mainnet"
|
||||
if (process.env.NETWORK === "testnet") network = "testnet"
|
||||
|
||||
const path = `${process.env.BITCORE_URL}api/BCH/${network}/address/${addr}/?unspent=true`
|
||||
const path = `${process.env.BLOCKBOOK_URL}api/BCH/${network}/address/${addr}/?unspent=true`
|
||||
|
||||
// Query the Bitcore Node API.
|
||||
// Query the Blockbook API.
|
||||
const axiosResponse = await axios.get(path)
|
||||
const retData = axiosResponse.data
|
||||
//console.log(`retData: ${util.inspect(retData)}`)
|
||||
@@ -249,7 +250,7 @@ async function utxosSingle(req, res, next) {
|
||||
}
|
||||
|
||||
wlogger.debug(
|
||||
`Executing bitcore/balanceSingle with this address: `,
|
||||
`Executing blockbook/balanceSingle with this address: `,
|
||||
address
|
||||
)
|
||||
|
||||
@@ -376,8 +377,8 @@ module.exports = {
|
||||
router,
|
||||
testableComponents: {
|
||||
root,
|
||||
balanceSingle
|
||||
//balanceBulk,
|
||||
balanceSingle,
|
||||
balanceBulk
|
||||
//utxosSingle,
|
||||
//utxosBulk
|
||||
}
|
||||
|
||||
+24
-14
@@ -175,7 +175,7 @@ describe("#Blockbook Router", () => {
|
||||
assert.isArray(result.txids)
|
||||
})
|
||||
})
|
||||
/*
|
||||
|
||||
describe("#Balance Bulk", () => {
|
||||
// details route handler.
|
||||
const balanceBulk = blockbookRoute.testableComponents.balanceBulk
|
||||
@@ -250,7 +250,7 @@ describe("#Blockbook Router", () => {
|
||||
})
|
||||
|
||||
it("should throw 500 when network issues", async () => {
|
||||
const savedUrl = process.env.BITCOINCOM_BASEURL
|
||||
const savedUrl = process.env.BLOCKBOOK_URL
|
||||
|
||||
try {
|
||||
req.body = {
|
||||
@@ -258,13 +258,13 @@ describe("#Blockbook Router", () => {
|
||||
}
|
||||
|
||||
// Switch the Insight URL to something that will error out.
|
||||
process.env.BITCOINCOM_BASEURL = "http://fakeurl/api/"
|
||||
process.env.BLOCKBOOK_URL = "http://fakeurl/api/"
|
||||
|
||||
const result = await balanceBulk(req, res)
|
||||
//console.log(`network issue result: ${util.inspect(result)}`)
|
||||
|
||||
// Restore the saved URL.
|
||||
process.env.BITCOINCOM_BASEURL = savedUrl
|
||||
process.env.BLOCKBOOK_URL = savedUrl
|
||||
|
||||
assert.isAbove(res.statusCode, 499, "HTTP status code 500 expected.")
|
||||
//assert.include(result.error, "ENOTFOUND", "Error message expected")
|
||||
@@ -275,7 +275,7 @@ describe("#Blockbook Router", () => {
|
||||
)
|
||||
} catch (err) {
|
||||
// Restore the saved URL.
|
||||
process.env.BITCOINCOM_BASEURL = savedUrl
|
||||
process.env.BLOCKBOOK_URL = savedUrl
|
||||
}
|
||||
})
|
||||
|
||||
@@ -296,10 +296,20 @@ describe("#Blockbook Router", () => {
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.isArray(result)
|
||||
assert.hasAllKeys(result[0], ["confirmed", "unconfirmed", "balance"])
|
||||
assert.isNumber(result[0].confirmed)
|
||||
assert.isNumber(result[0].unconfirmed)
|
||||
assert.isNumber(result[0].balance)
|
||||
assert.hasAnyKeys(result[0], [
|
||||
"page",
|
||||
"totalPages",
|
||||
"itemsOnPage",
|
||||
"address",
|
||||
"balance",
|
||||
"totalReceived",
|
||||
"totalSent",
|
||||
"unconfirmedBalance",
|
||||
"unconfirmedTxs",
|
||||
"txs",
|
||||
"txids"
|
||||
])
|
||||
assert.isArray(result[0].txids)
|
||||
})
|
||||
|
||||
it("should get details for multiple addresses", async () => {
|
||||
@@ -326,7 +336,7 @@ describe("#Blockbook Router", () => {
|
||||
assert.equal(result.length, 2, "2 outputs for 2 inputs")
|
||||
})
|
||||
})
|
||||
|
||||
/*
|
||||
describe("#UTXOs Single", () => {
|
||||
// details route handler.
|
||||
const utxosSingle = blockbookRoute.testableComponents.utxosSingle
|
||||
@@ -503,7 +513,7 @@ describe("#Blockbook Router", () => {
|
||||
})
|
||||
|
||||
it("should throw 500 when network issues", async () => {
|
||||
const savedUrl = process.env.BITCOINCOM_BASEURL
|
||||
const savedUrl = process.env.BLOCKBOOK_URL
|
||||
|
||||
try {
|
||||
req.body = {
|
||||
@@ -511,13 +521,13 @@ describe("#Blockbook Router", () => {
|
||||
}
|
||||
|
||||
// Switch the Insight URL to something that will error out.
|
||||
process.env.BITCOINCOM_BASEURL = "http://fakeurl/api/"
|
||||
process.env.BLOCKBOOK_URL = "http://fakeurl/api/"
|
||||
|
||||
const result = await utxosBulk(req, res)
|
||||
//console.log(`network issue result: ${util.inspect(result)}`)
|
||||
|
||||
// Restore the saved URL.
|
||||
process.env.BITCOINCOM_BASEURL = savedUrl
|
||||
process.env.BLOCKBOOK_URL = savedUrl
|
||||
|
||||
assert.isAbove(res.statusCode, 499, "HTTP status code 500 expected.")
|
||||
//assert.include(result.error, "ENOTFOUND", "Error message expected")
|
||||
@@ -528,7 +538,7 @@ describe("#Blockbook Router", () => {
|
||||
)
|
||||
} catch (err) {
|
||||
// Restore the saved URL.
|
||||
process.env.BITCOINCOM_BASEURL = savedUrl
|
||||
process.env.BLOCKBOOK_URL = savedUrl
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user