Skip to content

Commit

Permalink
add delivery depth service
Browse files Browse the repository at this point in the history
  • Loading branch information
drinkthere committed Oct 20, 2022
1 parent fe67fe0 commit 6316105
Showing 1 changed file with 78 additions and 1 deletion.
79 changes: 78 additions & 1 deletion v2/delivery/depth_service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,83 @@
package delivery

import "github.com/adshao/go-binance/v2/common"
import (
"context"
"github.com/adshao/go-binance/v2/common"
"net/http"
)

// DepthService show depth info
type DepthService struct {
c *Client
symbol string
limit *int
}

// Symbol set symbol
func (s *DepthService) Symbol(symbol string) *DepthService {
s.symbol = symbol
return s
}

// Limit set limit
func (s *DepthService) Limit(limit int) *DepthService {
s.limit = &limit
return s
}

// Do send request
func (s *DepthService) Do(ctx context.Context, opts ...RequestOption) (res *DepthResponse, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/dapi/v1/depth",
}
r.setParam("symbol", s.symbol)
if s.limit != nil {
r.setParam("limit", *s.limit)
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
j, err := newJSON(data)
if err != nil {
return nil, err
}
res = new(DepthResponse)
res.Time = j.Get("E").MustInt64()
res.TradeTime = j.Get("T").MustInt64()
res.LastUpdateID = j.Get("lastUpdateId").MustInt64()
res.Symbol = j.Get("symbol").MustString()
bidsLen := len(j.Get("bids").MustArray())
res.Bids = make([]Bid, bidsLen)
for i := 0; i < bidsLen; i++ {
item := j.Get("bids").GetIndex(i)
res.Bids[i] = Bid{
Price: item.GetIndex(0).MustString(),
Quantity: item.GetIndex(1).MustString(),
}
}
asksLen := len(j.Get("asks").MustArray())
res.Asks = make([]Ask, asksLen)
for i := 0; i < asksLen; i++ {
item := j.Get("asks").GetIndex(i)
res.Asks[i] = Ask{
Price: item.GetIndex(0).MustString(),
Quantity: item.GetIndex(1).MustString(),
}
}
return res, nil
}

// DepthResponse define depth info with bids and asks
type DepthResponse struct {
LastUpdateID int64 `json:"lastUpdateId"`
Symbol string `json:"symbol"`
Time int64 `json:"E"`
TradeTime int64 `json:"T"`
Bids []Bid `json:"bids"`
Asks []Ask `json:"asks"`
}

// Ask is a type alias for PriceLevel.
type Ask = common.PriceLevel
Expand Down

0 comments on commit 6316105

Please sign in to comment.