-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontractHelper.js
630 lines (520 loc) · 25.9 KB
/
contractHelper.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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
// contractHelper.js
import {
fetchPaginatedData,
sendContractQuery,
retryOperation,
log,
batchInsertOrUpdate,
checkProgress,
updateProgress,
db
} from './utils.js';
import axios from 'axios';
import pLimit from 'p-limit';
import { config } from './config.js';
// fetch all code IDs
export async function fetchCodeIds(restAddress) {
try {
const progress = checkProgress('fetchCodeIds');
if (progress.completed) {
log('Skipping fetchCodeIds: Already completed', 'INFO');
return;
}
let nextKey = null;
let totalRecorded = 0;
let batchCount = 0;
let batchProgressUpdates = [];
while (true) {
const response = await fetchPaginatedData(
`${restAddress}/cosmwasm/wasm/v1/code`,
'code_infos',
{
limit: config.paginationLimit,
nextKey,
retries: config.retryConfig.retries,
delay: config.retryConfig.delay,
backoffFactor: config.retryConfig.backoffFactor,
}
);
if (!Array.isArray(response) || response.length === 0) {
log('All code IDs recorded; no additional data found.', 'INFO');
break;
}
const batchData = response.map(({ code_id, creator, instantiate_permission }) =>
[code_id, creator, JSON.stringify(instantiate_permission)]
);
// Batch insert and log each processed batch
db.transaction(() => {
batchInsertOrUpdate('code_ids', ['code_id', 'creator', 'instantiate_permission'], batchData, 'code_id');
})();
batchCount += 1;
totalRecorded += batchData.length;
log(`Batch ${batchCount}: Recorded ${batchData.length} code IDs.`, 'INFO');
nextKey = response.pagination?.next_key || null;
if (!nextKey) {
log('No further pagination key found; pagination ended.', 'INFO');
break;
} else {
log(`Pagination continues; moving to the next page for code IDs.`, 'INFO');
}
if (batchCount === 1 || batchCount % 20 === 0) {
batchProgressUpdates.push({ step: 'fetchCodeIds', completed: 0, lastProcessed: response[response.length - 1].code_id });
}
}
// Final log for total number of code IDs recorded
if (totalRecorded > 0) {
log(`Total code IDs fetched and stored: ${totalRecorded}`, 'INFO');
batchProgressUpdates.push({ step: 'fetchCodeIds', completed: 1 });
} else {
log('No new code IDs recorded.', 'INFO');
}
// Apply batched progress updates
batchProgressUpdates.forEach(update => updateProgress(update.step, update.completed, update.lastProcessed));
} catch (error) {
log(`Error in fetchCodeIds: ${error.message}`, 'ERROR');
throw error;
}
}
// fetchContractAddressesByCodeId with unified progress tracking identifier
export async function fetchContractAddressesByCodeId(restAddress) {
try {
const progress = checkProgress('fetchContractsByCode'); // Unified identifier
if (progress.completed) {
log('Skipping fetchContractsByCode: Already completed', 'INFO');
return;
}
const codeIds = db.prepare('SELECT code_id FROM code_ids').all().map(row => row.code_id);
const startIndex = progress.last_processed ? codeIds.indexOf(progress.last_processed) + 1 : 0;
const limit = pLimit(config.concurrencyLimit);
let totalContracts = 0;
let batchProgressUpdates = [];
const fetchPromises = codeIds.slice(startIndex).map((code_id, index) => limit(async () => {
log(`Fetching contracts for code_id ${code_id}`, 'INFO');
let allContracts = [];
let nextKey = null;
let page = 1;
while (true) {
const paginatedUrl = `${restAddress}/cosmwasm/wasm/v1/code/${code_id}/contracts${nextKey ? `?pagination.key=${encodeURIComponent(nextKey)}` : ''}`;
log(`Fetching data for code_id ${code_id}, page ${page}`, 'DEBUG');
const response = await fetchPaginatedData(paginatedUrl, 'contracts', {
limit: config.paginationLimit,
retries: config.retryConfig.retries,
delay: config.retryConfig.delay,
backoffFactor: config.retryConfig.backoffFactor,
});
if (response.length > 0) {
allContracts.push(...response);
log(`Fetched ${response.length} items for code_id ${code_id} on page ${page}`, 'DEBUG');
} else {
log(`No more contracts found for code_id ${code_id} on page ${page}`, 'INFO');
break;
}
if (response.length < config.paginationLimit) break;
nextKey = response.pagination?.next_key || null;
if (!nextKey) break;
page += 1;
}
const contractCount = allContracts.length;
totalContracts += contractCount;
if (contractCount > 0) {
const batchData = allContracts.map(addr => [code_id, addr, null]);
await batchInsertOrUpdate('contracts', ['code_id', 'address', 'type'], batchData, 'address');
log(`Recorded ${contractCount} contracts for code_id ${code_id}`, 'INFO');
} else {
log(`No contracts found for code_id ${code_id}`, 'INFO');
}
if (index === 0 || index % 20 === 0) {
batchProgressUpdates.push({ step: 'fetchContractsByCode', completed: 0, lastProcessed: code_id });
}
}));
await Promise.allSettled(fetchPromises);
batchProgressUpdates.push({ step: 'fetchContractsByCode', completed: 1 });
batchProgressUpdates.forEach(update => updateProgress(update.step, update.completed, update.lastProcessed));
log(`Completed fetching contract addresses for all code IDs. Total contracts recorded: ${totalContracts}`, 'INFO');
} catch (error) {
log(`Error in fetchContractAddressesByCodeId: ${error.message}`, 'ERROR');
throw error;
}
}
// Fetch contract history for each contract address concurrently
export async function fetchContractHistory(restAddress) {
try {
const contracts = db.prepare('SELECT address FROM contracts').all().map(row => row.address);
const limit = pLimit(config.concurrencyLimit);
let batchProgressUpdates = [];
const historyPromises = contracts.map((contractAddress, index) => limit(async () => {
log(`Fetching contract history for ${contractAddress}`, 'INFO');
let nextKey = null;
while (true) {
// Correct history endpoint
const requestUrl = `${restAddress}/cosmwasm/wasm/v1/contract/${contractAddress}/history${nextKey ? `?pagination.key=${encodeURIComponent(nextKey)}` : ''}`;
try {
const response = await axios.get(requestUrl);
if (response.status !== 200 || !response.data.entries) {
log(`No valid history entries for ${contractAddress}.`, 'ERROR');
break;
}
const { entries, pagination } = response.data;
if (!Array.isArray(entries) || entries.length === 0) {
log(`No history entries found for ${contractAddress}`, 'INFO');
break;
}
// Insert history entries into the database
const insertData = entries.map(entry => [
contractAddress,
entry.operation || '',
entry.code_id || '',
entry.updated || '',
JSON.stringify(entry.msg).replace(/"/g, '""').replace(/\\/g, '\\\\'),
]);
await batchInsertOrUpdate(
'contract_history',
['contract_address', 'operation', 'code_id', 'updated', 'msg'],
insertData,
['contract_address', 'operation', 'code_id'] // Correct ON CONFLICT to use composite key
);
log(`Inserted ${entries.length} history entries for ${contractAddress}`, 'DEBUG');
// Update the nextKey for pagination
nextKey = pagination?.next_key || null;
if (!nextKey) break;
} catch (error) {
log(`Error querying history for ${contractAddress}: ${error.message}`, 'ERROR');
break;
}
}
if (index === 0 || index % 20 === 0) {
batchProgressUpdates.push({ step: 'fetchContractHistory', completed: 0, lastProcessed: contractAddress });
}
}));
await Promise.allSettled(historyPromises);
batchProgressUpdates.push({ step: 'fetchContractHistory', completed: 1 });
batchProgressUpdates.forEach(update => updateProgress(update.step, update.completed, update.lastProcessed));
log('Completed fetching contract history for all contracts.', 'INFO');
} catch (error) {
log(`Error in fetchContractHistory: ${error.message}`, 'ERROR');
throw error;
}
}
/// Fetch and store metadata for each contract address in concurrent batches
export async function fetchContractMetadata(restAddress) {
const batchSize = 50; // Reasonable batch size
const delayBetweenBatches = 50;
try {
const progress = checkProgress('fetchContractMetadata');
if (progress.completed) {
log('Skipping fetchContractMetadata: Already completed', 'INFO');
return;
}
const contractAddresses = db.prepare('SELECT address FROM contracts').all().map(row => row.address);
const totalContracts = contractAddresses.length;
const startIndex = progress.last_processed ? contractAddresses.indexOf(progress.last_processed) + 1 : 0;
let batchProgressUpdates = [];
log(`Starting fetchContractMetadata for ${totalContracts} contracts. Resuming from index ${startIndex}.`, 'INFO');
for (let i = startIndex; i < totalContracts; i += batchSize) {
const batch = contractAddresses.slice(i, i + batchSize);
const limit = pLimit(config.concurrencyLimit);
const fetchPromises = batch.map(contractAddress => limit(async () => {
try {
const requestUrl = `${restAddress}/cosmwasm/wasm/v1/contract/${contractAddress}`;
const response = await axios.get(requestUrl);
if (response?.data?.contract_info) {
const { code_id, creator, admin, label } = response.data.contract_info;
return [contractAddress, code_id, creator, admin, label];
}
} catch (error) {
log(`Failed to fetch metadata for ${contractAddress}: ${error.message}`, 'ERROR');
}
return null;
}));
const results = (await Promise.all(fetchPromises)).filter(Boolean);
if (results.length > 0) {
await batchInsertOrUpdate('contracts', ['address', 'code_id', 'creator', 'admin', 'label'], results, 'address');
log(`Batch inserted metadata for ${results.length} contracts (${i + results.length} of ${totalContracts})`, 'INFO');
}
batchProgressUpdates.push({ step: 'fetchContractMetadata', completed: 0, lastProcessed: batch[batch.length - 1] });
await new Promise(resolve => setTimeout(resolve, delayBetweenBatches));
}
batchProgressUpdates.push({ step: 'fetchContractMetadata', completed: 1 });
batchProgressUpdates.forEach(update => updateProgress(update.step, update.completed, update.lastProcessed));
log('Finished processing metadata for all contracts.', 'INFO');
} catch (error) {
log(`Error in fetchContractMetadata: ${error.message}`, 'ERROR');
throw error;
}
}
// Identify contract types concurrently with batch inserts and progress logging
export async function identifyContractTypes(restAddress) {
try {
const contracts = db.prepare('SELECT address FROM contracts').all().map(row => row.address);
const progress = checkProgress('identifyContractTypes');
const startIndex = progress.last_processed ? contracts.indexOf(progress.last_processed) + 1 : 0;
const batchSize = 50;
let batchData = [];
let processedCount = 0;
let batchProgressUpdates = [];
const limit = pLimit(config.concurrencyLimit);
const typePromises = contracts.slice(startIndex).map((contractAddress, index) => limit(async () => {
let contractType;
const testPayload = { "a": "b" }; // Intentionally incorrect payload to trigger an error response
try {
// No need to construct or pass headers here
const response = await sendContractQuery(restAddress, contractAddress, testPayload, false, true);
// Log the entire response message for debugging purposes
if (response?.message) {
log(`Debug: Full error message for ${contractAddress}: ${response.message}`, 'DEBUG');
// Attempt to extract the contract type from the error message, if available
const match = response.message.match(/Error parsing into type ([\w]+)::/);
if (match) {
contractType = match[1];
log(`Identified contract type for ${contractAddress}: ${contractType}`, 'INFO');
} else {
log(`No recognizable type in error message for contract ${contractAddress}`, 'INFO');
}
} else {
log(`No 'message' field in response for contract ${contractAddress}`, 'DEBUG');
}
contractType = contractType || 'unknown';
log(`Processed contract ${contractAddress} with type ${contractType}`, 'DEBUG');
} catch (error) {
// Log any unexpected errors that aren't related to type parsing
if (error?.response?.status !== 400) {
log(`Error determining contract type for ${contractAddress}: ${error.message}`, 'ERROR');
}
contractType = 'unknown';
}
batchData.push([contractAddress, contractType]);
processedCount++;
if (batchData.length >= batchSize) {
await batchInsertOrUpdate('contracts', ['address', 'type'], batchData, 'address');
batchData = [];
batchProgressUpdates.push({ step: 'identifyContractTypes', completed: 0, lastProcessed: contractAddress });
}
if (processedCount % 100 === 0 || processedCount === contracts.length) {
log(`Progress: Processed ${processedCount} / ${contracts.length} contracts`, 'INFO');
}
}));
await Promise.allSettled(typePromises);
// Insert remaining contracts if any are left in the batch
if (batchData.length > 0) {
await batchInsertOrUpdate('contracts', ['address', 'type'], batchData, 'address');
log(`Final batch inserted contract types for ${batchData.length} contracts`, 'DEBUG');
}
// Batch update progress
batchProgressUpdates.push({ step: 'identifyContractTypes', completed: 1 });
batchProgressUpdates.forEach(update => updateProgress(update.step, update.completed, update.lastProcessed));
log(`Finished identifying contract types for all contracts.`, 'INFO');
} catch (error) {
log(`Error in identifyContractTypes: ${error.message}`, 'ERROR');
throw error;
}
}
// Fetch tokens and their owners for relevant contracts
export async function fetchTokensAndOwners(restAddress) {
const delayBetweenBatches = 100;
const concurrencyLimit = config.concurrencyLimit || 5;
const limit = pLimit(concurrencyLimit);
try {
const progress = checkProgress('fetchTokensAndOwners');
const contracts = db.prepare("SELECT address, type FROM contracts WHERE type IN ('cw721_base', 'cw1155', 'cw404', 'cw20_base')").all();
const startIndex = progress.last_processed ? contracts.findIndex(contract => contract.address === progress.last_processed) + 1 : 0;
let batchProgressUpdates = [];
for (let i = startIndex; i < contracts.length; i++) {
const { address: contractAddress, type: contractType } = contracts[i];
let allTokens = [];
let ownershipData = [];
let lastTokenFetched = null;
log(`Fetching tokens for contract ${contractAddress} of type ${contractType}`, 'INFO');
if (contractType === 'cw20_base') {
// Handle cw20 contract type
const tokenInfoResponse = await sendContractQuery(restAddress, contractAddress, { token_info: {} }, false, false);
log(`Token info response for ${contractAddress}: ${JSON.stringify(tokenInfoResponse)}`, 'DEBUG');
const totalSupply = tokenInfoResponse?.data?.data?.total_supply;
if (!totalSupply) {
log(`No supply or unsupported contract spec for cw20 contract ${contractAddress}. Skipping...`, 'ERROR');
batchProgressUpdates.push({ step: 'fetchTokensAndOwners', completed: 0, lastProcessed: contractAddress });
continue;
}
log(`Recorded total supply for cw20 contract ${contractAddress}: ${totalSupply}`, 'INFO');
await batchInsertOrUpdate('contracts', ['address', 'tokens_minted'], [[contractAddress, totalSupply]], 'address');
let allAccounts = [];
let paginationKey = null;
do {
const accountsQueryPayload = { all_accounts: { limit: config.paginationLimit, ...(paginationKey && { start_after: paginationKey }) } };
const accountsResponse = await sendContractQuery(restAddress, contractAddress, accountsQueryPayload, false, false);
log(`Accounts response for ${contractAddress}: ${JSON.stringify(accountsResponse)}`, 'DEBUG');
const accounts = accountsResponse?.data?.data?.accounts || [];
if (accounts.length > 0) {
allAccounts.push(...accounts);
paginationKey = accounts[accounts.length - 1];
log(`Fetched ${accounts.length} accounts for cw20 contract ${contractAddress}. Accounts: ${accounts.join(', ')}`, 'DEBUG');
} else {
paginationKey = null;
}
} while (paginationKey);
const cw20OwnershipPromises = allAccounts.map(account => limit(async () => {
const balanceQueryPayload = { balance: { address: account } };
const balanceResponse = await sendContractQuery(restAddress, contractAddress, balanceQueryPayload, false, false);
log(`Balance response for account ${account} in ${contractAddress}: ${JSON.stringify(balanceResponse)}`, 'DEBUG');
const balance = balanceResponse?.data?.data?.balance;
if (balance) {
ownershipData.push([contractAddress, account, balance]);
log(`Recorded balance for cw20 contract ${contractAddress}: owner=${account}, balance=${balance}`, 'DEBUG');
} else {
log(`Failed to retrieve balance for account ${account} in cw20 contract ${contractAddress}.`, 'ERROR');
}
}));
await Promise.allSettled(cw20OwnershipPromises);
if (ownershipData.length > 0) {
await batchInsertOrUpdate('cw20_owners', ['contract_address', 'owner_address', 'balance'], ownershipData, ['contract_address', 'owner_address']);
log(`Inserted ${ownershipData.length} ownership records into cw20_owners for ${contractAddress}`, 'INFO');
}
batchProgressUpdates.push({ step: 'fetchTokensAndOwners', completed: 0, lastProcessed: contractAddress });
await new Promise(resolve => setTimeout(resolve, delayBetweenBatches));
continue;
}
// Process other contract types (cw1155, cw721_base, etc.)
while (true) {
const tokenQueryPayload = { all_tokens: { limit: config.paginationLimit, ...(lastTokenFetched && { start_after: lastTokenFetched }) } };
const response = await sendContractQuery(restAddress, contractAddress, tokenQueryPayload, false, false);
log(`Token response for ${contractAddress}: ${JSON.stringify(response)}`, 'DEBUG');
const tokenIds = response?.data?.data?.tokens || [];
if (tokenIds.length === 0) {
log(`No more tokens found for contract ${contractAddress}`, 'INFO');
break;
}
allTokens.push(...tokenIds);
lastTokenFetched = tokenIds[tokenIds.length - 1];
log(`Fetched ${tokenIds.length} tokens for contract ${contractAddress}`, 'DEBUG');
const tokenData = tokenIds.map(tokenId => [contractAddress, tokenId]);
await batchInsertOrUpdate('contract_tokens', ['contract_address', 'token_id'], tokenData, ['contract_address', 'token_id']);
}
if (allTokens.length > 0) {
await batchInsertOrUpdate('contracts', ['address', 'tokens_minted'], [[contractAddress, allTokens.length]], 'address');
log(`Updated tokens_minted for contract ${contractAddress} with total tokens: ${allTokens.length}`, 'INFO');
// Insert to `nft_owners` table
const nftOwnerData = allTokens.map(tokenId => [contractAddress, tokenId]);
await batchInsertOrUpdate('nft_owners', ['collection_address', 'token_id'], nftOwnerData, ['collection_address', 'token_id']);
log(`Inserted ${nftOwnerData.length} records into nft_owners for contract ${contractAddress}`, 'INFO');
} else {
log(`No tokens retrieved for contract ${contractAddress}. Retrying...`, 'WARN');
continue;
}
batchProgressUpdates.push({ step: 'fetchTokensAndOwners', completed: 0, lastProcessed: contractAddress });
await new Promise(resolve => setTimeout(resolve, delayBetweenBatches));
}
batchProgressUpdates.push({ step: 'fetchTokensAndOwners', completed: 1 });
batchProgressUpdates.forEach(update => updateProgress(update.step, update.completed, update.lastProcessed));
log('Finished processing tokens and ownership for all contracts.', 'INFO');
} catch (error) {
log(`Error in fetchTokensAndOwners: ${error.message}`, 'ERROR');
throw error;
}
}
// Fetch pointer data and store it in the database
export async function fetchPointerData(pointerApi) {
const chunkSize = config.chunkSize || 25; // Default to 25 if not defined
// Retrieve all addresses from the contracts table and split them into an array
const addressResult = db.prepare('SELECT address FROM contracts').all();
const contractAddresses = addressResult.map(row => row.address);
// Check if contractAddresses array is valid
if (contractAddresses.length === 0) {
log(`Error: No contract addresses found in the database`, 'ERROR');
return; // Exit function if no addresses are found
}
// Insert contract addresses into the pointer_data table if not already present
await db.transaction(() => {
const insertQuery = db.prepare(`
INSERT OR IGNORE INTO pointer_data (contract_address) VALUES (?)`);
contractAddresses.forEach(address => insertQuery.run(address));
})();
// Process the addresses in chunks and send requests to pointerApi
for (let i = 0; i < contractAddresses.length; i += chunkSize) {
const chunk = contractAddresses.slice(i, i + chunkSize); // Prepare a chunk of addresses
const payload = { addresses: chunk }; // Payload with addresses array
try {
// Send the batch request to the pointer API with retry logic
const response = await retryOperation(() => axios.post(pointerApi, payload));
if (response && response.status === 200 && Array.isArray(response.data)) {
// Parse the response for each address and prepare data for insertion
const batchData = response.data.map(({ address, pointerAddress, pointeeAddress, isBaseAsset, isPointer, pointerType }) => [
address,
pointerAddress || null,
pointeeAddress || null,
isBaseAsset ? 1 : 0,
isPointer ? 1 : 0,
pointerType || null
]);
// Insert or update batch data in the pointer_data table
await batchInsertOrUpdate(
'pointer_data',
['contract_address', 'pointer_address', 'pointee_address', 'is_base_asset', 'is_pointer', 'pointer_type'],
batchData,
'contract_address'
);
log(`Stored pointer data for ${batchData.length} addresses`, 'DEBUG');
} else {
log(`Unexpected or empty data while fetching pointer data. Status: ${response ? response.status : 'No response'}`, 'ERROR');
}
} catch (error) {
log(`Error processing pointer data chunk: ${error.message}`, 'ERROR');
}
}
log('Finished fetching pointer data for all addresses.', 'INFO');
}
// Fetch all evm wallet addresses for owners in nft_owners
export async function fetchAssociatedWallets(evmRpcAddress, concurrencyLimit = 5) {
try {
log('Starting fetchAssociatedWallets function...', 'INFO');
const progress = checkProgress('fetchAssociatedWallets');
const lastProcessedOwner = progress.last_processed;
const owners = db.prepare(`
SELECT DISTINCT owner
FROM nft_owners
WHERE owner NOT IN (SELECT wallet_address FROM wallet_associations)
AND owner > ?
ORDER BY owner ASC
`).all(lastProcessedOwner || '');
if (owners.length === 0) {
log('No unprocessed owners found in nft_owners table.', 'INFO');
return;
}
log(`Found ${owners.length} unique owners to process`, 'INFO');
let batchProgressUpdates = [];
for (let i = 0; i < owners.length; i += concurrencyLimit) {
const batch = owners.slice(i, i + concurrencyLimit);
log(`Processing batch ${Math.floor(i / concurrencyLimit) + 1} of ${Math.ceil(owners.length / concurrencyLimit)}`, 'INFO');
await Promise.all(
batch.map(async ({ owner }) => {
log(`Fetching EVM address for owner: ${owner}`, 'DEBUG');
const payload = {
jsonrpc: '2.0',
method: 'sei_getEVMAddress',
params: [owner],
id: 1,
};
try {
const response = await sendContractQuery(evmRpcAddress, '', payload, true, false);
if (response?.data?.result) {
await batchInsertOrUpdate('wallet_associations', ['wallet_address', 'evm_address'], [[owner, response.data.result]], 'wallet_address');
log(`Stored EVM address for owner: ${owner}`, 'INFO');
} else {
log(`No EVM address found for owner: ${owner}`, 'WARN');
}
} catch (error) {
log(`Error processing owner ${owner}: ${error.message}`, 'ERROR');
}
})
);
const lastOwnerInBatch = batch[batch.length - 1].owner;
batchProgressUpdates.push({ step: 'fetchAssociatedWallets', completed: 0, lastProcessed: lastOwnerInBatch });
log(`Updated progress for batch ${Math.floor(i / concurrencyLimit) + 1}`, 'DEBUG');
}
batchProgressUpdates.push({ step: 'fetchAssociatedWallets', completed: 1 });
batchProgressUpdates.forEach(update => updateProgress(update.step, update.completed, update.lastProcessed));
log('Finished processing all associated wallets.', 'INFO');
} catch (error) {
log(`Critical error in fetchAssociatedWallets: ${error.message}`, 'ERROR');
throw error;
}
}
export { axios };