mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-21 16:52:04 -07:00
feat(V5): Created v5 Fulcrum POST balance endpoint in bch-api
This commit is contained in:
+2
-2
@@ -24,8 +24,8 @@
|
|||||||
"coverage": "nyc report --reporter=text-lcov | coveralls",
|
"coverage": "nyc report --reporter=text-lcov | coveralls",
|
||||||
"coverage:report": "export NETWORK=mainnet && nyc --reporter=html mocha --timeout 25000 test/v4/",
|
"coverage:report": "export NETWORK=mainnet && nyc --reporter=html mocha --timeout 25000 test/v4/",
|
||||||
"docs": "./node_modules/.bin/apidoc -i src/routes/v4 -o docs",
|
"docs": "./node_modules/.bin/apidoc -i src/routes/v4 -o docs",
|
||||||
"test:temp1": "export NETWORK=mainnet && export TEST=integration && mocha -g '#getBalance' --exit --timeout 30000 test/v5/",
|
"test:temp1": "export NETWORK=mainnet && export TEST=integration && mocha -g '#balanceBulk' --exit --timeout 30000 test/v5/",
|
||||||
"test:temp2": "export NETWORK=mainnet && mocha -g '#getBalance' --exit --timeout 30000 test/v5/"
|
"test:temp2": "export NETWORK=mainnet && mocha -g '#balanceBulk' --exit --timeout 30000 test/v5/"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=10.15.1"
|
"node": ">=10.15.1"
|
||||||
|
|||||||
+67
-77
@@ -51,7 +51,7 @@ class Electrum {
|
|||||||
_this.router = router
|
_this.router = router
|
||||||
_this.router.get('/', _this.root)
|
_this.router.get('/', _this.root)
|
||||||
_this.router.get('/balance/:address', _this.getBalance)
|
_this.router.get('/balance/:address', _this.getBalance)
|
||||||
// _this.router.post('/balance', _this.balanceBulk)
|
_this.router.post('/balance', _this.balanceBulk)
|
||||||
// _this.router.get('/utxos/:address', _this.getUtxos)
|
// _this.router.get('/utxos/:address', _this.getUtxos)
|
||||||
// _this.router.post('/utxos', _this.utxosBulk)
|
// _this.router.post('/utxos', _this.utxosBulk)
|
||||||
// _this.router.get('/tx/data/:txid', _this.getTransactionDetails)
|
// _this.router.get('/tx/data/:txid', _this.getTransactionDetails)
|
||||||
@@ -136,82 +136,72 @@ class Electrum {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
// POST handler for bulk queries on address balance
|
// POST handler for bulk queries on address balance
|
||||||
// async balanceBulk (req, res, next) {
|
async balanceBulk (req, res, next) {
|
||||||
// try {
|
try {
|
||||||
// let addresses = req.body.addresses
|
const addresses = req.body.addresses
|
||||||
//
|
|
||||||
// // Reject if addresses is not an array.
|
// Reject if addresses is not an array.
|
||||||
// if (!Array.isArray(addresses)) {
|
if (!Array.isArray(addresses)) {
|
||||||
// res.status(400)
|
res.status(400)
|
||||||
// return res.json({
|
return res.json({
|
||||||
// error: 'addresses needs to be an array. Use GET for single address.'
|
success: false,
|
||||||
// })
|
error: 'addresses needs to be an array. Use GET for single address.'
|
||||||
// }
|
})
|
||||||
//
|
}
|
||||||
// // Enforce array size rate limits
|
|
||||||
// if (!_this.routeUtils.validateArraySize(req, addresses)) {
|
// Enforce array size rate limits
|
||||||
// res.status(400) // https://github.com/Bitcoin-com/rest.bitcoin.com/issues/330
|
if (!_this.routeUtils.validateArraySize(req, addresses)) {
|
||||||
// return res.json({
|
res.status(400) // https://github.com/Bitcoin-com/rest.bitcoin.com/issues/330
|
||||||
// error: 'Array too large.'
|
return res.json({
|
||||||
// })
|
success: false,
|
||||||
// }
|
error: 'Array too large.'
|
||||||
//
|
})
|
||||||
// wlogger.debug(
|
}
|
||||||
// 'Executing electrumx.js/balanceBulk with these addresses: ',
|
|
||||||
// addresses
|
wlogger.debug(
|
||||||
// )
|
'Executing electrumx.js/balanceBulk with these addresses: ',
|
||||||
//
|
addresses
|
||||||
// // Validate each element in the address array.
|
)
|
||||||
// for (let i = 0; i < addresses.length; i++) {
|
|
||||||
// const thisAddress = addresses[i]
|
// Validate each element in the address array.
|
||||||
//
|
for (let i = 0; i < addresses.length; i++) {
|
||||||
// // Ensure the input is a valid BCH address.
|
const thisAddress = addresses[i]
|
||||||
// try {
|
|
||||||
// _this.bchjs.Address.toLegacyAddress(thisAddress)
|
// Ensure the input is a valid BCH address.
|
||||||
// } catch (err) {
|
try {
|
||||||
// res.status(400)
|
_this.bchjs.Address.toLegacyAddress(thisAddress)
|
||||||
// return res.json({
|
} catch (err) {
|
||||||
// error: `Invalid BCH address. Double check your address is valid: ${thisAddress}`
|
res.status(400)
|
||||||
// })
|
return res.json({
|
||||||
// }
|
success: false,
|
||||||
//
|
error: `Invalid BCH address. Double check your address is valid: ${thisAddress}`
|
||||||
// // Prevent a common user error. Ensure they are using the correct network address.
|
})
|
||||||
// const networkIsValid = _this.routeUtils.validateNetwork(thisAddress)
|
}
|
||||||
// if (!networkIsValid) {
|
|
||||||
// res.status(400)
|
// Prevent a common user error. Ensure they are using the correct network address.
|
||||||
// return res.json({
|
const networkIsValid = _this.routeUtils.validateNetwork(thisAddress)
|
||||||
// error: `Invalid network for address ${thisAddress}. Trying to use a testnet address on mainnet, or vice versa.`
|
if (!networkIsValid) {
|
||||||
// })
|
res.status(400)
|
||||||
// }
|
return res.json({
|
||||||
// }
|
success: false,
|
||||||
//
|
error: `Invalid network for address ${thisAddress}. Trying to use a testnet address on mainnet, or vice versa.`
|
||||||
// // Loops through each address and creates an array of Promises, querying
|
})
|
||||||
// // ElectrumX API in parallel.
|
}
|
||||||
// addresses = addresses.map(async (address, index) => {
|
}
|
||||||
// // console.log(`address: ${address}`)
|
|
||||||
// const balance = await _this._balanceFromElectrumx(address)
|
const response = await this.axios.post(
|
||||||
//
|
`${this.fulcrumApi}electrumx/balance/`,
|
||||||
// return {
|
{ addresses }
|
||||||
// balance,
|
)
|
||||||
// address
|
res.status(200)
|
||||||
// }
|
return res.json(response.data)
|
||||||
// })
|
} catch (err) {
|
||||||
//
|
// Write out error to error log.
|
||||||
// // Wait for all parallel Insight requests to return.
|
wlogger.error('Error in electrumx.js/balanceBulk().', err)
|
||||||
// const result = await Promise.all(addresses)
|
|
||||||
//
|
return _this.errorHandler(err, res)
|
||||||
// // Return the array of retrieved address information.
|
}
|
||||||
// res.status(200)
|
}
|
||||||
// return res.json({
|
|
||||||
// success: true,
|
|
||||||
// balances: result
|
|
||||||
// })
|
|
||||||
// } catch (err) {
|
|
||||||
// wlogger.error('Error in electrumx.js/balanceBulk().', err)
|
|
||||||
//
|
|
||||||
// return _this.errorHandler(err, res)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Returns a promise that resolves to UTXO data for an address. Expects input
|
// Returns a promise that resolves to UTXO data for an address. Expects input
|
||||||
// to be a cash address, and input validation to have already been done by
|
// to be a cash address, and input validation to have already been done by
|
||||||
|
|||||||
@@ -215,6 +215,161 @@ describe('#Electrumx', () => {
|
|||||||
assert.property(result.balance, 'unconfirmed')
|
assert.property(result.balance, 'unconfirmed')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
describe('#balanceBulk', () => {
|
||||||
|
it('should throw 400 if addresses is empty', async () => {
|
||||||
|
const result = await electrumxRoute.balanceBulk(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.balanceBulk(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 array provided is empty', async () => {
|
||||||
|
req.body.addresses = []
|
||||||
|
|
||||||
|
const result = await electrumxRoute.balanceBulk(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.balanceBulk(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.balanceBulk(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.balanceBulk(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.balanceBulk(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.balanceBulk(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 balance 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.balances })
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call the details API.
|
||||||
|
const result = await electrumxRoute.balanceBulk(req, res)
|
||||||
|
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||||
|
|
||||||
|
assert.property(result, 'success')
|
||||||
|
assert.equal(result.success, true)
|
||||||
|
|
||||||
|
assert.property(result, 'balances')
|
||||||
|
assert.property(result.balances[0], 'balance')
|
||||||
|
assert.property(result.balances[0], 'address')
|
||||||
|
|
||||||
|
assert.property(result.balances[0].balance, 'confirmed')
|
||||||
|
assert.property(result.balances[0].balance, 'unconfirmed')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
// describe('#_utxosFromElectrumx', () => {
|
// describe('#_utxosFromElectrumx', () => {
|
||||||
// it('should throw error for invalid address', async () => {
|
// it('should throw error for invalid address', async () => {
|
||||||
|
|||||||
@@ -26,7 +26,19 @@ const balance = {
|
|||||||
unconfirmed: 0
|
unconfirmed: 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const balances = {
|
||||||
|
success: true,
|
||||||
|
balances: [
|
||||||
|
{
|
||||||
|
balance: { confirmed: 7000, unconfirmed: 0 },
|
||||||
|
address: 'bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
balance: { confirmed: 7000, unconfirmed: 0 },
|
||||||
|
address: 'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
const txHistory = [
|
const txHistory = [
|
||||||
{
|
{
|
||||||
height: 601861,
|
height: 601861,
|
||||||
@@ -128,5 +140,6 @@ module.exports = {
|
|||||||
txHistory,
|
txHistory,
|
||||||
mempool,
|
mempool,
|
||||||
txDetails,
|
txDetails,
|
||||||
blockHeaders
|
blockHeaders,
|
||||||
|
balances
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user