-
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.
- Loading branch information
jmzwar
committed
Sep 11, 2023
1 parent
05475e4
commit 5a2a8fa
Showing
1 changed file
with
50 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,50 @@ | ||
import axios from 'axios'; | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
import { IntegratorsVolumeResponse } from 'src/typings'; | ||
|
||
const INTEGRATORS_VOLUME_URL = | ||
'https://api.dune.com/api/v1/query/2647536/results'; | ||
|
||
const apiKey = process?.env?.NEXT_DUNE_API_KEY || ''; | ||
|
||
async function fetchDuneData() { | ||
try { | ||
const { data: integratorsData } = | ||
await axios.get<IntegratorsVolumeResponse>(INTEGRATORS_VOLUME_URL, { | ||
headers: { 'x-dune-api-key': apiKey }, | ||
}); | ||
|
||
const integratorsVolume = integratorsData.result.rows.sort((a, b) => | ||
b.day > a.day ? 1 : -1, | ||
); | ||
|
||
// Get the integrator volume for the latest date | ||
// and sort by daily fee | ||
const integratorForLatestDate = integratorsVolume | ||
.filter(item => item.day === integratorsVolume[0].day) | ||
.sort((a, b) => (a.daily_fee > b.daily_fee ? -1 : 1)) | ||
.map(item => item.tracking_code.split('\x00')[0]); | ||
|
||
return { | ||
integratorForLatestDate, | ||
}; | ||
} catch (error) { | ||
console.log('Error fetching dune data', error); | ||
return { | ||
integratorForLatestDate: null, | ||
}; | ||
} | ||
} | ||
|
||
export default async function handler( | ||
_req: NextApiRequest, | ||
res: NextApiResponse, | ||
) { | ||
try { | ||
const result = await fetchDuneData(); | ||
res.setHeader('Cache-Control', 's-maxage=3600'); | ||
res.status(200).json({ result }); | ||
} catch (err) { | ||
res.status(500).json({ error: 'failed to load data' }); | ||
} | ||
} |