From e91d0c4d1c7370dfc945d46d2a7512950b2ad293 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 22 Apr 2024 09:15:46 +0000 Subject: [PATCH 1/2] chore(deps): Bump actions/checkout from 4.1.1 to 4.1.3 Bumps [actions/checkout](https://github.com/actions/checkout) from 4.1.1 to 4.1.3. - [Release notes](https://github.com/actions/checkout/releases) - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md) - [Commits](https://github.com/actions/checkout/compare/b4ffde65f46336ab88eb53be808477a3936bae11...1d96c772d19495a3b5c517cd2bc0cb401ea0529f) --- updated-dependencies: - dependency-name: actions/checkout dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- .github/workflows/audit_and_lint.yml | 4 ++-- .github/workflows/codeql.yml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/audit_and_lint.yml b/.github/workflows/audit_and_lint.yml index 48fbbf8..7e56da1 100644 --- a/.github/workflows/audit_and_lint.yml +++ b/.github/workflows/audit_and_lint.yml @@ -21,7 +21,7 @@ jobs: steps: - name: Checkout - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 + uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f - name: Audit dependencies run: audit-ci --critical --report-type full @@ -36,7 +36,7 @@ jobs: steps: - name: Checkout - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 + uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f - name: Set pnpm cache directory run: pnpm config set store-dir .pnpm-store diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 9d17a7c..2c2a435 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -14,7 +14,7 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 + uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f - name: Initialize CodeQL uses: github/codeql-action/init@99c9897648dded3fe63d6f328c46089dd57735ca From 789d3fa64d89146e04bcd95bc8398b10cd10d8a4 Mon Sep 17 00:00:00 2001 From: drptbl Date: Thu, 25 Apr 2024 16:46:15 +0200 Subject: [PATCH 2/2] feat: add snx-buyback endpoint Signed-off-by: drptbl --- src/routes/v3/base/snx-buyback.js | 113 ++++++++++++++++++++++++++++++ src/server.js | 3 + 2 files changed, 116 insertions(+) create mode 100644 src/routes/v3/base/snx-buyback.js diff --git a/src/routes/v3/base/snx-buyback.js b/src/routes/v3/base/snx-buyback.js new file mode 100644 index 0000000..d0d5979 --- /dev/null +++ b/src/routes/v3/base/snx-buyback.js @@ -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; +} diff --git a/src/server.js b/src/server.js index 7431d2a..1121a7f 100644 --- a/src/server.js +++ b/src/server.js @@ -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'