forked from Burstall/hedera-nft-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNFTTransferTwoPKs.mjs
398 lines (328 loc) · 12.1 KB
/
NFTTransferTwoPKs.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import { Client, PrivateKey, AccountBalanceQuery, Hbar, AccountId, TokenAssociateTransaction, TransferTransaction, TokenId, HbarUnit } from '@hashgraph/sdk';
import dotenv from 'dotenv';
dotenv.config();
import fetch from 'cross-fetch';
import { exit } from 'process';
const maxRetries = 10;
let verbose = false;
const env = process.env.ENVIRONMENT ?? null;
const memo = process.env.MEMO || 'Airdrop';
const baseUrlForMainnet = 'https://mainnet-public.mirrornode.hedera.com';
const baseUrlForTestnet = 'http://testnet.mirrornode.hedera.com';
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);
exit(1);
}
}
async function getSerialsOwned(tokenId, wallet, excludeSerialsList = []) {
const baseUrl = env == 'MAIN' ? baseUrlForMainnet : baseUrlForTestnet;
const serialArr = [];
let routeUrl = '/api/v1/tokens/' + tokenId + '/nfts?account.id=' + wallet;
console.log('Fetching serials owned: ', baseUrl + routeUrl);
try {
do {
const json = await fetchJson(baseUrl + routeUrl);
for (let n = 0; n < json.nfts.length; n++) {
const nft = json.nfts[n];
const serial = nft.serial_number;
if (!excludeSerialsList.includes(serial)) serialArr.push(serial);
}
routeUrl = json.links.next;
}
while (routeUrl);
// ensure the array of serials is randomised.
return serialArr;
}
catch (err) {
console.log('Trying to find serials owned', wallet, baseUrl, routeUrl, serialArr);
console.error(err);
exit(1);
}
}
async function fetchJson(url, depth = 0) {
if (depth >= maxRetries) return null;
if (depth > (maxRetries / 2) && verbose) console.log('Attempt: ', depth, url);
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);
}
}
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;
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function fetchWithTimeout(resource, options = {}) {
const { timeout = 30000 } = options;
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeout);
const response = await fetch(resource, {
...options,
signal: controller.signal,
});
clearTimeout(id);
return response;
}
async function executeTransaction(signedTx, client, batchNum) {
console.log(`(Batch #${batchNum}) Tx being sent.`);
try {
const tokenTransferSubmit = await signedTx.execute(client);
// check it worked
const tokenTransferRx = await tokenTransferSubmit.getReceipt(client);
console.log(`(Batch #${batchNum}) Tx processed - status:`, tokenTransferRx.status.toString());
}
catch (err) {
console.log('Error occured executing tx:', err);
}
}
async function main() {
if (getArgFlag('h')) {
console.log('Usage: node NFTTransferTwoPKs.mjs -t <token> [-s <serials> | -r X] [-ft 0.0.ZZZ] [-v]');
console.log('Designed to move **ALL** of a token from one account to another when you have both keys');
console.log(' -t token the token to move');
console.log(' -s <serials> transgfer specific serial(s)');
console.log(' (comma seperated or - for range e.g. 2,5,10 or 1-10)');
console.log(' -ft 0.0.ZZZ pay with fungible token');
console.log(' -r X transfer a random X serials');
console.log(' -v verbose [debug]');
return;
}
const senderAcctId = process.env.SENDER_ACCOUNT_ID;
const senderPK = PrivateKey.fromString(process.env.SENDER_PRIVATE_KEY);
const recAccountId = process.env.RECEIVE_ACCOUNT_ID;
const recPrivateKey = PrivateKey.fromString(process.env.RECEIVE_PRIVATE_KEY);
// If we weren't able to grab it, we should throw a new error
if (!senderAcctId ||
!senderPK || !recAccountId || !recPrivateKey) {
throw new Error('variables for account ID / PKs must be present');
}
console.log('SENDER:', senderAcctId);
console.log('RECEIVER:', recAccountId);
const recAccountIdFromString = AccountId.fromString(recAccountId);
const senderAccountIdFromString = AccountId.fromString(senderAcctId);
console.log('Using ENIVRONMENT:', env);
console.log('Using MEMO:', memo);
verbose = getArgFlag('v');
if (env === undefined || env == null) {
console.log('Environment required, please specify test or main in the .env file');
return;
}
// known ID NFT used
// aim: transfer between two accounts
let tokenId;
if (getArgFlag('t')) {
tokenId = getArg('t');
}
else {
console.log('Token must be set. Please run again with -t <token>');
}
console.log('TOKEN:', tokenId);
let tokenBalMap = await getTokenBalanceMap(tokenId);
let requestedSerialsList = [];
if (getArgFlag('s')) {
const serialsArg = getArg('s');
// format csv or '-' for range
if (serialsArg.includes('-')) {
// inclusive range
const rangeSplit = serialsArg.split('-');
for (let i = rangeSplit[0]; i <= rangeSplit[1]; i++) {
requestedSerialsList.push(i);
}
}
else if (serialsArg.includes(',')) {
requestedSerialsList = serialsArg.split(',');
// array elements to numbers
requestedSerialsList = requestedSerialsList.map(Number);
}
else {
// only one serial to check
requestedSerialsList = [Number(serialsArg)];
}
}
const ownedSerialsList = await getSerialsOwned(tokenId, senderAcctId);
console.log(`Found ${ownedSerialsList.length} serials on account ${senderAcctId}`);
let selectedSerialsList = [];
const useFT = getArg('ft') ? true : false;
if (getArgFlag('r')) {
const randSize = Number(getArg('r'));
if (randSize > ownedSerialsList.length) {
console.log('Requested more random serials than owned - exiting');
console.log('REQUESTED:', randSize);
console.log('OWNED:', ownedSerialsList.length);
process.exit(1);
}
const shuffledSerials = shuffleArray(ownedSerialsList);
selectedSerialsList = shuffledSerials.slice(0, randSize);
console.log(`Sending ${selectedSerialsList.length} serials **RANDOMLY PICKED**`, selectedSerialsList);
}
else if (requestedSerialsList.length > 0) {
// check the requested serials are owned
if (requestedSerialsList.every(elem => ownedSerialsList.includes(elem))) {
console.log(`Sending ${requestedSerialsList.length} serials`, requestedSerialsList);
selectedSerialsList = requestedSerialsList;
}
else {
console.log('Sending account does not own all serials specified -- exiting');
console.log('REQUESTED:', requestedSerialsList);
console.log('OWNED:', ownedSerialsList);
process.exit(1);
}
}
else {
// all
selectedSerialsList = ownedSerialsList;
console.log('Sending **ALL** serials');
}
// Create our connection to the Hedera network
let client;
if (env == 'TEST') {
client = Client.forTestnet();
console.log('Sending tokens in *TESTNET*');
}
else if (env == 'MAIN') {
client = Client.forMainnet();
console.log('Sending tokens in *MAINNET*');
}
else {
console.log('ERROR: Must specify either MAIN or TEST as environment in .env file');
return;
}
client.setOperator(recAccountIdFromString, recPrivateKey);
const tokenIdFromString = TokenId.fromString(tokenId);
const recActBalQueryPreTx = await new AccountBalanceQuery()
.setAccountId(recAccountId)
.execute(client);
const recActBalPreTx = recActBalQueryPreTx.tokens._map.get(tokenId.toString());
// check association to other account
// check mirror node and live network call
if (!tokenBalMap.get(recAccountId) && !recActBalPreTx) {
console.log(`- ${recAccountId} needs to associate NFT with ID ${tokenId}`);
// associate
// Create the associate transaction and sign with Alice's key
const associateRecTx = await new TokenAssociateTransaction()
.setAccountId(recAccountIdFromString)
.setTokenIds([tokenIdFromString])
.freezeWith(client)
.sign(recPrivateKey);
// Submit the transaction to a Hedera network
const associateRecTxSubmit = await associateRecTx.execute(client);
// Get the transaction receipt
const associateRecRx = await associateRecTxSubmit.getReceipt(client);
// Confirm the association was successful
console.log(`- NFT association to ${recAccountId} account for NFT ${tokenId}: ${associateRecRx.status}\n`);
}
else {
console.log(`${recAccountId} balance: ${tokenBalMap.get(recAccountId)} NFT(s) of ID ${tokenId}`);
}
// Now send the tokens!
// 10 items per tx -> 8 for NFT & 2 for economics
const nftBatchSize = 8;
// step 1: break transactions into parts
// process easch instruction seperately to ensure success/failure lines up (less efficient of course).
// more important for NFTs given unique...FT can aggregate.
const promiseArray = [];
let txCount = 0;
for (let outer = 0; outer < selectedSerialsList.length; outer += nftBatchSize) {
txCount++;
const tokenTransferTx = new TransferTransaction();
for (let inner = 0; (inner < nftBatchSize) && ((outer + inner) < selectedSerialsList.length); inner++) {
const serial = selectedSerialsList[outer + inner];
tokenTransferTx.addNftTransfer(tokenIdFromString, serial, senderAccountIdFromString, recAccountIdFromString);
if (verbose) console.log(`Adding serial ${serial} of ${tokenId} to tx to send to ${recAccountId} from ${senderAcctId}`);
}
// need to expose economics given not treasury account
if (verbose) console.log('Sending NFT(s)');
if (!useFT) {
tokenTransferTx
.addHbarTransfer(recAccountIdFromString, new Hbar(-1, HbarUnit.Tinybar))
.addHbarTransfer(senderAccountIdFromString, new Hbar(1, HbarUnit.Tinybar))
.setTransactionMemo(memo)
.freezeWith(client);
}
else {
const fungibleToken = TokenId.fromString(getArg('ft'));
console.log('Paying with:', fungibleToken.toString());
tokenTransferTx
.addTokenTransfer(fungibleToken, recAccountIdFromString, -1)
.addTokenTransfer(fungibleToken, senderAccountIdFromString, 1)
.setTransactionMemo(memo)
.freezeWith(client);
}
// sign
let signedTx = await tokenTransferTx.sign(senderPK);
signedTx = await tokenTransferTx.sign(recPrivateKey);
// submit
promiseArray.push(executeTransaction(signedTx, client, txCount));
}
await Promise.all(promiseArray);
// check balances on all accounts
const senderActBal = await new AccountBalanceQuery()
.setAccountId(senderAcctId)
.execute(client);
const recActBal = await new AccountBalanceQuery()
.setAccountId(recAccountId)
.execute(client);
// refresh ownership from mirror nodes
tokenBalMap = await getTokenBalanceMap(tokenId);
console.log(`- ${senderAcctId} balance: ${senderActBal.tokens._map.get(tokenId.toString())} NFT(s) [network] / ${tokenBalMap.get(senderAcctId)} [mirror nodes] of ID ${tokenId}`);
console.log('Sender HBAR balance is: ' + senderActBal.hbars);
console.log(`- ${recAccountId} balance: ${recActBal.tokens._map.get(tokenId.toString())} NFT(s) [network] / ${tokenBalMap.get(recAccountId)} [mirror nodes] of ID ${tokenId}`);
console.log('Receiver HBAR balance is: ' + recActBal.hbars);
}
function shuffleArray(arr) {
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr;
}
main();