-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_index.js
157 lines (147 loc) · 4.85 KB
/
test_index.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
// Import necessary functions and modules
import {
fetchCodeIds,
fetchContractAddressesByCodeId,
fetchContractMetadata,
fetchContractHistory, // Import the new fetchContractHistory function
identifyContractTypes,
fetchTokensAndOwners,
fetchPointerData,
fetchAssociatedWallets
} from './contractHelper.js';
import {
log,
updateProgress,
db
} from './utils.js';
import { config } from './config.js';
import fs from 'fs';
// Ensure data and logs directories exist
if (!fs.existsSync('./data')) fs.mkdirSync('./data');
if (!fs.existsSync('./logs')) fs.mkdirSync('./logs');
// Create necessary tables if they don't exist
function initializeDatabase() {
const createTableStatements = [
`CREATE TABLE IF NOT EXISTS indexer_progress (
step TEXT PRIMARY KEY,
completed INTEGER DEFAULT 0,
last_processed TEXT,
last_fetched_token TEXT
)`,
`CREATE TABLE IF NOT EXISTS code_ids (
code_id TEXT PRIMARY KEY,
creator TEXT,
instantiate_permission TEXT
)`,
`CREATE TABLE IF NOT EXISTS contracts (
code_id TEXT,
address TEXT PRIMARY KEY,
type TEXT,
creator TEXT,
admin TEXT,
label TEXT,
token_ids TEXT
)`,
`CREATE TABLE IF NOT EXISTS contract_history (
contract_address TEXT,
operation TEXT,
code_id TEXT,
updated TEXT,
msg TEXT,
PRIMARY KEY (contract_address, operation, code_id)
)`,
`CREATE TABLE IF NOT EXISTS contract_tokens (
contract_address TEXT,
token_id TEXT,
contract_type TEXT,
PRIMARY KEY (contract_address, token_id)
)`,
`CREATE TABLE IF NOT EXISTS nft_owners (
collection_address TEXT,
token_id TEXT,
owner TEXT,
contract_type TEXT,
PRIMARY KEY (collection_address, token_id)
)`,
`CREATE TABLE IF NOT EXISTS pointer_data (
contract_address TEXT PRIMARY KEY,
pointer_address TEXT,
pointee_address TEXT,
is_base_asset INTEGER,
is_pointer INTEGER,
pointer_type TEXT
)`,
`CREATE TABLE IF NOT EXISTS wallet_associations (
wallet_address TEXT PRIMARY KEY,
evm_address TEXT
)`
];
try {
const dbInstance = db; // Using better-sqlite3 instance directly
dbInstance.transaction(() => {
for (const statement of createTableStatements) {
dbInstance.prepare(statement).run();
log(`Table created/verified: ${statement}`, 'INFO');
}
})();
log('All tables initialized successfully.', 'INFO');
} catch (error) {
log(`Failed during table initialization: ${error.message}`, 'ERROR');
throw error;
}
}
// Main function to run the test indexer
async function runTest() {
try {
log('Test Indexer started with log level: DEBUG', 'INFO');
initializeDatabase();
log('Test database initialized successfully.', 'INFO');
// Define the steps for the test
const steps = [
{ name: 'fetchCodeIds', action: () => fetchCodeIds(config.restAddress) },
{ name: 'fetchContractAddressesByCode', action: () => fetchContractAddressesByCodeId(config.restAddress) },
{ name: 'fetchContractMetadata', action: () => fetchContractMetadata(config.restAddress) },
{ name: 'fetchContractHistory', action: () => fetchContractHistory(config.restAddress) }, // Added the fetchContractHistory step
{ name: 'identifyContractTypes', action: () => identifyContractTypes(config.restAddress) },
{ name: 'fetchTokensAndOwners', action: () => fetchTokensAndOwners(config.restAddress) },
{ name: 'fetchPointerData', action: () => fetchPointerData(config.pointerApi) },
{ name: 'fetchAssociatedWallets', action: () => fetchAssociatedWallets(config.evmRpcAddress) }
];
// Execute each step sequentially
for (const step of steps) {
let retries = 3;
let completed = false;
while (retries > 0 && !completed) {
try {
log(`Running test step: ${step.name}`, 'INFO');
await step.action();
updateProgress(step.name, 1);
log(`Test step ${step.name} completed successfully.`, 'INFO');
completed = true;
} catch (error) {
retries--;
log(`Test step ${step.name} failed with error: ${error.message}. Retries left: ${retries}`, 'ERROR');
if (error.stack) {
log(`Stack trace: ${error.stack}`, 'DEBUG');
}
if (retries === 0) {
throw error; // Stop the test if retries are exhausted
}
}
}
}
log('All test steps completed successfully.', 'INFO');
} catch (error) {
log(`Test failed with error: ${error.message}`, 'ERROR');
if (error.stack) {
log(`Stack trace: ${error.stack}`, 'DEBUG');
}
}
}
// Start the test
runTest().catch((error) => {
log(`Test run failed: ${error.message}`, 'ERROR');
if (error.stack) {
log(`Stack trace: ${error.stack}`, 'DEBUG');
}
});