/
balances_from_state_export.go
273 lines (222 loc) · 7.85 KB
/
balances_from_state_export.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
package cmd
// modified from osmosis
// https://github.com/CosmosContracts/juno/v18/blob/main/cmd/osmosisd/cmd/balances_from_state_export.go
import (
"encoding/csv"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"github.com/spf13/cobra"
tmjson "github.com/cometbft/cometbft/libs/json"
tmtypes "github.com/cometbft/cometbft/types"
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
appparams "github.com/CosmosContracts/juno/v18/app/params"
)
const (
FlagMinimumStakeAmount = "minimum-stake-amount"
)
type DeriveSnapshot struct {
NumberAccounts uint64 `json:"num_accounts"`
Accounts map[string]DerivedAccount `json:"accounts"`
}
// DerivedAccount provide fields of snapshot per account
// It is the simplified struct we are presenting in this 'balances from state export' snapshot for people.
type DerivedAccount struct {
Address string `json:"address"`
LiquidBalances sdk.Coins `json:"liquid_balance"`
Staked math.Int `json:"staked"`
UnbondingStake math.Int `json:"unbonding_stake"`
Bonded sdk.Coins `json:"bonded"`
TotalBalances sdk.Coins `json:"total_balances"`
}
// newDerivedAccount returns a new derived account.
func newDerivedAccount(address string) DerivedAccount {
return DerivedAccount{
Address: address,
LiquidBalances: sdk.Coins{},
Staked: sdk.ZeroInt(),
UnbondingStake: sdk.ZeroInt(),
Bonded: sdk.Coins{},
}
}
// getGenStateFromPath returns a JSON genState message from inputted path.
func getGenStateFromPath(genesisFilePath string) (map[string]json.RawMessage, error) {
genState := make(map[string]json.RawMessage)
genesisFile, err := os.Open(filepath.Clean(genesisFilePath))
if err != nil {
return genState, err
}
defer genesisFile.Close()
byteValue, _ := io.ReadAll(genesisFile)
var doc tmtypes.GenesisDoc
err = tmjson.Unmarshal(byteValue, &doc)
if err != nil {
return genState, err
}
err = json.Unmarshal(doc.AppState, &genState)
if err != nil {
panic(err)
}
return genState, nil
}
// ExportAirdropSnapshotCmd generates a snapshot.json from a provided exported genesis.json.
func ExportDeriveBalancesCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "export-derive-balances [input-genesis-file] [output-snapshot-json]",
Short: "Export a derive balances from a provided genesis export",
Long: `Export a derive balances from a provided genesis export
Example:
junod export-derive-balances ../genesis.json ../snapshot.json
`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
serverCtx := server.GetServerContextFromCmd(cmd)
config := serverCtx.Config
config.SetRoot(clientCtx.HomeDir)
genesisFile := args[0]
genState, err := getGenStateFromPath(genesisFile)
if err != nil {
return err
}
snapshotOutput := args[1]
// Produce the map of address to total juno balance, both staked and UnbondingStake
snapshotAccs := make(map[string]DerivedAccount)
bankGenesis := banktypes.GenesisState{}
if len(genState["bank"]) > 0 {
clientCtx.Codec.MustUnmarshalJSON(genState["bank"], &bankGenesis)
}
for _, balance := range bankGenesis.Balances {
address := balance.Address
acc, ok := snapshotAccs[address]
if !ok {
acc = newDerivedAccount(address)
}
acc.LiquidBalances = balance.Coins
snapshotAccs[address] = acc
}
stakingGenesis := stakingtypes.GenesisState{}
if len(genState["staking"]) > 0 {
clientCtx.Codec.MustUnmarshalJSON(genState["staking"], &stakingGenesis)
}
for _, unbonding := range stakingGenesis.UnbondingDelegations {
address := unbonding.DelegatorAddress
acc, ok := snapshotAccs[address]
if !ok {
acc = newDerivedAccount(address)
}
unbondingJunos := sdk.NewInt(0)
for _, entry := range unbonding.Entries {
unbondingJunos = unbondingJunos.Add(entry.Balance)
}
acc.UnbondingStake = acc.UnbondingStake.Add(unbondingJunos)
snapshotAccs[address] = acc
}
// Make a map from validator operator address to the v036 validator type
validators := make(map[string]stakingtypes.Validator)
for _, validator := range stakingGenesis.Validators {
validators[validator.OperatorAddress] = validator
}
for _, delegation := range stakingGenesis.Delegations {
address := delegation.DelegatorAddress
acc, ok := snapshotAccs[address]
if !ok {
acc = newDerivedAccount(address)
}
val := validators[delegation.ValidatorAddress]
stakedJuno := delegation.Shares.MulInt(val.Tokens).Quo(val.DelegatorShares).RoundInt()
acc.Staked = acc.Staked.Add(stakedJuno)
snapshotAccs[address] = acc
}
// convert balances to underlying coins and sum up balances to total balance
for addr, account := range snapshotAccs {
// account.LiquidBalances = underlyingCoins(account.LiquidBalances)
// account.Bonded = underlyingCoins(account.Bonded)
account.TotalBalances = sdk.NewCoins().
Add(account.LiquidBalances...).
Add(sdk.NewCoin(appparams.BondDenom, account.Staked)).
Add(sdk.NewCoin(appparams.BondDenom, account.UnbondingStake)).
Add(account.Bonded...)
snapshotAccs[addr] = account
}
snapshot := DeriveSnapshot{
NumberAccounts: uint64(len(snapshotAccs)),
Accounts: snapshotAccs,
}
fmt.Printf("# accounts: %d\n", len(snapshotAccs))
// export snapshot json
snapshotJSON, err := json.MarshalIndent(snapshot, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal snapshot: %w", err)
}
err = os.WriteFile(snapshotOutput, snapshotJSON, 0o600)
return err
},
}
return cmd
}
// StakedToCSVCmd generates a airdrop.csv from a provided exported balances.json.
func StakedToCSVCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "staked-to-csv [input-balances-file] [output-airdrop-csv]",
Short: "Export a airdrop csv from a provided balances export",
Long: `Export a airdrop csv from a provided balances export (from export-derive-balances)
Example:
junod staked-to-csv ../balances.json ../airdrop.csv
`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
serverCtx := server.GetServerContextFromCmd(cmd)
config := serverCtx.Config
config.SetRoot(clientCtx.HomeDir)
balancesFile := args[0]
snapshotOutput := args[1]
minStakeAmount, _ := cmd.Flags().GetInt64(FlagMinimumStakeAmount)
var deriveSnapshot DeriveSnapshot
sourceFile, err := os.Open(balancesFile)
if err != nil {
return err
}
// remember to close the file at the end of the function
defer sourceFile.Close()
// decode the balances json file into the struct array
if err := json.NewDecoder(sourceFile).Decode(&deriveSnapshot); err != nil {
return err
}
// create a new file to store CSV data
outputFile, err := os.Create(snapshotOutput)
if err != nil {
return err
}
defer outputFile.Close()
// write the header of the CSV file
writer := csv.NewWriter(outputFile)
defer writer.Flush()
header := []string{"address", "staked"}
if err := writer.Write(header); err != nil {
return err
}
// iterate through all accounts, leave out accounts that do not meet the user provided min stake amount
for _, r := range deriveSnapshot.Accounts {
var csvRow []string
if r.Staked.GT(sdk.NewInt(minStakeAmount)) {
csvRow = append(csvRow, r.Address, r.Staked.String())
if err := writer.Write(csvRow); err != nil {
return err
}
}
}
return nil
},
}
cmd.Flags().Int64(FlagMinimumStakeAmount, 0, "Specify minimum amount (non inclusive) accounts must stake to be included in airdrop (default: 0)")
return cmd
}