-
Notifications
You must be signed in to change notification settings - Fork 39
/
tx.go
211 lines (167 loc) · 6.66 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
package cli
import (
"fmt"
"strings"
"github.com/pkg/errors"
"github.com/classic-terra/core/x/oracle/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"
"github.com/spf13/cobra"
)
// GetTxCmd returns the transaction commands for this module
func GetTxCmd() *cobra.Command {
oracleTxCmd := &cobra.Command{
Use: "oracle",
Short: "Oracle transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
oracleTxCmd.AddCommand(
GetCmdDelegateFeederPermission(),
GetCmdAggregateExchangeRatePrevote(),
GetCmdAggregateExchangeRateVote(),
)
return oracleTxCmd
}
// GetCmdDelegateFeederPermission will create a feeder permission delegation tx and sign it with the given key.
func GetCmdDelegateFeederPermission() *cobra.Command {
cmd := &cobra.Command{
Use: "set-feeder [feeder]",
Args: cobra.ExactArgs(1),
Short: "Delegate the permission to vote for the oracle to an address",
Long: strings.TrimSpace(`
Delegate the permission to submit exchange rate votes for the oracle to an address.
Delegation can keep your validator operator key offline and use a separate replaceable key online.
$ terrad tx oracle set-feeder terra1...
where "terra1..." is the address you want to delegate your voting rights to.
`),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
// Get from address
voter := clientCtx.GetFromAddress()
// The address the right is being delegated from
validator := sdk.ValAddress(voter)
feederStr := args[0]
feeder, err := sdk.AccAddressFromBech32(feederStr)
if err != nil {
return err
}
msgs := []sdk.Msg{types.NewMsgDelegateFeedConsent(validator, feeder)}
for _, msg := range msgs {
if err := msg.ValidateBasic(); err != nil {
return err
}
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msgs...)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// GetCmdAggregateExchangeRatePrevote will create a aggregateExchangeRatePrevote tx and sign it with the given key.
func GetCmdAggregateExchangeRatePrevote() *cobra.Command {
cmd := &cobra.Command{
Use: "aggregate-prevote [salt] [exchange-rates] [validator]",
Args: cobra.RangeArgs(2, 3),
Short: "Submit an oracle aggregate prevote for the exchange rates of Luna",
Long: strings.TrimSpace(`
Submit an oracle aggregate prevote for the exchange rates of Luna denominated in multiple denoms.
The purpose of aggregate prevote is to hide aggregate exchange rate vote with hash which is formatted
as hex string in SHA256("{salt}:{exchange_rate}{denom},...,{exchange_rate}{denom}:{voter}")
# Aggregate Prevote
$ terrad tx oracle aggregate-prevote 1234 8888.0ukrw,1.243uusd,0.99usdr
where "ukrw,uusd,usdr" is the denominating currencies, and "8888.0,1.243,0.99" is the exchange rates of micro Luna in micro denoms from the voter's point of view.
If voting from a voting delegate, set "validator" to the address of the validator to vote on behalf of:
$ terrad tx oracle aggregate-prevote 1234 8888.0ukrw,1.243uusd,0.99usdr terravaloper1...
`),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
salt := args[0]
exchangeRatesStr := args[1]
_, err = types.ParseExchangeRateTuples(exchangeRatesStr)
if err != nil {
return fmt.Errorf("given exchange_rates {%s} is not a valid format; exchange_rate should be formatted as DecCoins; %s", exchangeRatesStr, err.Error())
}
// Get from address
voter := clientCtx.GetFromAddress()
// By default the voter is voting on behalf of itself
validator := sdk.ValAddress(voter)
// Override validator if validator is given
if len(args) == 3 {
parsedVal, err := sdk.ValAddressFromBech32(args[2])
if err != nil {
return errors.Wrap(err, "validator address is invalid")
}
validator = parsedVal
}
hash := types.GetAggregateVoteHash(salt, exchangeRatesStr, validator)
msgs := []sdk.Msg{types.NewMsgAggregateExchangeRatePrevote(hash, voter, validator)}
for _, msg := range msgs {
if err := msg.ValidateBasic(); err != nil {
return err
}
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msgs...)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// GetCmdAggregateExchangeRateVote will create a aggregateExchangeRateVote tx and sign it with the given key.
func GetCmdAggregateExchangeRateVote() *cobra.Command {
cmd := &cobra.Command{
Use: "aggregate-vote [salt] [exchange-rates] [validator]",
Args: cobra.RangeArgs(2, 3),
Short: "Submit an oracle aggregate vote for the exchange_rates of Luna",
Long: strings.TrimSpace(`
Submit a aggregate vote for the exchange_rates of Luna w.r.t the input denom. Companion to a prevote submitted in the previous vote period.
$ terrad tx oracle aggregate-vote 1234 8888.0ukrw,1.243uusd,0.99usdr
where "ukrw,uusd,usdr" is the denominating currencies, and "8888.0,1.243,0.99" is the exchange rates of micro Luna in micro denoms from the voter's point of view.
"salt" should match the salt used to generate the SHA256 hex in the aggregated pre-vote.
If voting from a voting delegate, set "validator" to the address of the validator to vote on behalf of:
$ terrad tx oracle aggregate-vote 1234 8888.0ukrw,1.243uusd,0.99usdr terravaloper1....
`),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
salt := args[0]
exchangeRatesStr := args[1]
_, err = types.ParseExchangeRateTuples(exchangeRatesStr)
if err != nil {
return fmt.Errorf("given exchange_rate {%s} is not a valid format; exchange rate should be formatted as DecCoin; %s", exchangeRatesStr, err.Error())
}
// Get from address
voter := clientCtx.GetFromAddress()
// By default the voter is voting on behalf of itself
validator := sdk.ValAddress(voter)
// Override validator if validator is given
if len(args) == 3 {
parsedVal, err := sdk.ValAddressFromBech32(args[2])
if err != nil {
return errors.Wrap(err, "validator address is invalid")
}
validator = parsedVal
}
msgs := []sdk.Msg{types.NewMsgAggregateExchangeRateVote(salt, exchangeRatesStr, voter, validator)}
for _, msg := range msgs {
if err := msg.ValidateBasic(); err != nil {
return err
}
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msgs...)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}