forked from h4rkl/Ghetto-SolAir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (100 loc) · 2.93 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
const fs = require("fs");
const _ = require("lodash");
const Spreadsheet = require("@moreavy/simple-csv-tools");
const { PublicKey } = require("@solana/web3.js");
const ObjectsToCsv = require('objects-to-csv');
const deleteFile = (filepath) => {
fs.unlink(filepath, function (err) {
if (err) return console.log(err);
console.log('Deleted');
});
}
/**
* Turns a raw.csv file into a cleaned.json file
* contianing an array of key/vale pairs
*/
const csvToJSON = () => {
const sheet = new Spreadsheet("data/raw.csv");
const data = sheet.toArr("data/raw.json");
const head = ['timestamp','handle','address'];
const kvData = [];
_.forEach(data, (value) => {
var result = {};
for (var i = 0; i < head.length; i++) {
result[head[i]] = value[i].trim();
}
kvData.push(result);
});
const c = kvData.length;
deleteFile('data/cleaned.json');
fs.appendFile('data/cleaned.json', JSON.stringify(kvData), function (err) {
if (err) return console.log(err);
console.log(`Converted ${c} records`);
});
};
/**
* Removes all keys that aren't alpha-numeris and 43/44 chars in length
* Eg. base58 Solana keys
*/
const removeOffCurveKeys = async () => {
const d = require("./data/cleaned.json");
const curved = _.filter(d, v => {
// base58 only has upper, lower and numeric
const base58Str = v.address.replace(/\W+/g,'').replace(/\s+/g, "");
// Solana keys are 43/44 chars in length
if (base58Str.length === 43 || base58Str.length === 44) {
const key = new PublicKey(base58Str);
return PublicKey.isOnCurve(key);
}
return null;
});
console.log(`Remaining records: ${curved.length}`);
deleteFile('data/curved.json');
fs.appendFile('data/curved.json', JSON.stringify(curved), function (err) {
if (err) return console.log(err);
console.log('Curved');
});
};
/**
* Removes all the duplicates from the curved.json file by data.key
* @param {string} key
*/
const removeDupesByKey = async (key) => {
let d = require("./data/curved.json");
d = d.filter((value, index, self) =>
index === self.findIndex((t) => (
t[key] === value[key]
))
);
console.log(`There are ${d.length} addresses in the list`);
deleteFile('data/curved.json');
fs.appendFile('data/curved.json', JSON.stringify(d), function (err) {
if (err) return console.log(err);
console.log('Keyed the curve');
});
};
const bulkTokenCSV = async (amount, token) => {
const d = require("./data/curved.json");
const bulkData = [];
_.map(d, v => {
bulkData.push({
Address: v.address,
Amount: amount,
Coin: token
});
});
console.log(`There are ${bulkData.length} addresses in the list`);
deleteFile('data/bulkData.csv');
const csv = new ObjectsToCsv(bulkData);
await csv.toDisk('./data/bulkData.csv');
};
// csvToJSON();
// removeOffCurveKeys();
// bulkTokenCSV();
module.exports = {
deleteFile,
csvToJSON,
removeOffCurveKeys,
removeDupesByKey,
bulkTokenCSV
}