diff --git a/examples/websocket_api/account/AccountInformation/AccountInformation.go b/examples/websocket_api/account/AccountInformation/AccountInformation.go index 4d5bbec..8365671 100644 --- a/examples/websocket_api/account/AccountInformation/AccountInformation.go +++ b/examples/websocket_api/account/AccountInformation/AccountInformation.go @@ -13,7 +13,7 @@ func main() { } func AccountInformationExample() { - client := binance_connector.NewWebsocketAPIClient("api_key", "api_secret") + client := binance_connector.NewWebsocketAPIClient("api_key", "secret_key") err := client.Connect() if err != nil { log.Printf("Error: %v", err) diff --git a/margin.go b/margin.go index 3937b6f..c633634 100644 --- a/margin.go +++ b/margin.go @@ -6,292 +6,6 @@ import ( "net/http" ) -// Cross Margin Account Transfer API Endpoint -const ( - transferEndpoint = "/sapi/v1/margin/transfer" -) - -// TransferService transfer between spot and margin account -type TransferService struct { - c *Client - asset string - amount float64 - transferType int -} - -// Asset set asset -func (s *TransferService) Asset(asset string) *TransferService { - s.asset = asset - return s -} - -// Amount set amount -func (s *TransferService) Amount(amount float64) *TransferService { - s.amount = amount - return s -} - -// TransferType set transfer type -func (s *TransferService) TransferType(transferType int) *TransferService { - s.transferType = transferType - return s -} - -// Do send request -func (s *TransferService) Do(ctx context.Context, opts ...RequestOption) (res *TransferResponse, err error) { - r := &request{ - method: http.MethodPost, - endpoint: transferEndpoint, - secType: secTypeSigned, - } - r.setParam("asset", s.asset) - r.setParam("amount", s.amount) - r.setParam("type", s.transferType) - data, err := s.c.callAPI(ctx, r, opts...) - if err != nil { - return &TransferResponse{}, err - } - res = new(TransferResponse) - err = json.Unmarshal(data, res) - if err != nil { - return &TransferResponse{}, err - } - return res, nil -} - -// TransferResponse define transfer response -type TransferResponse struct { - TranId int64 `json:"tranId"` -} - -// Cross Margin Account Borrow API Endpoint -const ( - borrowEndpoint = "/sapi/v1/margin/loan" -) - -// BorrowService borrow from cross margin account -type BorrowService struct { - c *Client - asset string - amount float64 - isIsolated *string - symbol *string -} - -// Asset set asset -func (s *BorrowService) Asset(asset string) *BorrowService { - s.asset = asset - return s -} - -// Amount set amount -func (s *BorrowService) Amount(amount float64) *BorrowService { - s.amount = amount - return s -} - -// IsIsolated set isolated -func (s *BorrowService) IsIsolated(isIsolated string) *BorrowService { - s.isIsolated = &isIsolated - return s -} - -// Do send request -func (s *BorrowService) Do(ctx context.Context, opts ...RequestOption) (res *BorrowResponse, err error) { - r := &request{ - method: http.MethodPost, - endpoint: borrowEndpoint, - secType: secTypeSigned, - } - r.setParam("asset", s.asset) - r.setParam("amount", s.amount) - if s.isIsolated != nil { - r.setParam("isolatedSymbol", *s.isIsolated) - } - if s.symbol != nil { - r.setParam("symbol", *s.symbol) - } - data, err := s.c.callAPI(ctx, r, opts...) - if err != nil { - return &BorrowResponse{}, err - } - res = new(BorrowResponse) - err = json.Unmarshal(data, res) - if err != nil { - return &BorrowResponse{}, err - } - return res, nil -} - -// BorrowResponse define borrow response -type BorrowResponse struct { - TranId int64 `json:"tranId"` -} - -// Cross Margin Account Repay API Endpoint -const ( - repayEndpoint = "/sapi/v1/margin/repay" -) - -// RepayService repay to cross margin account -type RepayService struct { - c *Client - asset string - isIsolated *string - symbol *string - amount float64 -} - -// Asset set asset -func (s *RepayService) Asset(asset string) *RepayService { - s.asset = asset - return s -} - -// Amount set amount -func (s *RepayService) Amount(amount float64) *RepayService { - s.amount = amount - return s -} - -// Symbol set symbol -func (s *RepayService) Symbol(symbol string) *RepayService { - s.symbol = &symbol - return s -} - -// IsIsolated set isolated -func (s *RepayService) IsIsolated(isIsolated string) *RepayService { - s.isIsolated = &isIsolated - return s -} - -// Do send request -func (s *RepayService) Do(ctx context.Context, opts ...RequestOption) (res *RepayResponse, err error) { - r := &request{ - method: http.MethodPost, - endpoint: repayEndpoint, - secType: secTypeSigned, - } - r.setParam("asset", s.asset) - r.setParam("amount", s.amount) - if s.isIsolated != nil { - r.setParam("isIsolated", *s.isIsolated) - } - if s.symbol != nil { - r.setParam("symbol", *s.symbol) - } - data, err := s.c.callAPI(ctx, r, opts...) - if err != nil { - return &RepayResponse{}, err - } - res = new(RepayResponse) - err = json.Unmarshal(data, res) - if err != nil { - return &RepayResponse{}, err - } - return res, nil -} - -// RepayResponse define repay response -type RepayResponse struct { - TranId int64 `json:"tranId"` -} - -// Query Margin Asset API Endpoint -const ( - queryMarginAssetEndpoint = "/sapi/v1/margin/asset" -) - -// QueryMarginAssetService query margin asset -type QueryMarginAssetService struct { - c *Client - asset string -} - -// Asset set asset -func (s *QueryMarginAssetService) Asset(asset string) *QueryMarginAssetService { - s.asset = asset - return s -} - -// Do send request -func (s *QueryMarginAssetService) Do(ctx context.Context, opts ...RequestOption) (res *QueryMarginAssetResponse, err error) { - r := &request{ - method: http.MethodGet, - endpoint: queryMarginAssetEndpoint, - secType: secTypeAPIKey, - } - r.setParam("asset", s.asset) - data, err := s.c.callAPI(ctx, r, opts...) - if err != nil { - return &QueryMarginAssetResponse{}, err - } - res = new(QueryMarginAssetResponse) - err = json.Unmarshal(data, res) - if err != nil { - return &QueryMarginAssetResponse{}, err - } - return res, nil -} - -// QueryMarginAssetResponse define query margin asset response -type QueryMarginAssetResponse struct { - FullName string `json:"assetFullName"` - Name string `json:"assetName"` - Borrowable bool `json:"isBorrowable"` - Mortgageable bool `json:"isMortgageable"` - UserMinBorrow string `json:"userMinBorrow"` - UserMinRepay string `json:"userMinRepay"` -} - -// Query Cross Margin Pair API Endpoint -const ( - queryCrossMarginPairEndpoint = "/sapi/v1/margin/pair" -) - -// QueryCrossMarginPairService query cross margin pair -type QueryCrossMarginPairService struct { - c *Client - symbol string -} - -// Symbol set symbol -func (s *QueryCrossMarginPairService) Symbol(symbol string) *QueryCrossMarginPairService { - s.symbol = symbol - return s -} - -// Do send request -func (s *QueryCrossMarginPairService) Do(ctx context.Context, opts ...RequestOption) (res *QueryCrossMarginPairResponse, err error) { - r := &request{ - method: http.MethodGet, - endpoint: queryCrossMarginPairEndpoint, - secType: secTypeSigned, - } - r.setParam("symbol", s.symbol) - data, err := s.c.callAPI(ctx, r, opts...) - if err != nil { - return &QueryCrossMarginPairResponse{}, err - } - res = new(QueryCrossMarginPairResponse) - err = json.Unmarshal(data, res) - if err != nil { - return &QueryCrossMarginPairResponse{}, err - } - return res, nil -} - -// QueryCrossMarginPairResponse define query cross margin pair response -type QueryCrossMarginPairResponse struct { - SymbolDetail struct { - Symbol string `json:"symbol"` - IsMarginTrade bool `json:"isMarginTrade"` - IsBuyAllowed bool `json:"isBuyAllowed"` - IsSellAllowed bool `json:"isSellAllowed"` - } `json:"symbolDetail"` -} - // Get all margin assets API Endpoint const ( getAllMarginAssetsEndpoint = "/sapi/v1/margin/allAssets" @@ -303,7 +17,7 @@ type GetAllMarginAssetsService struct { } // Do send request -func (s *GetAllMarginAssetsService) Do(ctx context.Context, opts ...RequestOption) (res *GetAllMarginAssetsResponse, err error) { +func (s *GetAllMarginAssetsService) Do(ctx context.Context, opts ...RequestOption) (res []*GetAllMarginAssetsResponse, err error) { r := &request{ method: http.MethodGet, endpoint: getAllMarginAssetsEndpoint, @@ -311,29 +25,27 @@ func (s *GetAllMarginAssetsService) Do(ctx context.Context, opts ...RequestOptio } data, err := s.c.callAPI(ctx, r, opts...) if err != nil { - return &GetAllMarginAssetsResponse{}, err + return []*GetAllMarginAssetsResponse{}, err } - res = new(GetAllMarginAssetsResponse) - err = json.Unmarshal(data, res) + res = make([]*GetAllMarginAssetsResponse, 0) + err = json.Unmarshal(data, &res) if err != nil { - return &GetAllMarginAssetsResponse{}, err + return []*GetAllMarginAssetsResponse{}, err } return res, nil } // GetAllMarginAssetsResponse define get all margin assets response type GetAllMarginAssetsResponse struct { - AssetDetailList []struct { - AssetFullName string `json:"assetFullName"` - AssetName string `json:"assetName"` - IsBorrowable bool `json:"isBorrowable"` - IsMortgageable bool `json:"isMortgageable"` - MinLoanAmt string `json:"minLoanAmt"` - MaxLoanAmt string `json:"maxLoanAmt"` - MinMortgageAmt string `json:"minMortgageAmt"` - MaxMortgageAmt string `json:"maxMortgageAmt"` - Asset string `json:"asset"` - } `json:"assetDetailList"` + AssetFullName string `json:"assetFullName"` + AssetName string `json:"assetName"` + IsBorrowable bool `json:"isBorrowable"` + IsMortgageable bool `json:"isMortgageable"` + MinLoanAmt string `json:"minLoanAmt"` + MaxLoanAmt string `json:"maxLoanAmt"` + MinMortgageAmt string `json:"minMortgageAmt"` + MaxMortgageAmt string `json:"maxMortgageAmt"` + Asset string `json:"asset"` } // Get all margin pairs API Endpoint @@ -347,7 +59,7 @@ type GetAllMarginPairsService struct { } // Do send request -func (s *GetAllMarginPairsService) Do(ctx context.Context, opts ...RequestOption) (res *GetAllMarginPairsResponse, err error) { +func (s *GetAllMarginPairsService) Do(ctx context.Context, opts ...RequestOption) (res []*GetAllMarginPairsResponse, err error) { r := &request{ method: http.MethodGet, endpoint: getAllMarginPairsEndpoint, @@ -355,27 +67,25 @@ func (s *GetAllMarginPairsService) Do(ctx context.Context, opts ...RequestOption } data, err := s.c.callAPI(ctx, r, opts...) if err != nil { - return &GetAllMarginPairsResponse{}, err + return []*GetAllMarginPairsResponse{}, err } - res = new(GetAllMarginPairsResponse) - err = json.Unmarshal(data, res) + res = make([]*GetAllMarginPairsResponse, 0) + err = json.Unmarshal(data, &res) if err != nil { - return &GetAllMarginPairsResponse{}, err + return []*GetAllMarginPairsResponse{}, err } return res, nil } // GetAllMarginPairsResponse define get all margin pairs response type GetAllMarginPairsResponse struct { - SymbolDetailList []struct { - Base string `json:"base"` - Id int `json:"id"` - IsBuyAllowed bool `json:"isBuyAllowed"` - IsMarginTrade bool `json:"isMarginTrade"` - IsSellAllowed bool `json:"isSellAllowed"` - Quote string `json:"quote"` - Symbol string `json:"symbol"` - } `json:"symbolDetailList"` + Base string `json:"base"` + Id int `json:"id"` + IsBuyAllowed bool `json:"isBuyAllowed"` + IsMarginTrade bool `json:"isMarginTrade"` + IsSellAllowed bool `json:"isSellAllowed"` + Quote string `json:"quote"` + Symbol string `json:"symbol"` } // Query Margin Price Index API Endpoint @@ -928,254 +638,6 @@ type CrossMarginTransferHistoryResponse struct { Total int `json:"total"` } -// Query Loan Record (USER_DATA) API Endpoint -const ( - loanRecordEndpoint = "/sapi/v1/margin/loan" -) - -// LoanRecordService query loan record -type LoanRecordService struct { - c *Client - asset string - isolatedSymbol *string - txid *int64 - startTime *uint64 - endTime *uint64 - current *int - size *int - archived *string -} - -// Asset set asset -func (s *LoanRecordService) Asset(asset string) *LoanRecordService { - s.asset = asset - return s -} - -// IsolatedSymbol set isolatedSymbol -func (s *LoanRecordService) IsolatedSymbol(isolatedSymbol string) *LoanRecordService { - s.isolatedSymbol = &isolatedSymbol - return s -} - -// TxId set txid -func (s *LoanRecordService) TxId(txid int64) *LoanRecordService { - s.txid = &txid - return s -} - -// StartTime set startTime -func (s *LoanRecordService) StartTime(startTime uint64) *LoanRecordService { - s.startTime = &startTime - return s -} - -// EndTime set endTime -func (s *LoanRecordService) EndTime(endTime uint64) *LoanRecordService { - s.endTime = &endTime - return s -} - -// Current set current -func (s *LoanRecordService) Current(current int) *LoanRecordService { - s.current = ¤t - return s -} - -// Size set size -func (s *LoanRecordService) Size(size int) *LoanRecordService { - s.size = &size - return s -} - -// Archived set archived -func (s *LoanRecordService) Archived(archived string) *LoanRecordService { - s.archived = &archived - return s -} - -// Do send request -func (s *LoanRecordService) Do(ctx context.Context, opts ...RequestOption) (res *LoanRecordResponse, err error) { - r := &request{ - method: http.MethodGet, - endpoint: loanRecordEndpoint, - secType: secTypeSigned, - } - m := params{ - "asset": s.asset, - } - if s.isolatedSymbol != nil { - m["isolatedSymbol"] = *s.isolatedSymbol - } - if s.txid != nil { - m["txId"] = *s.txid - } - if s.startTime != nil { - m["startTime"] = *s.startTime - } - if s.endTime != nil { - m["endTime"] = *s.endTime - } - if s.current != nil { - m["current"] = *s.current - } - if s.size != nil { - m["size"] = *s.size - } - if s.archived != nil { - m["archived"] = *s.archived - } - r.setParams(m) - data, err := s.c.callAPI(ctx, r, opts...) - if err != nil { - return &LoanRecordResponse{}, err - } - res = new(LoanRecordResponse) - err = json.Unmarshal(data, res) - if err != nil { - return &LoanRecordResponse{}, err - } - return res, nil -} - -// LoanRecordResponse define loan record response -type LoanRecordResponse struct { - Rows []struct { - IsolatedSymbol string `json:"isolatedSymbol"` - TxId int64 `json:"txId"` - Asset string `json:"asset"` - Principal string `json:"principal"` - Timestamp uint64 `json:"timestamp"` - Status string `json:"status"` - } `json:"rows"` - Total int `json:"total"` -} - -// Query Repay Record (USER_DATA) API Endpoint -const ( - repayRecordEndpoint = "/sapi/v1/margin/repay" -) - -// RepayRecordService query repay record -type RepayRecordService struct { - c *Client - asset string - isolatedSymbol *string - txid *int64 - startTime *uint64 - endTime *uint64 - current *int - size *int - archived *string -} - -// Asset set asset -func (s *RepayRecordService) Asset(asset string) *RepayRecordService { - s.asset = asset - return s -} - -// IsolatedSymbol set isolatedSymbol -func (s *RepayRecordService) IsolatedSymbol(isolatedSymbol string) *RepayRecordService { - s.isolatedSymbol = &isolatedSymbol - return s -} - -// TxId set txid -func (s *RepayRecordService) TxId(txid int64) *RepayRecordService { - s.txid = &txid - return s -} - -// StartTime set startTime -func (s *RepayRecordService) StartTime(startTime uint64) *RepayRecordService { - s.startTime = &startTime - return s -} - -// EndTime set endTime -func (s *RepayRecordService) EndTime(endTime uint64) *RepayRecordService { - s.endTime = &endTime - return s -} - -// Current set current -func (s *RepayRecordService) Current(current int) *RepayRecordService { - s.current = ¤t - return s -} - -// Size set size -func (s *RepayRecordService) Size(size int) *RepayRecordService { - s.size = &size - return s -} - -// Archived set archived -func (s *RepayRecordService) Archived(archived string) *RepayRecordService { - s.archived = &archived - return s -} - -// Do send request -func (s *RepayRecordService) Do(ctx context.Context, opts ...RequestOption) (res *RepayRecordResponse, err error) { - r := &request{ - method: http.MethodGet, - endpoint: repayRecordEndpoint, - secType: secTypeSigned, - } - m := params{ - "asset": s.asset, - } - if s.isolatedSymbol != nil { - m["isolatedSymbol"] = *s.isolatedSymbol - } - if s.txid != nil { - m["txId"] = *s.txid - } - if s.startTime != nil { - m["startTime"] = *s.startTime - } - if s.endTime != nil { - m["endTime"] = *s.endTime - } - if s.current != nil { - m["current"] = *s.current - } - if s.size != nil { - m["size"] = *s.size - } - if s.archived != nil { - m["archived"] = *s.archived - } - r.setParams(m) - data, err := s.c.callAPI(ctx, r, opts...) - if err != nil { - return &RepayRecordResponse{}, err - } - res = new(RepayRecordResponse) - err = json.Unmarshal(data, res) - if err != nil { - return &RepayRecordResponse{}, err - } - return res, nil -} - -// RepayRecordResponse define repay record response -type RepayRecordResponse struct { - Rows []struct { - IsolatedSymbol string `json:"isolatedSymbol"` - Amount string `json:"amount"` - Asset string `json:"asset"` - Interest string `json:"interest"` - Principal string `json:"principal"` - Status string `json:"status"` - Timestamp uint64 `json:"timestamp"` - TxId int64 `json:"txId"` - } `json:"rows"` - Total int `json:"total"` -} - // Query Interest History (USER_DATA) API Endpoint const ( interestHistoryEndpoint = "/sapi/v1/margin/interestHistory" @@ -1507,25 +969,23 @@ func (s *MarginAccountOrderService) Do(ctx context.Context, opts ...RequestOptio // MarginAccountOrderResponse define margin account order response type MarginAccountOrderResponse struct { - AccountId uint64 `json:"accountId"` - ClientOrderId string `json:"clientOrderId"` - CummulativeQuoteQty string `json:"cummulativeQuoteQty"` - ExecutedQty string `json:"executedQty"` - IcebergQty string `json:"icebergQty"` - IsWorking bool `json:"isWorking"` - OrderId int `json:"orderId"` - OrigQty string `json:"origQty"` - Price string `json:"price"` - SelfTradePreventionMode string `json:"selfTradePreventionMode"` - Side string `json:"side"` - Status string `json:"status"` - StopPrice string `json:"stopPrice"` - Symbol string `json:"symbol"` - IsIsolated bool `json:"isIsolated"` - Time uint64 `json:"time"` - TimeInForce string `json:"timeInForce"` - OrderType string `json:"type"` - UpdateTime uint64 `json:"updateTime"` + ClientOrderId string `json:"clientOrderId"` + CumulativeQuoteQty string `json:"cumulativeQuoteQty"` + ExecutedQty string `json:"executedQty"` + IcebergQty string `json:"icebergQty"` + IsWorking bool `json:"isWorking"` + OrderId int `json:"orderId"` + OrigQty string `json:"origQty"` + Price string `json:"price"` + Side string `json:"side"` + Status string `json:"status"` + StopPrice string `json:"stopPrice"` + Symbol string `json:"symbol"` + IsIsolated bool `json:"isIsolated"` + Time uint64 `json:"time"` + TimeInForce string `json:"timeInForce"` + OrderType string `json:"type"` + UpdateTime uint64 `json:"updateTime"` } // Query Margin Account's Open Order (USER_DATA) API Endpoint @@ -1553,7 +1013,7 @@ func (s *MarginAccountOpenOrderService) IsIsolated(isIsolated string) *MarginAcc } // Do send request -func (s *MarginAccountOpenOrderService) Do(ctx context.Context, opts ...RequestOption) (res *MarginAccountOpenOrderResponse, err error) { +func (s *MarginAccountOpenOrderService) Do(ctx context.Context, opts ...RequestOption) (res []*MarginAccountOpenOrderResponse, err error) { r := &request{ method: http.MethodGet, endpoint: marginAccountOpenOrderEndpoint, @@ -1567,37 +1027,35 @@ func (s *MarginAccountOpenOrderService) Do(ctx context.Context, opts ...RequestO } data, err := s.c.callAPI(ctx, r, opts...) if err != nil { - return &MarginAccountOpenOrderResponse{}, err + return []*MarginAccountOpenOrderResponse{}, err } - res = new(MarginAccountOpenOrderResponse) - err = json.Unmarshal(data, res) + res = make([]*MarginAccountOpenOrderResponse, 0) + err = json.Unmarshal(data, &res) if err != nil { - return &MarginAccountOpenOrderResponse{}, err + return []*MarginAccountOpenOrderResponse{}, err } return res, nil } // MarginAccountOpenOrderResponse define margin account open order response type MarginAccountOpenOrderResponse struct { - Orders []struct { - ClientOrderId string `json:"clientOrderId"` - CumulativeQuoteQty string `json:"cumulativeQuoteQty"` - ExecutedQty string `json:"executedQty"` - IcebergQty string `json:"icebergQty"` - IsWorking bool `json:"isWorking"` - OrderId int `json:"orderId"` - OrigQty string `json:"origQty"` - Price string `json:"price"` - Side string `json:"side"` - Status string `json:"status"` - StopPrice string `json:"stopPrice"` - Symbol string `json:"symbol"` - IsIsolated bool `json:"isIsolated"` - Time uint64 `json:"time"` - TimeInForce string `json:"timeInForce"` - OrderType string `json:"type"` - UpdateTime uint64 `json:"updateTime"` - } `json:"orders"` + ClientOrderId string `json:"clientOrderId"` + CumulativeQuoteQty string `json:"cumulativeQuoteQty"` + ExecutedQty string `json:"executedQty"` + IcebergQty string `json:"icebergQty"` + IsWorking bool `json:"isWorking"` + OrderId int `json:"orderId"` + OrigQty string `json:"origQty"` + Price string `json:"price"` + Side string `json:"side"` + Status string `json:"status"` + StopPrice string `json:"stopPrice"` + Symbol string `json:"symbol"` + IsIsolated bool `json:"isIsolated"` + Time uint64 `json:"time"` + TimeInForce string `json:"timeInForce"` + OrderType string `json:"type"` + UpdateTime uint64 `json:"updateTime"` } // Query Margin Account's All Orders (USER_DATA) API Endpoint @@ -1692,25 +1150,23 @@ func (s *MarginAccountAllOrderService) Do(ctx context.Context, opts ...RequestOp // MarginAccountAllOrderResponse define margin account all order response type MarginAccountAllOrderResponse struct { - Orders []struct { - ClientOrderId string `json:"clientOrderId"` - CumulativeQuoteQty string `json:"cumulativeQuoteQty"` - ExecutedQty string `json:"executedQty"` - IcebergQty string `json:"icebergQty"` - IsWorking bool `json:"isWorking"` - OrderId int `json:"orderId"` - OrigQty string `json:"origQty"` - Price string `json:"price"` - Side string `json:"side"` - Status string `json:"status"` - StopPrice string `json:"stopPrice"` - Symbol string `json:"symbol"` - IsIsolated bool `json:"isIsolated"` - Time uint64 `json:"time"` - TimeInForce string `json:"timeInForce"` - OrderType string `json:"type"` - UpdateTime uint64 `json:"updateTime"` - } `json:"orders"` + ClientOrderId string `json:"clientOrderId"` + CumulativeQuoteQty string `json:"cumulativeQuoteQty"` + ExecutedQty string `json:"executedQty"` + IcebergQty string `json:"icebergQty"` + IsWorking bool `json:"isWorking"` + OrderId int `json:"orderId"` + OrigQty string `json:"origQty"` + Price string `json:"price"` + Side string `json:"side"` + Status string `json:"status"` + StopPrice string `json:"stopPrice"` + Symbol string `json:"symbol"` + IsIsolated bool `json:"isIsolated"` + Time uint64 `json:"time"` + TimeInForce string `json:"timeInForce"` + OrderType string `json:"type"` + UpdateTime uint64 `json:"updateTime"` } // Margin Account New OCO (TRADE) API Endpoint @@ -2168,7 +1624,7 @@ func (s *MarginAccountQueryAllOCOService) Limit(limit int) *MarginAccountQueryAl } // Do send request -func (s *MarginAccountQueryAllOCOService) Do(ctx context.Context, opts ...RequestOption) (res *MarginAccountQueryAllOCOResponse, err error) { +func (s *MarginAccountQueryAllOCOService) Do(ctx context.Context, opts ...RequestOption) (res []*MarginAccountQueryAllOCOResponse, err error) { r := &request{ method: http.MethodGet, endpoint: marginAccountQueryAllOCOEndpoint, @@ -2194,12 +1650,12 @@ func (s *MarginAccountQueryAllOCOService) Do(ctx context.Context, opts ...Reques } data, err := s.c.callAPI(ctx, r, opts...) if err != nil { - return &MarginAccountQueryAllOCOResponse{}, err + return []*MarginAccountQueryAllOCOResponse{}, err } - res = new(MarginAccountQueryAllOCOResponse) - err = json.Unmarshal(data, res) + res = make([]*MarginAccountQueryAllOCOResponse, 0) + err = json.Unmarshal(data, &res) if err != nil { - return &MarginAccountQueryAllOCOResponse{}, err + return []*MarginAccountQueryAllOCOResponse{}, err } return res, nil } @@ -2245,7 +1701,7 @@ func (s *MarginAccountQueryOpenOCOService) Symbol(symbol string) *MarginAccountQ } // Do send request -func (s *MarginAccountQueryOpenOCOService) Do(ctx context.Context, opts ...RequestOption) (res *MarginAccountQueryOpenOCOResponse, err error) { +func (s *MarginAccountQueryOpenOCOService) Do(ctx context.Context, opts ...RequestOption) (res []*MarginAccountQueryOpenOCOResponse, err error) { r := &request{ method: http.MethodGet, endpoint: marginAccountQueryOpenOCOEndpoint, @@ -2259,12 +1715,12 @@ func (s *MarginAccountQueryOpenOCOService) Do(ctx context.Context, opts ...Reque } data, err := s.c.callAPI(ctx, r, opts...) if err != nil { - return &MarginAccountQueryOpenOCOResponse{}, err + return []*MarginAccountQueryOpenOCOResponse{}, err } - res = new(MarginAccountQueryOpenOCOResponse) - err = json.Unmarshal(data, res) + res = make([]*MarginAccountQueryOpenOCOResponse, 0) + err = json.Unmarshal(data, &res) if err != nil { - return &MarginAccountQueryOpenOCOResponse{}, err + return []*MarginAccountQueryOpenOCOResponse{}, err } return res, nil } @@ -2545,215 +2001,6 @@ type MarginAccountSummaryResponse struct { ForceLiquidationBar string `json:"forceLiquidationBar"` } -// Isolated Margin Account Transfer (MARGIN) -const ( - marginIsolatedAccountTransferEndpoint = "/sapi/v1/margin/isolated/transfer" -) - -type MarginIsolatedAccountTransferService struct { - c *Client - asset string - symbol string - transFrom string - transTo string - amount float64 -} - -// Asset set asset -func (s *MarginIsolatedAccountTransferService) Asset(asset string) *MarginIsolatedAccountTransferService { - s.asset = asset - return s -} - -// Symbol set symbol -func (s *MarginIsolatedAccountTransferService) Symbol(symbol string) *MarginIsolatedAccountTransferService { - s.symbol = symbol - return s -} - -// TransFrom set transFrom -func (s *MarginIsolatedAccountTransferService) TransFrom(transFrom string) *MarginIsolatedAccountTransferService { - s.transFrom = transFrom - return s -} - -// TransTo set transTo -func (s *MarginIsolatedAccountTransferService) TransTo(transTo string) *MarginIsolatedAccountTransferService { - s.transTo = transTo - return s -} - -// Amount set amount -func (s *MarginIsolatedAccountTransferService) Amount(amount float64) *MarginIsolatedAccountTransferService { - s.amount = amount - return s -} - -// Do send request -func (s *MarginIsolatedAccountTransferService) Do(ctx context.Context, opts ...RequestOption) (res *MarginIsolatedAccountTransferResponse, err error) { - r := &request{ - method: http.MethodPost, - endpoint: marginIsolatedAccountTransferEndpoint, - secType: secTypeSigned, - } - m := params{ - "asset": s.asset, - "symbol": s.symbol, - "transFrom": s.transFrom, - "transTo": s.transTo, - "amount": s.amount, - } - r.setParams(m) - data, err := s.c.callAPI(ctx, r, opts...) - if err != nil { - return &MarginIsolatedAccountTransferResponse{}, err - } - res = new(MarginIsolatedAccountTransferResponse) - err = json.Unmarshal(data, res) - if err != nil { - return &MarginIsolatedAccountTransferResponse{}, err - } - return res, nil -} - -// MarginIsolatedAccountTransferService response -type MarginIsolatedAccountTransferResponse struct { - TranId string `json:"tranId"` -} - -// Isolated Margin Account Transfer History (MARGIN) -const ( - marginIsolatedAccountTransferHistoryEndpoint = "/sapi/v1/margin/isolated/transfer" -) - -type MarginIsolatedAccountTransferHistoryService struct { - c *Client - asset *string - symbol string - transFrom *string - transTo *string - startTime *uint64 - endTime *uint64 - current *int - size *int - archived *string -} - -// Asset set asset -func (s *MarginIsolatedAccountTransferHistoryService) Asset(asset string) *MarginIsolatedAccountTransferHistoryService { - s.asset = &asset - return s -} - -// Symbol set symbol -func (s *MarginIsolatedAccountTransferHistoryService) Symbol(symbol string) *MarginIsolatedAccountTransferHistoryService { - s.symbol = symbol - return s -} - -// TransFrom set transFrom -func (s *MarginIsolatedAccountTransferHistoryService) TransFrom(transFrom string) *MarginIsolatedAccountTransferHistoryService { - s.transFrom = &transFrom - return s -} - -// TransTo set transTo -func (s *MarginIsolatedAccountTransferHistoryService) TransTo(transTo string) *MarginIsolatedAccountTransferHistoryService { - s.transTo = &transTo - return s -} - -// StartTime set startTime -func (s *MarginIsolatedAccountTransferHistoryService) StartTime(startTime uint64) *MarginIsolatedAccountTransferHistoryService { - s.startTime = &startTime - return s -} - -// EndTime set endTime -func (s *MarginIsolatedAccountTransferHistoryService) EndTime(endTime uint64) *MarginIsolatedAccountTransferHistoryService { - s.endTime = &endTime - return s -} - -// Current set current -func (s *MarginIsolatedAccountTransferHistoryService) Current(current int) *MarginIsolatedAccountTransferHistoryService { - s.current = ¤t - return s -} - -// Size set size -func (s *MarginIsolatedAccountTransferHistoryService) Size(size int) *MarginIsolatedAccountTransferHistoryService { - s.size = &size - return s -} - -// Archived set archived -func (s *MarginIsolatedAccountTransferHistoryService) Archived(archived string) *MarginIsolatedAccountTransferHistoryService { - s.archived = &archived - return s -} - -// Do send request -func (s *MarginIsolatedAccountTransferHistoryService) Do(ctx context.Context, opts ...RequestOption) (res *MarginIsolatedAccountTransferHistoryResponse, err error) { - r := &request{ - method: http.MethodGet, - endpoint: marginIsolatedAccountTransferHistoryEndpoint, - secType: secTypeSigned, - } - m := params{ - "symbol": s.symbol, - } - if s.asset != nil { - m["asset"] = *s.asset - } - if s.transFrom != nil { - m["transFrom"] = *s.transFrom - } - if s.transTo != nil { - m["transTo"] = *s.transTo - } - if s.startTime != nil { - m["startTime"] = *s.startTime - } - if s.endTime != nil { - m["endTime"] = *s.endTime - } - if s.current != nil { - m["current"] = *s.current - } - if s.size != nil { - m["size"] = *s.size - } - if s.archived != nil { - m["archived"] = *s.archived - } - r.setParams(m) - data, err := s.c.callAPI(ctx, r, opts...) - if err != nil { - return &MarginIsolatedAccountTransferHistoryResponse{}, err - } - res = new(MarginIsolatedAccountTransferHistoryResponse) - err = json.Unmarshal(data, res) - if err != nil { - return &MarginIsolatedAccountTransferHistoryResponse{}, err - } - return res, nil -} - -// MarginIsolatedAccountTransferHistoryService response -type MarginIsolatedAccountTransferHistoryResponse struct { - Rows []struct { - Amount string `json:"amount"` - Asset string `json:"asset"` - Status string `json:"status"` - TimeStamp uint64 `json:"timeStamp"` - TxId int64 `json:"txId"` - TransFrom string `json:"transFrom"` - TransTo string `json:"transTo"` - } `json:"rows"` - Total int64 `json:"total"` -} - // Query Isolated Margin Account Info (USER_DATA) const ( marginIsolatedAccountInfoEndpoint = "/sapi/v1/margin/isolated/account" @@ -2974,55 +2221,6 @@ type MarginIsolatedAccountLimitResponse struct { MaxAccount int `json:"maxAccount"` } -// Query Isolated Margin Symbol (USER_DATA) -const ( - marginIsolatedSymbolEndpoint = "/sapi/v1/margin/isolated/pair" -) - -type MarginIsolatedSymbolService struct { - c *Client - symbol string -} - -// Symbol set symbol -func (s *MarginIsolatedSymbolService) Symbol(symbol string) *MarginIsolatedSymbolService { - s.symbol = symbol - return s -} - -// Do send request -func (s *MarginIsolatedSymbolService) Do(ctx context.Context, opts ...RequestOption) (res *MarginIsolatedSymbolResponse, err error) { - r := &request{ - method: http.MethodGet, - endpoint: marginIsolatedSymbolEndpoint, - secType: secTypeSigned, - } - m := params{ - "symbol": s.symbol, - } - r.setParams(m) - data, err := s.c.callAPI(ctx, r, opts...) - if err != nil { - return &MarginIsolatedSymbolResponse{}, err - } - res = new(MarginIsolatedSymbolResponse) - err = json.Unmarshal(data, res) - if err != nil { - return &MarginIsolatedSymbolResponse{}, err - } - return res, nil -} - -// MarginIsolatedSymbolService response -type MarginIsolatedSymbolResponse struct { - Symbol string `json:"symbol"` - Base string `json:"base"` - Quote string `json:"quote"` - IsMarginTrade bool `json:"isMarginTrade"` - IsBuyAllowed bool `json:"isBuyAllowed"` - IsSellAllowed bool `json:"isSellAllowed"` -} - // Get All Isolated Margin Symbol(USER_DATA) const ( marginIsolatedSymbolAllEndpoint = "/sapi/v1/margin/isolated/allPairs" @@ -3053,6 +2251,14 @@ func (s *AllIsolatedMarginSymbolService) Do(ctx context.Context, opts ...Request } // MarginIsolatedSymbolAllService returns all isolated margin symbols +type MarginIsolatedSymbolResponse struct { + Symbol string `json:"symbol"` + Base string `json:"base"` + Quote string `json:"quote"` + IsMarginTrade bool `json:"isMarginTrade"` + IsBuyAllowed bool `json:"isBuyAllowed"` + IsSellAllowed bool `json:"isSellAllowed"` +} // Toggle BNB Burn On Spot Trade And Margin Interest (USER_DATA) const ( @@ -3458,59 +2664,6 @@ type MarginCurrentOrderCountResponse struct { Count int `json:"count"` } -// Margin Dustlog (USER_DATA) -const ( - marginDustlogEndpoint = "/sapi/v1/margin/dribblet" -) - -type MarginDustlogService struct { - c *Client - startTime *uint64 - endTime *uint64 -} - -// StartTime set startTime -func (s *MarginDustlogService) StartTime(startTime uint64) *MarginDustlogService { - s.startTime = &startTime - return s -} - -// EndTime set endTime -func (s *MarginDustlogService) EndTime(endTime uint64) *MarginDustlogService { - s.endTime = &endTime - return s -} - -// Do send request -func (s *MarginDustlogService) Do(ctx context.Context, opts ...RequestOption) (res *MarginDustlogResponse, err error) { - r := &request{ - method: http.MethodGet, - endpoint: marginDustlogEndpoint, - secType: secTypeSigned, - } - if s.startTime != nil { - r.setParam("startTime", *s.startTime) - } - if s.endTime != nil { - r.setParam("endTime", *s.endTime) - } - data, err := s.c.callAPI(ctx, r) - if err != nil { - return - } - res = new(MarginDustlogResponse) - err = json.Unmarshal(data, res) - if err != nil { - return - } - return res, nil -} - -type MarginDustlogResponse struct { - Total uint8 `json:"total"` //Total counts of exchange - UserAssetDribblets []UserAssetDribblet `json:"userAssetDribblets"` -} - // UserAssetDribblet represents one dust log row type UserAssetDribblet struct { OperateTime int64 `json:"operateTime"` diff --git a/margin_test.go b/margin_test.go index d2125b0..afdcb24 100644 --- a/margin_test.go +++ b/margin_test.go @@ -15,92 +15,6 @@ func TestMargin(t *testing.T) { suite.Run(t, new(marginTestSuite)) } -func (s *marginTestSuite) TestTransfer() { - data := []byte(`{ - "tranId": 100000001 - }`) - s.mockDo(data, nil) - defer s.assertDo() - asset := "BTC" - amount := 1.000 - transferType := 1 - s.assertReq(func(r *request) { - e := newSignedRequest().setParams(params{ - "asset": asset, - "amount": amount, - "type": transferType, - }) - s.assertRequestEqual(e, r) - }) - res, err := s.client.NewTransferService().Asset(asset). - Amount(amount).TransferType(transferType).Do(newContext()) - s.r().NoError(err) - e := &TransferResponse{ - TranId: 100000001, - } - s.assertTransactionResponseEqual(e, res) -} - -func (s *marginTestSuite) assertTransactionResponseEqual(a, e *TransferResponse) { - s.r().Equal(a.TranId, e.TranId, "TranID") -} - -func (s *marginTestSuite) TestLoan() { - data := []byte(`{ - "tranId": 100000001 - }`) - s.mockDo(data, nil) - defer s.assertDo() - asset := "BTC" - amount := 1.000 - s.assertReq(func(r *request) { - e := newSignedRequest().setParams(params{ - "asset": asset, - "amount": amount, - }) - s.assertRequestEqual(e, r) - }) - res, err := s.client.NewBorrowService().Asset(asset). - Amount(amount).Do(newContext()) - s.r().NoError(err) - e := &BorrowResponse{ - TranId: 100000001, - } - s.assertBorrowResponseEqual(e, res) -} - -func (s *marginTestSuite) assertBorrowResponseEqual(a, e *BorrowResponse) { - s.r().Equal(a.TranId, e.TranId, "TranID") -} - -func (s *marginTestSuite) TestRepay() { - data := []byte(`{ - "tranId": 100000001 - }`) - s.mockDo(data, nil) - defer s.assertDo() - asset := "BTC" - amount := 1.000 - s.assertReq(func(r *request) { - e := newSignedRequest().setParams(params{ - "asset": asset, - "amount": amount, - }) - s.assertRequestEqual(e, r) - }) - res, err := s.client.NewRepayService().Asset(asset). - Amount(amount).Do(newContext()) - s.r().NoError(err) - e := &RepayResponse{ - TranId: 100000001, - } - s.assertRepayResponseEqual(e, res) -} - -func (s *marginTestSuite) assertRepayResponseEqual(a, e *RepayResponse) { - s.r().Equal(a.TranId, e.TranId, "TranID") -} - func (s *marginTestSuite) TestGetMaxBorrowable() { data := []byte(`{ "amount": "1.69248805" @@ -250,73 +164,32 @@ func (s *marginTestSuite) assertIsolatedMarginAllPairsEqual(e, a *MarginIsolated r.Equal(e.IsSellAllowed, a.IsSellAllowed, "IsSellAllowed") } -func (s *marginTestSuite) TestGetMarginAsset() { - data := []byte(`{ - "assetFullName": "Binance Coin", - "assetName": "BNB", - "isBorrowable": false, - "isMortgageable": true, - "userMinBorrow": "0.00000000", - "userMinRepay": "0.00000000" - }`) - s.mockDo(data, nil) - defer s.assertDo() - asset := "BNB" - s.assertReq(func(r *request) { - e := newRequest() - e.setParam("asset", asset) - s.assertRequestEqual(e, r) - }) - res, err := s.client.NewQueryMarginAssetService().Asset(asset).Do(newContext()) - s.r().NoError(err) - e := &QueryMarginAssetResponse{ - FullName: "Binance Coin", - Name: asset, - Borrowable: false, - Mortgageable: true, - UserMinBorrow: "0.00000000", - UserMinRepay: "0.00000000", - } - s.assertMarginAssetEqual(e, res) -} - -func (s *marginTestSuite) assertMarginAssetEqual(e, a *QueryMarginAssetResponse) { - r := s.r() - r.Equal(e.FullName, a.FullName, "FullName") - r.Equal(e.Name, a.Name, "Name") - r.Equal(e.Borrowable, a.Borrowable, "Borrowable") - r.Equal(e.Mortgageable, a.Mortgageable, "Mortgageable") - r.Equal(e.UserMinBorrow, a.UserMinBorrow, "UserMinBorrow") - r.Equal(e.UserMinRepay, a.UserMinRepay, "UserMinRepay") -} - func (s *marginTestSuite) TestGetAllMarginAssets() { - data := []byte(`{ - "assetDetailList": [ - { - "assetFullName": "Bitcoin", - "assetName": "BTC", - "isBorrowable": true, - "isMortgageable": true, - "minLoanAmt": "0.00010000", - "maxLoanAmt": "100000.00000000", - "minMortgageAmt": "0.00010000", - "maxMortgageAmt": "100000.00000000", - "asset": "BTC" - }, - { - "assetFullName": "Ethereum", - "assetName": "ETH", - "isBorrowable": true, - "isMortgageable": true, - "minLoanAmt": "0.01000000", - "maxLoanAmt": "10000.00000000", - "minMortgageAmt": "0.01000000", - "maxMortgageAmt": "10000.00000000", - "asset": "ETH" - } - ] - }`) + data := []byte(` + [ + { + "assetFullName": "Bitcoin", + "assetName": "BTC", + "isBorrowable": true, + "isMortgageable": true, + "minLoanAmt": "0.00010000", + "maxLoanAmt": "100000.00000000", + "minMortgageAmt": "0.00010000", + "maxMortgageAmt": "100000.00000000", + "asset": "BTC" + }, + { + "assetFullName": "Ethereum", + "assetName": "ETH", + "isBorrowable": true, + "isMortgageable": true, + "minLoanAmt": "0.01000000", + "maxLoanAmt": "10000.00000000", + "minMortgageAmt": "0.01000000", + "maxMortgageAmt": "10000.00000000", + "asset": "ETH" + } + ]`) s.mockDo(data, nil) defer s.assertDo() @@ -324,16 +197,16 @@ func (s *marginTestSuite) TestGetAllMarginAssets() { resp, err := s.client.NewGetAllMarginAssetsService().Do(context.Background()) s.r().NoError(err) - s.Len(resp.AssetDetailList, 2) - s.Equal("Bitcoin", resp.AssetDetailList[0].AssetFullName) - s.Equal("BTC", resp.AssetDetailList[0].AssetName) - s.True(resp.AssetDetailList[0].IsBorrowable) - s.True(resp.AssetDetailList[0].IsMortgageable) - s.Equal("0.00010000", resp.AssetDetailList[0].MinLoanAmt) - s.Equal("100000.00000000", resp.AssetDetailList[0].MaxLoanAmt) - s.Equal("0.00010000", resp.AssetDetailList[0].MinMortgageAmt) - s.Equal("100000.00000000", resp.AssetDetailList[0].MaxMortgageAmt) - s.Equal("BTC", resp.AssetDetailList[0].Asset) + s.Len(resp, 2) + s.Equal("Bitcoin", resp[0].AssetFullName) + s.Equal("BTC", resp[0].AssetName) + s.True(resp[0].IsBorrowable) + s.True(resp[0].IsMortgageable) + s.Equal("0.00010000", resp[0].MinLoanAmt) + s.Equal("100000.00000000", resp[0].MaxLoanAmt) + s.Equal("0.00010000", resp[0].MinMortgageAmt) + s.Equal("100000.00000000", resp[0].MaxMortgageAmt) + s.Equal("BTC", resp[0].Asset) } func (s *marginTestSuite) TestForceLiquidationRecordService() { @@ -422,9 +295,9 @@ func (s *marginTestSuite) TestInterestHistory() { data := []byte(` { "rows": [ - { + { "txId": 123, - "interestAccruedTime": 1613450271000, + "interestAccuredTime": 1613450271000, "asset": "BTC", "rawAsset": "BTC", "principal": "1.00000000", @@ -435,7 +308,7 @@ func (s *marginTestSuite) TestInterestHistory() { } ], "total": 1 - } + } `) s.mockDo(data, nil) @@ -453,7 +326,7 @@ func (s *marginTestSuite) TestInterestHistory() { s.r().NoError(err) s.Len(resp.Rows, 1) - s.Equal(uint64(1613450271000), resp.Rows[0].InterestAccruedTime) + s.Equal(uint64(1613450271000), resp.Rows[0].InterestAccuredTime) s.Equal("BTC", resp.Rows[0].Asset) s.Equal("BTC", resp.Rows[0].RawAsset) s.Equal("1.00000000", resp.Rows[0].Principal) @@ -464,69 +337,27 @@ func (s *marginTestSuite) TestInterestHistory() { s.Equal(1, resp.Total) } -func (s *marginTestSuite) TestLoanRecord() { - data := []byte(` - { - "rows": [ - { - "isolatedSymbol": "", - "txId": 123, - "asset": "BTC", - "principal": "1.00000000", - "timestamp": 1613450271000, - "status": "CONFIRMED" - } - ], - "total": 1 - } - `) - - s.mockDo(data, nil) - defer s.assertDo() - - resp, err := s.client.NewLoanRecordService(). - Asset("BTC"). - IsolatedSymbol("BTCUSDT"). - StartTime(1613450271000). - EndTime(1613536671000). - Current(1). - Size(500). - Archived("true"). - Do(context.Background()) - - s.r().NoError(err) - s.Len(resp.Rows, 1) - s.Equal("", resp.Rows[0].IsolatedSymbol) - s.Equal("BTC", resp.Rows[0].Asset) - s.Equal("1.00000000", resp.Rows[0].Principal) - s.Equal(uint64(1613450271000), resp.Rows[0].Timestamp) - s.Equal("CONFIRMED", resp.Rows[0].Status) - s.Equal(1, resp.Total) -} - func (s *marginTestSuite) TestMarginAccountAllOrderService() { data := []byte(` - { - "orders": [ - { - "clientOrderId": "example-client-order-id", - "cumulativeQuoteQty": "100.00000000", - "executedQty": "1.00000000", - "icebergQty": "0.00000000", - "isWorking": false, - "orderId": 123, - "origQty": "1.00000000", - "price": "100.00000000", - "side": "BUY", - "status": "FILLED", - "stopPrice": "0.00000000", - "symbol": "BTCUSDT", - "isIsolated": true, - "time": 1613450271000, - "updateTime": 1613450271000 - } - ] - } + [ + { + "clientOrderId": "example-client-order-id", + "cumulativeQuoteQty": "100.00000000", + "executedQty": "1.00000000", + "icebergQty": "0.00000000", + "isWorking": false, + "orderId": 123, + "origQty": "1.00000000", + "price": "100.00000000", + "side": "BUY", + "status": "FILLED", + "stopPrice": "0.00000000", + "symbol": "BTCUSDT", + "isIsolated": true, + "time": 1613450271000, + "updateTime": 1613450271000 + } + ] `) s.mockDo(data, nil) @@ -542,22 +373,22 @@ func (s *marginTestSuite) TestMarginAccountAllOrderService() { Do(context.Background()) s.r().NoError(err) - s.Len(resp.Orders, 1) - s.Equal("example-client-order-id", resp.Orders[0].ClientOrderId) - s.Equal("100.00000000", resp.Orders[0].CumulativeQuoteQty) - s.Equal("1.00000000", resp.Orders[0].ExecutedQty) - s.Equal("0.00000000", resp.Orders[0].IcebergQty) - s.False(resp.Orders[0].IsWorking) - s.Equal(123, resp.Orders[0].OrderId) - s.Equal("1.00000000", resp.Orders[0].OrigQty) - s.Equal("100.00000000", resp.Orders[0].Price) - s.Equal("BUY", resp.Orders[0].Side) - s.Equal("FILLED", resp.Orders[0].Status) - s.Equal("0.00000000", resp.Orders[0].StopPrice) - s.Equal("BTCUSDT", resp.Orders[0].Symbol) - s.True(resp.Orders[0].IsIsolated) - s.Equal(uint64(1613450271000), resp.Orders[0].Time) - s.Equal(uint64(1613450271000), resp.Orders[0].UpdateTime) + s.Len(resp, 1) + s.Equal("example-client-order-id", resp[0].ClientOrderId) + s.Equal("100.00000000", resp[0].CumulativeQuoteQty) + s.Equal("1.00000000", resp[0].ExecutedQty) + s.Equal("0.00000000", resp[0].IcebergQty) + s.False(resp[0].IsWorking) + s.Equal(123, resp[0].OrderId) + s.Equal("1.00000000", resp[0].OrigQty) + s.Equal("100.00000000", resp[0].Price) + s.Equal("BUY", resp[0].Side) + s.Equal("FILLED", resp[0].Status) + s.Equal("0.00000000", resp[0].StopPrice) + s.Equal("BTCUSDT", resp[0].Symbol) + s.True(resp[0].IsIsolated) + s.Equal(uint64(1613450271000), resp[0].Time) + s.Equal(uint64(1613450271000), resp[0].UpdateTime) } func (s *marginTestSuite) TestMarginAccountCancelAllOrders() { @@ -825,29 +656,27 @@ func (s *marginTestSuite) TestMarginAccountNewOrder() { func (s *marginTestSuite) TestMarginAccountOpenOrder() { data := []byte(` - { - "orders": [ - { - "clientOrderId": "abc123", - "cumulativeQuoteQty": "1.00000000", - "executedQty": "1.00000000", - "icebergQty": "0.00000000", - "isWorking": false, - "orderId": 123, - "origQty": "1.00000000", - "price": "10000.00000000", - "side": "BUY", - "status": "FILLED", - "stopPrice": "0.00000000", - "symbol": "BTCUSDT", - "isIsolated": false, - "time": 1619549055345, - "timeInForce": "GTC", - "type": "LIMIT", - "updateTime": 1619549055345 - } - ] - } + [ + { + "clientOrderId": "abc123", + "cumulativeQuoteQty": "1.00000000", + "executedQty": "1.00000000", + "icebergQty": "0.00000000", + "isWorking": false, + "orderId": 123, + "origQty": "1.00000000", + "price": "10000.00000000", + "side": "BUY", + "status": "FILLED", + "stopPrice": "0.00000000", + "symbol": "BTCUSDT", + "isIsolated": false, + "time": 1619549055345, + "timeInForce": "GTC", + "type": "LIMIT", + "updateTime": 1619549055345 + } + ] `) s.mockDo(data, nil) @@ -859,24 +688,24 @@ func (s *marginTestSuite) TestMarginAccountOpenOrder() { Do(context.Background()) s.r().NoError(err) - s.Len(resp.Orders, 1) - s.Equal("abc123", resp.Orders[0].ClientOrderId) - s.Equal("1.00000000", resp.Orders[0].CumulativeQuoteQty) - s.Equal("1.00000000", resp.Orders[0].ExecutedQty) - s.Equal("0.00000000", resp.Orders[0].IcebergQty) - s.Equal(false, resp.Orders[0].IsWorking) - s.Equal(123, resp.Orders[0].OrderId) - s.Equal("1.00000000", resp.Orders[0].OrigQty) - s.Equal("10000.00000000", resp.Orders[0].Price) - s.Equal("BUY", resp.Orders[0].Side) - s.Equal("FILLED", resp.Orders[0].Status) - s.Equal("0.00000000", resp.Orders[0].StopPrice) - s.Equal("BTCUSDT", resp.Orders[0].Symbol) - s.Equal(false, resp.Orders[0].IsIsolated) - s.Equal(uint64(1619549055345), resp.Orders[0].Time) - s.Equal("GTC", resp.Orders[0].TimeInForce) - s.Equal("LIMIT", resp.Orders[0].OrderType) - s.Equal(uint64(1619549055345), resp.Orders[0].UpdateTime) + s.Len(resp, 1) + s.Equal("abc123", resp[0].ClientOrderId) + s.Equal("1.00000000", resp[0].CumulativeQuoteQty) + s.Equal("1.00000000", resp[0].ExecutedQty) + s.Equal("0.00000000", resp[0].IcebergQty) + s.Equal(false, resp[0].IsWorking) + s.Equal(123, resp[0].OrderId) + s.Equal("1.00000000", resp[0].OrigQty) + s.Equal("10000.00000000", resp[0].Price) + s.Equal("BUY", resp[0].Side) + s.Equal("FILLED", resp[0].Status) + s.Equal("0.00000000", resp[0].StopPrice) + s.Equal("BTCUSDT", resp[0].Symbol) + s.Equal(false, resp[0].IsIsolated) + s.Equal(uint64(1619549055345), resp[0].Time) + s.Equal("GTC", resp[0].TimeInForce) + s.Equal("LIMIT", resp[0].OrderType) + s.Equal(uint64(1619549055345), resp[0].UpdateTime) } func (s *marginTestSuite) TestMarginAccountOrder() { @@ -987,28 +816,31 @@ func (s *marginTestSuite) TestMarginAccountQueryOCO() { } func (s *marginTestSuite) TestMarginAccountQueryOpenOCO() { - data := []byte(`{ - "orderListId": 1613450271000, - "contingencyType": "OCO", - "listStatusType": "EXEC_STARTED", - "listOrderStatus": "EXECUTING", - "listClientOrderId": "JYVppVf2SgZdG8", - "transactionTime": 1613450271000, - "symbol": "BTCUSDT", - "isIsolated": true, - "orders": [ - { - "symbol": "BTCUSDT", - "orderId": 123, - "clientOrderId": "abc123" - }, - { - "symbol": "BTCUSDT", - "orderId": 456, - "clientOrderId": "def456" - } - ] - }`) + data := []byte(` + [ + { + "orderListId": 1613450271000, + "contingencyType": "OCO", + "listStatusType": "EXEC_STARTED", + "listOrderStatus": "EXECUTING", + "listClientOrderId": "JYVppVf2SgZdG8", + "transactionTime": 1613450271000, + "symbol": "BTCUSDT", + "isIsolated": true, + "orders": [ + { + "symbol": "BTCUSDT", + "orderId": 123, + "clientOrderId": "abc123" + }, + { + "symbol": "BTCUSDT", + "orderId": 456, + "clientOrderId": "def456" + } + ] + } + ]`) s.mockDo(data, nil) defer s.assertDo() @@ -1019,21 +851,21 @@ func (s *marginTestSuite) TestMarginAccountQueryOpenOCO() { Do(context.Background()) s.r().NoError(err) - s.Equal(1613450271000, resp.OrderListId) - s.Equal("OCO", resp.ContingencyType) - s.Equal("EXEC_STARTED", resp.ListStatusType) - s.Equal("EXECUTING", resp.ListOrderStatus) - s.Equal("JYVppVf2SgZdG8", resp.ListClientOrderId) - s.Equal(uint64(1613450271000), resp.TransactionTime) - s.Equal("BTCUSDT", resp.Symbol) - s.True(resp.IsIsolated) - s.Len(resp.Orders, 2) - s.Equal("BTCUSDT", resp.Orders[0].Symbol) - s.Equal(123, resp.Orders[0].OrderId) - s.Equal("abc123", resp.Orders[0].ClientOrderId) - s.Equal("BTCUSDT", resp.Orders[1].Symbol) - s.Equal(456, resp.Orders[1].OrderId) - s.Equal("def456", resp.Orders[1].ClientOrderId) + s.Equal(1613450271000, resp[0].OrderListId) + s.Equal("OCO", resp[0].ContingencyType) + s.Equal("EXEC_STARTED", resp[0].ListStatusType) + s.Equal("EXECUTING", resp[0].ListOrderStatus) + s.Equal("JYVppVf2SgZdG8", resp[0].ListClientOrderId) + s.Equal(uint64(1613450271000), resp[0].TransactionTime) + s.Equal("BTCUSDT", resp[0].Symbol) + s.True(resp[0].IsIsolated) + s.Len(resp[0].Orders, 2) + s.Equal("BTCUSDT", resp[0].Orders[0].Symbol) + s.Equal(123, resp[0].Orders[0].OrderId) + s.Equal("abc123", resp[0].Orders[0].ClientOrderId) + s.Equal("BTCUSDT", resp[0].Orders[1].Symbol) + s.Equal(456, resp[0].Orders[1].OrderId) + s.Equal("def456", resp[0].Orders[1].ClientOrderId) } func (s *marginTestSuite) TestMarginAccountSummary() { @@ -1186,146 +1018,6 @@ func (s *marginTestSuite) TestMarginCurrentOrderCount() { s.Equal(0, resp[0].Count) } -func (s *marginTestSuite) TestListDustLog() { - data := []byte(` - { - "total": 8, - "userAssetDribblets": [ - { - "totalTransferedAmount": "0.00132256", - "totalServiceChargeAmount": "0.00002699", - "transId": 45178372831, - "userAssetDribbletDetails": [ - { - "transId": 4359321, - "serviceChargeAmount": "0.000009", - "amount": "0.0009", - "operateTime": 1615985535000, - "transferedAmount": "0.000441", - "fromAsset": "USDT" - }, - { - "transId": 4359321, - "serviceChargeAmount": "0.00001799", - "amount": "0.0009", - "operateTime": 1615985535000, - "transferedAmount": "0.00088156", - "fromAsset": "ETH" - } - ] - }, - { - "operateTime":1616203180000, - "totalTransferedAmount": "0.00058795", - "totalServiceChargeAmount": "0.000012", - "transId": 4357015, - "userAssetDribbletDetails": [ - { - "transId": 4357015, - "serviceChargeAmount": "0.00001", - "amount": "0.001", - "operateTime": 1616203180000, - "transferedAmount": "0.00049", - "fromAsset": "USDT" - }, - { - "transId": 4357015, - "serviceChargeAmount": "0.000002", - "amount": "0.0001", - "operateTime": 1616203180000, - "transferedAmount": "0.00009795", - "fromAsset": "ETH" - } - ] - } - ] - } - `) - s.mockDo(data, nil) - defer s.assertDo() - - s.assertReq(func(r *request) { - e := newSignedRequest() - s.assertRequestEqual(e, r) - }) - - res, err := s.client.NewMarginDustlogService().Do(context.Background()) - r := s.r() - r.NoError(err) - rows := res.UserAssetDribblets - s.Len(rows, 2) - s.Len(rows[0].UserAssetDribbletDetails, 2) - s.Len(rows[1].UserAssetDribbletDetails, 2) - - s.assertUserAssetDribbletEqual(&UserAssetDribblet{ - TotalTransferedAmount: "0.00132256", - TotalServiceChargeAmount: "0.00002699", - TransId: 45178372831, - UserAssetDribbletDetails: []UserAssetDribbletDetail{ - { - TransId: 4359321, - ServiceChargeAmount: "0.000009", - Amount: "0.0009", - OperateTime: 1615985535000, - TransferedAmount: "0.000441", - FromAsset: "USDT", - }, - { - TransId: 4359321, - ServiceChargeAmount: "0.00001799", - Amount: "0.0009", - OperateTime: 1615985535000, - TransferedAmount: "0.00088156", - FromAsset: "ETH", - }, - }, - }, &rows[0]) - s.assertUserAssetDribbletEqual(&UserAssetDribblet{ - TotalTransferedAmount: "0.00058795", - TotalServiceChargeAmount: "0.000012", - TransId: 4357015, - UserAssetDribbletDetails: []UserAssetDribbletDetail{ - { - TransId: 4357015, - ServiceChargeAmount: "0.00001", - Amount: "0.001", - OperateTime: 1616203180000, - TransferedAmount: "0.00049", - FromAsset: "USDT", - }, - { - TransId: 4357015, - ServiceChargeAmount: "0.000002", - Amount: "0.0001", - OperateTime: 1616203180000, - TransferedAmount: "0.00009795", - FromAsset: "ETH", - }, - }, - OperateTime: 1616203180000, - }, &rows[1]) -} - -func (s *marginTestSuite) assertUserAssetDribbletEqual(e, a *UserAssetDribblet) { - r := s.r() - r.Equal(e.TotalTransferedAmount, a.TotalTransferedAmount, `TotalTransferedAmount`) - r.Equal(e.TotalServiceChargeAmount, a.TotalServiceChargeAmount, `TotalServiceChargeAmount`) - r.Equal(e.TransId, a.TransId, `TransID`) - s.assertUserAssetDribbletDetailEqual(&e.UserAssetDribbletDetails[0], &a.UserAssetDribbletDetails[0]) - s.assertUserAssetDribbletDetailEqual(&e.UserAssetDribbletDetails[1], &a.UserAssetDribbletDetails[1]) - r.Equal(e.OperateTime, a.OperateTime, `OperateTime`) -} - -func (s *marginTestSuite) assertUserAssetDribbletDetailEqual(e, a *UserAssetDribbletDetail) { - r := s.r() - r.Equal(e.TransId, a.TransId, `TransID`) - r.Equal(e.ServiceChargeAmount, a.ServiceChargeAmount, `ServiceChargeAmount`) - r.Equal(e.Amount, a.Amount, `Amount`) - r.Equal(e.OperateTime, a.OperateTime, `OperateTime`) - r.Equal(e.TransferedAmount, a.TransferedAmount, `TransferedAmount`) - r.Equal(e.FromAsset, a.FromAsset, `FromAsset`) -} - func (s *marginTestSuite) TestMarginInterestRateHistory() { data := []byte(` [ @@ -1497,85 +1189,6 @@ func (s *marginTestSuite) TestMarginIsolatedAccountLimit() { s.Equal(10, resp.MaxAccount) } -func (s *marginTestSuite) TestMarginIsolatedAccountTransfer() { - data := []byte(`{"tranId":"12345"}`) - s.mockDo(data, nil) - defer s.assertDo() - - resp, err := s.client.NewMarginIsolatedAccountTransferService(). - Asset("BTC"). - Symbol("BTCUSDT"). - TransFrom("BTC"). - TransTo("USDT"). - Amount(1.0). - Do(context.Background()) - - s.r().NoError(err) - s.Equal("12345", resp.TranId) -} - -func (s *marginTestSuite) TestMarginIsolatedAccountTransferHistory() { - data := []byte(`{ - "rows": [ - { - "amount": "1.0", - "asset": "BTC", - "status": "CONFIRMED", - "timestamp": 1527777532000, - "txId": 100000001, - "transferId": 200000001, - "transFrom": "ETH", - "transTo": "BTC" - }, - { - "amount": "2.0", - "asset": "BTC", - "status": "CONFIRMED", - "type": "ROLL_OUT", - "timestamp": 1527777532000, - "txId": 100000002, - "transferId": 200000002, - "transFrom": "BTC", - "transTo": "ETH" - } - ], - "total": 2 - }`) - - s.mockDo(data, nil) - defer s.assertDo() - - resp, err := s.client.NewMarginIsolatedAccountTransferHistoryService(). - Symbol("BTCUSDT"). - Asset("BTC"). - TransFrom("BTC"). - TransTo("ETH"). - StartTime(1527777532000). - EndTime(1527777532000). - Current(1). - Size(500). - Archived("true"). - Do(context.Background()) - - s.r().NoError(err) - s.Len(resp.Rows, 2) - s.Equal("BTC", resp.Rows[0].Asset) - s.Equal("1.0", resp.Rows[0].Amount) - s.Equal("CONFIRMED", resp.Rows[0].Status) - s.Equal(uint64(1527777532000), resp.Rows[0].TimeStamp) - s.Equal(int64(100000001), resp.Rows[0].TxId) - s.Equal("ETH", resp.Rows[0].TransFrom) - s.Equal("BTC", resp.Rows[0].TransTo) - s.Equal("BTC", resp.Rows[1].Asset) - s.Equal("2.0", resp.Rows[1].Amount) - s.Equal("CONFIRMED", resp.Rows[1].Status) - s.Equal(uint64(1527777532000), resp.Rows[1].TimeStamp) - s.Equal(int64(100000002), resp.Rows[1].TxId) - s.Equal("BTC", resp.Rows[1].TransFrom) - s.Equal("ETH", resp.Rows[1].TransTo) - s.Equal(int64(2), resp.Total) -} - func (s *marginTestSuite) TestMarginIsolatedMarginFee() { data := []byte(` [ @@ -1662,32 +1275,6 @@ func (s *marginTestSuite) TestMarginIsolatedMarginTier() { s.Equal("50.00000000", resp[0].QuoteAssetMaxBorrowable) } -func (s *marginTestSuite) TestMarginIsolatedSymbol() { - data := []byte(`{ - "symbol": "BTCUSDT", - "base": "BTC", - "quote": "USDT", - "isMarginTrade": true, - "isBuyAllowed": true, - "isSellAllowed": true - }`) - - s.mockDo(data, nil) - defer s.assertDo() - - resp, err := s.client.NewMarginIsolatedSymbolService(). - Symbol("BTCUSDT"). - Do(context.Background()) - - s.r().NoError(err) - s.Equal("BTCUSDT", resp.Symbol) - s.Equal("BTC", resp.Base) - s.Equal("USDT", resp.Quote) - s.True(resp.IsMarginTrade) - s.True(resp.IsBuyAllowed) - s.True(resp.IsSellAllowed) -} - func (s *marginTestSuite) TestMarginSmallLiabilityExchange() { data := []byte(`[{}]`) s.mockDo(data, nil) @@ -1789,100 +1376,3 @@ func (s *marginTestSuite) TestMarginToggleBnbBurn() { s.Equal(true, resp.SpotBNBBurn) s.Equal(false, resp.InterestBNBBurn) } - -func (s *marginTestSuite) TestQueryCrossMarginPair() { - data := []byte(` - { - "symbolDetail": { - "symbol": "BTCUSDT", - "isMarginTrade": true, - "isBuyAllowed": true, - "isSellAllowed": true - } - }`) - - s.mockDo(data, nil) - defer s.assertDo() - - resp, err := s.client.NewQueryCrossMarginPairService(). - Symbol("BTCUSDT"). - Do(context.Background()) - - s.r().NoError(err) - s.Equal("BTCUSDT", resp.SymbolDetail.Symbol) - s.True(resp.SymbolDetail.IsMarginTrade) - s.True(resp.SymbolDetail.IsBuyAllowed) - s.True(resp.SymbolDetail.IsSellAllowed) -} - -func (s *marginTestSuite) TestQueryMarginAsset() { - data := []byte(` - { - "assetFullName": "Bitcoin", - "assetName": "BTC", - "isBorrowable": true, - "isMortgageable": true, - "userMinBorrow": "0.00100000", - "userMinRepay": "0.00000000" - } - `) - - s.mockDo(data, nil) - defer s.assertDo() - - resp, err := s.client.NewQueryMarginAssetService(). - Asset("BTC"). - Do(context.Background()) - - s.r().NoError(err) - s.Equal("Bitcoin", resp.FullName) - s.Equal("BTC", resp.Name) - s.True(resp.Borrowable) - s.True(resp.Mortgageable) - s.Equal("0.00100000", resp.UserMinBorrow) - s.Equal("0.00000000", resp.UserMinRepay) -} - -func (s *marginTestSuite) TestRepayRecord() { - data := []byte(` - { - "rows": [ - { - "isolatedSymbol": "BTCUSDT", - "amount": "1.00000000", - "asset": "BTC", - "interest": "0.00000005", - "principal": "1.00000000", - "status": "CONFIRMED", - "timestamp": 1613450271000, - "txId": 123 - } - ], - "total": 1 - } - `) - - s.mockDo(data, nil) - defer s.assertDo() - - resp, err := s.client.NewRepayRecordService(). - Asset("BTC"). - IsolatedSymbol("BTCUSDT"). - StartTime(1613450271000). - EndTime(1613536671000). - Current(1). - Size(500). - Archived("true"). - Do(context.Background()) - - s.r().NoError(err) - s.Len(resp.Rows, 1) - s.Equal("BTCUSDT", resp.Rows[0].IsolatedSymbol) - s.Equal("1.00000000", resp.Rows[0].Amount) - s.Equal("BTC", resp.Rows[0].Asset) - s.Equal("0.00000005", resp.Rows[0].Interest) - s.Equal("1.00000000", resp.Rows[0].Principal) - s.Equal("CONFIRMED", resp.Rows[0].Status) - s.Equal(uint64(1613450271000), resp.Rows[0].Timestamp) - s.Equal(1, resp.Total) -}