-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaccountInfo.mjs
156 lines (119 loc) · 3.81 KB
/
accountInfo.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { Client, PrivateKey, AccountBalanceQuery, TokenId, AccountInfoQuery } from '@hashgraph/sdk';
import dotenv from 'dotenv';
dotenv.config();
let env;
let verbose = false;
function getArg(arg) {
const customIndex = process.argv.indexOf(`-${arg}`);
let customValue;
if (customIndex > -1) {
// Retrieve the value after --custom
customValue = process.argv[customIndex + 1];
}
return customValue;
}
function getArgFlag(arg) {
const customIndex = process.argv.indexOf(`-${arg}`);
if (customIndex > -1) {
return true;
}
return false;
}
async function main() {
// Grab your Hedera testnet account ID and private key from your .env file
const myAccountId = process.env.MY_ACCOUNT_ID;
const myPrivateKey = PrivateKey.fromString(process.env.MY_PRIVATE_KEY);
// If we weren't able to grab it, we should throw a new error
if (myAccountId == null ||
myPrivateKey == null) {
throw new Error('Environment variables for account ID / PKs must be present');
}
const help = getArgFlag('h');
if (help) {
console.log('Usage: node accountInfo.mjs -e [test|main] -t <tokenId> -acc <account>');
process.exit(0);
}
const tokenId = getArg('t');
const tokenListFile = getArg('l');
const tokenList = [];
if (tokenId === undefined && tokenListFile === undefined) {
console.log('token ID or list required, run: node accountInfo.mjs -h ');
process.exit(1);
}
if (!tokenListFile === undefined) {
// TODO: read in tokens
}
else {
// check if tokens are comma seperated
const commaList = tokenId.split(',');
for (let i = 0; i < commaList.length; i++) {
tokenList.push(commaList[i]);
}
}
env = getArg('e');
if (env === undefined) {
console.log('Environment required, specify test or main -> run: node accountInfo.mjs -h ');
process.exit(1);
}
verbose = getArgFlag('v');
let acct = myAccountId;
if (getArgFlag('acc')) {
acct = getArg('acc');
}
let tokenListString = '';
for (let i = 0 ; i < tokenList.length; i++) {
if (tokenListString == '') { tokenListString += `${tokenList[i]}`; }
else { tokenListString += `, ${tokenList[i]}`; }
}
console.log(`Checking ${acct} for tokens: ${tokenListString}`);
// Create our connection to the Hedera network
let client;
if (env.toLowerCase() == 'main') {
client = Client.forMainnet().setOperator(
myAccountId,
myPrivateKey,
);
env = 'MAIN';
}
else if (env.toLowerCase() == 'test') {
client = Client.forTestnet().setOperator(
myAccountId,
myPrivateKey,
);
env = 'TEST';
}
else {
console.log('must specify -e with \'test\' or \'main\' -- no quotes!');
return;
}
let tokenIdFromString;
for (let c = 0; c < tokenList.length; c++) {
tokenIdFromString = TokenId.fromString(tokenList[c]);
console.log(`check: ${tokenIdFromString}`);
await checkAccountBalances(acct, tokenIdFromString, client);
}
}
async function checkAccountBalances(accountId, tokenId, client) {
const acctInfoTx = await new AccountInfoQuery().setAccountId(accountId).execute(client);
console.log('Account Infor:\n', JSON.stringify(acctInfoTx, null, 4));
const balanceCheckTx = await new AccountBalanceQuery().setAccountId(accountId).execute(client);
const tokenMap = balanceCheckTx.tokens._map;
console.log(`Found ${tokenMap.size} unique associated tokens`);
// console.log(JSON.stringify(balanceCheckTx, null, 4));
const ownedBalance = tokenMap.get(`${tokenId}`) || -1;
// console.log(tokenId.toString(), JSON.stringify(balanceCheckTx.tokens[tokenId.toString()], null, 4));
if (verbose) {
tokenMap.forEach((key, value) => {
console.log(key, value);
});
}
// console.log(tokenId, balanceCheckTx.tokens);
if (ownedBalance < 0) {
console.log(`- NETWORK ${accountId} does not have ${tokenId} associated`);
}
else {
console.log(`- NETWORK ${accountId} balance: ${ownedBalance} NFT(s) of ID ${tokenId}`);
}
return ownedBalance;
}
main();