|
1 |
| -import type { |
2 |
| - EthStoreResponse, |
3 |
| - MetricReturnData, |
4 |
| - TimestampedData, |
5 |
| -} from "@/lib/types" |
| 1 | +import type { EthStakedResponse, MetricReturnData } from "@/lib/types" |
6 | 2 |
|
7 |
| -import { weiToRoundedEther } from "@/lib/utils/weiToRoundedEther" |
| 3 | +import { DUNE_API_URL } from "../constants" |
8 | 4 |
|
9 |
| -import { BEACONCHA_IN_URL, DAYS_TO_FETCH } from "@/lib/constants" |
10 |
| - |
11 |
| -const MS_PER_DAY = 1000 * 60 * 60 * 24 |
12 |
| -const DAY_DELTA = 5 |
| 5 | +const DUNE_API_KEY = process.env.DUNE_API_KEY |
13 | 6 |
|
14 | 7 | export const fetchTotalEthStaked = async (): Promise<MetricReturnData> => {
|
15 |
| - const { href: ethstoreLatest } = new URL( |
16 |
| - "api/v1/ethstore/latest", |
17 |
| - BEACONCHA_IN_URL |
| 8 | + if (!DUNE_API_KEY) { |
| 9 | + console.error("Dune API key not found") |
| 10 | + return { error: "Dune API key not found" } |
| 11 | + } |
| 12 | + |
| 13 | + const url = new URL( |
| 14 | + "api/v1/endpoints/pablop/eth-staked/results", |
| 15 | + DUNE_API_URL |
18 | 16 | )
|
19 | 17 |
|
20 | 18 | try {
|
21 |
| - // 1- Use initial call to `latest` to fetch current Beacon Chain "day" (for use in secondary fetches) |
22 |
| - const ethstoreLatestResponse = await fetch(ethstoreLatest) |
23 |
| - if (!ethstoreLatestResponse.ok) { |
24 |
| - console.log( |
25 |
| - ethstoreLatestResponse.status, |
26 |
| - ethstoreLatestResponse.statusText |
27 |
| - ) |
28 |
| - throw new Error("Failed to fetch Ethstore latest data") |
| 19 | + const ethStakedResponse = await fetch(url, { |
| 20 | + headers: { "X-Dune-API-Key": DUNE_API_KEY }, |
| 21 | + }) |
| 22 | + if (!ethStakedResponse.ok) { |
| 23 | + console.log(ethStakedResponse.status, ethStakedResponse.statusText) |
| 24 | + throw new Error("Failed to fetch eth staked data") |
29 | 25 | }
|
30 | 26 |
|
31 |
| - const ethstoreJson: EthStoreResponse = await ethstoreLatestResponse.json() |
| 27 | + const ethStakedJson: EthStakedResponse = await ethStakedResponse.json() |
32 | 28 | const {
|
33 |
| - data: { day, effective_balances_sum_wei }, |
34 |
| - } = ethstoreJson |
35 |
| - const valueTotalEth = weiToRoundedEther(effective_balances_sum_wei) |
36 |
| - |
37 |
| - const data: TimestampedData<number>[] = [ |
38 |
| - { timestamp: new Date().getTime(), value: valueTotalEth }, |
39 |
| - ] |
40 |
| - |
41 |
| - // 2- Perform multiple API calls to fetch data for the last 90 days, `getData` for caching |
42 |
| - for (let i = DAY_DELTA; i <= DAYS_TO_FETCH; i += DAY_DELTA) { |
43 |
| - const lookupDay = day - i |
44 |
| - const timestamp = new Date().getTime() - i * MS_PER_DAY |
| 29 | + result: { rows = [] }, |
| 30 | + } = ethStakedJson |
45 | 31 |
|
46 |
| - const { href: ethstoreDay } = new URL( |
47 |
| - `api/v1/ethstore/${lookupDay}`, |
48 |
| - BEACONCHA_IN_URL |
49 |
| - ) |
50 |
| - |
51 |
| - const ethstoreDayResponse = await fetch(ethstoreDay) |
52 |
| - if (!ethstoreDayResponse.ok) { |
53 |
| - console.log(ethstoreDayResponse.status, ethstoreDayResponse.statusText) |
54 |
| - throw new Error("Failed to fetch Ethstore day data") |
55 |
| - } |
56 |
| - |
57 |
| - const ethstoreDayJson: EthStoreResponse = await ethstoreDayResponse.json() |
58 |
| - const { |
59 |
| - data: { effective_balances_sum_wei: sumWei }, |
60 |
| - } = ethstoreDayJson |
61 |
| - const value = weiToRoundedEther(sumWei) |
62 |
| - |
63 |
| - data.push({ timestamp, value }) |
64 |
| - } |
| 32 | + const data = rows.map((row) => ({ |
| 33 | + timestamp: new Date(row.time).getTime(), |
| 34 | + value: row.cum_deposited_eth, |
| 35 | + })) |
65 | 36 |
|
| 37 | + // data is already sorted...but just in case |
66 | 38 | data.sort((a, b) => a.timestamp - b.timestamp)
|
67 | 39 |
|
| 40 | + const { value } = data[data.length - 1] |
| 41 | + |
68 | 42 | return {
|
69 |
| - data, // historical data: { timestamp: unix-milliseconds, value } |
70 |
| - value: valueTotalEth, // current value (number, unformatted) |
| 43 | + data, |
| 44 | + value, |
71 | 45 | }
|
72 | 46 | } catch (error: unknown) {
|
73 | 47 | console.error((error as Error).message)
|
|
0 commit comments