-
Notifications
You must be signed in to change notification settings - Fork 340
/
tx.go
83 lines (66 loc) · 2.1 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
package cli
import (
"fmt"
"strconv"
"time"
"github.com/spf13/cobra"
"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/cosmos/cosmos-sdk/version"
tmtime "github.com/tendermint/tendermint/types/time"
"github.com/kava-labs/kava/x/pricefeed/types"
)
// GetTxCmd returns the transaction commands for this module
func GetTxCmd() *cobra.Command {
pricefeedTxCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Pricefeed transactions subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
cmds := []*cobra.Command{
GetCmdPostPrice(),
}
for _, cmd := range cmds {
flags.AddTxFlagsToCmd(cmd)
}
pricefeedTxCmd.AddCommand(cmds...)
return pricefeedTxCmd
}
// GetCmdPostPrice cli command for posting prices.
func GetCmdPostPrice() *cobra.Command {
return &cobra.Command{
Use: "postprice [marketID] [price] [expiry]",
Short: "post the latest price for a particular market with a given expiry as a UNIX time",
Example: fmt.Sprintf("%s tx %s postprice bnb:usd 25 9999999999 --from validator",
version.AppName, types.ModuleName),
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
price, err := sdk.NewDecFromStr(args[1])
if err != nil {
return err
}
expiryInt, err := strconv.ParseInt(args[2], 10, 64)
if err != nil {
return fmt.Errorf("invalid expiry %s: %w", args[2], err)
}
if expiryInt > types.MaxExpiry {
return fmt.Errorf("invalid expiry; got %d, max: %d", expiryInt, types.MaxExpiry)
}
expiry := tmtime.Canonical(time.Unix(expiryInt, 0))
from := clientCtx.GetFromAddress()
msg := types.NewMsgPostPrice(from.String(), args[0], price, expiry)
if err = msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
}