From c0d83b2f910967b87de8d167e1d6a4760fe67133 Mon Sep 17 00:00:00 2001 From: jmzwar Date: Fri, 13 Oct 2023 16:01:27 +0200 Subject: [PATCH] new api routes --- pages/api/delegation.ts | 64 ++++++++++++++++++++++++++++++++ pages/api/mintburn.ts | 62 +++++++++++++++++++++++++++++++ pages/api/tvl.ts | 82 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 208 insertions(+) create mode 100644 pages/api/delegation.ts create mode 100644 pages/api/mintburn.ts create mode 100644 pages/api/tvl.ts diff --git a/pages/api/delegation.ts b/pages/api/delegation.ts new file mode 100644 index 00000000..99847448 --- /dev/null +++ b/pages/api/delegation.ts @@ -0,0 +1,64 @@ +import axios from 'axios'; +import type { NextApiRequest, NextApiResponse } from 'next'; +import { cache } from 'src/utils/cache'; + +const DELEGATION_URL = 'https://api.dune.com/api/v1/query/3060540/results'; + +const apiKey = process?.env?.NEXT_DUNE_API_KEY || ''; + +interface DelegationResponse { + result: { + rows: Delegation[]; + }; +} + +interface Delegation { + ID: string; + blockchain: string; + cumDelegation: number; + currentName: string; + daily_delegations: number; + daily_delegations_USD: number; + day: string; + poolId: string; + token: string; + tokenPrice: number; +} + +async function fetchDuneData() { + try { + const { data } = await axios.get(DELEGATION_URL, { + headers: { 'x-dune-api-key': apiKey }, + }); + + return { + delegation: data.result.rows, + }; + } catch (error) { + console.log('Error fetching dune data', error); + return { + result: null, + }; + } +} + +export default async function handler( + _req: NextApiRequest, + res: NextApiResponse, +) { + try { + const cacheKey = 'delegation'; + const cachedData = cache.get(cacheKey); + + if (cachedData) { + res.status(200).json({ result: cachedData }); + return; + } + + const result = await fetchDuneData(); + cache.set(cacheKey, result, 3600); + res.status(200).json({ result }); + } catch (err) { + res.status(500).json({ error: 'failed to load data' }); + } +} diff --git a/pages/api/mintburn.ts b/pages/api/mintburn.ts new file mode 100644 index 00000000..6a5d2b67 --- /dev/null +++ b/pages/api/mintburn.ts @@ -0,0 +1,62 @@ +import axios from 'axios'; +import type { NextApiRequest, NextApiResponse } from 'next'; +import { cache } from 'src/utils/cache'; + +const MINT_BURN_URL = 'https://api.dune.com/api/v1/query/3059967/results'; + +const apiKey = process?.env?.NEXT_DUNE_API_KEY || ''; + +interface MintBurnResponse { + result: { + rows: MintBurn[]; + }; +} + +interface MintBurn { + day: string; + eth_burns: number; + eth_mints: number; + eth_snxUSD_supply: number; + op_burns: number; + op_mints: number; + op_snxUSD_supply: number; + token: string; +} + +async function fetchDuneData() { + try { + const { data } = await axios.get(MINT_BURN_URL, { + headers: { 'x-dune-api-key': apiKey }, + }); + + return { + mintburn: data.result.rows, + }; + } catch (error) { + console.log('Error fetching dune data', error); + return { + result: null, + }; + } +} + +export default async function handler( + _req: NextApiRequest, + res: NextApiResponse, +) { + try { + const cacheKey = 'mintburn'; + const cachedData = cache.get(cacheKey); + + if (cachedData) { + res.status(200).json({ result: cachedData }); + return; + } + + const result = await fetchDuneData(); + cache.set(cacheKey, result, 3600); + res.status(200).json({ result }); + } catch (err) { + res.status(500).json({ error: 'failed to load data' }); + } +} diff --git a/pages/api/tvl.ts b/pages/api/tvl.ts new file mode 100644 index 00000000..7f649def --- /dev/null +++ b/pages/api/tvl.ts @@ -0,0 +1,82 @@ +import axios from 'axios'; +import type { NextApiRequest, NextApiResponse } from 'next'; +import { cache } from 'src/utils/cache'; + +const TVL_BY_LAYER_URL = 'https://api.dune.com/api/v1/query/3060865/results'; +const TVL_PROTOCOLS_URL = 'https://api.dune.com/api/v1/query/3051552/results'; + +const apiKey = process?.env?.NEXT_DUNE_API_KEY || ''; + +interface TVLLayerResponse { + result: { + rows: TVLByLayer[]; + }; +} + +interface TVLProtocolResponse { + result: { + rows: TVLProtocol[]; + }; +} + +interface TVLByLayer { + day: string; + eth_SNX: number; + op_SNX: number; +} + +interface TVLProtocol { + bal: number; + bal_usd: number; + blockchain: string; + day: string; + layer_usd: number; + token: string; + total_token: number; + total_token_usd: number; + total_usd: number; +} + +async function fetchDuneData() { + try { + const [{ data: tvlByLayer }, { data: tvlByProtocol }] = await Promise.all([ + axios.get(TVL_BY_LAYER_URL, { + headers: { 'x-dune-api-key': apiKey }, + }), + axios.get(TVL_PROTOCOLS_URL, { + headers: { 'x-dune-api-key': apiKey }, + }), + ]); + + return { + tvlByLayer: tvlByLayer?.result.rows, + tvlByProtocol: tvlByProtocol?.result.rows, + }; + } catch (error) { + console.log('Error fetching dune data', error); + return { + result: null, + }; + } +} + +export default async function handler( + _req: NextApiRequest, + res: NextApiResponse, +) { + try { + const cacheKey = 'tvl'; + const cachedData = cache.get(cacheKey); + + if (cachedData) { + res.status(200).json({ result: cachedData }); + return; + } + + const result = await fetchDuneData(); + cache.set(cacheKey, result, 3600); + res.status(200).json({ result }); + } catch (err) { + res.status(500).json({ error: 'failed to load data' }); + } +}