-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx_order.go
152 lines (135 loc) · 4.57 KB
/
tx_order.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
package cli
import (
"fmt"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/coinexchain/cet-sdk/modules/market/internal/types"
"github.com/coinexchain/cosmos-utils/client/cliutil"
)
const (
FlagSymbol = "trading-pair"
FlagOrderType = "order-type"
FlagPrice = "price"
FlagQuantity = "quantity"
FlagSide = "side"
FlagOrderID = "order-id"
FlagBlocks = "blocks"
FlagTime = "time"
FlagIdentify = "identify"
)
var createOrderFlags = []string{
FlagSymbol,
FlagOrderType,
FlagPrice,
FlagQuantity,
FlagSide,
FlagPricePrecision,
FlagIdentify,
}
func CreateIOCOrderTxCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "create-ioc-order",
Short: "Create an IOC order and sign tx",
Long: `Create an IOC order and sign tx, broadcast to nodes.
Example:
cetcli tx market create-ioc-order --trading-pair=btc/cet \
--order-type=2 --price=520 --quantity=10000000 \
--side=1 --price-precision=10 --from=bob --identify=1 \
--chain-id=coinexdex --gas=10000 --fees=1000cet`,
RunE: func(cmd *cobra.Command, args []string) error {
return createAndBroadCastOrder(cdc, false)
},
}
markCreateOrderFlags(cmd)
return cmd
}
func CreateGTEOrderTxCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "create-gte-order",
Short: "Create an GTE order and sign tx",
Long: `Create an GTE order and sign tx, broadcast to nodes.
Example:
cetcli tx market create-gte-order --trading-pair=btc/cet \
--order-type=2 --price=520 --quantity=10000000 --side=1 \
--price-precision=10 --blocks=100000 --from=bob --identify=1 \
--chain-id=coinexdex --gas=10000 --fees=1000cet`,
RunE: func(cmd *cobra.Command, args []string) error {
return createAndBroadCastOrder(cdc, true)
},
}
markCreateOrderFlags(cmd)
cmd.Flags().Int(FlagBlocks, 10000, "the gte order will exist at least blocks in blockChain")
return cmd
}
func createAndBroadCastOrder(cdc *codec.Codec, isGTE bool) error {
msg, err := parseCreateOrderFlags(isGTE)
if err != nil {
if isGTE {
return errors.Errorf("errors : %s, please see help : "+
"$ cetcli tx market create-gte-order -h", err.Error())
}
return errors.Errorf("errors : %s, please see help : "+
"$ cetcli tx market create-ioc-order -h", err.Error())
}
return cliutil.CliRunCommand(cdc, msg)
}
func parseCreateOrderFlags(isGTE bool) (*types.MsgCreateOrder, error) {
for _, flag := range createOrderFlags {
if viper.Get(flag) == nil {
return nil, fmt.Errorf("--%s flag is a noop" + flag)
}
}
msg := &types.MsgCreateOrder{
Identify: byte(viper.GetInt(FlagIdentify)),
TradingPair: viper.GetString(FlagSymbol),
OrderType: byte(viper.GetInt(FlagOrderType)),
Side: byte(viper.GetInt(FlagSide)),
Price: viper.GetInt64(FlagPrice),
PricePrecision: byte(viper.GetInt(FlagPricePrecision)),
Quantity: viper.GetInt64(FlagQuantity),
ExistBlocks: viper.GetInt64(FlagBlocks),
TimeInForce: types.IOC,
}
if isGTE {
msg.TimeInForce = types.GTE
}
return msg, nil
}
func markCreateOrderFlags(cmd *cobra.Command) {
cmd.Flags().String(FlagSymbol, "", "The trading pair symbol")
cmd.Flags().Int(FlagOrderType, 2, "The identify of the price limit : 2; (Currently, only price limit orders are supported)")
cmd.Flags().Int(FlagPrice, 100, "The price of the order")
cmd.Flags().Int(FlagQuantity, 100, "The number of tokens will be trade in the order ")
cmd.Flags().Int(FlagSide, 1, "The buying or selling direction of an order.(buy : 1; sell : 2)")
cmd.Flags().Int(FlagPricePrecision, 8, "The price precision in the order")
cmd.Flags().Int(FlagIdentify, 0, "A transaction can contain multiple order "+
"creation messages, the identify field was added to the order creation message to give each "+
"order a unique ID. So the order ID consists of user address, user sequence, identify.")
for _, flag := range createOrderFlags {
cmd.MarkFlagRequired(flag)
}
}
func CancelOrder(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "cancel-order",
Short: "cancel order in blockchain",
Long: `cancel order in blockchain.
Examples:
cetcli tx market cancel-order --order-id=[id] \
--trust-node=true --from=bob --chain-id=coinexdex`,
RunE: func(cmd *cobra.Command, args []string) error {
msg := &types.MsgCancelOrder{
OrderID: viper.GetString(FlagOrderID),
}
return cliutil.CliRunCommand(cdc, msg)
},
}
markQueryOrDelCmd(cmd)
return cmd
}
func markQueryOrDelCmd(cmd *cobra.Command) {
cmd.Flags().String(FlagOrderID, "", "The order id")
cmd.MarkFlagRequired(FlagOrderID)
}