-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Showing
5 changed files
with
148 additions
and
53 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
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,17 @@ | ||
import { MetricReturnData } from "../types" | ||
|
||
export const fetchEthPrice = async (): Promise<MetricReturnData> => { | ||
try { | ||
const data: { ethereum: { usd: number } } = await fetch( | ||
"https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd" | ||
).then((res) => res.json()) | ||
const { | ||
ethereum: { usd }, | ||
} = data | ||
if (!usd) throw new Error("Unable to fetch ETH price from CoinGecko") | ||
return { value: usd } | ||
} catch (error: unknown) { | ||
console.error((error as Error).message) | ||
return { error: (error as Error).message } | ||
} | ||
} |
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 type { GrowThePieData } from "../types" | ||
|
||
type DataItem = { | ||
metric_key: string | ||
origin_key: string | ||
date: string | ||
value: number | ||
} | ||
|
||
const TXCOSTS_MEDIAN_USD = "txcosts_median_usd" | ||
const TXCOUNT = "txcount" | ||
|
||
export const fetchGrowThePie = async (): Promise<GrowThePieData> => { | ||
const url = "https://api.growthepie.xyz/v1/fundamentals_full.json" | ||
try { | ||
const response = await fetch(url) | ||
if (!response.ok) { | ||
console.log(response.status, response.statusText) | ||
throw new Error("Failed to fetch GrowThePie data") | ||
} | ||
const data: DataItem[] = await response.json() | ||
|
||
const mostRecentDate = data.reduce((latest, item) => { | ||
const itemDate = new Date(item.date) | ||
return itemDate > new Date(latest) ? item.date : latest | ||
}, data[0].date) | ||
|
||
const todaysData = data.filter( | ||
(item) => | ||
item.date === mostRecentDate && | ||
[TXCOSTS_MEDIAN_USD, TXCOUNT].includes(item.metric_key) | ||
) | ||
|
||
let totalTxCount = 0 | ||
let weightedSum = 0 | ||
|
||
todaysData.forEach((item) => { | ||
if (item.metric_key !== TXCOSTS_MEDIAN_USD) return | ||
|
||
const txCountItem = todaysData.find( | ||
(txItem) => | ||
txItem.metric_key === TXCOUNT && txItem.origin_key === item.origin_key | ||
) | ||
if (!txCountItem) return | ||
|
||
totalTxCount += txCountItem.value | ||
weightedSum += item.value * txCountItem.value | ||
}) | ||
|
||
// The weighted average of txcosts_median_usd, by txcount on each network (origin_key) | ||
const weightedAverage = totalTxCount ? weightedSum / totalTxCount : 0 | ||
|
||
return { | ||
txCount: { value: totalTxCount }, | ||
txCostsMedianUsd: { value: weightedAverage }, | ||
} | ||
} catch (error: unknown) { | ||
console.error((error as Error).message) | ||
return { | ||
txCount: { error: (error as Error).message }, | ||
txCostsMedianUsd: { error: (error as Error).message }, | ||
} | ||
} | ||
} |
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
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