1
2
3
4
5
6
7
8
9
10
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON";
export let currency = async (desired, base = "usd", amount = 1) => {
let { rates } = await fetchJSON(`https://open.er-api.com/v6/latest/${base}`);
if (rates && rates[desired.toUpperCase()]) return amount * (rates[desired.toUpperCase()]);
else {
let { rates } = await fetchJSON("https://api.coingecko.com/api/v3/exchange_rates");
return amount * rates[desired.toLowerCase()]?.value / rates[base.toLowerCase()]?.value;
}
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
March 7, 2024