-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdistribution.go
516 lines (488 loc) · 17 KB
/
distribution.go
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
package main
import (
"fmt"
"os"
"slices"
"github.com/go-echarts/go-echarts/v2/charts"
"github.com/go-echarts/go-echarts/v2/components"
"github.com/go-echarts/go-echarts/v2/opts"
"github.com/pkg/browser"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
)
// Some constants
var (
// list of ICF wallets
icfWallets = []string{
// Source https://github.com/gnolang/bounties/issues/18#issuecomment-1034700230
"cosmos1z8mzakma7vnaajysmtkwt4wgjqr2m84tzvyfkz",
"cosmos1unc788q8md2jymsns24eyhua58palg5kc7cstv",
// The 2 addresses above have been emptied in favour of the following 2
"cosmos1sufkm72dw7ua9crpfhhp0dqpyuggtlhdse98e7",
"cosmos1z6czaavlk6kjd48rpf58kqqw9ssad2uaxnazgl",
// From other investigations
"cosmos17u903qxqc6dzn3chvmc9zzp9fl4xja0pwggfj7",
}
)
type airdrop struct {
// params hold the distribution parameters that resulted in this airdrop
params distriParams
// addresses contains the airdrop amount per address.
addresses map[string]sdk.Int
addressesDetail []addrAmtDetail
// nonVotersMultiplier ensures that non-voters don't hold more than 1/3 of
// the supply
nonVotersMultiplier sdk.Dec
// $ATOM distribution
atom distrib
// $ATONE distribution
atone distrib
// Amount of $ATOM slashed for the ICF
icfSlash sdk.Dec
// Amount minted for CP
communityPool sdk.Dec
// Amount minted for reserved address
reservedAddr sdk.Dec
}
type addrAmtDetail struct {
Address string `json:"address"`
YesDetail amtDetail `json:"yesDetail"`
NoDetail amtDetail `json:"noDetail"`
NWVDetail amtDetail `json:"nwvDetail"`
AbsDetail amtDetail `json:"absDetail"`
DnvDetail amtDetail `json:"dnvDetail"`
LiquidDetail amtDetail `json:"liquidDetail"`
Total sdk.Dec `json:"total"`
}
type amtDetail struct {
AtomAmt sdk.Dec `json:"atomAmt"`
Multiplier sdk.Dec `json:"multiplier"`
BonusMalus sdk.Dec `json:"bonusMalus"`
Factor sdk.Dec `json:"factor"`
AtoneAmt sdk.Dec `json:"atoneAmt"`
}
type distrib struct {
// total supply of the distrib
supply sdk.Dec
// votes holds the part of the distrib per vote.
votes voteMap
// unstaked is part of the distrib for unstaked amounts.
unstaked sdk.Dec
}
type distriParams struct {
yesVotesMultiplier sdk.Dec
noVotesMultiplier sdk.Dec
bonus sdk.Dec
malus sdk.Dec
supplyFactor sdk.Dec
supplyMintFactor sdk.Dec
}
func (d distriParams) String() string {
return fmt.Sprintf("Yes x%.1f / No x%.1f",
d.yesVotesMultiplier.MustFloat64(), d.noVotesMultiplier.MustFloat64())
}
func defaultDistriParams() distriParams {
return distriParams{
yesVotesMultiplier: sdk.OneDec(), // Y get x1
noVotesMultiplier: sdk.NewDec(9), // N & NWV get x9
bonus: sdk.NewDecWithPrec(103, 2), // 3% bonus
malus: sdk.NewDecWithPrec(97, 2), // -3% malus
supplyFactor: sdk.NewDecWithPrec(1, 1), // Decrease final supply by a factor of 10
supplyMintFactor: sdk.OneDec().Quo(sdk.NewDec(9)), // 1/9 of the total supply is minted for the CP and a reserved address
}
}
func (d distrib) votePercentages() map[govtypes.VoteOption]sdk.Dec {
percs := make(map[govtypes.VoteOption]sdk.Dec)
for k, v := range d.votes {
percs[k] = v.Quo(d.supply)
}
return percs
}
func distribution(accounts []Account, params distriParams, prefix string) (airdrop, error) {
airdrop := airdrop{
params: params,
addresses: make(map[string]sdk.Int),
icfSlash: sdk.ZeroDec(),
atom: distrib{
supply: sdk.ZeroDec(),
votes: newVoteMap(),
unstaked: sdk.ZeroDec(),
},
atone: distrib{
supply: sdk.ZeroDec(),
votes: newVoteMap(),
unstaked: sdk.ZeroDec(),
},
}
for _, acc := range accounts {
var (
voteWeights = acc.voteWeights()
// Detail of vote distribution for $ATOM
yesAtomAmt = voteWeights[govtypes.OptionYes].Mul(acc.StakedAmount)
noAtomAmt = voteWeights[govtypes.OptionNo].Mul(acc.StakedAmount)
noWithVetoAtomAmt = voteWeights[govtypes.OptionNoWithVeto].Mul(acc.StakedAmount)
abstainAtomAmt = voteWeights[govtypes.OptionAbstain].Mul(acc.StakedAmount)
noVoteAtomAmt = voteWeights[govtypes.OptionEmpty].Mul(acc.StakedAmount)
)
// increment $ATOM votes
airdrop.atom.votes.add(govtypes.OptionYes, yesAtomAmt)
airdrop.atom.votes.add(govtypes.OptionNo, noAtomAmt)
airdrop.atom.votes.add(govtypes.OptionNoWithVeto, noWithVetoAtomAmt)
airdrop.atom.votes.add(govtypes.OptionAbstain, abstainAtomAmt)
airdrop.atom.votes.add(govtypes.OptionEmpty, noVoteAtomAmt)
// increment $ATOM supply
airdrop.atom.supply = airdrop.atom.supply.Add(acc.StakedAmount.Add(acc.LiquidAmount))
airdrop.atom.unstaked = airdrop.atom.unstaked.Add(acc.LiquidAmount)
}
// Compute nonVotersMultiplier to have non-voters <= 33%
var (
yesAtoneTotalAmt = airdrop.atom.votes[govtypes.OptionYes].Mul(params.yesVotesMultiplier)
noAtoneTotalAmt = airdrop.atom.votes[govtypes.OptionNo].Add(airdrop.atom.votes[govtypes.OptionNoWithVeto]).Mul(params.noVotesMultiplier)
noVotersAtomTotalAmt = airdrop.atom.votes[govtypes.OptionAbstain].Add(airdrop.atom.votes[govtypes.OptionEmpty]).Add(airdrop.atom.unstaked)
targetNonVotersPerc = sdk.NewDecWithPrec(33, 2)
)
// Formula is:
// nonVotersMultiplier = (t x (yesAtone + noAtone)) / ((1 - t) x nonVoterAtom)
// where t is the targetNonVotersPerc
airdrop.nonVotersMultiplier = targetNonVotersPerc.Mul(yesAtoneTotalAmt.Add(noAtoneTotalAmt)).
Quo((sdk.OneDec().Sub(targetNonVotersPerc)).Mul(noVotersAtomTotalAmt))
for _, acc := range accounts {
if slices.Contains(icfWallets, acc.Address) {
// Slash ICF
airdrop.icfSlash = airdrop.icfSlash.Add(acc.LiquidAmount).Add(acc.StakedAmount)
continue
}
var (
voteWeights = acc.voteWeights()
yesAtomAmt = voteWeights[govtypes.OptionYes].Mul(acc.StakedAmount)
noAtomAmt = voteWeights[govtypes.OptionNo].Mul(acc.StakedAmount)
noWithVetoAtomAmt = voteWeights[govtypes.OptionNoWithVeto].Mul(acc.StakedAmount)
abstainAtomAmt = voteWeights[govtypes.OptionAbstain].Mul(acc.StakedAmount)
noVoteAtomAmt = voteWeights[govtypes.OptionEmpty].Mul(acc.StakedAmount)
// Apply airdrop multipliers:
// Yes: x yesVotesMultiplier
// No: x noVotesMultiplier
// NoWithVeto: x noVotesMultiplier x bonus
// Abstain: x nonVotersMultiplier
// Didn't vote: x nonVotersMultiplier x malus
yesAirdropAmt = yesAtomAmt.Mul(params.yesVotesMultiplier).Mul(params.supplyFactor)
noAirdropAmt = noAtomAmt.Mul(params.noVotesMultiplier).Mul(params.supplyFactor)
noWithVetoAirdropAmt = noWithVetoAtomAmt.Mul(params.noVotesMultiplier).Mul(params.bonus).Mul(params.supplyFactor)
abstainAirdropAmt = abstainAtomAmt.Mul(airdrop.nonVotersMultiplier).Mul(params.supplyFactor)
noVoteAirdropAmt = noVoteAtomAmt.Mul(airdrop.nonVotersMultiplier).Mul(params.malus).Mul(params.supplyFactor)
// Liquid amount gets the same multiplier as those who didn't vote.
liquidMultiplier = airdrop.nonVotersMultiplier.Mul(params.malus)
// total airdrop for this account
liquidAirdropAmt = acc.LiquidAmount.Mul(liquidMultiplier).Mul(params.supplyFactor)
stakedAirdropAmt = yesAirdropAmt.Add(noAirdropAmt).Add(noWithVetoAirdropAmt).
Add(abstainAirdropAmt).Add(noVoteAirdropAmt)
airdropAmt = liquidAirdropAmt.Add(stakedAirdropAmt)
)
// increment airdrop votes
airdrop.atone.votes.add(govtypes.OptionYes, yesAirdropAmt)
airdrop.atone.votes.add(govtypes.OptionNo, noAirdropAmt)
airdrop.atone.votes.add(govtypes.OptionNoWithVeto, noWithVetoAirdropAmt)
airdrop.atone.votes.add(govtypes.OptionAbstain, abstainAirdropAmt)
airdrop.atone.votes.add(govtypes.OptionEmpty, noVoteAirdropAmt)
// increment airdrop supply
airdrop.atone.supply = airdrop.atone.supply.Add(airdropAmt)
airdrop.atone.unstaked = airdrop.atone.unstaked.Add(liquidAirdropAmt)
// add address and amount (skipping 0 balance)
if amtInt := airdropAmt.RoundInt(); !amtInt.IsZero() {
addr := acc.Address
if prefix != "" {
// Derive address from "cosmos" to prefix parameter
var err error
addr, err = convertBech32(acc.Address, "cosmos", prefix)
if err != nil {
return airdrop, err
}
}
// Fill with "cosmos" prefixed address
airdrop.addresses[addr] = amtInt
ad := addrAmtDetail{
Address: addr,
YesDetail: amtDetail{
AtomAmt: yesAtomAmt,
Multiplier: params.yesVotesMultiplier,
BonusMalus: sdk.OneDec(),
Factor: params.supplyFactor,
AtoneAmt: yesAirdropAmt,
},
NoDetail: amtDetail{
AtomAmt: noAtomAmt,
Multiplier: params.noVotesMultiplier,
BonusMalus: sdk.OneDec(),
Factor: params.supplyFactor,
AtoneAmt: noAirdropAmt,
},
NWVDetail: amtDetail{
AtomAmt: noWithVetoAtomAmt,
Multiplier: params.noVotesMultiplier,
BonusMalus: params.bonus,
Factor: params.supplyFactor,
AtoneAmt: noWithVetoAirdropAmt,
},
AbsDetail: amtDetail{
AtomAmt: abstainAtomAmt,
Multiplier: airdrop.nonVotersMultiplier,
BonusMalus: sdk.OneDec(),
Factor: params.supplyFactor,
AtoneAmt: abstainAirdropAmt,
},
DnvDetail: amtDetail{
AtomAmt: noVoteAtomAmt,
Multiplier: airdrop.nonVotersMultiplier,
BonusMalus: params.malus,
Factor: params.supplyFactor,
AtoneAmt: noVoteAirdropAmt,
},
LiquidDetail: amtDetail{
AtomAmt: acc.LiquidAmount,
Multiplier: airdrop.nonVotersMultiplier,
BonusMalus: params.malus,
Factor: params.supplyFactor,
AtoneAmt: liquidAirdropAmt,
},
Total: airdropAmt,
}
airdrop.addressesDetail = append(airdrop.addressesDetail, ad)
amt := yesAirdropAmt.Add(noAirdropAmt).Add(noWithVetoAirdropAmt).Add(abstainAirdropAmt).Add(noVoteAirdropAmt).Add(liquidAirdropAmt)
if !amt.Equal(airdropAmt) {
panic(fmt.Sprintf("WRONG %+v\n", ad))
}
}
}
// Compute minted part
minted := airdrop.atone.supply.Mul(params.supplyMintFactor)
airdrop.communityPool = minted.Quo(sdk.NewDec(2))
airdrop.reservedAddr = minted.Quo(sdk.NewDec(2))
return airdrop, nil
}
// convenient type for manipulating vote counts.
type voteMap map[govtypes.VoteOption]sdk.Dec
var (
allVoteOptions = []govtypes.VoteOption{
govtypes.OptionEmpty,
govtypes.OptionYes,
govtypes.OptionAbstain,
govtypes.OptionNo,
govtypes.OptionNoWithVeto,
}
activeVoteOptions = []govtypes.VoteOption{
govtypes.OptionYes,
govtypes.OptionNo,
govtypes.OptionNoWithVeto,
}
)
func newVoteMap() voteMap {
m := make(map[govtypes.VoteOption]sdk.Dec)
for _, v := range allVoteOptions {
m[v] = sdk.ZeroDec()
}
return m
}
func (m voteMap) add(v govtypes.VoteOption, d sdk.Dec) {
m[v] = m[v].Add(d)
}
func printAirdropsStats(chartMode bool, airdrops []airdrop) error {
if chartMode {
f, err := os.CreateTemp("", "chart*.html")
if err != nil {
return err
}
defer f.Close()
page := components.NewPage()
page.PageTitle = "$ATONE distributions"
page.AddCharts(
newBarChart(airdrops),
newPieChart("$ATOM distribution", airdrops[0].atom),
)
for _, airdrop := range airdrops {
page.AddCharts(
newPieChart(fmt.Sprintf("$ATONE distribution %s", airdrop.params), airdrop.atone),
)
}
page.Render(f)
fmt.Printf("Charts rendered in %s\n", f.Name())
browser.OpenFile(f.Name())
return nil
}
printDistrib := func(d distrib) {
table := newMarkdownTable("", "TOTAL", "DID NOT VOTE", "YES", "NO", "NOWITHVETO", "ABSTAIN", "NOT STAKED")
table.Append([]string{
"Distributed",
humand(d.supply),
humand(d.votes[govtypes.OptionEmpty]),
humand(d.votes[govtypes.OptionYes]),
humand(d.votes[govtypes.OptionNo]),
humand(d.votes[govtypes.OptionNoWithVeto]),
humand(d.votes[govtypes.OptionAbstain]),
humand(d.unstaked),
})
votePercs := d.votePercentages()
table.Append([]string{
"Percentage over total",
"",
humanPercentI(votePercs[govtypes.OptionEmpty]),
humanPercentI(votePercs[govtypes.OptionYes]),
humanPercentI(votePercs[govtypes.OptionNo]),
humanPercentI(votePercs[govtypes.OptionNoWithVeto]),
humanPercentI(votePercs[govtypes.OptionAbstain]),
humanPercentI(d.unstaked.Quo(d.supply)),
})
table.Render()
fmt.Println()
}
fmt.Println("$ATOM distribution")
printDistrib(airdrops[0].atom)
for _, airdrop := range airdrops {
fmt.Printf("$ATONE distribution (params: %s) (ratio: x%.3f, nonVotersMultiplier: %.3f, icfSlash: %s $ATOM)\n",
airdrop.params,
airdrop.atone.supply.Quo(airdrop.atom.supply).MustFloat64(),
airdrop.nonVotersMultiplier.MustFloat64(),
humand(airdrop.icfSlash),
)
printDistrib(airdrop.atone)
fmt.Printf(
"ATONE TOTAL SUPPLY = DISTRIBUTED(%s) + COMMUNITY_POOL(%s) + RESERVED_ADDRESS(%s) = %s\n",
humand(airdrop.atone.supply), humand(airdrop.communityPool), humand(airdrop.reservedAddr),
humand(airdrop.atone.supply.Add(airdrop.communityPool).Add(airdrop.reservedAddr)),
)
}
return nil
}
func newBarChart(airdrops []airdrop) *charts.Bar {
bar := charts.NewBar()
bar.SetGlobalOptions(
charts.WithTitleOpts(opts.Title{Title: "Votes distribution"}),
charts.WithLegendOpts(opts.Legend{Show: true, Right: "right", Orient: "vertical"}),
charts.WithTooltipOpts(opts.Tooltip{
Show: true,
Formatter: opts.FuncOpts("function(params){ return params.value.toFixed(2)+'%'}"),
}),
)
bar.SetXAxis([]string{"Yes", "No", "NWV", "Abstain", "DNV", "Unstaked"})
generateData := func(d distrib) []opts.BarData {
var (
votePercs = d.votePercentages()
data = make([]opts.BarData, 6)
oneHundred = sdk.NewDec(100)
)
data[0] = opts.BarData{
Name: "Yes",
Value: votePercs[govtypes.OptionYes].Mul(oneHundred).MustFloat64(),
}
data[1] = opts.BarData{
Name: "No",
Value: votePercs[govtypes.OptionNo].Mul(oneHundred).MustFloat64(),
}
data[2] = opts.BarData{
Name: "NWV",
Value: votePercs[govtypes.OptionNoWithVeto].Mul(oneHundred).MustFloat64(),
}
data[3] = opts.BarData{
Name: "Abstain",
Value: votePercs[govtypes.OptionAbstain].Mul(oneHundred).MustFloat64(),
}
data[4] = opts.BarData{
Name: "DNV",
Value: votePercs[govtypes.OptionEmpty].Mul(oneHundred).MustFloat64(),
}
data[5] = opts.BarData{
Name: "Unstaked",
Value: d.unstaked.Quo(d.supply).Mul(oneHundred).MustFloat64(),
}
return data
}
bar.AddSeries("$ATOM", generateData(airdrops[0].atom))
for _, airdrop := range airdrops {
bar.AddSeries(fmt.Sprintf("$ATONE %s", airdrop.params), generateData(airdrop.atone))
}
return bar
}
func newPieChart(title string, d distrib) *charts.Pie {
pie := charts.NewPie()
pie.SetGlobalOptions(
charts.WithTitleOpts(opts.Title{
Title: title,
}),
charts.WithLegendOpts(opts.Legend{
Show: false,
}),
charts.WithTooltipOpts(opts.Tooltip{
Show: true,
Formatter: opts.FuncOpts("function(params){ return params.name+': '+params.value.toFixed(2)+'%'}"),
}),
)
var (
data = make([]opts.PieData, 6)
dataSum = make([]opts.PieData, 3)
votePercs = d.votePercentages()
oneHundred = sdk.NewDec(100)
)
data[0] = opts.PieData{
Name: "Yes",
ItemStyle: &opts.ItemStyle{Color: "#ff8b87"},
Value: votePercs[govtypes.OptionYes].Mul(oneHundred).MustFloat64(),
}
dataSum[0] = opts.PieData{
Name: "Yes",
ItemStyle: &opts.ItemStyle{Color: "#ff8b87"},
Value: data[0].Value,
}
data[1] = opts.PieData{
Name: "No",
ItemStyle: &opts.ItemStyle{Color: "#9FDFBF"},
Value: votePercs[govtypes.OptionNo].Mul(oneHundred).MustFloat64(),
}
data[2] = opts.PieData{
Name: "NWV",
ItemStyle: &opts.ItemStyle{Color: "#88d8b0"},
Value: votePercs[govtypes.OptionNoWithVeto].Mul(oneHundred).MustFloat64(),
}
dataSum[1] = opts.PieData{
Name: "No+NWV",
ItemStyle: &opts.ItemStyle{Color: "#6cac8c"},
Value: data[1].Value.(float64) + data[2].Value.(float64),
}
data[3] = opts.PieData{
Name: "Abstain",
ItemStyle: &opts.ItemStyle{Color: "#eac086"},
Value: votePercs[govtypes.OptionAbstain].Mul(oneHundred).MustFloat64(),
}
data[4] = opts.PieData{
Name: "DNV",
ItemStyle: &opts.ItemStyle{Color: "#ffcd94"},
Value: votePercs[govtypes.OptionEmpty].Mul(oneHundred).MustFloat64(),
}
data[5] = opts.PieData{
Name: "Unstaked",
ItemStyle: &opts.ItemStyle{Color: "#ffe0bd"},
Value: d.unstaked.Quo(d.supply).Mul(oneHundred).MustFloat64(),
}
dataSum[2] = opts.PieData{
Name: "Non voters",
ItemStyle: &opts.ItemStyle{Color: "#ffad60"},
Value: data[3].Value.(float64) + data[4].Value.(float64) + data[5].Value.(float64),
}
pie.AddSeries("pie", data,
charts.WithLabelOpts(opts.Label{
Show: true,
Formatter: opts.FuncOpts("function(params){ return params.name+': '+params.value.toFixed(2)+'%'}"),
}),
charts.WithPieChartOpts(opts.PieChart{
Radius: []string{"45%", "80%"},
}),
)
pie.AddSeries("pie2", dataSum,
charts.WithLabelOpts(opts.Label{
Show: false,
Formatter: opts.FuncOpts("function(params){ return params.name+': '+params.value.toFixed(2)+'%'}"),
}),
charts.WithPieChartOpts(opts.PieChart{
Radius: []string{"0%", "46%"},
}),
)
return pie
}