-
Notifications
You must be signed in to change notification settings - Fork 197
/
parser.go
151 lines (131 loc) · 4.32 KB
/
parser.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
package cli
import (
"os"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/gogo/protobuf/proto"
"github.com/spf13/cobra"
"github.com/Stride-Labs/stride/v22/x/staketia/types"
)
//////////////////////////////////////////////
// LOGIC FOR PARSING OVERWRITE RECORD JSONS //
//////////////////////////////////////////////
// Parse the overwrite delegationrecord json file into a proto message
func parseOverwriteDelegationRecordFile(cdc codec.JSONCodec,
parseOverWriteRecordFile string,
delegationRecord proto.Message,
) (err error) {
// Defer with a recover to set the error,
// if an expected sdk.Int field is not included in the JSON
// will panic and the CLI can SEGFAULT
defer func() {
if r := recover(); r != nil {
err = sdkerrors.ErrInvalidRequest
}
}()
contents, err := os.ReadFile(parseOverWriteRecordFile)
if err != nil {
return err
}
if err = cdc.UnmarshalJSON(contents, delegationRecord); err != nil {
return err
}
return err
}
// Parse the overwrite unbondingrecord json file into a proto message
func parseOverwriteUnbondingRecordFile(cdc codec.JSONCodec,
parseOverWriteRecordFile string,
unbondingRecord proto.Message,
) (err error) {
// Defer with a recover to set the error,
// if an expected sdk.Int field is not included in the JSON
// will panic and the CLI can SEGFAULT
defer func() {
if r := recover(); r != nil {
err = sdkerrors.ErrInvalidRequest
}
}()
contents, err := os.ReadFile(parseOverWriteRecordFile)
if err != nil {
return err
}
if err = cdc.UnmarshalJSON(contents, unbondingRecord); err != nil {
return err
}
return err
}
// Parse the overwrite redemptionrecord json file into a proto message
func parseOverwriteRedemptionRecordFile(cdc codec.JSONCodec,
parseOverWriteRecordFile string,
redemptionRecord proto.Message,
) (err error) {
// Defer with a recover to set the error,
// if an expected sdk.Int field is not included in the JSON
// will panic and the CLI can SEGFAULT
defer func() {
if r := recover(); r != nil {
err = sdkerrors.ErrInvalidRequest
}
}()
contents, err := os.ReadFile(parseOverWriteRecordFile)
if err != nil {
return err
}
if err = cdc.UnmarshalJSON(contents, redemptionRecord); err != nil {
return err
}
return err
}
//////////////////////////////////////////////
// LOGIC FOR BROADCASTING OVERWRITE RECORDS //
//////////////////////////////////////////////
// helper to parse delegation record and broadcast OverwriteDelegationRecord
func parseAndBroadcastOverwriteDelegation(clientCtx client.Context, cmd *cobra.Command, recordContents string) error {
var delegationRecord types.DelegationRecord
// parse the input json
if err := parseOverwriteDelegationRecordFile(clientCtx.Codec, recordContents, &delegationRecord); err != nil {
return err
}
msg := types.NewMsgOverwriteDelegationRecord(
clientCtx.GetFromAddress().String(),
delegationRecord,
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
}
// helper to parse unbonding record and broadcast OverwriteUnbondingRecord
func parseAndBroadcastOverwriteUnbonding(clientCtx client.Context, cmd *cobra.Command, recordContents string) error {
var unbondingRecord types.UnbondingRecord
// parse the input json
if err := parseOverwriteUnbondingRecordFile(clientCtx.Codec, recordContents, &unbondingRecord); err != nil {
return err
}
msg := types.NewMsgOverwriteUnbondingRecord(
clientCtx.GetFromAddress().String(),
unbondingRecord,
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
}
// helper to parse redemption record and broadcast OverwriteRedemptionRecord
func parseAndBroadcastOverwriteRedemption(clientCtx client.Context, cmd *cobra.Command, recordContents string) error {
var redemptionRecord types.RedemptionRecord
// parse the input json
if err := parseOverwriteRedemptionRecordFile(clientCtx.Codec, recordContents, &redemptionRecord); err != nil {
return err
}
msg := types.NewMsgOverwriteRedemptionRecord(
clientCtx.GetFromAddress().String(),
redemptionRecord,
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
}