Skip to content

Commit

Permalink
refactor: sc-pool-apy endpoint cached
Browse files Browse the repository at this point in the history
Signed-off-by: drptbl <jakub.mucha@icloud.com>
  • Loading branch information
drptbl committed Apr 20, 2024
1 parent f26eb25 commit 41ae914
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions src/routes/v3/base/sc-pool-apy.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
const express = require('express');
const router = express.Router();

const { log, postgresClient, getCache, setCache } = require('../../../utils');

const cacheKey = 'sc-pool-apy';

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

/**
* @openapi
* /v3/Base/sc-pool-apy:
Expand Down Expand Up @@ -40,30 +49,14 @@ const { log, postgresClient, getCache, setCache } = require('../../../utils');
*/
router.get('/', async (req, res, next) => {
try {
const cacheKey = 'sc-pool-apy';
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 queryResult = await postgresClient.query(
'select ts, pool_id, collateral_type, apr_7d, apr_7d_pnl, apr_7d_rewards from base_mainnet.fct_core_apr WHERE pool_id = 1 order by ts desc limit 1;',
);

const { apr_7d, apr_7d_pnl, apr_7d_rewards } = queryResult.rows[0];
const aprPnl = parseFloat(apr_7d_pnl);
const aprRewards = parseFloat(apr_7d_rewards);
const aprCombined = parseFloat(apr_7d);

const responseData = {
aprPnl,
aprRewards,
aprCombined,
};
log.debug('Setting cache..');
await setCache(cacheKey, responseData, 60);
const responseData = await fetchDataFromPostgres();
res.json(responseData);
}
} catch (error) {
Expand All @@ -73,3 +66,24 @@ router.get('/', async (req, res, next) => {
});

module.exports = router;

async function fetchDataFromPostgres() {
log.debug('[v3BaseSCPoolAPY] Fetching data from postgres..');
const queryResult = await postgresClient.query(
'select ts, pool_id, collateral_type, apr_7d, apr_7d_pnl, apr_7d_rewards from base_mainnet.fct_core_apr WHERE pool_id = 1 order by ts desc limit 1;',
);

const { apr_7d, apr_7d_pnl, apr_7d_rewards } = queryResult.rows[0];
const aprPnl = parseFloat(apr_7d_pnl);
const aprRewards = parseFloat(apr_7d_rewards);
const aprCombined = parseFloat(apr_7d);

const responseData = {
aprPnl,
aprRewards,
aprCombined,
};
log.debug('[v3BaseSCPoolAPY] Setting cache..');
await setCache(cacheKey, responseData, 60);
return responseData;
}

0 comments on commit 41ae914

Please sign in to comment.