-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistribute.js
More file actions
102 lines (84 loc) · 2.45 KB
/
Copy pathdistribute.js
File metadata and controls
102 lines (84 loc) · 2.45 KB
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
const { ethers } = require('ethers');
const fs = require('fs');
const _ = require("lodash");
const sourceCsv = process.argv[2]
const targetCsv = process.argv[3]
if (process.argv.length !== 4) {
console.error("Must specify 2 arguments <source.csv> and <target.csv>")
process.exit(1)
}
if (!fs.existsSync(sourceCsv)) {
console.error(`Source file does not exist: ${sourceCsv}`)
process.exit(1)
}
if (fs.existsSync(targetCsv)) {
console.error(`Target file already exists: ${sourceCsv}`)
process.exit(1)
}
let distribution = [
{
tokenId: 1,
amount: 817
},
{
tokenId: 2,
amount: 817
},
{
tokenId: 3,
amount: 817
},
]
async function readCsv() {
const fileContent = fs.readFileSync(sourceCsv).toString()
const lines = fileContent.split('\n')
const seenAddresses = new Set()
const addresses = []
let totalCount = 0
for (let line of lines) {
line = line.trim();
if (!line) {
console.warn("Empty line when parsing CSV, skipping...")
continue
}
const [address,tokenId, amount] = line.split(',')
const checksumAddress = ethers.getAddress(address);
totalCount++;
if (seenAddresses.has(checksumAddress))
continue;
seenAddresses.add(checksumAddress)
addresses.push(address)
}
return addresses;
}
function getTotalDistribution() {
let count = 0;
for (const token of distribution)
count += token.amount
return count;
}
async function distribute() {
const addresses = await readCsv()
const totalDistributionCount = getTotalDistribution()
if (addresses.length !== totalDistributionCount) {
console.error(`Found ${addresses.length} addresses but total distribution count was ${totalDistributionCount}`)
process.exit(1)
}
console.log(`Address and total distribution count match (${totalDistributionCount})!`)
const shuffledAddresses = _.shuffle(addresses)
const lines = []
for (const addr of shuffledAddresses) {
const tokenId = distribution[0].tokenId
distribution[0].amount--
if (distribution[0].amount === 0)
distribution.shift()
lines.push([addr, tokenId])
}
const shuffledLines = _.shuffle(lines)
let csv = ''
for (const line of shuffledLines) {
csv += `${line[0]},${line[1]}\n`
}
fs.writeFileSync(targetCsv, csv)
}
distribute()