Skip to content

Commit

Permalink
Leverage token leaderboard routes.
Browse files Browse the repository at this point in the history
  • Loading branch information
peiman3 committed Jan 18, 2025
1 parent 88730b9 commit 14cd917
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 0 deletions.
99 changes: 99 additions & 0 deletions src/routes/v3/base/lt-leaderboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const express = require('express');
const router = express.Router();
const { log, postgresClient, getCache, setCache } = require('../../../utils');

const cacheKey = 'base-lt-leaderboard';

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/lt-leaderboard:
* get:
* tags:
* - v3
* description: Returns leverage token leaderboard on base.
* responses:
* 200:
* description: Successful response.
* content:
* application/json:
* schema:
* type: array
* items:
* type: object
* properties:
* epoch_start:
* type: string
* format: date-time
* example: '2024-05-23T14:00:00.000Z'
* account:
* type: string
* example: "0xAa4a1d3c0d96B0D8C7AE4F71939D0Fa36bd97Aa7"
* total_fees_paid:
* type: string
* example: "954.605497900439850"
* fees_paid_pct:
* type: string
* example: "0.210334039122012452233641885"
* rank:
* type: string
* example: "1"
* 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(`[ltBaseLeaderboard] Error: ${error.message}`);
next(error);
}
});

module.exports = router;

async function fetchDataFromPostgres() {
log.debug('[ltBaseLeaderboard] Fetching data from postgres..');
const queryResult = await postgresClient.query(
`select
epoch_start,
account,
total_fees_paid,
fees_paid_pct,
rank
from prod_base_mainnet.lt_leaderboard
WHERE epoch_start = date '2025-01-14'
;`,
);

const responseData = queryResult.rows;
log.debug('[ltBaseLeaderboard] Setting cache..');
await setCache(cacheKey, responseData, 60);
return responseData;
}
99 changes: 99 additions & 0 deletions src/routes/v3/optimism/lt-leaderboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const express = require('express');
const router = express.Router();
const { log, postgresClient, getCache, setCache } = require('../../../utils');

const cacheKey = 'optimism-lt-leaderboard';

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/Optimism/lt-leaderboard:
* get:
* tags:
* - v3
* description: Returns leverage token leaderboard on optimism.
* responses:
* 200:
* description: Successful response.
* content:
* application/json:
* schema:
* type: array
* items:
* type: object
* properties:
* epoch_start:
* type: string
* format: date-time
* example: '2024-05-23T14:00:00.000Z'
* account:
* type: string
* example: "0xAa4a1d3c0d96B0D8C7AE4F71939D0Fa36bd97Aa7"
* total_fees_paid:
* type: string
* example: "954.605497900439850"
* fees_paid_pct:
* type: string
* example: "0.210334039122012452233641885"
* rank:
* type: string
* example: "1"
* 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(`[ltOptimismLeaderboard] Error: ${error.message}`);
next(error);
}
});

module.exports = router;

async function fetchDataFromPostgres() {
log.debug('[ltOptimismLeaderboard] Fetching data from postgres..');
const queryResult = await postgresClient.query(
`select
epoch_start,
account,
total_fees_paid,
fees_paid_pct,
rank
from prod_optimism_mainnet.lt_leaderboard
WHERE epoch_start = date '2025-01-14'
;`,
);

const responseData = queryResult.rows;
log.debug('[ltOptimismLeaderboard] Setting cache..');
await setCache(cacheKey, responseData, 60);
return responseData;
}
6 changes: 6 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ redisClient.on('ready', () => {
const v3BaseSNXBuybackRouter = require('./routes/v3/base/snx-buyback.js');
app.use('/v3/base/snx-buyback', v3BaseSNXBuybackRouter);

const v3BaseLTLeaderboardRouter = require('./routes/v3/base/lt-leaderboard.js');
app.use('/v3/base/lt-leaderboard', v3BaseLTLeaderboardRouter);

const v3ArbitrumSCPoolAPYRouter = require('./routes/v3/arbitrum/sc-pool-apy.js');
app.use('/v3/arbitrum/sc-pool-apy', v3ArbitrumSCPoolAPYRouter);

Expand All @@ -126,6 +129,9 @@ redisClient.on('ready', () => {
const v3MainnetScPoolAPYAllRouter = require('./routes/v3/mainnet/sc-pool-apy-all.js');
app.use('/v3/mainnet/sc-pool-apy-all', v3MainnetScPoolAPYAllRouter);

const v3OptimismLTLeaderboardRouter = require('./routes/v3/optimism/lt-leaderboard.js');
app.use('/v3/optimism/lt-leaderboard', v3OptimismLTLeaderboardRouter);

// const v3SnaxTestnetVotesRouter = require('./routes/v3/snax-testnet/votes.js');
// app.use('/v3/snax-testnet/votes', v3SnaxTestnetVotesRouter);

Expand Down

0 comments on commit 14cd917

Please sign in to comment.