-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
36 lines (31 loc) · 1.2 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
type Response = [string, number][];
/**
* Fetches the current prices for various currencies from the Mempool.space API.
*
* note: we use the mempool.space API, when we reach the rate limit, we get a 429 error.
*
* @param currency - The currency (USD, EUR, GBP, CAD, CHF, AUD, JPY) to filter the prices for. If not provided, returns all available prices.
* @returns An array of tuples, where the first element is the currency code and the second element is the current price.
* @throws {Error} If the specified currency is not found in the API response.
*/
export async function orakelOtter(currency: string = "ALL"): Promise<Response> {
try {
const response = await fetch("https://mempool.space/api/v1/prices/");
const pricesObj = await response.json();
const prices = Object.entries(pricesObj);
if (currency === "ALL") {
return prices as Response;
}
const filteredPrice = prices.filter(
(price) => price[0] === currency?.toUpperCase()
);
if (filteredPrice.length > 0) {
return [...prices[0], filteredPrice[0]] as Response;
} else {
throw new Error(`Currency ${currency} not found.`);
}
} catch (error) {
console.error(error);
return [];
}
}