-
Notifications
You must be signed in to change notification settings - Fork 209
/
tx.go
139 lines (112 loc) · 2.99 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
package cli
import (
"fmt"
cltypes "github.com/akash-network/akash-api/go/node/client/types"
sdkclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"
types "github.com/akash-network/akash-api/go/node/provider/v1beta3"
aclient "github.com/akash-network/node/client"
"github.com/akash-network/node/x/provider/config"
)
// GetTxCmd returns the transaction commands for provider module
func GetTxCmd(key string) *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Short: "Provider transaction subcommands",
SuggestionsMinimumDistance: 2,
RunE: sdkclient.ValidateCmd,
}
cmd.AddCommand(
cmdCreate(key),
cmdUpdate(key),
)
return cmd
}
func cmdCreate(key string) *cobra.Command {
cmd := &cobra.Command{
Use: "create [config-file]",
Short: fmt.Sprintf("Create a %s", key),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
cctx, err := sdkclient.GetClientTxContext(cmd)
if err != nil {
return err
}
opts, err := cltypes.ClientOptionsFromFlags(cmd.Flags())
if err != nil {
return err
}
cl, err := aclient.DiscoverClient(ctx, cctx, opts...)
if err != nil {
return err
}
// TODO: enable reading files with non-local URIs
cfg, err := config.ReadConfigPath(args[0])
if err != nil {
err = fmt.Errorf("%w: ReadConfigPath err: %q", err, args[0])
return err
}
msg := &types.MsgCreateProvider{
Owner: cctx.GetFromAddress().String(),
HostURI: cfg.Host,
Info: cfg.Info,
Attributes: cfg.GetAttributes(),
}
if err := msg.ValidateBasic(); err != nil {
return err
}
resp, err := cl.Tx().Broadcast(ctx, []sdk.Msg{msg})
if err != nil {
return err
}
return cl.PrintMessage(resp)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
func cmdUpdate(key string) *cobra.Command {
cmd := &cobra.Command{
Use: "update [config-file]",
Short: fmt.Sprintf("Update %s", key),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
cctx, err := sdkclient.GetClientTxContext(cmd)
if err != nil {
return err
}
opts, err := cltypes.ClientOptionsFromFlags(cmd.Flags())
if err != nil {
return err
}
cl, err := aclient.DiscoverClient(ctx, cctx, opts...)
if err != nil {
return err
}
cfg, err := config.ReadConfigPath(args[0])
if err != nil {
return err
}
msg := &types.MsgUpdateProvider{
Owner: cctx.GetFromAddress().String(),
HostURI: cfg.Host,
Info: cfg.Info,
Attributes: cfg.GetAttributes(),
}
if err := msg.ValidateBasic(); err != nil {
return err
}
resp, err := cl.Tx().Broadcast(ctx, []sdk.Msg{msg})
if err != nil {
return err
}
return cl.PrintMessage(resp)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}