-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
61 lines (49 loc) · 1.47 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
const seedrandom = require('seedrandom')
const fs = require('fs')
//https://twitter.com/thefakesama/status/1633593993895243778
const rng = seedrandom('696969')
const ethers = require('ethers')
const list = JSON.parse(fs.readFileSync('staking_results.json', 'utf8'))
const winners = {}
// extracting addresses and weights list
const addresses = []
const weights = []
list.filter(item => item.safari_tokens_total !== '0').map((item) => {
addresses.push(item.wallet_address)
weights.push(Math.round(ethers.formatUnits(item.safari_tokens_total, 'gwei')))
winners[item.wallet_address] = 0
})
// cumulating weights
const cumulated = weights.slice()
for (let i = 0; i < cumulated.length; i++) {
cumulated[i] += cumulated[i - 1] || 0;
}
console.log(addresses.length, weights.length, cumulated.length)
// drawing
const draws = []
for (let round = 0; round < 350; round++) {
const rnd = rng()
let random = rnd * cumulated[cumulated.length - 1];
let i = 0
for (i; i < cumulated.length; i++)
if (cumulated[i] > random)
break;
draws.push(i)
}
// counting winners
draws.map((draw) => {
const addy = addresses[draw]
if (winners[addy]) {
winners[addy]++
} else {
winners[addy] = 1
}
})
// double check
let total = 0
Object.keys(winners).map((addy) => {
total += winners[addy]
})
console.log('Total samples:', total)
fs.writeFileSync('winners.json', JSON.stringify(winners, null, 2))
console.log('./winners.json')