Runs every 8 hrs
Fork
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
import { set } from "https://esm.town/v/std/set?v=11";
import { email } from "https://esm.town/v/std/email?v=9";
import { formatAsCurrency } from "https://esm.town/v/panphora/formatAsCurrency";
let { lastBtcPrice } = await import("https://esm.town/v/panphora/lastBtcPrice");
import process from "node:process";
import { fetchJSON } from "https://esm.town/v/panphora/fetchJSON";
export async function btcPrice() {
let btc = await fetchJSON(
`https://rest.coinapi.io/v1/quotes/COINBASE_SPOT_BTC_USD/current`,
{
headers: {
"X-CoinAPI-Key": process.env.COIN_API,
},
},
);
let btcPrice = btc.last_trade.price; // this is a number
if (Math.abs(btcPrice - lastBtcPrice) > 2500) {
// only alert me when the price changes +/- $2500
let formattedBtcPrice = formatAsCurrency(btcPrice);
await email({
text: formattedBtcPrice,
subject: "BTC PRICE ALERT: " + formattedBtcPrice,
});
// set the new price to compare to to the current price
lastBtcPrice = btcPrice;
await set("lastBtcPrice", lastBtcPrice);
}
return btcPrice;
}