mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-22 09:12:05 -07:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7cafb11c0d | ||
|
|
dce665b174 | ||
|
|
43c1ee9b69 | ||
|
|
4b95fa127c | ||
|
|
65314439d7 | ||
|
|
36a84726af |
Generated
+765
-19870
File diff suppressed because it is too large
Load Diff
+1
-1
@@ -29,7 +29,7 @@
|
|||||||
"node": ">=10.15.1"
|
"node": ">=10.15.1"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@psf/bch-js": "^4.15.19",
|
"@psf/bch-js": "^4.15.21",
|
||||||
"apidoc": "^0.26.0",
|
"apidoc": "^0.26.0",
|
||||||
"axios": "^0.21.1",
|
"axios": "^0.21.1",
|
||||||
"bitcore-lib-cash": "^8.23.1",
|
"bitcore-lib-cash": "^8.23.1",
|
||||||
|
|||||||
@@ -27,11 +27,15 @@ class Price {
|
|||||||
this.coinexPriceUrl =
|
this.coinexPriceUrl =
|
||||||
'https://api.coinex.com/v1/market/ticker?market=bchausdt'
|
'https://api.coinex.com/v1/market/ticker?market=bchausdt'
|
||||||
|
|
||||||
|
this.bchCoinexPriceUrl =
|
||||||
|
'https://api.coinex.com/v1/market/ticker?market=bchusdt'
|
||||||
|
|
||||||
this.router = express.Router()
|
this.router = express.Router()
|
||||||
this.router.get('/', _this.root)
|
this.router.get('/', _this.root)
|
||||||
this.router.get('/usd', _this.getUSD)
|
this.router.get('/usd', _this.getUSD)
|
||||||
this.router.get('/rates', _this.getBCHRate)
|
this.router.get('/rates', _this.getBCHRate)
|
||||||
this.router.get('/bchausd', _this.getBCHAUSD)
|
this.router.get('/bchausd', _this.getBCHAUSD)
|
||||||
|
this.router.get('/bchusd', _this.getBCHUSD)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DRY error handler.
|
// DRY error handler.
|
||||||
@@ -150,6 +154,40 @@ class Price {
|
|||||||
return _this.errorHandler(err, res)
|
return _this.errorHandler(err, res)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @api {get} /price/bchusd Get the USD price of BCH
|
||||||
|
* @apiName Get the USD price of BCH
|
||||||
|
* @apiGroup Price
|
||||||
|
* @apiDescription Get the USD price of BCH from Coinex.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @apiExample Example usage:
|
||||||
|
* curl -X GET "https://api.fullstack.cash/v4/price/bchusd" -H "accept: application/json"
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
async getBCHUSD (req, res, next) {
|
||||||
|
try {
|
||||||
|
// Request options
|
||||||
|
const opt = {
|
||||||
|
method: 'get',
|
||||||
|
baseURL: _this.bchCoinexPriceUrl,
|
||||||
|
timeout: 15000
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await axios.request(opt)
|
||||||
|
// console.log(`response.data: ${JSON.stringify(response.data, null, 2)}`)
|
||||||
|
|
||||||
|
const price = Number(response.data.data.ticker.last)
|
||||||
|
|
||||||
|
return res.json({ usd: price })
|
||||||
|
} catch (err) {
|
||||||
|
// Write out error to error log.
|
||||||
|
wlogger.error('Error in price.js/getBCHUSD().', err)
|
||||||
|
|
||||||
|
return _this.errorHandler(err, res)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Price
|
module.exports = Price
|
||||||
|
|||||||
@@ -1979,6 +1979,11 @@ class Slp {
|
|||||||
try {
|
try {
|
||||||
const utxos = req.body.utxos
|
const utxos = req.body.utxos
|
||||||
|
|
||||||
|
// Extract a delay value if the user passed it in.
|
||||||
|
const usrObjIn = req.body.usrObj
|
||||||
|
let utxoDelay = 0
|
||||||
|
if (usrObjIn && usrObjIn.utxoDelay) { utxoDelay = usrObjIn.utxoDelay }
|
||||||
|
|
||||||
// console.log('req: ', req)
|
// console.log('req: ', req)
|
||||||
// console.log(`req._remoteAddress: ${req._remoteAddress}`)
|
// console.log(`req._remoteAddress: ${req._remoteAddress}`)
|
||||||
|
|
||||||
@@ -1988,7 +1993,8 @@ class Slp {
|
|||||||
ip: req._remoteAddress,
|
ip: req._remoteAddress,
|
||||||
jwtToken: req.locals.jwtToken,
|
jwtToken: req.locals.jwtToken,
|
||||||
proLimit: req.locals.proLimit,
|
proLimit: req.locals.proLimit,
|
||||||
apiLevel: req.locals.apiLevel
|
apiLevel: req.locals.apiLevel,
|
||||||
|
utxoDelay
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate inputs
|
// Validate inputs
|
||||||
@@ -2049,7 +2055,8 @@ class Slp {
|
|||||||
|
|
||||||
res.status(500)
|
res.status(500)
|
||||||
return res.json({
|
return res.json({
|
||||||
error: 'Error in hydrateUtxos()'
|
error: 'Undetermined error in hydrateUtxos()',
|
||||||
|
message: err.message
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,14 @@ describe('#price', () => {
|
|||||||
const result = await price.getBCHAUSD(req, res)
|
const result = await price.getBCHAUSD(req, res)
|
||||||
console.log(`result: ${util.inspect(result)}`)
|
console.log(`result: ${util.inspect(result)}`)
|
||||||
|
|
||||||
|
assert.isNumber(result.usd)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
describe('#getBCHUSD', () => {
|
||||||
|
it('should get the USD price of BCH', async () => {
|
||||||
|
const result = await price.getBCHUSD(req, res)
|
||||||
|
console.log(`result: ${util.inspect(result)}`)
|
||||||
|
|
||||||
assert.isNumber(result.usd)
|
assert.isNumber(result.usd)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -277,4 +277,70 @@ describe('#PriceRouter', () => {
|
|||||||
assert.isNumber(result.usd)
|
assert.isNumber(result.usd)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('#getBCHUSD', () => {
|
||||||
|
// const getNetworkInfo = controlRoute.testableComponents.getNetworkInfo
|
||||||
|
|
||||||
|
it('should throw 500 when network issues', async () => {
|
||||||
|
uut.bchCoinexPriceUrl = 'http://fakeurl/api/'
|
||||||
|
|
||||||
|
await uut.getBCHUSD(req, res)
|
||||||
|
// console.log(`result: ${util.inspect(result)}`)
|
||||||
|
|
||||||
|
assert.isAbove(
|
||||||
|
res.statusCode,
|
||||||
|
499,
|
||||||
|
'HTTP status code 500 or greater expected.'
|
||||||
|
)
|
||||||
|
// console.log(res)
|
||||||
|
assert.include(
|
||||||
|
res.output.error,
|
||||||
|
'Network error: Could not communicate with full node or other external service'
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns proper error when downstream service stalls', async () => {
|
||||||
|
// Mock the timeout error.
|
||||||
|
sandbox.stub(uut.axios, 'request').throws({ code: 'ECONNABORTED' })
|
||||||
|
|
||||||
|
const result = await uut.getBCHUSD(req, res)
|
||||||
|
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||||
|
|
||||||
|
assert.isAbove(res.statusCode, 499, 'HTTP status code 503 expected.')
|
||||||
|
assert.include(
|
||||||
|
result.error,
|
||||||
|
'Could not communicate with full node',
|
||||||
|
'Error message expected'
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns proper error when downstream service is down', async () => {
|
||||||
|
// Mock the timeout error.
|
||||||
|
sandbox.stub(uut.axios, 'request').throws({ code: 'ECONNREFUSED' })
|
||||||
|
|
||||||
|
const result = await uut.getBCHUSD(req, res)
|
||||||
|
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||||
|
|
||||||
|
assert.isAbove(res.statusCode, 499, 'HTTP status code 503 expected.')
|
||||||
|
assert.include(
|
||||||
|
result.error,
|
||||||
|
'Could not communicate with full node',
|
||||||
|
'Error message expected'
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should get the USD price of BCH', async () => {
|
||||||
|
// Mock the RPC call for unit tests.
|
||||||
|
if (process.env.TEST === 'unit') {
|
||||||
|
sandbox
|
||||||
|
.stub(uut.axios, 'request')
|
||||||
|
.resolves({ data: mockData.mockCoinexFeed })
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await uut.getBCHUSD(req, res)
|
||||||
|
// console.log(`result: ${util.inspect(result)}`)
|
||||||
|
|
||||||
|
assert.isNumber(result.usd)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user