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

fetch token balances from an Ethereum wallet. uses Alchemy. don't abuse my API key or I'll turn this off

Use it like: https://jamiedubs-ethereumtokenbalances.web.val.run/?address=0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { alchemyFetch } from "https://esm.town/v/jamiedubs/alchemyClient";
export async function getBalances(address: string) {
const [tokens, eth] = await Promise.all([
alchemyFetch(`getTokenBalances?address=${address}`),
alchemyFetch(`getBalance?address=${address}`),
]);
return { tokens, eth };
}
export default async function ethereumTokenBalances(req: Request): Promise<Response> {
const searchParams = new URL(req.url).searchParams;
const address = searchParams.get("address");
const format = searchParams.get("format")?.toLowerCase() ?? "text";
if (!address) {
return Response.json({ error: "you must specify ?address" });
}
const json = await getBalances(address);
return Response.json(json);
}
jamiedubs-ethereumtokenbalances.web.val.run
September 14, 2024