Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

simple wrapper for Alchemy's blockchain APIs, for fetching blockchain data. Ethereum, Solana, etc

uses my personal ALCHEMY_API_KEY, I forget if you can use it too or not! get your own if it doesn't work

used by some of my other vals:

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
import { fetch } from "https://esm.town/v/std/fetch";
import process from "node:process";
type TokenParam = "erc20" | "nft" | string;
export async function alchemyFetch(path: string, tokens: TokenParam = "erc20") {
const apiKey = process.env.ALCHEMY_API_KEY;
if (!apiKey) throw new Error("missing ALCHEMY_API_KEY");
const baseUrl = tokens == "nft" ? "https://eth-mainnet.g.alchemy.com/nft/v2" : "https://eth-mainnet.g.alchemy.com/v2";
const rpcUrl = `${baseUrl}/${apiKey}`;
const url = `${rpcUrl}/${path}`;
const response = await fetch(url, {
method: "GET",
headers: {
"accept": "application/json",
},
});
console.log("Alchemy response", response);
if (!response) {
return { error: "no response from Alchemy" };
} else if (!response.ok) {
return { error: `failed with status: ${response.statusText}` };
}
else {
const json = await response.json();
console.log("response OK", json);
return json;
}
}
September 14, 2024