-
Notifications
You must be signed in to change notification settings - Fork 590
/
gov.go
149 lines (124 loc) · 5.19 KB
/
gov.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
package types
import (
"fmt"
"strings"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)
const (
ProposalTypeUploadCosmWasmPoolCodeAndWhiteList = "UploadCosmWasmPoolCodeAndWhiteListProposal"
ProposalTypeMigratePoolContractsProposal = "MigratePoolContractsProposal"
)
func init() {
govtypes.RegisterProposalType(ProposalTypeUploadCosmWasmPoolCodeAndWhiteList)
govtypes.RegisterProposalTypeCodec(&UploadCosmWasmPoolCodeAndWhiteListProposal{}, "osmosis/UploadCosmWasmPoolCodeAndWhiteListProposal")
govtypes.RegisterProposalType(ProposalTypeMigratePoolContractsProposal)
govtypes.RegisterProposalTypeCodec(&MigratePoolContractsProposal{}, "osmosis/MigratePoolContractsProposal")
}
var (
_ govtypes.Content = &UploadCosmWasmPoolCodeAndWhiteListProposal{}
_ govtypes.Content = &MigratePoolContractsProposal{}
)
// NewUploadCosmWasmPoolCodeAndWhiteListProposal returns a new instance of an upload cosmwasm pool code and whitelist proposal struct.
func NewUploadCosmWasmPoolCodeAndWhiteListProposal(title, description string, wasmByteCode []byte) govtypes.Content {
return &UploadCosmWasmPoolCodeAndWhiteListProposal{
Title: title,
Description: description,
WASMByteCode: wasmByteCode,
}
}
func (p *UploadCosmWasmPoolCodeAndWhiteListProposal) GetTitle() string { return p.Title }
// GetDescription gets the description of the proposal
func (p *UploadCosmWasmPoolCodeAndWhiteListProposal) GetDescription() string { return p.Description }
// ProposalRoute returns the router key for the proposal
func (p *UploadCosmWasmPoolCodeAndWhiteListProposal) ProposalRoute() string { return RouterKey }
// ProposalType returns the type of the proposal
func (p *UploadCosmWasmPoolCodeAndWhiteListProposal) ProposalType() string {
return ProposalTypeUploadCosmWasmPoolCodeAndWhiteList
}
// ValidateBasic validates a governance proposal's abstract and basic contents
func (p *UploadCosmWasmPoolCodeAndWhiteListProposal) ValidateBasic() error {
err := govtypes.ValidateAbstract(p)
if err != nil {
return err
}
if len(p.WASMByteCode) == 0 {
return fmt.Errorf("wasm byte code cannot be nil")
}
return nil
}
// String returns a string containing the pool incentives proposal.
func (p UploadCosmWasmPoolCodeAndWhiteListProposal) String() string {
var b strings.Builder
b.WriteString(fmt.Sprintf(`Upload CosmWasm Pool Code and WhiteList Proposal:
Title: %s
Description: %s
`, p.Title, p.Description))
return b.String()
}
// NewMigratePoolContractsProposal returns a new instance of a contact code migration proposal.
func NewMigratePoolContractsProposal(title, description string, poolCodeIds []uint64, newCodeId uint64, wasmByteCode []byte) govtypes.Content {
return &MigratePoolContractsProposal{
Title: title,
Description: description,
PoolIds: []uint64{},
NewCodeId: newCodeId,
WASMByteCode: wasmByteCode,
}
}
func (p *MigratePoolContractsProposal) GetTitle() string { return p.Title }
// GetDescription gets the description of the proposal
func (p *MigratePoolContractsProposal) GetDescription() string { return p.Description }
// ProposalRoute returns the router key for the proposal
func (p *MigratePoolContractsProposal) ProposalRoute() string { return RouterKey }
// ProposalType returns the type of the proposal
func (p *MigratePoolContractsProposal) ProposalType() string {
return ProposalTypeMigratePoolContractsProposal
}
// ValidateBasic validates a governance proposal's abstract and basic contents
func (p *MigratePoolContractsProposal) ValidateBasic() error {
err := govtypes.ValidateAbstract(p)
if err != nil {
return err
}
if err := ValidateMigrationProposalConfiguration(p.PoolIds, p.NewCodeId, p.WASMByteCode); err != nil {
return err
}
return nil
}
// String returns a string containing the pool incentives proposal.
func (p MigratePoolContractsProposal) String() string {
var b strings.Builder
b.WriteString(fmt.Sprintf(`Migrate CosmWasm Pool Code and WhiteList Proposal:
Title: %s
Description: %s
PoolIds: %v
NewCodeId: %d
Upload Wasm Code Given: %t
`, p.Title, p.Description, p.PoolIds, p.NewCodeId, len(p.WASMByteCode) > 0))
return b.String()
}
// ValidateMigrationProposalConfiguration validates the migration proposal configuration.
// It has two options to perform the migration.
//
// 1. If the codeID is non-zero, it will migrate the pool contracts to a given codeID assuming that it has already
// been uploaded. uploadByteCode must be empty in such a case. Fails if codeID does not exist.
// Fails if uploadByteCode is not empty.
//
// 2. If the codeID is zero, it will upload the given uploadByteCode and use the new resulting code id to migrate
// the pool to. Errors if uploadByteCode is empty or invalid.
//
// For any of the options, it also validates that pool id list is not empty. Returns error if it is.
func ValidateMigrationProposalConfiguration(poolIds []uint64, newCodeId uint64, uploadByteCode []byte) error {
if len(poolIds) == 0 {
return ErrEmptyPoolIds
}
isNewCodeIdGiven := newCodeId != 0
isUploadByteCodeGiven := len(uploadByteCode) != 0
if !isNewCodeIdGiven && !isUploadByteCodeGiven {
return ErrNoneOfCodeIdAndContractCodeSpecified
}
if isNewCodeIdGiven && isUploadByteCodeGiven {
return ErrBothOfCodeIdAndContractCodeSpecified
}
return nil
}