Skip to content

Commit

Permalink
Merge branch 'adshao:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
kislikjeka authored Jul 15, 2021
2 parents 8ea623b + 355dc5a commit 076fe97
Show file tree
Hide file tree
Showing 9 changed files with 894 additions and 21 deletions.
3 changes: 3 additions & 0 deletions v2/account_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ type Account struct {
CanTrade bool `json:"canTrade"`
CanWithdraw bool `json:"canWithdraw"`
CanDeposit bool `json:"canDeposit"`
UpdateTime uint64 `json:"updateTime"`
AccountType string `json:"accountType"`
Balances []Balance `json:"balances"`
Permissions []string `json:"permissions"`
}

// Balance define user balance of your account
Expand Down
48 changes: 28 additions & 20 deletions v2/account_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,31 @@ func TestAccountService(t *testing.T) {

func (s *accountServiceTestSuite) TestGetAccount() {
data := []byte(`{
"makerCommission": 15,
"takerCommission": 15,
"buyerCommission": 0,
"sellerCommission": 0,
"canTrade": true,
"canWithdraw": true,
"canDeposit": true,
"balances": [
{
"asset": "BTC",
"free": "4723846.89208129",
"locked": "0.00000000"
},
{
"asset": "LTC",
"free": "4763368.68006011",
"locked": "0.00000000"
}
]
}`)
"makerCommission": 15,
"takerCommission": 15,
"buyerCommission": 0,
"sellerCommission": 0,
"canTrade": true,
"canWithdraw": true,
"canDeposit": true,
"updateTime": 123456789,
"accountType": "SPOT",
"balances": [
{
"asset": "BTC",
"free": "4723846.89208129",
"locked": "0.00000000"
},
{
"asset": "LTC",
"free": "4763368.68006011",
"locked": "0.00000000"
}
],
"permissions": [
"SPOT"
]
}`)
s.mockDo(data, nil)
defer s.assertDo()
s.assertReq(func(r *request) {
Expand All @@ -53,6 +58,8 @@ func (s *accountServiceTestSuite) TestGetAccount() {
CanTrade: true,
CanWithdraw: true,
CanDeposit: true,
UpdateTime: 123456789,
AccountType: "SPOT",
Balances: []Balance{
{
Asset: "BTC",
Expand All @@ -65,6 +72,7 @@ func (s *accountServiceTestSuite) TestGetAccount() {
Locked: "0.00000000",
},
},
Permissions: []string{"SPOT"},
}
s.assertAccountEqual(e, res)
}
Expand Down
15 changes: 15 additions & 0 deletions v2/delivery/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,21 @@ func (c *Client) NewKlinesService() *KlinesService {
return &KlinesService{c: c}
}

// NewListPriceChangeStatsService init list prices change stats service
func (c *Client) NewListPriceChangeStatsService() *ListPriceChangeStatsService {
return &ListPriceChangeStatsService{c: c}
}

// NewListPricesService init listing prices service
func (c *Client) NewListPricesService() *ListPricesService {
return &ListPricesService{c: c}
}

// NewListBookTickersService init listing booking tickers service
func (c *Client) NewListBookTickersService() *ListBookTickersService {
return &ListBookTickersService{c: c}
}

// NewStartUserStreamService init starting user stream service
func (c *Client) NewStartUserStreamService() *StartUserStreamService {
return &StartUserStreamService{c: c}
Expand Down
176 changes: 176 additions & 0 deletions v2/delivery/ticker_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package delivery

import (
"context"
"encoding/json"
)

// ListBookTickersService list best price/qty on the order book for a symbol or symbols.
type ListBookTickersService struct {
c *Client
symbol *string
pair *string
}

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

// Pair set pair.
func (s *ListBookTickersService) Pair(pair string) *ListBookTickersService {
s.pair = &pair
return s
}

// Do send request.
func (s *ListBookTickersService) Do(ctx context.Context, opts ...RequestOption) (res []*BookTicker, err error) {
r := &request{
method: "GET",
endpoint: "/dapi/v1/ticker/bookTicker",
}
if s.symbol != nil {
r.setParam("symbol", *s.symbol)
}
if s.pair != nil {
r.setParam("pair", *s.pair)
}

data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*BookTicker{}, err
}
res = make([]*BookTicker, 0)
err = json.Unmarshal(data, &res)
if err != nil {
return []*BookTicker{}, err
}
return res, nil
}

// BookTicker define book ticker info.
type BookTicker struct {
Symbol string `json:"symbol"`
Pair string `json:"pair"`
BidPrice string `json:"bidPrice"`
BidQuantity string `json:"bidQty"`
AskPrice string `json:"askPrice"`
AskQuantity string `json:"askQty"`
}

// ListPricesService list latest price for a symbol or symbols.
type ListPricesService struct {
c *Client
symbol *string
pair *string
}

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

// Pair set pair.
func (s *ListPricesService) Pair(pair string) *ListPricesService {
s.pair = &pair
return s
}

// Do send request.
func (s *ListPricesService) Do(ctx context.Context, opts ...RequestOption) (res []*SymbolPrice, err error) {
r := &request{
method: "GET",
endpoint: "/dapi/v1/ticker/price",
}
if s.symbol != nil {
r.setParam("symbol", *s.symbol)
}
if s.pair != nil {
r.setParam("pair", *s.pair)
}

data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*SymbolPrice{}, err
}
res = make([]*SymbolPrice, 0)
err = json.Unmarshal(data, &res)
if err != nil {
return []*SymbolPrice{}, err
}
return res, nil
}

// SymbolPrice define symbol, price and pair.
type SymbolPrice struct {
Symbol string `json:"symbol"`
Pair string `json:"ps"`
Price string `json:"price"`
}

// ListPriceChangeStatsService show stats of price change in last 24 hours for single symbol, all symbols or pairs of symbols.
type ListPriceChangeStatsService struct {
c *Client
symbol *string
pair *string
}

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

// Pair set pair.
func (s *ListPriceChangeStatsService) Pair(pair string) *ListPriceChangeStatsService {
s.pair = &pair
return s
}

// Do send request.
func (s *ListPriceChangeStatsService) Do(ctx context.Context, opts ...RequestOption) (res []*PriceChangeStats, err error) {
r := &request{
method: "GET",
endpoint: "/dapi/v1/ticker/24hr",
}
if s.symbol != nil {
r.setParam("symbol", *s.symbol)
}
if s.pair != nil {
r.setParam("pair", *s.pair)
}

data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return res, err
}
res = make([]*PriceChangeStats, 0)
err = json.Unmarshal(data, &res)
if err != nil {
return nil, err
}
return res, nil
}

// PriceChangeStats define price change stats.
type PriceChangeStats struct {
Symbol string `json:"symbol"`
Pair string `json:"pair"`
PriceChange string `json:"priceChange"`
PriceChangePercent string `json:"priceChangePercent"`
WeightedAvgPrice string `json:"weightedAvgPrice"`
LastPrice string `json:"lastPrice"`
LastQuantity string `json:"lastQty"`
OpenPrice string `json:"openPrice"`
HighPrice string `json:"highPrice"`
LowPrice string `json:"lowPrice"`
Volume string `json:"volume"`
BaseVolume string `json:"baseVolume"`
OpenTime int64 `json:"openTime"`
CloseTime int64 `json:"closeTime"`
FristID int64 `json:"firstId"`
LastID int64 `json:"lastId"`
Count int64 `json:"count"`
}
Loading

0 comments on commit 076fe97

Please sign in to comment.