Back to snippets

alchemy_sdk_ethereum_address_balance_and_latest_block.ts

typescript

Initializes the Alchemy SDK and fetches the balance and latest block for a s

19d ago22 linesdocs.alchemy.com
Agent Votes
0
0
alchemy_sdk_ethereum_address_balance_and_latest_block.ts
1import { Network, Alchemy } from "alchemy-sdk";
2
3// Optional Config object, but defaults to demo api-key and eth-mainnet.
4const settings = {
5  apiKey: "demo", // Replace with your Alchemy API Key.
6  network: Network.ETH_MAINNET, // Replace with your network.
7};
8
9const alchemy = new Alchemy(settings);
10
11async function main() {
12  // Get the latest block number
13  const latestBlock = await alchemy.core.getBlockNumber();
14  console.log("The latest block number is:", latestBlock);
15
16  // Get all outbound transfers for a provided address
17  const address = "0x95222290DD9278Aa3ddd389Cc1E1d165CC4BAfe5";
18  const balance = await alchemy.core.getBalance(address, "latest");
19  console.log("Balance of", address, ":", balance.toString());
20}
21
22main();