Skip to content

Commit

Permalink
Future proofs order updating by utilising asset types. Expands testin…
Browse files Browse the repository at this point in the history
…g to accomodate. Adds shiny new time type. Expands wrapper websocket functionality definitions
  • Loading branch information
gloriousCode committed Feb 24, 2020
1 parent 734905a commit b287f45
Show file tree
Hide file tree
Showing 39 changed files with 295 additions and 113 deletions.
8 changes: 4 additions & 4 deletions cmd/gctcli/commands.go
Expand Up @@ -3017,12 +3017,12 @@ func getAuditEvent(c *cli.Context) error {
}
}

s, err := time.Parse(timeFormat, startTime)
s, err := time.Parse(common.SimpleTimeFormat, startTime)
if err != nil {
return fmt.Errorf("invalid time format for start: %v", err)
}

e, err := time.Parse(timeFormat, endTime)
e, err := time.Parse(common.SimpleTimeFormat, endTime)
if err != nil {
return fmt.Errorf("invalid time format for end: %v", err)
}
Expand All @@ -3045,8 +3045,8 @@ func getAuditEvent(c *cli.Context) error {

result, err := client.GetAuditEvent(context.Background(),
&gctrpc.GetAuditEventRequest{
StartDate: s.In(loc).Format(timeFormat),
EndDate: e.In(loc).Format(timeFormat),
StartDate: s.In(loc).Format(common.SimpleTimeFormat),
EndDate: e.In(loc).Format(common.SimpleTimeFormat),
Limit: int32(limit),
OrderBy: order,
Offset: int32(offset),
Expand Down
2 changes: 1 addition & 1 deletion exchanges/binance/binance_test.go
Expand Up @@ -552,7 +552,7 @@ func TestWsOrderUpdateHandling(t *testing.T) {
pressXToJSON := []byte(`{
"e": "executionReport",
"E": 1499405658658,
"s": "ETHBTC",
"s": "BTCUSDT",
"c": "mUvoqJxFIILMdfAW5iGSOW",
"S": "BUY",
"o": "LIMIT",
Expand Down
10 changes: 8 additions & 2 deletions exchanges/binance/binance_websocket.go
Expand Up @@ -225,6 +225,12 @@ func (b *Binance) wsHandleData(respRaw []byte) error {
Err: err,
}
}
p := currency.NewPairFromString(data.Symbol)
var a asset.Item
a, err = b.GetPairAssetType(p)
if err != nil {
return err
}
b.Websocket.DataHandler <- &order.Detail{
Price: data.Price,
Amount: data.Quantity,
Expand All @@ -235,9 +241,9 @@ func (b *Binance) wsHandleData(respRaw []byte) error {
Type: oType,
Side: oSide,
Status: oStatus,
AssetType: asset.Spot,
AssetType: a,
Date: time.Unix(0, data.OrderCreationTime*int64(time.Millisecond)),
Pair: currency.NewPairFromString(data.Symbol),
Pair: p,
}
case "listStatus":
var data wsListStauts
Expand Down
14 changes: 8 additions & 6 deletions exchanges/binance/binance_wrapper.go
Expand Up @@ -96,12 +96,14 @@ func (b *Binance) SetDefaults() {
CryptoWithdrawalFee: true,
},
WebsocketCapabilities: protocol.Features{
TradeFetching: true,
TickerFetching: true,
KlineFetching: true,
OrderbookFetching: true,
AccountInfo: true,
GetOrder: true,
TradeFetching: true,
TickerFetching: true,
KlineFetching: true,
OrderbookFetching: true,
AuthenticatedEndpoints: true,
AccountInfo: true,
GetOrder: true,
GetOrders: true,
},
WithdrawPermissions: exchange.AutoWithdrawCrypto |
exchange.NoFiatWithdrawals,
Expand Down
2 changes: 2 additions & 0 deletions exchanges/bitfinex/bitfinex_wrapper.go
Expand Up @@ -113,6 +113,8 @@ func (b *Bitfinex) SetDefaults() {
AuthenticatedEndpoints: true,
MessageCorrelation: true,
DeadMansSwitch: true,
GetOrders: true,
GetOrder: true,
},
WithdrawPermissions: exchange.AutoWithdrawCryptoWithAPIPermission |
exchange.AutoWithdrawFiatWithAPIPermission,
Expand Down
2 changes: 2 additions & 0 deletions exchanges/bitmex/bitmex_wrapper.go
Expand Up @@ -122,6 +122,8 @@ func (b *Bitmex) SetDefaults() {
AuthenticatedEndpoints: true,
AccountInfo: true,
DeadMansSwitch: true,
GetOrders: true,
GetOrder: true,
},
WithdrawPermissions: exchange.AutoWithdrawCryptoWithAPIPermission |
exchange.WithdrawCryptoWithEmail |
Expand Down
7 changes: 6 additions & 1 deletion exchanges/bitstamp/bitstamp_websocket.go
Expand Up @@ -114,10 +114,15 @@ func (b *Bitstamp) wsHandleData(respRaw []byte) error {
if wsTradeTemp.Data.Type == -1 {
side = order.Sell
}
var a asset.Item
a, err = b.GetPairAssetType(p)
if err != nil {
return err
}
b.Websocket.DataHandler <- wshandler.TradeData{
Timestamp: time.Unix(wsTradeTemp.Data.Timestamp, 0),
CurrencyPair: p,
AssetType: asset.Spot,
AssetType: a,
Exchange: b.Name,
EventType: order.UnknownType,
Price: wsTradeTemp.Data.Price,
Expand Down
9 changes: 8 additions & 1 deletion exchanges/btcmarkets/btcmarkets_websocket.go
Expand Up @@ -234,6 +234,12 @@ func (b *BTCMarkets) wsHandleData(respRaw []byte) error {
Err: err,
}
}
p := currency.NewPairFromString(orderData.MarketID)
var a asset.Item
a, err = b.GetPairAssetType(p)
if err != nil {
return err
}
b.Websocket.DataHandler <- &order.Detail{
Price: price,
Amount: originalAmount,
Expand All @@ -244,9 +250,10 @@ func (b *BTCMarkets) wsHandleData(respRaw []byte) error {
Type: oType,
Side: oSide,
Status: oStatus,
AssetType: asset.Spot,
AssetType: a,
Date: orderData.Timestamp,
Trades: trades,
Pair: p,
}
case "error":
var wsErr WsError
Expand Down
2 changes: 2 additions & 0 deletions exchanges/btcmarkets/btcmarkets_wrapper.go
Expand Up @@ -101,6 +101,8 @@ func (b *BTCMarkets) SetDefaults() {
AccountInfo: true,
Subscribe: true,
AuthenticatedEndpoints: true,
GetOrders: true,
GetOrder: true,
},
WithdrawPermissions: exchange.AutoWithdrawCrypto |
exchange.AutoWithdrawFiat,
Expand Down
32 changes: 25 additions & 7 deletions exchanges/btse/btse_websocket.go
Expand Up @@ -160,6 +160,12 @@ func (b *BTSE) wsHandleData(respRaw []byte) error {
Err: err,
}
}
p := currency.NewPairFromString(notification.Data[i].Symbol)
var a asset.Item
a, err = b.GetPairAssetType(p)
if err != nil {
return err
}
b.Websocket.DataHandler <- &order.Detail{
Price: notification.Data[i].Price,
Amount: notification.Data[i].Size,
Expand All @@ -169,9 +175,9 @@ func (b *BTSE) wsHandleData(respRaw []byte) error {
Type: oType,
Side: oSide,
Status: oStatus,
AssetType: asset.Spot,
AssetType: a,
Date: time.Unix(0, notification.Data[i].Timestamp*int64(time.Millisecond)),
Pair: currency.NewPairFromString(notification.Data[i].Symbol),
Pair: p,
}
}

Expand All @@ -186,10 +192,16 @@ func (b *BTSE) wsHandleData(respRaw []byte) error {
if tradeHistory.Data[x].Gain == -1 {
side = order.Sell
}
p := currency.NewPairFromString(strings.Replace(tradeHistory.Topic, "tradeHistory:", "", 1))
var a asset.Item
a, err = b.GetPairAssetType(p)
if err != nil {
return err
}
b.Websocket.DataHandler <- wshandler.TradeData{
Timestamp: time.Unix(0, tradeHistory.Data[x].TransactionTime*int64(time.Millisecond)),
CurrencyPair: currency.NewPairFromString(strings.Replace(tradeHistory.Topic, "tradeHistory:", "", 1)),
AssetType: asset.Spot,
CurrencyPair: p,
AssetType: a,
Exchange: b.Name,
Price: tradeHistory.Data[x].Price,
Amount: tradeHistory.Data[x].Amount,
Expand Down Expand Up @@ -236,15 +248,21 @@ func (b *BTSE) wsHandleData(respRaw []byte) error {
Amount: amount,
})
}
newOB.AssetType = asset.Spot
newOB.Pair = currency.NewPairFromString(t.Topic[strings.Index(t.Topic, ":")+1 : strings.Index(t.Topic, "_")])
p := currency.NewPairFromString(t.Topic[strings.Index(t.Topic, ":")+1 : strings.Index(t.Topic, "_")])
var a asset.Item
a, err = b.GetPairAssetType(p)
if err != nil {
return err
}
newOB.Pair = p
newOB.AssetType = a
newOB.ExchangeName = b.Name
err = b.Websocket.Orderbook.LoadSnapshot(&newOB)
if err != nil {
return err
}
b.Websocket.DataHandler <- wshandler.WebsocketOrderbookUpdate{Pair: newOB.Pair,
Asset: asset.Spot,
Asset: a,
Exchange: b.Name}
default:
b.Websocket.DataHandler <- wshandler.UnhandledMessageWarning{Message: b.Name + wshandler.UnhandledMessage + string(respRaw)}
Expand Down
2 changes: 2 additions & 0 deletions exchanges/btse/btse_wrapper.go
Expand Up @@ -95,6 +95,8 @@ func (b *BTSE) SetDefaults() {
TradeFetching: true,
Subscribe: true,
Unsubscribe: true,
GetOrders: true,
GetOrder: true,
},
WithdrawPermissions: exchange.NoAPIWithdrawalMethods,
},
Expand Down
2 changes: 1 addition & 1 deletion exchanges/coinbasepro/coinbasepro_test.go
Expand Up @@ -921,7 +921,7 @@ func TestWsOrderStuff(t *testing.T) {
}
pressXToJSON = []byte(`{
"type": "activate",
"product_id": "test-product",
"product_id": "BTC-USD",
"timestamp": "1483736448.299000",
"user_id": "12",
"profile_id": "30000727-d308-cf50-7b1c-c06deb1934fc",
Expand Down
10 changes: 8 additions & 2 deletions exchanges/coinbasepro/coinbasepro_websocket.go
Expand Up @@ -179,6 +179,12 @@ func (c *CoinbasePro) wsHandleData(respRaw []byte) error {
if wsOrder.Reason == "canceled" {
oStatus = order.Cancelled
}
p := currency.NewPairFromString(wsOrder.ProductID)
var a asset.Item
a, err = c.GetPairAssetType(p)
if err != nil {
return err
}
c.Websocket.DataHandler <- &order.Detail{
HiddenOrder: wsOrder.Private,
Price: wsOrder.Price,
Expand All @@ -194,9 +200,9 @@ func (c *CoinbasePro) wsHandleData(respRaw []byte) error {
Type: oType,
Side: oSide,
Status: oStatus,
AssetType: asset.Spot,
AssetType: a,
Date: createdDate,
Pair: currency.NewPairFromString(wsOrder.ProductID),
Pair: p,
}
case "match":
var wsOrder wsOrderReceived
Expand Down
2 changes: 2 additions & 0 deletions exchanges/coinbasepro/coinbasepro_wrapper.go
Expand Up @@ -106,6 +106,8 @@ func (c *CoinbasePro) SetDefaults() {
Unsubscribe: true,
AuthenticatedEndpoints: true,
MessageSequenceNumbers: true,
GetOrders: true,
GetOrder: true,
},
WithdrawPermissions: exchange.AutoWithdrawCryptoWithAPIPermission |
exchange.AutoWithdrawFiatWithAPIPermission,
Expand Down
2 changes: 1 addition & 1 deletion exchanges/coinbene/coinbene_test.go
Expand Up @@ -661,7 +661,7 @@ func TestWsUserOrder(t *testing.T) {
"orderId": "580721369818955776",
"direction": "openLong",
"leverage": "20",
"symbol": "ETHUSDT",
"symbol": "BTCUSDT",
"orderType": "limit",
"quantity": "7",
"orderPrice": "146.30",
Expand Down
10 changes: 8 additions & 2 deletions exchanges/coinbene/coinbene_websocket.go
Expand Up @@ -345,6 +345,12 @@ func (c *Coinbene) wsHandleData(respRaw []byte) error {
Err: err,
}
}
p := currency.NewPairFromString(orders.Data[i].Symbol)
var a asset.Item
a, err = c.GetPairAssetType(p)
if err != nil {
return err
}
c.Websocket.DataHandler <- &order.Detail{
Price: orders.Data[i].OrderPrice,
Amount: orders.Data[i].Quantity,
Expand All @@ -355,10 +361,10 @@ func (c *Coinbene) wsHandleData(respRaw []byte) error {
ID: orders.Data[i].OrderID,
Type: oType,
Status: oStatus,
AssetType: asset.PerpetualSwap,
AssetType: a,
Date: orders.Data[i].OrderTime,
Leverage: orders.Data[i].Leverage,
Pair: currency.NewPairFromString(orders.Data[i].Symbol),
Pair: p,
}
}
default:
Expand Down
2 changes: 2 additions & 0 deletions exchanges/coinbene/coinbene_wrapper.go
Expand Up @@ -108,6 +108,8 @@ func (c *Coinbene) SetDefaults() {
Subscribe: true,
Unsubscribe: true,
AuthenticatedEndpoints: true,
GetOrders: true,
GetOrder: true,
},
WithdrawPermissions: exchange.NoFiatWithdrawals |
exchange.WithdrawCryptoViaWebsiteOnly,
Expand Down
8 changes: 7 additions & 1 deletion exchanges/coinut/coinut_websocket.go
Expand Up @@ -387,6 +387,7 @@ func (c *COINUT) parseOrderContainer(oContainer *wsOrderContainer) (*order.Detai
if len(oContainer.Reasons) > 0 {
return nil, fmt.Errorf("%s - Order rejected: %v", c.Name, oContainer.Reasons)
}

o := &order.Detail{
Price: oContainer.Price,
Amount: oContainer.Quantity,
Expand All @@ -396,7 +397,6 @@ func (c *COINUT) parseOrderContainer(oContainer *wsOrderContainer) (*order.Detai
ID: orderID,
Side: oSide,
Status: oStatus,
AssetType: asset.Spot,
Date: time.Unix(0, oContainer.Timestamp),
Trades: nil,
}
Expand Down Expand Up @@ -427,6 +427,12 @@ func (c *COINUT) parseOrderContainer(oContainer *wsOrderContainer) (*order.Detai
} else {
o.Pair = currency.NewPairFromString(c.instrumentMap.LookupInstrument(oContainer.InstrumentID))
}
var a asset.Item
a, err = c.GetPairAssetType(o.Pair)
if err != nil {
return o, err
}
o.AssetType = a
return o, nil
}

Expand Down
7 changes: 4 additions & 3 deletions exchanges/exchange.go
Expand Up @@ -212,9 +212,10 @@ func (e *Base) GetAssetTypes() asset.Items {

// GetPairAssetType returns the associated asset type for the currency pair
func (e *Base) GetPairAssetType(c currency.Pair) (asset.Item, error) {
for i := range e.GetAssetTypes() {
if e.GetEnabledPairs(e.GetAssetTypes()[i]).Contains(c, true) {
return e.GetAssetTypes()[i], nil
assetTypes := e.GetAssetTypes()
for i := range assetTypes {
if e.GetEnabledPairs(assetTypes[i]).Contains(c, true) {
return assetTypes[i], nil
}
}
return "", errors.New("asset type not associated with currency pair")
Expand Down
8 changes: 4 additions & 4 deletions exchanges/gateio/gateio_test.go
Expand Up @@ -604,7 +604,7 @@ func TestWsTicker(t *testing.T) {
"method": "ticker.update",
"params":
[
"BOT_USDT",
"BTC_USDT",
{
"period": 86400,
"open": "0",
Expand All @@ -630,7 +630,7 @@ func TestWsTrade(t *testing.T) {
"method": "trades.update",
"params":
[
"ETH_USDT",
"BTC_USDT",
[
{
"id": 7172173,
Expand Down Expand Up @@ -669,7 +669,7 @@ func TestWsDepth(t *testing.T) {
]
]
},
"EOS_USDT"
"BTC_USDT"
],
"id": null
}`)
Expand Down Expand Up @@ -710,7 +710,7 @@ func TestWsOrderUpdate(t *testing.T) {
3,
{
"id": 34628963,
"market": "EOS_USDT",
"market": "BTC_USDT",
"orderType": 1,
"type": 2,
"user": 602123,
Expand Down

0 comments on commit b287f45

Please sign in to comment.