diff --git a/v2/delivery/depth_service.go b/v2/delivery/depth_service.go index a3b1883a..786597d1 100644 --- a/v2/delivery/depth_service.go +++ b/v2/delivery/depth_service.go @@ -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