Skip to content

Commit

Permalink
feat: add snx-buyback endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: drptbl <[email protected]>
  • Loading branch information
drptbl committed Apr 25, 2024
1 parent d30bf9d commit 789d3fa
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
113 changes: 113 additions & 0 deletions src/routes/v3/base/snx-buyback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
const express = require('express');
const router = express.Router();
const { log, postgresClient, getCache, setCache } = require('../../../utils');

const cacheKey = 'snx-buyback';

fetchDataFromPostgres();
const cacheTime =
((process.env.CACHE_TIME =
typeof process.env.CACHE_TIME === 'string'
? parseInt(process.env.CACHE_TIME)
: process.env.CACHE_TIME) -
30) *
1000;
setInterval(fetchDataFromPostgres, cacheTime < 30000 ? 30000 : cacheTime);

/**
* @openapi
* /v3/Base/snx-buyback:
* get:
* tags:
* - v3
* description: Returns SNX buyback details on Base.
* responses:
* 200:
* description: Successful response.
* content:
* application/json:
* schema:
* type: array
* items:
* type: object
* properties:
* ts:
* type: number
* example: "1714003200000"
* snxAmount:
* type: number
* example: "202.23566293647977"
* usdAmount:
* type: number
* example: "588.0897936329317"
* cumulativeSnxAmount:
* type: number
* example: "24451.665275214178"
* cumulativeUsdAmount:
* type: number
* example: "95386.36662445917"
* 401:
* description: Unauthorized.
* 403:
* description: You have been banned by WAF.
* 429:
* description: Too many requests, you're being rate-limited.
* 5XX:
* description: Service unavailable.
* default:
* description: Unexpected error.
*/
router.get('/', async (req, res, next) => {
try {
log.debug('Checking cache..');
const cachedResponse = await getCache(cacheKey);
if (cachedResponse) {
log.debug('Cache found');
res.json(cachedResponse);
} else {
log.debug('Cache not found, executing..');
const responseData = await fetchDataFromPostgres();
res.json(responseData);
}
} catch (error) {
log.error(`[v3BaseSNXBuyback] Error: ${error.message}`);
next(error);
}
});

module.exports = router;

async function fetchDataFromPostgres() {
log.debug('[v3BaseSNXBuyback] Fetching data from postgres..');
const queryResult = await postgresClient.query(
'select ts, snx_amount, usd_amount, cumulative_snx_amount, cumulative_usd_amount from base_mainnet.fct_buyback_daily;',
);

const data = queryResult.rows.map(parseAndRenameKeys);

const responseData = data;
log.debug('[v3BaseSNXBuyback] Setting cache..');
await setCache(cacheKey, responseData, 60);
return responseData;
}

function toCamelCase(str) {
return str.replace(/([-_][a-z])/gi, (group) =>
group.toUpperCase().replace('-', '').replace('_', ''),
);
}

function parseAndRenameKeys(obj) {
const newObj = {};
for (const key in obj) {
const camelCaseKey = toCamelCase(key);
if (key === 'ts') {
newObj[camelCaseKey] = Date.parse(obj[key]);
} else if (!isNaN(obj[key])) {
newObj[camelCaseKey] = parseFloat(obj[key]);
} else {
newObj[camelCaseKey] = obj[key];
}
}
return newObj;
}
3 changes: 3 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ redisClient.on('ready', () => {
const v3BaseSCPoolAPYRouter = require('./routes/v3/base/sc-pool-apy.js');
app.use('/v3/base/sc-pool-apy', v3BaseSCPoolAPYRouter);

const v3BaseSNXBuybackRouter = require('./routes/v3/base/snx-buyback.js');
app.use('/v3/base/snx-buyback', v3BaseSNXBuybackRouter);

log.debug('[Express] Starting server..');
const port =
typeof process.env.API_PORT === 'string'
Expand Down

0 comments on commit 789d3fa

Please sign in to comment.