-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-balance-from-address-or-domain.ts
33 lines (24 loc) · 1.31 KB
/
check-balance-from-address-or-domain.ts
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
31
32
33
import { clusterApiUrl, Connection, LAMPORTS_PER_SOL, PublicKey } from "@solana/web3.js";
import { getDomainKey, NameRegistryState, getAllDomains, performReverseLookup, getDomainKeySync } from "@bonfida/spl-name-service";
const suppliedPublicKey = process.argv[2]; // Bp45u6whcYadT8bGuBv334cKfreEfdSJ72muaVRaLKMw OR toly.sol
if (!suppliedPublicKey) {
throw new Error("Provide a public key to check the balance of!");
}
const isDomain = suppliedPublicKey.includes(".");
const connection = new Connection(clusterApiUrl("mainnet-beta"));
const publicKey = isDomain ? await getPublicKeyFromSolDomain(suppliedPublicKey) : new PublicKey(suppliedPublicKey);
if (!PublicKey.isOnCurve(publicKey)) {
throw new Error("Invalid public key");
}
const balanceInLamports = await connection.getBalance(publicKey);
const balanceInSOL = balanceInLamports / LAMPORTS_PER_SOL;
console.log(
`✅ Finished! The balance for the wallet at address ${publicKey} is ${balanceInSOL}!`,
);
// get funds from devnet faucet: https://faucet.solana.com/
async function getPublicKeyFromSolDomain(domain: string):Promise<PublicKey>{
const { pubkey } = getDomainKeySync(domain);
const owner = (await NameRegistryState.retrieve(connection, pubkey)).registry.owner;
console.log(`The owner of SNS Domain: ${domain} is: `, owner.toBase58());
return owner;
}