forked from DimensionDev/MysteryBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.ts
282 lines (263 loc) · 10.7 KB
/
helper.ts
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
import { providers, utils, Wallet, Contract, BigNumber } from "ethers";
import * as readline from "readline";
import * as fs from "fs";
import { getGasPrice } from "./utils";
import { generateCreateBoxPara, openBoxParameters, seconds_in_a_day } from "./test/constants";
import { project_secret } from "./project.secret";
// TODO: organize these into a config file
import MysteryBoxArtifact from "./abi/MysteryBox.json";
let MysteryBoxAddress: string;
let MysteryBoxApp: Contract;
import NFTArtifact from "./abi/MaskEnumerableNFT.json";
let NFTAddress: string;
let NFTApp: Contract;
// rinkeby
const whitelistContractAddr = "0x50eCEebb7360Efb93094dDEA692e04274E548b1d";
import whitelistArtifact from "./abi/WhitelistQlf.json";
let whitelistApp: Contract;
let NetworkProvider: providers.Provider;
let adminWallet: Wallet;
let testWallet_0: Wallet;
let testWallet_1: Wallet;
// const network = 'rinkeby';
let network = "matic_mainnet";
const transactionParameters = {
gasLimit: BigNumber.from(6000000),
gasPrice: utils.parseUnits("1", "gwei"),
};
const txParameters = {
gasLimit: BigNumber.from(6000000),
value: BigNumber.from(0),
};
const createBoxParameter = generateCreateBoxPara(network);
async function isContractAddress(addr) {
{
const mainnetProvider = new providers.JsonRpcProvider(
"https://mainnet.infura.io/v3/" + project_secret.infura_project_id,
);
const code = await mainnetProvider.getCode(addr);
if (code != "0x") {
console.log(addr + " is a contract, ETH");
return true;
}
}
{
const bscProvider = new providers.JsonRpcProvider("https://bsc-dataseed1.binance.org:443");
const code = await bscProvider.getCode(addr);
if (code != "0x") {
console.log(addr + " is a contract, ETH");
return true;
}
}
{
const polygonProvider = new providers.JsonRpcProvider("https://polygon-rpc.com/");
const code = await polygonProvider.getCode(addr);
if (code != "0x") {
console.log(addr + " is a contract, ETH");
return true;
}
}
return false;
}
async function readFirstColumn(filePath, result) {
const readInterface = readline.createInterface({
input: fs.createReadStream(filePath),
// output: process.stdout,
// TODO: `console` option not found in `readline.createInterface`
// console: false,
});
let lines: string[] = [];
for await (const line of readInterface) {
lines.push(line);
}
// console.log('total: ' + lines.length);
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const words = line.split(" ");
result.push(words[0]);
}
}
async function main() {
if (network === "mainnet") {
// set gas price carefully, it is expensive.
// transactionParameters.nonce = 3440;
// console.log("Force Nonce: " + transactionParameters.nonce);
// transactionParameters.gasPrice = utils.parseUnits('25', 'gwei');
const gasPriceList = await getGasPrice(false);
console.log("Ethereum standard gas price: " + utils.formatUnits(gasPriceList.standard, "gwei") + " Gwei");
transactionParameters.gasPrice = gasPriceList.standard;
/*
NetworkProvider = new providers.JsonRpcProvider(
'https://mainnet.infura.io/v3/' + project_secret.infura_project_id,
);
*/
NetworkProvider = new providers.WebSocketProvider(
"wss://ropsten.infura.io/ws/v3/" + project_secret.infura_project_id,
);
} else if (network === "rinkeby") {
{
const deploymentInfo = require("./.openzeppelin/rinkeby.json");
if (deploymentInfo.proxies.length < 1) {
throw "project not deployed properly";
}
NFTAddress = "0x0c8FB5C985E00fb1D002b6B9700084492Fb4B9A8";
const lastOne = deploymentInfo.proxies.length - 1;
MysteryBoxAddress = deploymentInfo.proxies[lastOne].address;
}
NetworkProvider = new providers.JsonRpcProvider("https://rinkeby.infura.io/v3/" + project_secret.infura_project_id);
} else if (network === "matic_mainnet") {
{
const deploymentInfo = require("./.openzeppelin/unknown-31337.json");
if (deploymentInfo.proxies.length < 1) {
throw "project not deployed properly";
}
NFTAddress = "0x49C2a3D93C4B94eAd101d9936f1ebCA634394a78";
const lastOne = deploymentInfo.proxies.length - 1;
MysteryBoxAddress = deploymentInfo.proxies[lastOne].address;
}
NetworkProvider = new providers.JsonRpcProvider("https://polygon-rpc.com/");
} else {
//
NetworkProvider = new providers.JsonRpcProvider("https://xxxx.infura.io/v3/" + project_secret.infura_project_id);
}
adminWallet = new Wallet(project_secret.test_private_key[0], NetworkProvider);
testWallet_0 = new Wallet(project_secret.test_private_key[1], NetworkProvider);
testWallet_1 = new Wallet(project_secret.test_private_key[2], NetworkProvider);
MysteryBoxApp = new Contract(MysteryBoxAddress, MysteryBoxArtifact, adminWallet);
NFTApp = new Contract(NFTAddress, NFTArtifact, adminWallet);
whitelistApp = new Contract(whitelistContractAddr, whitelistArtifact, adminWallet);
console.log("MysteryBoxAddress: " + MysteryBoxAddress);
console.log("network: " + network + " adminWallet address: " + adminWallet.address);
console.log("network: " + network + " testWallet_0 address: " + testWallet_0.address);
console.log("network: " + network + " testWallet_1 address: " + testWallet_1.address);
const action = process.argv[2];
if (action === "create") {
const isApproved = await NFTApp.isApprovedForAll(testWallet_0.address, MysteryBoxApp.address);
if (!isApproved) {
const tx = await NFTApp.connect(testWallet_0).setApprovalForAll(MysteryBoxApp.address, true);
await tx.wait();
}
const tx = await MysteryBoxApp.connect(testWallet_0).createBox(...Object.values(createBoxParameter));
await tx.wait();
} else if (action === "open") {
const box_id = BigNumber.from(process.argv[3]);
openBoxParameters.box_id = box_id;
txParameters.value = createBoxParameter.payment[0][1];
const tx = await MysteryBoxApp.connect(testWallet_1).openBox(...Object.values(openBoxParameters), txParameters);
await tx.wait();
} else if (action === "test") {
{
const info = await MysteryBoxApp.getBoxInfo(1);
console.log(JSON.stringify(info, null, 2));
}
} else if (action === "addAdmin") {
const adminList = [
"0x3A6690B247b467243F4C2F61Dd4100e18a336990",
"0x67fA392717324B63Cb3793860eA099C1436e6458",
"0xaa0065375E1194d45Ede6804AA9d7e01a326aaDD",
"0x790116d0685eB197B886DAcAD9C247f785987A4a",
"0x67fA392717324B63Cb3793860eA099C1436e6458",
"0x13Fe3f4F1385F9CF4C7bC766F1E9B5AF028f9B73",
];
const tx = await MysteryBoxApp.connect(adminWallet).addAdmin(adminList);
const receipt = await tx.wait();
} else if (action === "removeAdmin") {
const adminList = ["0xD5C2c5f2E10802846bbD86C5Fd8438b7CA4Ff83A"];
const tx = await MysteryBoxApp.connect(adminWallet).removeAdmin(adminList);
const receipt = await tx.wait();
} else if (action === "set_mask_addr") {
const mask_addr = "0x46eD2e50A9f27de0dC47b04E7580E8E91fCE7246";
const tx = await MysteryBoxApp.connect(adminWallet).setMaskTokenAddr(mask_addr);
await tx.wait();
} else if (action === "create_invalid") {
const now = Math.floor(new Date().getTime() / 1000);
if (true) {
createBoxParameter.start_time = now + 120 * seconds_in_a_day;
const tx = await MysteryBoxApp.connect(testWallet_0).createBox(...Object.values(createBoxParameter));
const receipt = await tx.wait();
console.log(receipt);
}
if (true) {
createBoxParameter.start_time = 0;
createBoxParameter.end_time = now + 60;
const tx = await MysteryBoxApp.connect(testWallet_0).createBox(...Object.values(createBoxParameter));
const receipt = await tx.wait();
console.log(receipt);
}
} else if (action === "open_invalid") {
txParameters.value = createBoxParameter.payment[0][1];
openBoxParameters.box_id = BigNumber.from(3);
const tx = await MysteryBoxApp.connect(testWallet_1).openBox(...Object.values(openBoxParameters), txParameters);
await tx.wait();
} else if (action === "set_resource") {
const resource_list = [
"https://raw.githubusercontent.com/andy-at-mask/MysteryBoxAsset/master/json/default.json",
"https://raw.githubusercontent.com/andy-at-mask/MysteryBoxAsset/master/json/gif.json",
"https://raw.githubusercontent.com/andy-at-mask/MysteryBoxAsset/master/json/mp4.json",
];
for (let i = 0; i < resource_list.length; i++) {
console.log("setting: " + i + " URI: " + resource_list[i]);
const tx = await NFTApp.set_resource(i, resource_list[i]);
await tx.wait();
}
} else if (action === "write_whitelist") {
const batch_number = 450;
const wallet_list = [];
const whitelist_file = "./scripts/mask_holder.txt";
await readFirstColumn(whitelist_file, wallet_list);
const blacklist = [];
const blacklist_file = "./scripts/blacklist.txt";
await readFirstColumn(blacklist_file, blacklist);
// console.log(blacklist);
let whitelist = [];
console.log("total: " + wallet_list.length);
for (let i = 0; i < wallet_list.length; i++) {
const addr = wallet_list[i];
if (blacklist_file.indexOf(addr) >= 0) {
console.log(addr + " in blacklist");
continue;
}
// const whitelisted = await whitelistApp.white_list(addr);
// if (whitelisted) {
// console.log(addr + " whitelisted already");
// continue;
// }
whitelist.push(addr);
if (whitelist.length >= batch_number) {
console.log("Writing: " + whitelist.length + " addresses");
const tx = await whitelistApp.add_white_list(whitelist);
await tx.wait();
whitelist = [];
console.log("whitelist writing done!!");
}
}
console.log("Writing: " + whitelist.length + " addresses");
const tx = await whitelistApp.add_white_list(whitelist);
await tx.wait();
} else if (action === "check_whitelist") {
const wallet_list = [];
const whitelist_file = "./scripts/mask_holder.txt";
await readFirstColumn(whitelist_file, wallet_list);
const blacklist = [];
const blacklist_file = "./scripts/blacklist.txt";
await readFirstColumn(blacklist_file, blacklist);
// console.log(blacklist);
const whitelist = [];
console.log("total: " + wallet_list.length);
for (let i = 0; i < wallet_list.length; i++) {
const addr = wallet_list[i];
if (blacklist_file.indexOf(addr) >= 0) {
// console.log(addr + ' in blacklist');
continue;
}
const whitelisted = await whitelistApp.white_list(addr);
console.log("checking: " + addr);
if (!whitelisted) {
console.log(addr + " NOT whitelisted");
}
}
} else {
throw "unknown command option";
}
}
main().then(function () {});