-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathautoInscriber.js
More file actions
203 lines (182 loc) · 6.15 KB
/
Copy pathautoInscriber.js
File metadata and controls
203 lines (182 loc) · 6.15 KB
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
// translated and rewritten version of
// https://github.com/martinseeger2002/dogcoin_ordinal_auto_inscriber/tree/main
const fs = require("fs");
const path = require("path");
const { exec } = require("child_process");
const mainPath = process.cwd();
const bellsAddress = "B6CV1RTyCgivPVkDZBApW9EuXfb5efHuxX"; // Address to inscribe to
const directory = path.join(mainPath, "build/webp"); // Replace with directory to the collection
const filePrefix = "Bellooors"; // Replace with collection name. The file name without the serial number
const fileExtension = "webp"; // Replace with file extension
const start = 6; // Enter range of files to inscribe from
const end = 8888; // Enter range of files to inscribe to
let counter = start - 1;
let txidMatch = "";
let isPending = false;
let imgPath = "";
const commands = [];
function runNodeCommands(start, end, directory, filePrefix, fileExtension) {
for (let i = start; i <= end; i++) {
const fileNumber = String(i).padStart(5, "0");
const imagePath = path.join(
directory,
`${filePrefix}${fileNumber}.${fileExtension}`
);
if (!fs.existsSync(imagePath)) {
console.log(`File not found: ${imagePath}`);
continue;
}
const baseFileName = path.basename(imagePath, `.${fileExtension}`);
commands.push(() => mintFile(baseFileName, imagePath));
}
executeCommands(commands);
}
function executeCommands(commands) {
if (commands.length === 0) return;
const command = commands[0];
command()
.then(() => {
setTimeout(() => {
commands.shift(); // Only shift if successful
syncWallet()
.then(() => {
executeCommands(commands);
})
.catch((err) => {
console.error("Error syncing wallet:", err);
executeCommands(commands);
});
}, 60000); // Wait 60 seconds before executing the next command
})
.catch((err) => {
// On error, retry the same command after a delay
setTimeout(() => {
syncWallet()
.then(() => {
executeCommands(commands);
})
.catch((err) => {
console.error("Error syncing wallet:", err);
executeCommands(commands);
});
}, 60000); // Wait 60 seconds before retrying the command again
});
}
function mintFile(baseFileName, imagePath) {
return new Promise((resolve, reject) => {
const mintCommand = `node bellinals.js mint ${bellsAddress} ${imagePath}`;
console.log(`Inscribing ${imagePath}`);
imgPath = imagePath;
exec(mintCommand, (error, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
if (error) {
if (
stdout.match("Transaction already in block chain") ||
stdout.match("bad-txns-inputs-missingorspent") ||
stderr.match("Cannot read properties of undefined")
) {
fs.unlinkSync("pending-txs.json");
console.log("Retrying " + (counter + 1) + " after a few moments...");
return reject(error);
}
console.error(`Error in mint command: ${error.message}`);
if (!isPending) {
txidMatch = stdout.match(/txid: (\w+)/);
isPending = true;
}
return reject(error);
}
if (!isPending) {
txidMatch = stdout.match(/txid: (\w+)/);
}
if (txidMatch) {
const txid = txidMatch[1];
console.log(
"Successful mint, updating JSON file, continuing in 60 sec...."
);
isPending = false;
if (!fs.existsSync("pending-txs.json")) {
updateJsonFile(imgPath, txid).then(resolve).catch(reject);
} else {
fs.unlinkSync("pending-txs.json");
console.log("Error with pending-txs.json");
console.log("Retrying " + (counter + 1) + " after a few moments...");
return reject(
new Error("Error with pending-txs.json, retrying " + (counter + 1))
);
}
} else if (stderr.match("Cannot read properties of undefined")) {
fs.unlinkSync("pending-txs.json");
console.log("Retrying " + (counter + 1) + " after a few moments...");
isPending = false;
return reject(error);
} else {
console.log("Unknown response, stopping the loop.");
reject(new Error("Unknown response"));
}
});
});
}
function syncWallet() {
return new Promise((resolve, reject) => {
const syncCommand = `node bellinals wallet sync`;
exec(syncCommand, (error, stdout, stderr) => {
if (error) {
console.error(`Error in wallet sync command: ${error.message}`);
console.error(`Command: ${syncCommand}`);
console.error(`Stdout: ${stdout}`);
console.error(`Stderr: ${stderr}`);
return reject(error);
}
console.log(`${stdout}`);
if (stdout.match("inscription complete continue to next file.")) {
const txid = txidMatch[1];
isPending = false;
if (!fs.existsSync("pending-txs.json")) {
updateJsonFile(imgPath, txid)
.then(() => {
commands.shift();
resolve();
})
.catch((err) => {
console.error(`Error updating JSON file: ${err.message}`);
return reject(err);
});
} else {
return reject(
new Error("Error with pending-txs.json, retrying " + (counter + 1))
);
}
} else {
resolve();
}
});
});
}
function updateJsonFile(imagePath, txid) {
return new Promise((resolve, reject) => {
const jsonDir = "build";
if (!fs.existsSync(jsonDir)) {
fs.mkdirSync(jsonDir, { recursive: true });
}
const jsonFileName = path.join(jsonDir, "inscriptions.json");
let data = [];
if (fs.existsSync(jsonFileName)) {
data = JSON.parse(fs.readFileSync(jsonFileName, "utf-8"));
}
data[counter].id = txid + "i0";
counter++;
console.log(
"Updated metadata",
counter,
"for",
imagePath,
"with id",
txid + "i0"
);
fs.writeFileSync(jsonFileName, JSON.stringify(data, null, 4));
resolve();
});
}
runNodeCommands(start, end, directory, filePrefix, fileExtension);