-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
util.go
118 lines (95 loc) · 2.67 KB
/
util.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
package cli
import (
"encoding/json"
"fmt"
"os"
"github.com/spf13/pflag"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
govutils "github.com/cosmos/cosmos-sdk/x/gov/client/utils"
)
type legacyProposal struct {
Title string
Description string
Type string
Deposit string
}
// validate the legacyProposal
func (p legacyProposal) validate() error {
if p.Type == "" {
return fmt.Errorf("proposal type is required")
}
if p.Title == "" {
return fmt.Errorf("proposal title is required")
}
if p.Description == "" {
return fmt.Errorf("proposal description is required")
}
return nil
}
// parseSubmitLegacyProposal reads and parses the legacy proposal.
func parseSubmitLegacyProposal(fs *pflag.FlagSet) (*legacyProposal, error) {
proposal := &legacyProposal{}
proposalFile, _ := fs.GetString(FlagProposal)
if proposalFile == "" {
proposalType, _ := fs.GetString(FlagProposalType)
proposal.Title, _ = fs.GetString(FlagTitle)
proposal.Description, _ = fs.GetString(FlagDescription)
proposal.Type = govutils.NormalizeProposalType(proposalType)
proposal.Deposit, _ = fs.GetString(FlagDeposit)
if err := proposal.validate(); err != nil {
return nil, err
}
return proposal, nil
}
for _, flag := range ProposalFlags {
if v, _ := fs.GetString(flag); v != "" {
return nil, fmt.Errorf("--%s flag provided alongside --proposal, which is a noop", flag)
}
}
contents, err := os.ReadFile(proposalFile)
if err != nil {
return nil, err
}
err = json.Unmarshal(contents, proposal)
if err != nil {
return nil, err
}
if err := proposal.validate(); err != nil {
return nil, err
}
return proposal, nil
}
// proposal defines the new Msg-based proposal.
type proposal struct {
// Msgs defines an array of sdk.Msgs proto-JSON-encoded as Anys.
Messages []json.RawMessage `json:"messages,omitempty"`
Metadata string `json:"metadata"`
Deposit string `json:"deposit"`
}
// parseSubmitProposal reads and parses the proposal.
func parseSubmitProposal(cdc codec.Codec, path string) ([]sdk.Msg, string, sdk.Coins, error) {
var proposal proposal
contents, err := os.ReadFile(path)
if err != nil {
return nil, "", nil, err
}
err = json.Unmarshal(contents, &proposal)
if err != nil {
return nil, "", nil, err
}
msgs := make([]sdk.Msg, len(proposal.Messages))
for i, anyJSON := range proposal.Messages {
var msg sdk.Msg
err := cdc.UnmarshalInterfaceJSON(anyJSON, &msg)
if err != nil {
return nil, "", nil, err
}
msgs[i] = msg
}
deposit, err := sdk.ParseCoinsNormalized(proposal.Deposit)
if err != nil {
return nil, "", nil, err
}
return msgs, proposal.Metadata, deposit, nil
}