-
Notifications
You must be signed in to change notification settings - Fork 1
/
autostaking.go
139 lines (127 loc) · 4.26 KB
/
autostaking.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
package main
import (
"encoding/json"
"fmt"
"os"
"slices"
"sort"
h "github.com/dustin/go-humanize"
tmjson "github.com/cometbft/cometbft/libs/json"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)
func autoStaking(genesisPath string) error {
bz, err := os.ReadFile(genesisPath)
if err != nil {
return fmt.Errorf("readfile %s: %w", genesisPath, err)
}
var genesisState map[string]json.RawMessage
if err := tmjson.Unmarshal(bz, &genesisState); err != nil {
return fmt.Errorf("tmjson.Unmarshal: %w", err)
}
var appState map[string]json.RawMessage
if err := tmjson.Unmarshal(genesisState["app_state"], &appState); err != nil {
return fmt.Errorf("tmjson.Unmarshal appstate: %w", err)
}
// get all balances
var bankGenesisState banktypes.GenesisState
err = tmjson.Unmarshal(appState["bank"], &bankGenesisState)
if err != nil {
return fmt.Errorf("unmarshal auth: %w", err)
}
var (
minTokens = sdk.NewInt(25_000_000)
supply = sdk.ZeroInt()
totalStake = sdk.ZeroInt()
numStakes = 0
validatorLen = 30
validators = make([]int64, validatorLen)
stakeds int64
stakes = make(map[int]int)
stakeSplitCondition = sdk.NewInt(1_000_000_000_000)
// This algorithm splits the stake into parts and stake those parts one by
// one into the validator that has the less stake.
basicAlgo = func(balIdx int, stake sdk.Int) {
// to prevent staking multiple times over the same validator
// adjust split amount for the whale account
splitStake := sdk.NewInt(1)
switch {
case stake.LT(sdk.NewInt(500_000_000)):
splitStake = stake.QuoRaw(5)
case stake.LT(sdk.NewInt(10_000_000_000)):
splitStake = stake.QuoRaw(10)
default:
splitStake = stake.QuoRaw(20)
}
for ; stake.GTE(sdk.DefaultPowerReduction); stake = stake.Sub(splitStake) {
// find validator which has the less stake
valIdx := slices.Index(validators, slices.Min(validators))
staked := sdk.MinInt(stake, splitStake).Int64()
stakeds += staked
validators[valIdx] += staked
stakes[balIdx]++
// if balIdx == 0 || balIdx == 663 || balIdx == 1 || balIdx == 662 {
// fmt.Println(balIdx, "stake", humani(staked), "valIdx", valIdx)
// }
}
}
// staking distrib from terra
// https://github.com/terra-money/core/blob/release/v2.0/app/app.go#L841
valIdx = 0
terraAlgo = func(balIdx int, stake sdk.Int) {
// to prevent staking multiple times over the same validator
// adjust split amount for the whale account
splitStake := stakeSplitCondition
if stake.GT(stakeSplitCondition.MulRaw(int64(validatorLen))) {
splitStake = stake.QuoRaw(int64(validatorLen))
}
// if a vesting account has more staking token than `stakeSplitCondition`,
// split staking balance to distribute staking power evenly
// Ex) 2_200_000_000_000
// stake 1_000_000_000_000 to val1
// stake 1_000_000_000_000 to val2
// stake 200_000_000_000 to val3
for ; stake.GTE(sdk.DefaultPowerReduction); stake = stake.Sub(splitStake) {
staked := sdk.MinInt(stake, splitStake).Int64()
stakeds += staked
validators[valIdx%validatorLen] += staked
stakes[balIdx]++
// increase index only when staking happened
valIdx++
}
}
)
bals := bankGenesisState.Balances
sort.Slice(bals, func(i, j int) bool {
return bals[i].Coins.IsAllGT(bals[j].Coins)
})
for i := 0; i < 4; i++ {
fmt.Println("BAL", i, human(bals[i].Coins.AmountOf("ugovgen")))
}
_, _ = terraAlgo, basicAlgo
for balIdx, bal := range bals {
tokens := bal.Coins.AmountOf("ugovgen")
supply = supply.Add(tokens)
if tokens.LTE(minTokens) {
// Don't stake when tokens < minToken
continue
}
numStakes++
// take 50%
stake := tokens.QuoRaw(2)
totalStake = totalStake.Add(stake)
basicAlgo(balIdx, stake)
}
// for k, v := range stakes {
// if v > 5 {
// fmt.Println("STAKE", k, v)
// }
// }
for i := 0; i < validatorLen; i++ {
fmt.Println("VAL", i, humani(validators[i]))
}
fmt.Println("count", h.Comma(int64(numStakes)), h.Comma(int64(len(bankGenesisState.Balances))))
fmt.Println("amount", human(totalStake), humani(stakeds), human(supply))
fmt.Println("staking ratio", totalStake.ToLegacyDec().Quo(supply.ToLegacyDec()))
return nil
}