-
Notifications
You must be signed in to change notification settings - Fork 7
/
tx.go
117 lines (100 loc) · 2.83 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
package cli
import (
"bufio"
"strconv"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
"github.com/sharering/shareledger/x/booking/types"
"github.com/sharering/shareledger/x/myutils"
)
const (
bookFee = 0.05
completeFee = 0.03
)
func GetTxCmd(storeKey string, cdc *codec.Codec) *cobra.Command {
bookCmd := &cobra.Command{
Use: types.ModuleName,
Short: "booking transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
bookCmd.AddCommand(flags.PostCommands(
GetCmdBook(cdc),
GetCmdComplete(cdc),
)...)
return bookCmd
}
func GetCmdBook(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "book [uuid] [duration]",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
keySeed := viper.GetString(myutils.FlagKeySeed)
seed, err := myutils.GetKeeySeedFromFile(keySeed)
if err != nil {
return err
}
cliCtx, txBldr, err := myutils.GetTxBldrAndCtxFromSeed(inBuf, cdc, seed)
if err != nil {
return err
}
txFee, err := myutils.GetFeeFromShrp(cdc, cliCtx, bookFee)
if err != nil {
return err
}
txBldr = txBldr.WithFees(txFee)
uuid := args[0]
duration, err := strconv.Atoi(args[1])
if err != nil {
return err
}
msg := types.NewMsgBook(cliCtx.GetFromAddress(), uuid, int64(duration))
err = msg.ValidateBasic()
if err != nil {
return err
}
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
cmd.Flags().String(myutils.FlagKeySeed, "", "path to key_seed.json")
return cmd
}
func GetCmdComplete(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "complete [bookID]",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
keySeed := viper.GetString(myutils.FlagKeySeed)
seed, err := myutils.GetKeeySeedFromFile(keySeed)
if err != nil {
return err
}
cliCtx, txBldr, err := myutils.GetTxBldrAndCtxFromSeed(inBuf, cdc, seed)
if err != nil {
return err
}
txFee, err := myutils.GetFeeFromShrp(cdc, cliCtx, completeFee)
if err != nil {
return err
}
txBldr = txBldr.WithFees(txFee)
bookID := args[0]
msg := types.NewMsgComplete(cliCtx.GetFromAddress(), bookID)
err = msg.ValidateBasic()
if err != nil {
return err
}
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
cmd.Flags().String(myutils.FlagKeySeed, "", "path to key_seed.json")
return cmd
}