-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.js
180 lines (155 loc) · 4.53 KB
/
generate.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
const { Asset, Event } = require('./db');
const topCollections = require("./topCollection200.json");
const { getTokenWithRarity } = require('./utils/rarity');
const fs = require('fs');
const { createObjectCsvWriter: createCsvWriter } = require('csv-writer');
const MINT_ADDRESS = '0x0000000000000000000000000000000000000000';
async function saveFile(dataRows, dataFile) {
const firstRow = dataRows[0];
const header = Object.keys(firstRow).map(_ => {
return {
id: _,
title: _
}
}).filter(_ => _.id != 'TOKEN_ID');
header.unshift({
id: 'TOKEN_ID',
title: 'TOKEN_ID'
})
const csvWriter = createCsvWriter({
path: dataFile,
header: header
});
await csvWriter.writeRecords(dataRows)
console.log('rows', dataRows.length, firstRow)
}
async function runKsTest(slug) {
try {
var exec = require("child_process").exec;
const comand = `python3 ks_test.py custom ${slug}`;
console.log("run", comand);
exec(
comand,
{
cwd: __dirname + "/analytics/",
},
function (error, stdout, stderr) {
stdout.on("data", (buf) => console.log(buf.toString()));
}
);
} catch (e) {}
}
async function generateTokenData(collection) {
const datasetbaseDir = `./dataset/${collection.slug}`;
const dataFile = `${datasetbaseDir}/minting.csv`
let allTokens = await Asset.findAll({
where: {
collection: collection.slug
},
raw: true
});
allTokens = allTokens
.map((_) => {
_.traits = JSON.parse(_.traits);
return _;
})
.filter((_) => _.traits.length);
if (allTokens.length == 0) {
console.log('traits is empty')
if (fs.existsSync(dataFile)) fs.unlinkSync(dataFile);
return
}
const tokenRarityMap = {};
const tokensWithRarity = getTokenWithRarity(allTokens);
tokensWithRarity.forEach((_) => {
tokenRarityMap[_.token_id] = {
rarity_rank: _.rarity_rank,
rarity_score: _.rarity_score
}
})
const allEvents = await Event.findAll({
where: {
event_type: 'transfer',
collection_slug: collection.slug
},
raw: true
});
const allSaleEvents = await Event.findAll({
where: {
event_type: 'successful',
collection_slug: collection.slug
},
raw: true
});
const fetchedTokens = allTokens.filter(_ => _.fetched == 1)
const mintEvents = allEvents.filter(_ => _.from_account && _.from_account == MINT_ADDRESS)
console.log({
slug: collection.slug,
fetchedTokens : fetchedTokens.length,
allEvents: allEvents.length,
mintEvents: mintEvents.length,
allTokens: allTokens.length,
allSaleEvents: allSaleEvents.length
});
if (!fs.existsSync(datasetbaseDir)) {
fs.mkdirSync(datasetbaseDir);
}
const uniqueMintSet = new Set();
const mintingRows = mintEvents
.reduce((total, item) => {
const rarity = tokenRarityMap[item.token_id];
const row = {
txid: item.transaction,
to_account: item.to_account,
TOKEN_ID: item.token_id,
current_owner: item.owner,
rank: rarity ? rarity.rarity_rank : null,
time: item.timestamp,
};
if (!uniqueMintSet.has(item.token_id)) {
total.push(row);
uniqueMintSet.add(item.token_id);
}
return total;
}, [])
.filter((_) => _.rank);
if (mintingRows.length) {
await saveFile(mintingRows, dataFile)
runKsTest(collection.slug);
}
if (allSaleEvents.length) {
const saleWithRarity = allSaleEvents.map(item => {
const rarity = tokenRarityMap[item.token_id];
const row = {
txid: item.transaction,
seller: item.seller,
winner_account: item.winner_account,
TOKEN_ID: item.token_id,
current_owner: item.owner,
rank: rarity ? rarity.rarity_rank : null,
time: item.timestamp,
payment_token: item.payment_token,
price: item.price,
};
return row
}).filter(_ => _.rank)
await saveFile(saleWithRarity, `${datasetbaseDir}/sales.csv`)
}
}
async function main() {
for (let index = 0; index < topCollections.length; index++) {
const topCollection = topCollections[index];
if (topCollection.stats.totalSupply > 20000 && ['lostpoets','adam-bomb-squad', 'emblem-vault'].indexOf(topCollection.slug) == -1) {
console.log('skip')
continue;
}
if (['decentraland-wearables', 'cryptokitties', 'decentraland', 'parallelalpha'].indexOf(topCollection.slug) > -1) {
continue;
}
await generateTokenData(topCollection);
}
}
module.exports = {
main,
generateTokenData,
};