From 50bbdabf434e799ad263e398138e9353f15d61aa Mon Sep 17 00:00:00 2001 From: Scott Date: Thu, 25 Feb 2021 17:13:21 +1100 Subject: [PATCH] BugFix: RPCServer cannot retrieve open orders/getOrder due to unset asset type (#634) * Fixes issue where getorders could not work due to unset asset type in rpcserver.go. Adds test. Also adds start and end date to the cli. * A few fixes * lint * fixes oopsie that affected doopsie * Ensures dates are set for all open order implementations. Adds new filter to ensure orders without dates are returned rather than filtered. Fixes up Binance OpenOrders implementation. Adds some extra typeconverts for binance * Add updated time to Binance GetActiveOrders. Update rpcserver.go to only set the time if its not empty. Also addressed bad expected value * Actually fixes things this time * Improves recvWindow to process openOrders * Adds asset type to getOrder as well * Fixes tests * Adds missing date fields * Fixes default time, updates default errors * Default start to last month, instead of last year --- cmd/gctcli/commands.go | 121 +- engine/orders.go | 2 +- engine/orders_types.go | 5 + engine/rpcserver.go | 143 +- engine/rpcserver_test.go | 181 +- exchanges/alphapoint/alphapoint_wrapper.go | 4 +- exchanges/asset/asset.go | 8 +- exchanges/binance/binance.go | 35 +- exchanges/binance/binance_cfutures.go | 4 +- exchanges/binance/binance_test.go | 18 +- exchanges/binance/binance_wrapper.go | 245 +- exchanges/binance/cfutures_types.go | 48 +- exchanges/binance/type_convert.go | 54 + exchanges/binance/ufutures_types.go | 95 +- exchanges/bitfinex/bitfinex_wrapper.go | 4 +- exchanges/bithumb/bithumb_wrapper.go | 4 +- exchanges/bitmex/bitmex_wrapper.go | 5 +- exchanges/bitstamp/bitstamp_wrapper.go | 4 +- exchanges/bittrex/bittrex_test.go | 4 +- exchanges/bittrex/bittrex_wrapper.go | 4 +- exchanges/btcmarkets/btcmarkets_wrapper.go | 2 +- exchanges/btse/btse_wrapper.go | 2 +- exchanges/coinbasepro/coinbasepro_wrapper.go | 4 +- exchanges/coinut/coinut_wrapper.go | 4 +- exchanges/exmo/exmo_wrapper.go | 4 +- exchanges/ftx/ftx_wrapper.go | 6 +- exchanges/gateio/gateio_wrapper.go | 4 +- exchanges/gemini/gemini_wrapper.go | 6 +- exchanges/hitbtc/hitbtc_wrapper.go | 4 +- exchanges/huobi/huobi_wrapper.go | 8 +- exchanges/itbit/itbit_wrapper.go | 4 +- exchanges/kraken/kraken_wrapper.go | 10 +- exchanges/lakebtc/lakebtc_wrapper.go | 4 +- .../localbitcoins/localbitcoins_wrapper.go | 8 +- exchanges/order/order_test.go | 21 +- exchanges/order/order_types.go | 10 +- exchanges/order/orders.go | 21 +- exchanges/poloniex/poloniex_wrapper.go | 6 +- exchanges/yobit/yobit_test.go | 4 +- exchanges/yobit/yobit_wrapper.go | 6 +- exchanges/zb/zb_wrapper.go | 4 +- gctrpc/rpc.pb.go | 2520 +++++++++-------- gctrpc/rpc.proto | 3 + gctrpc/rpc.swagger.json | 9 + gctrpc/rpc_grpc.pb.go | 22 +- 45 files changed, 2066 insertions(+), 1618 deletions(-) diff --git a/cmd/gctcli/commands.go b/cmd/gctcli/commands.go index 82e77637..8a6f4108 100644 --- a/cmd/gctcli/commands.go +++ b/cmd/gctcli/commands.go @@ -16,6 +16,7 @@ import ( "github.com/thrasher-corp/gocryptotrader/currency" "github.com/thrasher-corp/gocryptotrader/gctrpc" "github.com/urfave/cli" + "google.golang.org/grpc" ) var startTime, endTime, order string @@ -1228,7 +1229,7 @@ func getForexRates(_ *cli.Context) error { var getOrdersCommand = cli.Command{ Name: "getorders", Usage: "gets the open orders", - ArgsUsage: " ", + ArgsUsage: " ", Action: getOrders, Flags: []cli.Flag{ cli.StringFlag{ @@ -1243,10 +1244,26 @@ var getOrdersCommand = cli.Command{ Name: "pair", Usage: "the currency pair to get orders for", }, + cli.StringFlag{ + Name: "start", + Usage: "start date, optional. Will filter any results before this date", + Value: time.Now().AddDate(0, -1, 0).Format(common.SimpleTimeFormat), + Destination: &startTime, + }, + cli.StringFlag{ + Name: "end", + Usage: "end date, optional. Will filter any results after this date", + Value: time.Now().Format(common.SimpleTimeFormat), + Destination: &endTime, + }, }, } func getOrders(c *cli.Context) error { + if c.NArg() == 0 && c.NumFlags() == 0 { + return cli.ShowCommandHelp(c, "getorders") + } + var exchangeName string var assetType string var currencyPair string @@ -1287,7 +1304,33 @@ func getOrders(c *cli.Context) error { return err } - conn, err := setupClient() + if !c.IsSet("start") { + if c.Args().Get(3) != "" { + startTime = c.Args().Get(3) + } + } + + if !c.IsSet("end") { + if c.Args().Get(4) != "" { + endTime = c.Args().Get(4) + } + } + var s, e time.Time + s, err = time.Parse(common.SimpleTimeFormat, startTime) + if err != nil { + return fmt.Errorf("invalid time format for start: %v", err) + } + e, err = time.Parse(common.SimpleTimeFormat, endTime) + if err != nil { + return fmt.Errorf("invalid time format for end: %v", err) + } + + if e.Before(s) { + return errors.New("start cannot be after end") + } + + var conn *grpc.ClientConn + conn, err = setupClient() if err != nil { return err } @@ -1302,6 +1345,8 @@ func getOrders(c *cli.Context) error { Base: p.Base.String(), Quote: p.Quote.String(), }, + StartDate: negateLocalOffset(s), + EndDate: negateLocalOffset(e), }) if err != nil { return err @@ -1322,13 +1367,17 @@ var getOrderCommand = cli.Command{ Usage: "the exchange to get the order for", }, cli.StringFlag{ - Name: "order_id", - Usage: "the order id to retrieve", + Name: "asset", + Usage: "required asset type", }, cli.StringFlag{ Name: "pair", Usage: "the pair to retrieve", }, + cli.StringFlag{ + Name: "order_id", + Usage: "the order id to retrieve", + }, }, } @@ -1340,19 +1389,32 @@ func getOrder(c *cli.Context) error { var exchangeName string var orderID string var currencyPair string - - if c.IsSet("pair") { - currencyPair = c.String("pair") - } else { - currencyPair = c.Args().Get(2) - } + var assetType string if c.IsSet("exchange") { exchangeName = c.String("exchange") } else { exchangeName = c.Args().First() } + if !validExchange(exchangeName) { + return errInvalidExchange + } + if c.IsSet("asset") { + assetType = c.String("asset") + } else { + assetType = c.Args().Get(1) + } + assetType = strings.ToLower(assetType) + if !validAsset(assetType) { + return errInvalidAsset + } + + if c.IsSet("pair") { + currencyPair = c.String("pair") + } else { + currencyPair = c.Args().Get(2) + } if !validPair(currencyPair) { return errInvalidPair } @@ -1362,14 +1424,10 @@ func getOrder(c *cli.Context) error { return err } - if !validExchange(exchangeName) { - return errInvalidExchange - } - if c.IsSet("order_id") { orderID = c.String("order_id") } else { - orderID = c.Args().Get(1) + orderID = c.Args().Get(3) } conn, err := setupClient() @@ -1387,6 +1445,7 @@ func getOrder(c *cli.Context) error { Base: p.Base.String(), Quote: p.Quote.String(), }, + Asset: assetType, }) if err != nil { return err @@ -2702,7 +2761,7 @@ var withdrawalRequestCommand = cli.Command{ Flags: []cli.Flag{ cli.StringFlag{ Name: "id", - Usage: "", + Usage: "withdrawal id", }, }, Action: withdrawlRequestByID, @@ -2714,11 +2773,11 @@ var withdrawalRequestCommand = cli.Command{ Flags: []cli.Flag{ cli.StringFlag{ Name: "exchange", - Usage: "", + Usage: "exchange name", }, cli.StringFlag{ Name: "id", - Usage: "", + Usage: "withdrawal id", }, }, Action: withdrawlRequestByExchangeID, @@ -2730,11 +2789,11 @@ var withdrawalRequestCommand = cli.Command{ Flags: []cli.Flag{ cli.StringFlag{ Name: "exchange", - Usage: "", + Usage: "exchange name", }, cli.Int64Flag{ Name: "limit", - Usage: "", + Usage: "max number of withdrawals to return", }, cli.StringFlag{ Name: "currency", @@ -2750,23 +2809,23 @@ var withdrawalRequestCommand = cli.Command{ Flags: []cli.Flag{ cli.StringFlag{ Name: "exchange", - Usage: "", + Usage: "the currency used in to withdraw", }, cli.StringFlag{ Name: "start", - Usage: "", + Usage: "the start date to get withdrawals from. Any withdrawal before this date will be filtered", Value: time.Now().AddDate(0, -1, 0).Format(common.SimpleTimeFormat), Destination: &startTime, }, cli.StringFlag{ Name: "end", - Usage: "", + Usage: "the end date to get withdrawals from. Any withdrawal after this date will be filtered", Value: time.Now().Format(common.SimpleTimeFormat), Destination: &endTime, }, cli.Int64Flag{ Name: "limit", - Usage: "", + Usage: "max number of withdrawals to return", }, }, Action: withdrawlRequestByDate, @@ -3575,12 +3634,12 @@ var gctScriptCommand = cli.Command{ Flags: []cli.Flag{ cli.StringFlag{ Name: "filename", - Usage: "", + Usage: "the script filename", Destination: &filename, }, cli.StringFlag{ Name: "path", - Usage: "