-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #258 from Synthetixio/feature/new-api-route
Feature/new api route
- Loading branch information
Showing
3 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }); | ||
} | ||
} |