-
Notifications
You must be signed in to change notification settings - Fork 0
/
catch_smart_addr.js
103 lines (94 loc) · 2.92 KB
/
catch_smart_addr.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
// 1. 加载金狗的全部交易(csv)
// 2. 过滤 public mint 的address
// 3. 去重
// 4. 写入数据库进行记录
// 金狗项目名,与 csv 文件相同
const { collection } = require("minimist")(process.argv.slice(2));
const { method } = require("minimist")(process.argv.slice(3));
const csvFile = __dirname + "/inputs/transactions/" + collection + ".csv";
// 初始化 csv 解析器
const fs = require("fs");
const parser = require("csv-parse");
// 数据库相关
const dbFile = __dirname + "/dbs/mint.db";
let db;
let collectionAddress;
const initDb = async () => {
console.log("Start initializing database.");
const sqlite3 = require("sqlite3").verbose();
const { open } = require("sqlite");
db = await open({
filename: dbFile,
driver: sqlite3.Database,
});
console.log("Finish initializing database.");
};
const filterCsv = () => {
return new Promise((resolve, reject) => {
console.log("Start parsing csv file.");
const addressList = [];
fs.createReadStream(csvFile)
.pipe(
parser.parse({
columns: true,
delimiter: ",",
trim: true,
})
)
.on("error", reject)
.on("data", function (csvrow) {
// 保留 public mint 的addr
methodArr = method.split("|");
if (
-1 != methodArr.indexOf(csvrow["Method"]) &&
csvrow["Status"] == ""
) {
//console.log(csvrow["From"]);
addressList.push(csvrow["From"]);
if (collectionAddress == null) {
collectionAddress = csvrow["To"];
}
}
})
.on("end", function () {
console.log("Finish parsing csv file.");
// 去重
const records = [...new Set(addressList)];
resolve(records);
});
});
};
const save = async (records) => {
records.forEach((addr, index) => {
sql =
"insert into tb_collection_address (collection, collection_address, address, status) values ('" +
collection +
"', '" +
collectionAddress.toLowerCase() +
"', '" +
addr.toLowerCase() +
"', 0)";
console.log(sql);
runSql(sql);
});
};
const runSql = async (sql) => {
db.run(sql, (err) => {
if (null != err) {
console.log(err);
process.exit();
}
});
};
const main = async () => {
console.log("Start catching smart address");
// 初始化数据库
await initDb();
// 处理 csv 文件
const records = await filterCsv();
console.log(records);
// 写入数据库
await save(records);
console.log("Finish catching smart address");
};
main();