/
tx.go
133 lines (116 loc) · 3.85 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
package cli
import (
"bufio"
"fmt"
"strings"
"github.com/cosmos/cosmos-sdk/client/context"
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"
authtxb "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/omexapp/omexchain/x/order/types"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// GetTxCmd returns the transaction commands for this module
func GetTxCmd(cdc *codec.Codec) *cobra.Command {
txCmd := &cobra.Command{
Use: "order",
Short: "Order transactions subcommands",
}
txCmd.AddCommand(client.PostCommands(
getCmdNewOrder(cdc),
getCmdCancelOrder(cdc),
)...)
return txCmd
}
func getCmdNewOrder(cdc *codec.Codec) *cobra.Command {
// new order flags
var product string
var side string
var price string
var quantity string
cmd := &cobra.Command{
Use: "new",
Short: "place a new order",
RunE: func(cmd *cobra.Command, args []string) error {
if len(product) == 0 || len(side) == 0 || len(price) == 0 || len(quantity) == 0 {
return errors.New("invalid param format")
}
if len(args) > 0 {
return errors.New(`invalid param format. tips:use comma "," to place multi orders`)
}
productArr := strings.Split(product, ",")
if len(productArr) == 0 {
return errors.New("invalid param counts")
}
err := handleNewOrder(cmd, cdc, product, side, price, quantity)
return err
},
}
cmd.Flags().StringVarP(&product, "product", "", "", "Trading pair in full name of the tokens: ${baseAssetSymbol}_${quoteAssetSymbol}, for example \"mycoin_omt\".")
cmd.Flags().StringVarP(&side, "side", "s", "", "BUY or SELL (default \"SELL\")")
cmd.Flags().StringVarP(&price, "price", "p", "", "The price of the order")
cmd.Flags().StringVarP(&quantity, "quantity", "q", "", "The quantity of the order")
return cmd
}
func handleNewOrder(cmd *cobra.Command, cdc *codec.Codec, product string, side string, price string, quantity string) error {
var items []types.OrderItem
productArr := strings.Split(product, ",")
sideArr := strings.Split(side, ",")
priceArr := strings.Split(price, ",")
quantityArr := strings.Split(quantity, ",")
if len(productArr) != len(sideArr) {
return errors.New("invalid param side counts")
}
if len(productArr) != len(priceArr) {
return errors.New("invalid param price counts")
}
if len(productArr) != len(quantityArr) {
return errors.New("invalid param quantity counts")
}
for i := 0; i < len(productArr); i++ {
product := productArr[i]
side := sideArr[i]
price, err := sdk.NewDecFromStr(priceArr[i])
if err != nil {
return errors.New(err.Error())
}
quantity, err := sdk.NewDecFromStr(quantityArr[i])
if err != nil {
return errors.New(err.Error())
}
items = append(items, types.OrderItem{
Product: product,
Side: side,
Price: price,
Quantity: quantity,
})
}
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := authtxb.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
cliCtx := context.NewCLIContext().WithCodec(cdc)
msg := types.NewMsgNewOrders(cliCtx.GetFromAddress(), items)
err := utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
return err
}
func getCmdCancelOrder(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "cancel [order-id]",
Short: "cancel order",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
orderIDs := strings.Split(args[0], ",")
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := authtxb.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
cliCtx := context.NewCLIContext().WithCodec(cdc)
msg := types.NewMsgCancelOrders(cliCtx.GetFromAddress(), orderIDs)
err := utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
if err != nil {
fmt.Println(err)
}
return err
},
}
}