Skip to content

Commit

Permalink
Linter. Fix: open orders updates
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed May 5, 2021
1 parent ff3078a commit a4f8e9a
Show file tree
Hide file tree
Showing 18 changed files with 337 additions and 351 deletions.
11 changes: 11 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
linters:
enable:
- bodyclose
- goconst
- gocritic
- gofmt
- interfacer
- maligned
- prealloc
- unconvert
- unparam
5 changes: 4 additions & 1 deletion examples/auth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import (
)

func main() {
c := ws.NewAuth("", "")
c := ws.NewAuth(
"", "",
ws.WithParams(ws.NewDefaultSandboxAuthParameters()),
)

if err := c.Connect(); err != nil {
log.Fatal("Error connecting to web socket : ", err)
Expand Down
11 changes: 5 additions & 6 deletions examples/trades/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,23 @@ import (

func main() {
c := ws.New()
err := c.Connect()
if err != nil {

if err := c.Connect(); err != nil {
log.Fatal("Error connecting to web socket : ", err)
}

pairs := []string{ws.BTCUSD}

// subscribe to BTCUSD trades
err = c.SubscribeTrades(pairs)
if err != nil {
if err := c.SubscribeTrades(pairs); err != nil {
log.Fatal(err)
}

go func() {
time.Sleep(time.Second * 30)
log.Print("Unsubsribing...")
err = c.Unsubscribe(ws.ChanTrades, pairs)
if err != nil {

if err := c.Unsubscribe(ws.ChanTrades, pairs); err != nil {
log.Fatal(err)
}
log.Print("Success!")
Expand Down
26 changes: 13 additions & 13 deletions rest/methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestKraken_Assets(t *testing.T) {
assets: nil,
},
want: map[string]Asset{
"ADA": Asset{
"ADA": {
AlternateName: "ADA",
AssetClass: "currency",
Decimals: 8,
Expand All @@ -127,7 +127,7 @@ func TestKraken_Assets(t *testing.T) {
assets: []string{"ADA"},
},
want: map[string]Asset{
"ADA": Asset{
"ADA": {
AlternateName: "ADA",
AssetClass: "currency",
Decimals: 8,
Expand Down Expand Up @@ -190,7 +190,7 @@ func TestKraken_AssetPairs(t *testing.T) {
pairs: []string{"ADACAD"},
},
want: map[string]AssetPair{
"ADACAD": AssetPair{
"ADACAD": {
Altname: "ADACAD",
WSName: "ADA/CAD",
AssetClassBase: "currency",
Expand Down Expand Up @@ -222,7 +222,7 @@ func TestKraken_AssetPairs(t *testing.T) {
pairs: nil,
},
want: map[string]AssetPair{
"ADACAD": AssetPair{
"ADACAD": {
Altname: "ADACAD",
WSName: "ADA/CAD",
AssetClassBase: "currency",
Expand Down Expand Up @@ -307,7 +307,7 @@ func TestKraken_Ticker(t *testing.T) {
pairs: []string{"ADACAD"},
},
want: map[string]Ticker{
"ADACAD": Ticker{
"ADACAD": {
Ask: Level{
Price: 0.108312,
WholeLotVolume: 6418.,
Expand Down Expand Up @@ -373,7 +373,7 @@ func TestKraken_Candles(t *testing.T) {
response := OHLCResponse{
Last: 1554222360,
Candles: map[string][]Candle{
"ADACAD": []Candle{{
"ADACAD": {{
Time: 1554179640,
Open: 0.0005000,
High: 0.0005000,
Expand Down Expand Up @@ -496,26 +496,26 @@ func TestKraken_GetOrderBook(t *testing.T) {
depth: 2,
},
want: map[string]OrderBook{
"ADACAD": OrderBook{
"ADACAD": {
Asks: []OrderBookItem{
OrderBookItem{
{
Price: 0.109441,
Volume: 6741.072,
Timestamp: 1554223624,
},
OrderBookItem{
{
Price: 0.109442,
Volume: 4950.724,
Timestamp: 1554223614,
},
},
Bids: []OrderBookItem{
OrderBookItem{
{
Price: 0.090494,
Volume: 2789.652,
Timestamp: 1554223622,
},
OrderBookItem{
{
Price: 0.090493,
Volume: 6379.886,
Timestamp: 1554223620,
Expand Down Expand Up @@ -584,7 +584,7 @@ func TestKraken_GetTrades(t *testing.T) {
want: TradeResponse{
Last: "1554221914617956627",
ADACAD: []Trade{
Trade{
{
Price: 0.093280,
Volume: 2968.26413227,
Time: 1553959154.2509,
Expand Down Expand Up @@ -655,7 +655,7 @@ func TestKraken_GetSpread(t *testing.T) {
want: SpreadResponse{
Last: 1554224725,
ADACAD: []Spread{
Spread{
{
Time: 1554224145,
Ask: 0.109331,
Bid: 0.091118,
Expand Down
8 changes: 5 additions & 3 deletions rest/private_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ func (api *Kraken) QueryOrders(needTrades bool, userRef string, txIDs ...string)
if userRef != "" {
data.Set("userref", userRef)
}
if len(txIDs) > 50 {

switch {
case len(txIDs) > 50:
return nil, fmt.Errorf("Maximum count of requested orders is 50")
} else if len(txIDs) == 0 {
case len(txIDs) == 0:
return nil, fmt.Errorf("txIDs is required")
} else {
default:
data.Set("txid", strings.Join(txIDs, ","))
}

Expand Down
24 changes: 12 additions & 12 deletions rest/private_methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestKraken_GetDepositMethods(t *testing.T) {
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader(depositMethodsJSON)),
},
want: []DepositMethods{DepositMethods{Method: "Ether (Hex)", Limit: false, Fee: "0.0000000000", GenAddress: true}},
want: []DepositMethods{{Method: "Ether (Hex)", Limit: false, Fee: "0.0000000000", GenAddress: true}},
wantErr: false,
},
}
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestKraken_GetDepositStatuses(t *testing.T) {
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader(depositStatusesJSON)),
},
want: []DepositStatuses{DepositStatuses{Method: "Ether (Hex)", Aclass: "currency", Asset: "XETH", Refid: "sometest1",
want: []DepositStatuses{{Method: "Ether (Hex)", Aclass: "currency", Asset: "XETH", Refid: "sometest1",
Txid: "sometest2", Info: "sometest3", Amount: "6.91", Fee: "0.0000000000", Time: 1617014556, Status: "Success"},
},
wantErr: false,
Expand Down Expand Up @@ -244,7 +244,7 @@ func TestKraken_GetOpenOrders(t *testing.T) {
},
want: OpenOrdersResponse{
Orders: map[string]OrderInfo{
"OR3XZM-5EN2R-LS5X51": OrderInfo{
"OR3XZM-5EN2R-LS5X51": {
RefID: nil,
UserRef: nil,
Status: "open",
Expand Down Expand Up @@ -320,7 +320,7 @@ func TestKraken_GetClosedOrders(t *testing.T) {
want: ClosedOrdersResponse{
Count: 20,
Orders: map[string]OrderInfo{
"OK46ER-A2BXK-YOLKE1": OrderInfo{
"OK46ER-A2BXK-YOLKE1": {
RefID: nil,
UserRef: nil,
Status: "canceled",
Expand Down Expand Up @@ -396,7 +396,7 @@ func TestKraken_QueryOrders(t *testing.T) {
Body: ioutil.NopCloser(bytes.NewReader(queryOrdersJSON)),
},
want: map[string]OrderInfo{
"OLNYE1-H3BBJ-JD2LGC": OrderInfo{
"OLNYE1-H3BBJ-JD2LGC": {
RefID: nil,
UserRef: nil,
Status: "canceled",
Expand Down Expand Up @@ -473,7 +473,7 @@ func TestKraken_GetTradesHistory(t *testing.T) {
want: TradesHistoryResponse{
Count: 1,
Trades: map[string]PrivateTrade{
"TO3MMA-BSBGV-XUV4A1": PrivateTrade{
"TO3MMA-BSBGV-XUV4A1": {
OrderID: "OSQQQ5-MBKL6-O4YYE1",
PositionID: "TYE7IH-QCG76-BVMCM1",
Pair: "XXBTZUSD",
Expand Down Expand Up @@ -534,7 +534,7 @@ func TestKraken_QueryTrades(t *testing.T) {
Body: ioutil.NopCloser(bytes.NewReader(queryTradesJSON)),
},
want: map[string]PrivateTrade{
"TO3MMA-BSBGV-XUV4A1": PrivateTrade{
"TO3MMA-BSBGV-XUV4A1": {
OrderID: "OSQQQ5-MBKL6-O4YYE1",
PositionID: "TYE7IH-QCG76-BVMCM1",
Pair: "XXBTZUSD",
Expand Down Expand Up @@ -594,7 +594,7 @@ func TestKraken_GetOpenPositions(t *testing.T) {
Body: ioutil.NopCloser(bytes.NewReader(openPositionsJSON)),
},
want: map[string]Position{
"TYE7IH-QCG76-BVMCM1": Position{
"TYE7IH-QCG76-BVMCM1": {
OrderID: "OK7SOC-SGF3O-F54S51",
Status: "open",
Pair: "XXBTZUSD",
Expand Down Expand Up @@ -658,7 +658,7 @@ func TestKraken_GetLedgersInfo(t *testing.T) {
},
want: LedgerInfoResponse{
Ledgers: map[string]Ledger{
"LGPNZQ-2SLSA-C7QCT1": Ledger{
"LGPNZQ-2SLSA-C7QCT1": {
RefID: "TI2NBU-IICD2-BAVYO1",
Time: 1570623111.9096,
LedgerType: "rollover",
Expand Down Expand Up @@ -715,7 +715,7 @@ func TestKraken_QueryLedgers(t *testing.T) {
Body: ioutil.NopCloser(bytes.NewReader(queryLedgerJSON)),
},
want: map[string]Ledger{
"LTCH4T-LG5FS-MKGVD1": Ledger{
"LTCH4T-LG5FS-MKGVD1": {
RefID: "TYE7IH-QCG76-BVMCM1",
Time: 1570551111.2568,
LedgerType: "rollover",
Expand Down Expand Up @@ -774,7 +774,7 @@ func TestKraken_GetTradeVolume(t *testing.T) {
Currency: "ZUSD",
Volume: 1000,
Fees: map[string]Fees{
"XXBTZUSD": Fees{
"XXBTZUSD": {
Fee: 0.16,
MinFee: 0.1,
MaxFee: 0.26,
Expand All @@ -784,7 +784,7 @@ func TestKraken_GetTradeVolume(t *testing.T) {
},
},
FeesMaker: map[string]Fees{
"XXBTZUSD": Fees{
"XXBTZUSD": {
Fee: 0.06,
MinFee: 0,
MaxFee: 0.16,
Expand Down
2 changes: 1 addition & 1 deletion rest/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,8 @@ type TradesHistoryResponse struct {
// DepositMethods - respons on GetDepositMethods request
type DepositMethods struct {
Method string `json:"method"`
Limit bool `json:"limit"`
Fee string `json:"fee"`
Limit bool `json:"limit"`
GenAddress bool `json:"gen-address"`
}

Expand Down
22 changes: 15 additions & 7 deletions websocket/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,20 @@ type AuthClient struct {
}

// NewAuth - constructor for AuthClient
func NewAuth(key, secret string) *AuthClient {
func NewAuth(key, secret string, opts ...AuthOption) *AuthClient {
api := rest.New(key, secret)
data, err := api.GetWebSocketsToken()
if err != nil {
panic(err)
}

params := NewDefaultAuthParameters()
c := &AuthClient{
Client: &Client{
asyncFactory: &websocketAsynchronousFactory{
parameters: params,
},
isConnected: false,
parameters: params,
listener: make(chan interface{}),
terminal: false,
shutdown: make(chan struct{}),
asynchronous: nil,
heartbeat: time.Now().Add(params.HeartbeatTimeout),
hbChannel: make(chan error),
subscriptions: make(map[int64]*SubscriptionStatus),
factories: make(map[string]ParseFactory),
Expand All @@ -45,6 +39,20 @@ func NewAuth(key, secret string) *AuthClient {
token: data.Token,
tokenExpiresTimer: time.NewTimer(time.Duration(data.Expires)),
}

for i := range opts {
opts[i](c)
}

if c.parameters == nil {
c.parameters = NewDefaultAuthParameters()
}

c.asyncFactory = &websocketAsynchronousFactory{
parameters: c.parameters,
}
c.heartbeat = time.Now().Add(c.parameters.HeartbeatTimeout)

c.createFactories()
c.createAuthFactories()
return c
Expand Down
Loading

0 comments on commit a4f8e9a

Please sign in to comment.