-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
337 lines (306 loc) · 8.68 KB
/
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
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
require("dotenv").config();
const { promises, stat } = require("fs");
const config = require("./config.json");
const {
deploy,
upgrade,
getSmartContract,
signAndSendTx,
} = require("./common");
let chalk;
import("chalk").then((module) => {
chalk = module.default;
});
const stakingModuleTypes = [
"CodingDivisionSfts",
"Bloodshed",
"Nosferatu",
"VestaXDAO",
"SnakesSfts",
"SharesSfts",
"XBunnies",
];
const readline = require("readline");
const {
TokenIdentifierValue,
U8Value,
U32Value,
U64Value,
} = require("@multiversx/sdk-core/out");
const run = () => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question(
"Please choose an option: \n 1. Deploy \n 2. Upgrade \n 3. Register new staking pool \n 4. Configure pool scores\n> ",
async (option) => {
switch (option) {
case "1":
await deploy();
rl.close();
break;
case "2":
await upgrade();
rl.close();
break;
case "3":
rl.question(
"Enter NFT/SFT collection token identifier: ",
async (tokenIdentifier) => {
console.log("Please select a staking module type: ");
stakingModuleTypes.forEach((moduleType, index) =>
console.log(`${index + 1}. ${moduleType}`)
);
rl.question(
"Enter the number corresponding to the module type: ",
async (moduleType) => {
await handleRegisterNewStakingPool(
tokenIdentifier,
moduleType
);
rl.close();
}
);
}
);
break;
case "4":
rl.question(".CSV configuration file path: ", async (filePath) => {
rl.question(
"Does .CSV file include headers? (Y/N): ",
async (includesHeaders) => {
const hasHeaders = includesHeaders.toLowerCase() === "y";
await handleConfigurePoolScores(filePath, hasHeaders);
rl.close();
}
);
});
break;
default:
console.log("Invalid option");
rl.close();
}
}
);
};
const handleRegisterNewStakingPool = async (tokenIdentifier, moduleType) => {
let message = chalk.white("Registering new staking pool for ");
message += chalk.yellow(tokenIdentifier);
message += chalk.white(" with module type ");
message += chalk.yellow(stakingModuleTypes[moduleType - 1]);
message += chalk.white("... ");
process.stdout.write(message + "\n");
let contract = await getSmartContract();
let tx = contract.methodsExplicit
.createPool([
new TokenIdentifierValue(tokenIdentifier),
new U8Value(parseInt(moduleType) + 1),
])
.withChainID("D")
.withGasLimit(15_000_000);
let status = await signAndSendTx(tx);
message += status ? chalk.green("SUCCESS") : chalk.red("FAILED");
process.stdout.write(message + "\n");
};
const handleConfigurePoolScores = async (filePath, hasHeaders) => {
let fileContent = await promises.readFile(filePath, "utf8");
const lines = fileContent
.split("\n")
.filter((_, index) => (hasHeaders ? index > 0 : true))
.map((line) => line.trim());
console.log(`${lines.length} transactions to be sent`);
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
try {
await handleConfigureScore(line);
} catch (error) {
console.error(`Error processing line ${i}: ${error}`);
}
}
};
const handleConfigureScore = async (row) => {
let columns = row.split(",");
let tokenIdentifier = columns[0];
let stakingModuleType = columns[1] === "" ? 1 : parseInt(columns[1]);
let nonces = columns[2].length === 0 ? [] : columns[2].split(";");
let nonceRangeStart = columns[3] === "" ? undefined : columns[3];
let nonceRangeEnd = columns[4] === "" ? undefined : columns[4];
let score = columns[5];
let fullSetBonusScore = columns[6] === "" ? undefined : columns[6];
process.stdout.write(
getPoolRowMessage(
tokenIdentifier,
stakingModuleType,
nonces,
nonceRangeStart,
nonceRangeEnd,
score,
fullSetBonusScore
)
);
process.stdout.write("\n");
let status = false;
if (
nonces.length === 0 &&
nonceRangeStart === undefined &&
nonceRangeEnd === undefined
) {
status = await sendSetBaseAssetScoreTx(
tokenIdentifier,
stakingModuleType,
score
);
}
if (nonces.length > 0) {
status = await sendSetNonceAssetScoreTx(
tokenIdentifier,
stakingModuleType,
nonces,
score
);
}
if (nonceRangeStart !== undefined && nonceRangeEnd !== undefined) {
status = await sendSetNonceAssetScoreByRangeTx(
tokenIdentifier,
stakingModuleType,
nonceRangeStart,
nonceRangeEnd,
score
);
}
if (fullSetBonusScore !== undefined) {
let fullSetScoreResult = await sendSetFullSetBonusScoreTx(
tokenIdentifier,
stakingModuleType,
fullSetBonusScore
);
status = status && fullSetScoreResult;
}
// process.stdout.write(status ? chalk.green("SUCCESS") : chalk.red("FAILED"));
// process.stdout.write("\n");
process.stdout.write(
getPoolRowMessage(
tokenIdentifier,
stakingModuleType,
nonces,
nonceRangeStart,
nonceRangeEnd,
score,
fullSetBonusScore,
status
) + "\n"
);
};
const sendSetBaseAssetScoreTx = async (
tokenIdentifier,
stakingModuleType,
score
) => {
let contract = await getSmartContract();
let tx = contract.methodsExplicit
.setBaseAssetScore([
new TokenIdentifierValue(tokenIdentifier),
new U32Value(stakingModuleType),
new U32Value(score),
])
.withGasLimit(15_000_000);
return await signAndSendTx(tx);
};
const sendSetNonceAssetScoreTx = async (
tokenIdentifier,
stakingModuleType,
nonces,
score
) => {
let contract = await getSmartContract();
let noncesArg = nonces.map((nonce) => new U64Value(nonce));
let tx = contract.methodsExplicit
.setNonceAssetScore(
[
new TokenIdentifierValue(tokenIdentifier),
new U8Value(stakingModuleType),
new U32Value(score),
].concat(noncesArg)
)
.withGasLimit(15_000_000 + nonces.length * 100_000);
return await signAndSendTx(tx);
};
const sendSetNonceAssetScoreByRangeTx = async (
tokenIdentifier,
stakingModuleType,
nonceRangeStart,
nonceRangeEnd,
score
) => {
let contract = await getSmartContract();
let gasLimit = 15_000_000 + (nonceRangeEnd - nonceRangeStart) * 100_000;
let tx = contract.methodsExplicit
.setNonceAssetScoreByRange([
new TokenIdentifierValue(tokenIdentifier),
new U8Value(stakingModuleType),
new U32Value(score),
new U64Value(nonceRangeStart),
new U64Value(nonceRangeEnd),
])
.withGasLimit(gasLimit > 600_000_000 ? 600_000_000 : gasLimit);
return await signAndSendTx(tx);
};
const sendSetFullSetBonusScoreTx = async (
tokenIdentifier,
stakingModuleType,
score
) => {
let contract = await getSmartContract();
let tx = contract.methodsExplicit
.setFullSetScore([
new TokenIdentifierValue(tokenIdentifier),
new U8Value(stakingModuleType),
new U32Value(score),
])
.withGasLimit(15_000_000);
return await signAndSendTx(tx);
};
const getPoolRowMessage = (
tokenIdentifier,
stakingModuleType,
nonces,
nonceRangeStart,
nonceRangeEnd,
score,
fullSetBonusScore,
statusSuccessful
) => {
let statusMessage = chalk.white("Setting up ");
statusMessage += chalk.yellow(tokenIdentifier);
statusMessage += chalk.white(", module type ");
statusMessage += chalk.yellow(stakingModuleType);
if (nonces.length > 0) {
statusMessage += chalk.white(" for ");
statusMessage += chalk.yellow(nonces.join(", "));
statusMessage += chalk.white(" nonces");
}
if (nonceRangeStart !== undefined && nonceRangeEnd !== undefined) {
statusMessage += chalk.white(" for nonces between ");
statusMessage += chalk.yellow(nonceRangeStart);
statusMessage += chalk.white(" and ");
statusMessage += chalk.yellow(nonceRangeEnd);
}
statusMessage += chalk.white(" with score ");
statusMessage += chalk.yellow(score);
if (fullSetBonusScore !== undefined) {
statusMessage += chalk.white(" and full set bonus score ");
statusMessage += chalk.yellow(fullSetBonusScore);
}
statusMessage += chalk.white("... ");
if (statusSuccessful === undefined) {
return statusMessage;
}
if (statusSuccessful) {
statusMessage += chalk.green("SUCCESS");
} else {
statusMessage += chalk.red("FAILED");
}
return statusMessage;
};
run();