Skip to content

Commit

Permalink
Merge branch 'master' into feat/wss-futures-client
Browse files Browse the repository at this point in the history
  • Loading branch information
xyq-c-cpp authored Nov 6, 2024
2 parents dd81108 + a836b57 commit ae51521
Show file tree
Hide file tree
Showing 8 changed files with 763 additions and 130 deletions.
8 changes: 7 additions & 1 deletion v2/delivery/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"github.com/adshao/go-binance/v2/common"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"time"

"github.com/adshao/go-binance/v2/common"

"github.com/bitly/go-simplejson"
)

Expand Down Expand Up @@ -490,3 +491,8 @@ func (c *Client) NewChangePositionModeService() *ChangePositionModeService {
func (c *Client) NewGetPositionModeService() *GetPositionModeService {
return &GetPositionModeService{c: c}
}

// NewGetFundingRateService init funding rate service
func (c *Client) NewGetFundingInfoService() *GetFundingInfoService {
return &GetFundingInfoService{c: c}
}
37 changes: 37 additions & 0 deletions v2/delivery/funding_info_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package delivery

import (
"context"
"encoding/json"
"net/http"
)

type GetFundingInfoService struct {
c *Client
}

type FundingInfo struct {
Symbol string `json:"symbol"`
AdjustedFundingRateCap string `json:"adjustedFundingRateCap"`
AdjustedFundingRateFloor string `json:"adjustedFundingRateFloor"`
FundingIntervalHours int `json:"fundingIntervalHours"`
Disclaimer bool `json:"disclaimer"`
}

func (s *GetFundingInfoService) Do(ctx context.Context, opts ...RequestOption) (fundingInfo []*FundingInfo, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/dapi/v1/fundingInfo",
secType: secTypeNone,
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
fundingInfo = make([]*FundingInfo, 0)
err = json.Unmarshal(data, &fundingInfo)
if err != nil {
return nil, err
}
return fundingInfo, nil
}
55 changes: 55 additions & 0 deletions v2/delivery/funding_info_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package delivery

import (
"testing"

"github.com/stretchr/testify/suite"
)

type fundingInfoServiceTestSuite struct {
baseTestSuite
}

func TestFundingInfoService(t *testing.T) {
suite.Run(t, new(fundingInfoServiceTestSuite))
}

func (s *fundingInfoServiceTestSuite) TestGetFundingInfo() {
data := []byte(`[
{
"symbol": "BLZUSDT",
"adjustedFundingRateCap": "0.3",
"adjustedFundingRateFloor": "-0.3",
"fundingIntervalHours": 8,
"disclaimer": true
}
]`)
s.mockDo(data, nil)
defer s.assertDo()

s.assertReq(func(r *request) {
e := newRequest().setParams(params{})
s.assertRequestEqual(e, r)
})

fundingInfo, err := s.client.NewGetFundingInfoService().Do(newContext())
s.r().NoError(err)
s.Len(fundingInfo, 1)
e := &FundingInfo{
Symbol: "BLZUSDT",
AdjustedFundingRateCap: "0.3",
AdjustedFundingRateFloor: "-0.3",
FundingIntervalHours: 8,
Disclaimer: true,
}
s.assertFundingInfoEqual(e, fundingInfo[0])
}

func (s *fundingInfoServiceTestSuite) assertFundingInfoEqual(e, a *FundingInfo) {
r := s.r()
r.Equal(e.Symbol, a.Symbol, "Symbol")
r.Equal(e.AdjustedFundingRateCap, a.AdjustedFundingRateCap, "AdjustedFundingRateCap")
r.Equal(e.AdjustedFundingRateFloor, a.AdjustedFundingRateFloor, "AdjustedFundingRateFloor")
r.Equal(e.FundingIntervalHours, a.FundingIntervalHours, "FundingIntervalHours")
r.Equal(e.Disclaimer, a.Disclaimer, "Disclaimer")
}
22 changes: 22 additions & 0 deletions v2/futures/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ const (
OrderTypeTakeProfit OrderType = "TAKE_PROFIT"
OrderTypeTakeProfitMarket OrderType = "TAKE_PROFIT_MARKET"
OrderTypeTrailingStopMarket OrderType = "TRAILING_STOP_MARKET"
OrderTypeLiquidation OrderType = "LIQUIDATION"

TimeInForceTypeGTC TimeInForceType = "GTC" // Good Till Cancel
TimeInForceTypeIOC TimeInForceType = "IOC" // Immediate or Cancel
Expand Down Expand Up @@ -166,6 +167,7 @@ const (
UserDataEventTypeAccountUpdate UserDataEventType = "ACCOUNT_UPDATE"
UserDataEventTypeOrderTradeUpdate UserDataEventType = "ORDER_TRADE_UPDATE"
UserDataEventTypeAccountConfigUpdate UserDataEventType = "ACCOUNT_CONFIG_UPDATE"
UserDataEventTypeTradeLite UserDataEventType = "TRADE_LITE"

UserDataEventReasonTypeDeposit UserDataEventReasonType = "DEPOSIT"
UserDataEventReasonTypeWithdraw UserDataEventReasonType = "WITHDRAW"
Expand Down Expand Up @@ -698,3 +700,23 @@ func (c *Client) NewGetFeeBurnService() *GetFeeBurnService {
func (c *Client) NewFeeBurnService() *FeeBurnService {
return &FeeBurnService{c: c}
}

// NewListConvertAssetsService init list convert assets service
func (c *Client) NewListConvertExchangeInfoService() *ListConvertExchangeInfoService {
return &ListConvertExchangeInfoService{c: c}
}

// NewCreateConvertQuoteService init create convert quote service
func (c *Client) NewCreateConvertQuoteService() *CreateConvertQuoteService {
return &CreateConvertQuoteService{c: c}
}

// NewCreateConvertService init accept convert quote service
func (c *Client) NewConvertAcceptService() *ConvertAcceptService {
return &ConvertAcceptService{c: c}
}

// NewGetConvertStatusService init get convert status service
func (c *Client) NewGetConvertStatusService() *ConvertStatusService {
return &ConvertStatusService{c: c}
}
238 changes: 238 additions & 0 deletions v2/futures/convert_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
package futures

import (
"context"
"encoding/json"
"net/http"
)

type ListConvertExchangeInfoService struct {
c *Client
fromAsset string
toAsset string
}

type ConvertExchangeInfo struct {
FromAsset string `json:"fromAsset"`
ToAsset string `json:"toAsset"`
FromAssetMinAmount string `json:"fromAssetMinAmount"`
FromAssetMaxAmount string `json:"fromAssetMaxAmount"`
ToAssetMinAmount string `json:"toAssetMinAmount"`
ToAssetMaxAmount string `json:"toAssetMaxAmount"`
}

func (l *ListConvertExchangeInfoService) FromAsset(fromAsset string) *ListConvertExchangeInfoService {
l.fromAsset = fromAsset
return l
}

func (l *ListConvertExchangeInfoService) ToAsset(toAsset string) *ListConvertExchangeInfoService {
l.toAsset = toAsset
return l
}

func (l *ListConvertExchangeInfoService) Do(ctx context.Context, opts ...RequestOption) (res []ConvertExchangeInfo, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/fapi/v1/convert/exchangeInfo",
secType: secTypeNone,
}
if l.fromAsset != "" {
r.setParam("fromAsset", l.fromAsset)
}
if l.toAsset != "" {
r.setParam("toAsset", l.toAsset)
}

data, _, err := l.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
res = make([]ConvertExchangeInfo, 0, 50)
if err := json.Unmarshal(data, &res); err != nil {
return nil, err
}
return res, nil
}

type ConvertValidTime string

const (
ConvertValidTime10S ConvertValidTime = "10s"
ConvertValidTime30S ConvertValidTime = "30s"
ConvertValidTime1M ConvertValidTime = "1m"
ConvertValidTime2M ConvertValidTime = "2m"
)

type CreateConvertQuoteService struct {
c *Client
fromAsset string
toAsset string
fromAmount string
toAmount string
validTime ConvertValidTime
}

func (c *CreateConvertQuoteService) FromAsset(fromAsset string) *CreateConvertQuoteService {
c.fromAsset = fromAsset
return c
}

func (c *CreateConvertQuoteService) ToAsset(toAsset string) *CreateConvertQuoteService {
c.toAsset = toAsset
return c
}

func (c *CreateConvertQuoteService) FromAmount(fromAmount string) *CreateConvertQuoteService {
c.fromAmount = fromAmount
return c
}

func (c *CreateConvertQuoteService) ToAmount(toAmount string) *CreateConvertQuoteService {
c.toAmount = toAmount
return c
}

func (c *CreateConvertQuoteService) ValidTime(validTime ConvertValidTime) *CreateConvertQuoteService {
c.validTime = validTime
return c
}

type ConvertQuote struct {
QuoteId string `json:"quoteId"`
Ratio string `json:"ratio"`
InverseRatio string `json:"inverseRatio"`
ValidTimestamp int64 `json:"validTimestamp"`
ToAmount string `json:"toAmount"`
FromAmount string `json:"fromAmount"`
}

func (c *CreateConvertQuoteService) Do(ctx context.Context, opts ...RequestOption) (res *ConvertQuote, err error) {
r := &request{
method: http.MethodPost,
endpoint: "/fapi/v1/convert/getQuote",
secType: secTypeSigned,
}
m := params{
"fromAsset": c.fromAsset,
"toAsset": c.toAsset,
}
if c.fromAmount != "" {
m["fromAmount"] = c.fromAmount
}
if c.toAmount != "" {
m["toAmount"] = c.toAmount
}
if c.validTime != "" {
m["validTime"] = c.validTime
}
r.setFormParams(m)
data, _, err := c.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
res = new(ConvertQuote)
if err := json.Unmarshal(data, res); err != nil {
return nil, err
}
return res, nil
}

type ConvertAcceptService struct {
c *Client
quoteId string
}

func (c *ConvertAcceptService) QuoteId(quoteId string) *ConvertAcceptService {
c.quoteId = quoteId
return c
}

type ConvertAcceptStatus string

const (
ConvertAcceptStatusProcess ConvertAcceptStatus = "PROCESS"
ConvertAcceptStatusAcceptSuccess ConvertAcceptStatus = "ACCEPT_SUCCESS"
ConvertAcceptStatusSuccess ConvertAcceptStatus = "SUCCESS"
ConvertAcceptStatusFailed ConvertAcceptStatus = "FAILED"
)

type ConvertResult struct {
OrderId string `json:"orderId"`
CreateTime int64 `json:"createTime"`
OrderStatus ConvertAcceptStatus `json:"orderStatus"`
}

func (c *ConvertAcceptService) Do(ctx context.Context, opts ...RequestOption) (res *ConvertResult, err error) {
r := &request{
method: http.MethodPost,
endpoint: "/fapi/v1/convert/acceptQuote",
secType: secTypeSigned,
}
m := params{
"quoteId": c.quoteId,
}
r.setFormParams(m)
data, _, err := c.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
res = new(ConvertResult)
if err := json.Unmarshal(data, res); err != nil {
return nil, err
}
return res, nil
}

type ConvertStatusService struct {
c *Client
quoteId string
orderId string
}

func (c *ConvertStatusService) QuoteId(quoteId string) *ConvertStatusService {
c.quoteId = quoteId
return c
}

func (c *ConvertStatusService) OrderId(orderId string) *ConvertStatusService {
c.orderId = orderId
return c
}

type ConvertStatusResult struct {
OrderId string `json:"orderId"`
OrderStatus ConvertAcceptStatus `json:"orderStatus"`
FromAsset string `json:"fromAsset"`
FromAmount string `json:"fromAmount"`
ToAsset string `json:"toAsset"`
ToAmount string `json:"toAmount"`
Ratio string `json:"ratio"`
InverseRatio string `json:"inverseRatio"`
CreateTime int64 `json:"createTime"`
}

func (c *ConvertStatusService) Do(ctx context.Context, opts ...RequestOption) (res *ConvertStatusResult, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/fapi/v1/convert/orderStatus",
secType: secTypeSigned,
}
m := params{
"quoteId": c.quoteId,
}
if c.orderId != "" {
m["orderId"] = c.orderId
}
r.setParam("quoteId", c.quoteId)
r.setFormParams(m)
data, _, err := c.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
res = new(ConvertStatusResult)
if err := json.Unmarshal(data, res); err != nil {
return nil, err
}
return res, nil
}
Loading

0 comments on commit ae51521

Please sign in to comment.