/
tx.go
258 lines (215 loc) · 6.96 KB
/
tx.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
package cli
import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"github.com/CosmWasm/wasmd/x/wasm/ioutils"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
govcli "github.com/cosmos/cosmos-sdk/x/gov/client/cli"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/osmosis-labs/osmosis/osmoutils/osmocli"
"github.com/osmosis-labs/osmosis/v17/x/cosmwasmpool/cosmwasm/msg"
"github.com/osmosis-labs/osmosis/v17/x/cosmwasmpool/model"
"github.com/osmosis-labs/osmosis/v17/x/cosmwasmpool/types"
)
func NewTxCmd() *cobra.Command {
txCmd := osmocli.TxIndexCmd(types.ModuleName)
osmocli.AddTxCmd(txCmd, NewCreateCWPoolCmd)
return txCmd
}
func NewCreateCWPoolCmd() (*osmocli.TxCliDesc, *model.MsgCreateCosmWasmPool) {
return &osmocli.TxCliDesc{
Use: "create-pool [code-id] [instantiate-msg] [sender]",
Short: "create a cosmwasm pool",
Example: "osmosisd tx cosmwasmpool create-pool 1 uion,uosmo --from lo-test1 --keyring-backend test --chain-id localosmosis --fees 875uosmo -b=block",
NumArgs: 2,
ParseAndBuildMsg: BuildCreatePoolMsg,
}, &model.MsgCreateCosmWasmPool{}
}
func BuildCreatePoolMsg(clientCtx client.Context, args []string, flags *pflag.FlagSet) (sdk.Msg, error) {
codeId, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return nil, err
}
denoms := strings.Split(args[1], ",")
// Construct instantiate msg
instantiateMsg := &msg.InstantiateMsg{
PoolAssetDenoms: denoms,
}
msgBz, err := json.Marshal(instantiateMsg)
if err != nil {
return nil, err
}
return &model.MsgCreateCosmWasmPool{
CodeId: codeId,
InstantiateMsg: msgBz,
Sender: clientCtx.GetFromAddress().String(),
}, nil
}
func NewCmdUploadCodeIdAndWhitelistProposal() *cobra.Command {
cmd := &cobra.Command{
Use: "upload-code-id-and-whitelist [wasm-file-path] [flags]",
Args: cobra.ExactArgs(1),
Short: "Submit an upload code id and whitelist proposal",
Example: "osmosisd tx gov submit-proposal upload-code-id-and-whitelist x/cosmwasmpool/bytecode/transmuter.wasm --from lo-test1 --keyring-backend test --title \"Test\" --description \"Test\" -b=block --chain-id localosmosis --fees=100000uosmo --gas=20000000",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
content, err := parseUploadCodeIdAndWhitelistProposal(cmd, args[0])
if err != nil {
return err
}
from := clientCtx.GetFromAddress()
depositStr, err := cmd.Flags().GetString(govcli.FlagDeposit)
if err != nil {
return err
}
deposit, err := sdk.ParseCoinsNormalized(depositStr)
if err != nil {
return err
}
msg, err := govtypes.NewMsgSubmitProposal(content, deposit, from)
if err != nil {
return err
}
if err = msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
cmd.Flags().String(govcli.FlagTitle, "", "title of proposal")
cmd.Flags().String(govcli.FlagDescription, "", "description of proposal")
cmd.Flags().String(govcli.FlagDeposit, "", "deposit of proposal")
cmd.Flags().Bool(govcli.FlagIsExpedited, false, "If true, makes the proposal an expedited one")
cmd.Flags().String(govcli.FlagProposal, "", "Proposal file path (if this path is given, other proposal flags are ignored)")
return cmd
}
func parseUploadCodeIdAndWhitelistProposal(cmd *cobra.Command, fileName string) (govtypes.Content, error) {
title, err := cmd.Flags().GetString(govcli.FlagTitle)
if err != nil {
return nil, err
}
description, err := cmd.Flags().GetString(govcli.FlagDescription)
if err != nil {
return nil, err
}
wasm, err := parseWasmByteCode(fileName)
if err != nil {
return nil, err
}
content := &types.UploadCosmWasmPoolCodeAndWhiteListProposal{
Title: title,
Description: description,
WASMByteCode: wasm,
}
return content, nil
}
func parseWasmByteCode(fileName string) ([]byte, error) {
if len(fileName) == 0 {
return nil, fmt.Errorf("invalid input file. Provide file argument")
}
wasm, err := os.ReadFile(fileName)
if err != nil {
return nil, err
}
// gzip the wasm file
if ioutils.IsWasm(wasm) {
wasm, err = ioutils.GzipIt(wasm)
if err != nil {
return nil, err
}
} else if !ioutils.IsGzip(wasm) {
return nil, fmt.Errorf("invalid input file. Use wasm binary or gzip")
}
return wasm, nil
}
func NewCmdMigratePoolContractsProposal() *cobra.Command {
cmd := &cobra.Command{
Use: "migrate-cw-pool-contracts [pool-ids] [new-code-id] [wasm-file-path] [flags]",
Args: cobra.ExactArgs(3),
Short: "Submit a migrate cw pool contracts proposal",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
content, err := parseMigratePoolContractsProposal(cmd, args)
if err != nil {
return err
}
from := clientCtx.GetFromAddress()
depositStr, err := cmd.Flags().GetString(govcli.FlagDeposit)
if err != nil {
return err
}
deposit, err := sdk.ParseCoinsNormalized(depositStr)
if err != nil {
return err
}
msg, err := govtypes.NewMsgSubmitProposal(content, deposit, from)
if err != nil {
return err
}
if err = msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
cmd.Flags().String(govcli.FlagTitle, "", "title of proposal")
cmd.Flags().String(govcli.FlagDescription, "", "description of proposal")
cmd.Flags().String(govcli.FlagDeposit, "", "deposit of proposal")
cmd.Flags().Bool(govcli.FlagIsExpedited, false, "If true, makes the proposal an expedited one")
cmd.Flags().String(govcli.FlagProposal, "", "Proposal file path (if this path is given, other proposal flags are ignored)")
return cmd
}
func parseMigratePoolContractsProposal(cmd *cobra.Command, args []string) (govtypes.Content, error) {
title, err := cmd.Flags().GetString(govcli.FlagTitle)
if err != nil {
return nil, err
}
description, err := cmd.Flags().GetString(govcli.FlagDescription)
if err != nil {
return nil, err
}
poolIdsStr := strings.Split(args[0], ",")
poolIds := make([]uint64, len(poolIdsStr))
for i, poolIdStr := range poolIdsStr {
poolId, err := strconv.ParseUint(poolIdStr, 10, 64)
if err != nil {
return nil, err
}
poolIds[i] = poolId
}
newCodeId, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return nil, err
}
wasm, err := parseWasmByteCode(args[2])
if err != nil {
return nil, err
}
// TODO: implement this later if needed.
emptyMigrateMsg, err := json.Marshal(struct{}{})
if err != nil {
return nil, err
}
content := &types.MigratePoolContractsProposal{
Title: title,
Description: description,
PoolIds: poolIds,
NewCodeId: newCodeId,
WASMByteCode: wasm,
MigrateMsg: emptyMigrateMsg,
}
return content, nil
}