-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathassociateToken.js
312 lines (260 loc) · 8.34 KB
/
associateToken.js
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
const {
Client, PrivateKey, AccountBalanceQuery, TokenAssociateTransaction, TokenDissociateTransaction, TokenId,
} = require('@hashgraph/sdk');
require('dotenv').config();
const fetch = require('cross-fetch');
const baseUrlForMainnet = 'https://mainnet-public.mirrornode.hedera.com';
const baseUrlForTestnet = 'http://testnet.mirrornode.hedera.com';
const maxRetries = 3;
let env;
async function getTokenBalanceMap(tokenId) {
let routeUrl = '/api/v1/tokens/' + tokenId + '/balances/';
const baseUrl = env == 'MAIN' ? baseUrlForMainnet : baseUrlForTestnet;
const tokenBalMap = new Map();
try {
do {
if (verbose) console.log(baseUrl + routeUrl);
const json = await fetchJson(baseUrl + routeUrl);
if (json == null) {
console.log('FATAL ERROR: no NFTs found', baseUrl + routeUrl);
// unlikely to get here but a sensible default
return;
}
for (let b = 0 ; b < json.balances.length; b++) {
const entry = json.balances[b];
const account = entry.account;
const balance = entry.balance;
tokenBalMap.set(account, balance);
}
routeUrl = json.links.next;
}
while (routeUrl);
if (verbose) console.log(tokenBalMap);
return tokenBalMap;
}
catch (err) {
console.log('Trying to find balances for', tokenId, baseUrl, routeUrl);
console.error(err);
process.exit(1);
}
}
const accountTokenOwnershipMap = new Map();
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 associateToken.js -e [test|main] -t <tokenId> -a [ass|dis]');
console.log(' -t <tokenId> tokenID or can be , seperated (no spaces)');
console.log(' e.g. 0.0.614759,0.0.614777,0.0.727536');
console.log(' -a [ass|dis] associate or disassociate');
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 associateToken.js -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]);
}
}
const action = getArg('a');
let associate;
if (action == 'ass') {
associate = true;
}
else if (action == 'dis') {
associate = false;
}
else {
console.log('Need to specify ass(ociate) or dis(associate) -> run: node associateToken.js -h ');
process.exit(1);
}
env = getArg('e');
if (env === undefined) {
console.log('Environment required, specify test or main -> run: node associateToken.js -h ');
process.exit(1);
}
verbose = getArgFlag('v');
const force = getArgFlag('force');
let tokenListString = '';
for (let i = 0 ; i < tokenList.length; i++) {
if (tokenListString == '') { tokenListString += `${tokenList[i]}`; }
else { tokenListString += `, ${tokenList[i]}`; }
}
console.log(`action: ${action} on ${myAccountId} 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;
// run pre checks to allow bulk processing
const checkedTokenList = [];
for (let c = 0; c < tokenList.length; c++) {
tokenIdFromString = TokenId.fromString(tokenList[c]);
console.log(`pre-check: ${tokenIdFromString}`);
//* *CHARGEABLE** consider moving to mirror nodes
const [ownedBalance, mirrorNodeOwnedBalance] = await checkAccountBalances(myAccountId, tokenIdFromString, client);
if (associate) {
if (ownedBalance >= 0) {
// already countAssociated
console.log(`Skipping: ${myAccountId} already has ${tokenIdFromString} associated`);
continue;
}
else {
checkedTokenList.push(tokenIdFromString);
}
}
else if (ownedBalance < 0 && mirrorNodeOwnedBalance < 0) {
// already countAssociated
console.log(`Skipping: ${myAccountId} already has ${tokenIdFromString} DISassociated`);
continue;
}
else if (ownedBalance > 0 && !force) {
console.log(`Skipping: ${myAccountId} still owns ${ownedBalance} -> ${tokenIdFromString} use -force to overide`);
continue;
}
else {
checkedTokenList.push(tokenIdFromString);
}
}
if (checkedTokenList.length == 0) {
console.log('No tokens passed the pre-check');
process.exit(1);
}
let transaction;
if (associate) {
transaction = await new TokenAssociateTransaction()
.setAccountId(myAccountId)
.setTokenIds(checkedTokenList)
.freezeWith(client)
.sign(myPrivateKey);
}
else {
// Dissociate a token from an account and freeze the unsigned transaction for signing
transaction = await new TokenDissociateTransaction()
.setAccountId(myAccountId)
.setTokenIds(checkedTokenList)
.freezeWith(client)
.sign(myPrivateKey);
}
const tokenTransferSubmit = await transaction.execute(client);
const tokenTransferRx = await tokenTransferSubmit.getReceipt(client);
console.log('The transaction consensus status is ' + tokenTransferRx.status.toString());
console.log('Running verification:');
for (let z = 0; z < checkedTokenList.length; z++) {
await checkAccountBalances(myAccountId, checkedTokenList[z], client, z ? 0 : true, false);
}
process.exit(0);
}
async function checkAccountBalances(accountId, tokenId, client, force = false) {
// Save multiple transactions byt using a Map of existing results
// force option to get an update
let tokenMap = accountTokenOwnershipMap.get(accountId) || null;
const mirrorNodeTokenBalMap = await getTokenBalanceMap(tokenId);
if (tokenMap == null || force) {
const balanceCheckTx = await new AccountBalanceQuery().setAccountId(accountId).execute(client);
tokenMap = balanceCheckTx.tokens._map;
console.log(`Found ${tokenMap.size} unique associated tokens`);
}
const ownedBalance = tokenMap.get(`${tokenId}`) || -1;
const mirrorNodeOwnedBalance = mirrorNodeTokenBalMap.get(`${accountId}`);
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}`);
}
if (mirrorNodeOwnedBalance < 0) {
console.log(`- MIRROR NODE ${accountId} does not have ${tokenId} associated`);
}
else {
console.log(`- MIRROR NODE ${accountId} balance: ${mirrorNodeOwnedBalance} NFT(s) of ID ${tokenId}`);
}
return [ownedBalance, mirrorNodeOwnedBalance];
}
async function fetchJson(url, depth = 0) {
if (depth >= maxRetries) return null;
depth++;
try {
const res = await fetchWithTimeout(url);
if (res.status != 200) {
await sleep(500 * depth);
return await fetchJson(url, depth);
}
return res.json();
}
catch (err) {
await sleep(500 * depth);
return await fetchJson(url, depth);
}
}
async function fetchWithTimeout(resource, options = {}) {
const { timeout = 5000 } = options;
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeout);
const response = await fetch(resource, {
...options,
signal: controller.signal,
});
clearTimeout(id);
return response;
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
main();