/
tx.go
240 lines (198 loc) · 6.27 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
package cli
import (
"errors"
"strconv"
"strings"
"time"
"github.com/spf13/cobra"
govcli "github.com/cosmos/cosmos-sdk/x/gov/client/cli"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govtypesv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
"github.com/osmosis-labs/osmosis/osmoutils/osmocli"
"github.com/osmosis-labs/osmosis/v24/x/incentives/types"
lockuptypes "github.com/osmosis-labs/osmosis/v24/x/lockup/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// GetTxCmd returns the transaction commands for this module.
func GetTxCmd() *cobra.Command {
cmd := osmocli.TxIndexCmd(types.ModuleName)
cmd.AddCommand(
NewCreateGaugeCmd(),
NewAddToGaugeCmd(),
NewCreateGroupCmd(),
)
return cmd
}
// NewCreateGaugeCmd broadcasts a CreateGauge message.
func NewCreateGaugeCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create-gauge [lockup_denom] [reward] [poolId] [flags]",
Short: "create a gauge to distribute rewards to users. For duration lock gauges set poolId = 0 and for all CL (no-lock) gauges set it to a CL poolId.",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
denom := args[0]
txf, err := tx.NewFactoryCLI(clientCtx, cmd.Flags())
if err != nil {
return err
}
txf = txf.WithTxConfig(clientCtx.TxConfig).WithAccountRetriever(clientCtx.AccountRetriever)
coins, err := sdk.ParseCoinsNormalized(args[1])
if err != nil {
return err
}
var startTime time.Time
timeStr, err := cmd.Flags().GetString(FlagStartTime)
if err != nil {
return err
}
if timeStr == "" { // empty start time
startTime = time.Unix(0, 0)
} else if timeUnix, err := strconv.ParseInt(timeStr, 10, 64); err == nil { // unix time
startTime = time.Unix(timeUnix, 0)
} else if timeRFC, err := time.Parse(time.RFC3339, timeStr); err == nil { // RFC time
startTime = timeRFC
} else { // invalid input
return errors.New("invalid start time format")
}
epochs, err := cmd.Flags().GetUint64(FlagEpochs)
if err != nil {
return err
}
perpetual, err := cmd.Flags().GetBool(FlagPerpetual)
if err != nil {
return err
}
if perpetual {
epochs = 1
}
duration, err := cmd.Flags().GetDuration(FlagDuration)
if err != nil {
return err
}
poolId, err := strconv.ParseUint(args[2], 10, 64)
if err != nil {
return err
}
var distributeTo lockuptypes.QueryCondition
// if poolId is 0 it is a guaranteed lock gauge
// if poolId is > 0 it is a guaranteed no-lock gauge
if poolId == 0 {
distributeTo = lockuptypes.QueryCondition{
LockQueryType: lockuptypes.ByDuration,
Denom: denom,
Duration: duration,
Timestamp: time.Unix(0, 0), // XXX check
}
} else if poolId > 0 {
distributeTo = lockuptypes.QueryCondition{
LockQueryType: lockuptypes.NoLock,
Duration: duration,
}
}
msg := types.NewMsgCreateGauge(
epochs == 1,
clientCtx.GetFromAddress(),
distributeTo,
coins,
startTime,
epochs,
poolId,
)
return tx.GenerateOrBroadcastTxWithFactory(clientCtx, txf, msg)
},
}
cmd.Flags().AddFlagSet(FlagSetCreateGauge())
flags.AddTxFlagsToCmd(cmd)
return cmd
}
func NewAddToGaugeCmd() *cobra.Command {
return osmocli.BuildTxCli[*types.MsgAddToGauge](&osmocli.TxCliDesc{
Use: "add-to-gauge",
Short: "add coins to gauge to distribute more rewards to users",
})
}
func NewCreateGroupCmd() *cobra.Command {
return osmocli.BuildTxCli[*types.MsgCreateGroup](&osmocli.TxCliDesc{
Use: "create-group",
Short: "create a group in order to split incentives between pools",
})
}
// NewCmdHandleCreateGroupsProposal implements a command handler for the group creation proposal transaction.
func NewCmdHandleCreateGroupsProposal() *cobra.Command {
cmd := &cobra.Command{
Use: "create-groups-proposal [pool-id-pairs] [flags]",
Args: cobra.ExactArgs(1),
Short: "Submit a create groups proposal",
Long: strings.TrimSpace(`Submit a create groups proposal.
Passing in pool-id-pairs separated by commas would be parsed automatically to a single set for a single group.
If a semicolon is presented, that would be parsed as pool IDs for separate group.
Don't forget the single quotes around the pool IDs!
Ex) create-groups-proposal '1,2;3,4,5;6,7' ->
Group 1: Pool IDs 1, 2
Group 2: Pool IDs 3, 4, 5
Group 3: Pool IDs 6, 7
`),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, proposalTitle, summary, deposit, isExpedited, authority, err := osmocli.GetProposalInfo(cmd)
if err != nil {
return err
}
content, err := parseCreateGroupArgToContent(cmd, args[0])
if err != nil {
return err
}
contentMsg, err := v1.NewLegacyContent(content, authority.String())
if err != nil {
return err
}
msg := v1.NewMsgExecLegacyContent(contentMsg.Content, authority.String())
proposalMsg, err := v1.NewMsgSubmitProposal([]sdk.Msg{msg}, deposit, clientCtx.GetFromAddress().String(), "", proposalTitle, summary, isExpedited)
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), proposalMsg)
},
}
osmocli.AddCommonProposalFlags(cmd)
return cmd
}
func parseCreateGroupArgToContent(cmd *cobra.Command, arg string) (govtypesv1beta1.Content, error) {
title, err := cmd.Flags().GetString(govcli.FlagTitle)
if err != nil {
return nil, err
}
description, err := cmd.Flags().GetString(govcli.FlagSummary)
if err != nil {
return nil, err
}
createGroupRecords, err := ParseCreateGroupRecords(arg)
if err != nil {
return nil, err
}
content := &types.CreateGroupsProposal{
Title: title,
Description: description,
CreateGroups: createGroupRecords,
}
return content, nil
}
func ParseCreateGroupRecords(arg string) ([]types.CreateGroup, error) {
poolIds2DArray, err := osmocli.ParseStringTo2DArray(arg)
if err != nil {
return nil, err
}
createGroupRecords := []types.CreateGroup{}
for _, poolIds := range poolIds2DArray {
createGroupRecords = append(createGroupRecords, types.CreateGroup{
PoolIds: poolIds,
})
}
return createGroupRecords, nil
}