Skip to content

Commit

Permalink
Merge pull request #258 from Synthetixio/feature/new-api-route
Browse files Browse the repository at this point in the history
Feature/new api route
  • Loading branch information
jmzwar authored Oct 13, 2023
2 parents a343879 + c0d83b2 commit 02dbe0d
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 0 deletions.
64 changes: 64 additions & 0 deletions pages/api/delegation.ts
Original file line number Diff line number Diff line change
@@ -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<DelegationResponse>(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' });
}
}
62 changes: 62 additions & 0 deletions pages/api/mintburn.ts
Original file line number Diff line number Diff line change
@@ -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<MintBurnResponse>(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' });
}
}
82 changes: 82 additions & 0 deletions pages/api/tvl.ts
Original file line number Diff line number Diff line change
@@ -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<TVLLayerResponse>(TVL_BY_LAYER_URL, {
headers: { 'x-dune-api-key': apiKey },
}),
axios.get<TVLProtocolResponse>(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' });
}
}

0 comments on commit 02dbe0d

Please sign in to comment.