mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-22 01:02:05 -07:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
84701e90dc | ||
|
|
34ed5ddc4c | ||
|
|
9a8cd4b664 | ||
|
|
2a0b06ca96 | ||
|
|
9c70fc5007 | ||
|
|
b24b659124 | ||
|
|
7ad5acef34 | ||
|
|
304a3f1d5a | ||
|
|
86a58e9ff3 | ||
|
|
d7d722b27b | ||
|
|
d439165dd2 | ||
|
|
65356051f8 | ||
|
|
9728b64edb |
+1
-1
@@ -17,7 +17,7 @@
|
||||
"test": "npm run lint && npm run test-v3",
|
||||
"lint": "standard --env mocha --fix",
|
||||
"test-v3": "export NETWORK=mainnet && nyc --reporter=text mocha --timeout 60000 test/v3/",
|
||||
"test:temp": "export NETWORK=mainnet && export TEST=integration && mocha --timeout 25000 test/v3/encryption.js",
|
||||
"test:temp": "export NETWORK=mainnet && export TEST=integration && mocha --timeout 25000 test/v3/integration/price.js",
|
||||
"test:integration": "mocha test/v3/integration",
|
||||
"test:integration:slpdb": "mocha --timeout 25000 -g '#validate2Single' test/v3/integration/slp*.js",
|
||||
"coverage": "nyc report --reporter=text-lcov | coveralls",
|
||||
|
||||
@@ -33,7 +33,9 @@ util.inspect.defaultOptions = { depth: 1 }
|
||||
// let _this
|
||||
|
||||
// Set default rate limit value for testing
|
||||
const PRO_PASSES = process.env.PRO_PASSES ? process.env.PRO_PASSES : 'testpassword'
|
||||
const PRO_PASSES = process.env.PRO_PASS
|
||||
? process.env.PRO_PASS
|
||||
: 'testpassword'
|
||||
// Convert the pro-tier password string into an array split by ':'.
|
||||
const PRO_PASS = PRO_PASSES.split(':')
|
||||
|
||||
@@ -47,6 +49,8 @@ class AuthMW {
|
||||
// Initialize passport for 'anonymous' authentication.
|
||||
passport.use(new AnonymousStrategy())
|
||||
|
||||
console.log(`PRO_PASS: ${JSON.stringify(PRO_PASS, null, 2)}`)
|
||||
|
||||
// Initialize passport for 'basic' authentication.
|
||||
passport.use(
|
||||
new BasicStrategy({ passReqToCallback: true }, function (
|
||||
|
||||
@@ -12,10 +12,12 @@
|
||||
|
||||
'use strict'
|
||||
|
||||
const jwt = require('jsonwebtoken')
|
||||
|
||||
const wlogger = require('../util/winston-logging')
|
||||
const config = require('../../config')
|
||||
|
||||
const jwt = require('jsonwebtoken')
|
||||
const ANON_LIMITS = 50
|
||||
|
||||
// Redis
|
||||
const redisOptions = {
|
||||
@@ -111,7 +113,7 @@ class RateLimits {
|
||||
}
|
||||
|
||||
// Default value is 50 points per request = 20 RPM
|
||||
let rateLimit = 50
|
||||
let rateLimit = ANON_LIMITS
|
||||
|
||||
// Only evaluate the JWT token if the user is not using Basic Authentication.
|
||||
if (!req.locals.proLimit) {
|
||||
@@ -146,6 +148,7 @@ class RateLimits {
|
||||
origin &&
|
||||
(origin.toString().indexOf('wallet.fullstack.cash') > -1 ||
|
||||
origin.toString().indexOf('sandbox.fullstack.cash') > -1 ||
|
||||
origin.toString().indexOf('splitbch.com') > -1 ||
|
||||
origin === 'slp-api')
|
||||
) {
|
||||
pointsToConsume = 10
|
||||
@@ -154,9 +157,11 @@ class RateLimits {
|
||||
|
||||
// For internal calls, increase rate limits to as fast as possible.
|
||||
if (
|
||||
key.toString().indexOf('172.17.') > -1
|
||||
// Comment out the line below when running bch-js e2e rate limit tests.
|
||||
// key.toString().indexOf('::ffff:127.0.0.1') > -1
|
||||
key.toString().indexOf('::ffff:127.0.0.1') > -1 ||
|
||||
|
||||
// Do not comment out this line.
|
||||
key.toString().indexOf('172.17.') > -1
|
||||
) {
|
||||
pointsToConsume = 1
|
||||
res.locals.pointsToConsume = pointsToConsume // Feedback for tests.
|
||||
@@ -197,7 +202,7 @@ class RateLimits {
|
||||
// Calculates the points consumed, based on the jwt information and the route
|
||||
// requested.
|
||||
calcPoints (jwtInfo) {
|
||||
let retVal = 50 // By default, use anonymous tier.
|
||||
let retVal = ANON_LIMITS // By default, use anonymous tier.
|
||||
|
||||
try {
|
||||
// console.log(`jwtInfo: ${JSON.stringify(jwtInfo, null, 2)}`)
|
||||
@@ -216,12 +221,12 @@ class RateLimits {
|
||||
if (level40Routes.includes(resource)) {
|
||||
if (apiLevel >= 40) retVal = 10
|
||||
// else if (apiLevel >= 10) retVal = 10
|
||||
else retVal = 50
|
||||
else retVal = ANON_LIMITS
|
||||
|
||||
// Normal indexer routes
|
||||
} else if (level30Routes.includes(resource)) {
|
||||
if (apiLevel >= 30) retVal = 10
|
||||
else retVal = 50
|
||||
else retVal = ANON_LIMITS
|
||||
|
||||
// Full node tier
|
||||
} else if (apiLevel >= 20) {
|
||||
@@ -229,7 +234,7 @@ class RateLimits {
|
||||
|
||||
// Free tier, full node only.
|
||||
} else {
|
||||
retVal = 50
|
||||
retVal = ANON_LIMITS
|
||||
}
|
||||
}
|
||||
|
||||
@@ -237,7 +242,7 @@ class RateLimits {
|
||||
} catch (err) {
|
||||
wlogger.error('Error in route-ratelimit.js/calcPoints()')
|
||||
// throw err
|
||||
retVal = 50
|
||||
retVal = ANON_LIMITS
|
||||
}
|
||||
|
||||
return retVal
|
||||
|
||||
+39
-2
@@ -24,11 +24,14 @@ class Price {
|
||||
this.routeUtils = routeUtils
|
||||
|
||||
this.priceUrl = 'https://api.coinbase.com/v2/exchange-rates?currency=BCH'
|
||||
this.coinexPriceUrl =
|
||||
'https://api.coinex.com/v1/market/ticker?market=bchausdt'
|
||||
|
||||
this.router = express.Router()
|
||||
this.router.get('/', _this.root)
|
||||
this.router.get('/usd', _this.getUSD)
|
||||
this.router.get('/rates', _this.getBCHRate)
|
||||
this.router.get('/bchausd', _this.getBCHAUSD)
|
||||
}
|
||||
|
||||
// DRY error handler.
|
||||
@@ -53,7 +56,7 @@ class Price {
|
||||
* @api {get} /price/usd Get the USD price of BCH
|
||||
* @apiName Get the USD price of BCH
|
||||
* @apiGroup Price
|
||||
* @apiDescription Get the USD price of BCH
|
||||
* @apiDescription Get the USD price of BCH from Coinbase.
|
||||
*
|
||||
*
|
||||
* @apiExample Example usage:
|
||||
@@ -85,7 +88,7 @@ class Price {
|
||||
* @api {get} /price/usd Get rates for several different currencies
|
||||
* @apiName Get rates for several different currencies
|
||||
* @apiGroup Price
|
||||
* @apiDescription Get rates for several different currencies
|
||||
* @apiDescription Get rates for several different currencies from Coinbase.
|
||||
*
|
||||
*
|
||||
* @apiExample Example usage:
|
||||
@@ -113,6 +116,40 @@ class Price {
|
||||
return _this.errorHandler(err, res)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @api {get} /price/bchausd Get the USD price of BCHA
|
||||
* @apiName Get the USD price of BCHA
|
||||
* @apiGroup Price
|
||||
* @apiDescription Get the USD price of BCHA from Coinex.
|
||||
*
|
||||
*
|
||||
* @apiExample Example usage:
|
||||
* curl -X GET "https://api.fullstack.cash/v3/price/bchausd" -H "accept: application/json"
|
||||
*
|
||||
*/
|
||||
async getBCHAUSD (req, res, next) {
|
||||
try {
|
||||
// Request options
|
||||
const opt = {
|
||||
method: 'get',
|
||||
baseURL: _this.coinexPriceUrl,
|
||||
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/getBCHAUSD().', err)
|
||||
|
||||
return _this.errorHandler(err, res)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Price
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
'use strict'
|
||||
|
||||
// const assert = require('chai').assert
|
||||
const assert = require('chai').assert
|
||||
// const axios = require('axios')
|
||||
|
||||
// Used for debugging.
|
||||
@@ -29,6 +29,17 @@ describe('#price', () => {
|
||||
it('should get the USD price', async () => {
|
||||
const result = await price.getUSD(req, res)
|
||||
console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.isNumber(result.usd)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#getBCHAUSD', () => {
|
||||
it('should get the USD price of BCHA', async () => {
|
||||
const result = await price.getBCHAUSD(req, res)
|
||||
console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.isNumber(result.usd)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -216,6 +216,26 @@ const mockCoinbaseFeed = {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
mockCoinbaseFeed
|
||||
const mockCoinexFeed = {
|
||||
code: 0,
|
||||
data: {
|
||||
date: 1605649499848,
|
||||
ticker: {
|
||||
vol: '26717.39062407',
|
||||
low: '10.4000',
|
||||
open: '11.8000',
|
||||
high: '19.0029',
|
||||
last: '18.5000',
|
||||
buy: '18.0100',
|
||||
buy_amount: '200.00000000',
|
||||
sell: '18.5000',
|
||||
sell_amount: '47.89580474'
|
||||
}
|
||||
},
|
||||
message: 'OK'
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
mockCoinbaseFeed,
|
||||
mockCoinexFeed
|
||||
}
|
||||
|
||||
@@ -211,4 +211,70 @@ describe('#PriceRouter', () => {
|
||||
assert.property(result, 'error')
|
||||
})
|
||||
})
|
||||
|
||||
describe('#getBCHAUSD', () => {
|
||||
// const getNetworkInfo = controlRoute.testableComponents.getNetworkInfo
|
||||
|
||||
it('should throw 500 when network issues', async () => {
|
||||
uut.coinexPriceUrl = 'http://fakeurl/api/'
|
||||
|
||||
await uut.getBCHAUSD(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.getBCHAUSD(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.getBCHAUSD(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.getBCHAUSD(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.isNumber(result.usd)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user