-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
75 lines (68 loc) · 2.66 KB
/
index.mjs
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { clusterApiUrl, Connection } from "@solana/web3.js";
import { TOKEN_PROGRAM_ID } from "@solana/spl-token";
import { TokenListProvider} from '@solana/spl-token-registry';
import { log } from 'console';
import { HttpsProxyAgent } from 'hpagent';
import fetch from 'node-fetch';
import { config } from 'dotenv';
config();
const proxy = process.env.HTTP_PROXY;
const proxyAgent = new HttpsProxyAgent ({ proxy });
const solanaConnection = new Connection(clusterApiUrl("mainnet-beta"), {
commitment: 'confirmed',
fetch: async (input, options) => {
const processedInput =
typeof input === 'string' && input.slice(0, 2) === '//'
? 'https:' + input
: input;
const result = await fetch(processedInput, {
...options,
agent: proxyAgent,
});
log('RESPONSE STATUS', result.status);
return result;
},
});
const walletToQuery = process?.argv[2];
if(!walletToQuery) {
throw new Error('No wallet address given');
}
// An example taken from some article online
async function getTokenRegistry(){
const tokenListProvider = new TokenListProvider;
const tokens = await tokenListProvider.resolve();
const tokenList = tokens.filterByClusterSlug('mainnet-beta').getList();
return(tokenList);
}
async function getTokenAccounts(wallet, solanaConnection) {
const filters = [
{
dataSize: 165, // size of account (bytes)
},
{
memcmp: {
offset: 32, // location of our query in the account (bytes)
bytes: wallet, // our search criteria, a base58 encoded string
},
}];
const accounts = await solanaConnection.getParsedProgramAccounts(
TOKEN_PROGRAM_ID, // new PublicKey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")
{filters: filters}
);
console.log(`Found ${accounts.length} token account(s) for wallet ${wallet}.`);
const tokenList = await getTokenRegistry();
accounts.forEach((account, i) => {
// Parse the account data
const parsedAccountInfo = account.account.data;
const mintAddress = parsedAccountInfo["parsed"]["info"]["mint"];
const tokenBalance = parsedAccountInfo["parsed"]["info"]["tokenAmount"]["uiAmount"];
// Find this token in the Token Registry
const tokenName = tokenList.find(token=>token.address === mintAddress);
// Log results
console.log(`Token Account No. ${i + 1}: ${account.pubkey.toString()}`);
console.log(`--Token Mint: ${mintAddress}`);
if(tokenName) {console.log(`--Name: ${tokenName.name}`)}
console.log(`--Token Balance: ${tokenBalance}`);
});
}
getTokenAccounts(walletToQuery, solanaConnection);