Skip to content

Commit

Permalink
[okx] Add GetFundingRateHistory Api
Browse files Browse the repository at this point in the history
  • Loading branch information
nntaoli committed Jun 4, 2024
1 parent ef162b0 commit 0c34c16
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 54 deletions.
1 change: 1 addition & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type IFuturesPubRest interface {
//GetFundingRate
//获取资金费率,仅适用于永续合约
GetFundingRate(pair model.CurrencyPair, opts ...model.OptionParameter) (rate *model.FundingRate, responseBody []byte, err error)
GetFundingRateHistory(pair model.CurrencyPair, limit int, opts ...model.OptionParameter) (rates []model.FundingRate, responseBody []byte, err error)
}

// IFuturesPrvRest includes some special interface implementations for futures supplement.
Expand Down
14 changes: 14 additions & 0 deletions okx/common/pub.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ func (okx *OKxV5) GetFundingRate(pair CurrencyPair, opts ...OptionParameter) (ra
return rate, nil, err
}

func (okx *OKxV5) GetFundingRateHistory(pair CurrencyPair, limit int, opts ...OptionParameter) (rates []FundingRate, responseBody []byte, err error) {
reqUrl := fmt.Sprintf("%s%s", okx.UriOpts.Endpoint, okx.UriOpts.GetFundingRateHistoryUri)
param := url.Values{}
param.Set("instId", pair.Symbol)
param.Set("limit", fmt.Sprint(limit))
MergeOptionParams(&param, opts...)
data, responseBody, err := okx.DoNoAuthRequest(http.MethodGet, reqUrl, &param)
if err != nil {
return nil, responseBody, err
}
rates, err = okx.UnmarshalOpts.GetFundingRateHistoryResponseUnmarshaler(data)
return rates, nil, err
}

func (okx *OKxV5) DoNoAuthRequest(httpMethod, reqUrl string, params *url.Values) ([]byte, []byte, error) {
reqBody := ""
if http.MethodGet == httpMethod {
Expand Down
18 changes: 18 additions & 0 deletions okx/common/unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,24 @@ func (un *RespUnmarshaler) UnmarshalGetFundingRateResponse(data []byte) (*Fundin
return &rate, nil
}

func (un *RespUnmarshaler) UnmarshalGetFundingRateHistoryResponse(data []byte) ([]FundingRate, error) {
var rates []FundingRate
_, err := jsonparser.ArrayEach(data, func(item []byte, dataType jsonparser.ValueType, offset int, err error) {
var rate FundingRate
err = jsonparser.ObjectEach(item, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
switch string(key) {
case "fundingRate":
rate.Rate = cast.ToFloat64(string(value))
case "fundingTime":
rate.Tm = cast.ToInt64(string(value))
}
return nil
})
rates = append(rates, rate)
})
return rates, err
}

func (un *RespUnmarshaler) UnmarshalResponse(data []byte, res interface{}) error {
return json.Unmarshal(data, res)
}
56 changes: 29 additions & 27 deletions okx/common/v5.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,37 @@ func New() *OKxV5 {

f := &OKxV5{
UriOpts: UriOptions{
Endpoint: "https://www.okx.com",
KlineUri: "/api/v5/market/candles",
TickerUri: "/api/v5/market/ticker",
DepthUri: "/api/v5/market/books",
NewOrderUri: "/api/v5/trade/order",
GetOrderUri: "/api/v5/trade/order",
GetHistoryOrdersUri: "/api/v5/trade/orders-history",
GetPendingOrdersUri: "/api/v5/trade/orders-pending",
CancelOrderUri: "/api/v5/trade/cancel-order",
GetAccountUri: "/api/v5/account/balance",
GetPositionsUri: "/api/v5/account/positions",
GetExchangeInfoUri: "/api/v5/public/instruments",
GetFundingRateUri: "/api/v5/public/funding-rate",
Endpoint: "https://www.okx.com",
KlineUri: "/api/v5/market/candles",
TickerUri: "/api/v5/market/ticker",
DepthUri: "/api/v5/market/books",
NewOrderUri: "/api/v5/trade/order",
GetOrderUri: "/api/v5/trade/order",
GetHistoryOrdersUri: "/api/v5/trade/orders-history",
GetPendingOrdersUri: "/api/v5/trade/orders-pending",
CancelOrderUri: "/api/v5/trade/cancel-order",
GetAccountUri: "/api/v5/account/balance",
GetPositionsUri: "/api/v5/account/positions",
GetExchangeInfoUri: "/api/v5/public/instruments",
GetFundingRateUri: "/api/v5/public/funding-rate",
GetFundingRateHistoryUri: "/api/v5/public/funding-rate-history",
},
UnmarshalOpts: UnmarshalerOptions{
ResponseUnmarshaler: unmarshaler.UnmarshalResponse,
KlineUnmarshaler: unmarshaler.UnmarshalGetKlineResponse,
TickerUnmarshaler: unmarshaler.UnmarshalTicker,
DepthUnmarshaler: unmarshaler.UnmarshalDepth,
CreateOrderResponseUnmarshaler: unmarshaler.UnmarshalCreateOrderResponse,
GetPendingOrdersResponseUnmarshaler: unmarshaler.UnmarshalGetPendingOrdersResponse,
GetHistoryOrdersResponseUnmarshaler: unmarshaler.UnmarshalGetHistoryOrdersResponse,
CancelOrderResponseUnmarshaler: unmarshaler.UnmarshalCancelOrderResponse,
GetOrderInfoResponseUnmarshaler: unmarshaler.UnmarshalGetOrderInfoResponse,
GetAccountResponseUnmarshaler: unmarshaler.UnmarshalGetAccountResponse,
GetPositionsResponseUnmarshaler: unmarshaler.UnmarshalGetPositionsResponse,
GetFuturesAccountResponseUnmarshaler: unmarshaler.UnmarshalGetFuturesAccountResponse,
GetExchangeInfoResponseUnmarshaler: unmarshaler.UnmarshalGetExchangeInfoResponse,
GetFundingRateResponseUnmarshaler: unmarshaler.UnmarshalGetFundingRateResponse,
ResponseUnmarshaler: unmarshaler.UnmarshalResponse,
KlineUnmarshaler: unmarshaler.UnmarshalGetKlineResponse,
TickerUnmarshaler: unmarshaler.UnmarshalTicker,
DepthUnmarshaler: unmarshaler.UnmarshalDepth,
CreateOrderResponseUnmarshaler: unmarshaler.UnmarshalCreateOrderResponse,
GetPendingOrdersResponseUnmarshaler: unmarshaler.UnmarshalGetPendingOrdersResponse,
GetHistoryOrdersResponseUnmarshaler: unmarshaler.UnmarshalGetHistoryOrdersResponse,
CancelOrderResponseUnmarshaler: unmarshaler.UnmarshalCancelOrderResponse,
GetOrderInfoResponseUnmarshaler: unmarshaler.UnmarshalGetOrderInfoResponse,
GetAccountResponseUnmarshaler: unmarshaler.UnmarshalGetAccountResponse,
GetPositionsResponseUnmarshaler: unmarshaler.UnmarshalGetPositionsResponse,
GetFuturesAccountResponseUnmarshaler: unmarshaler.UnmarshalGetFuturesAccountResponse,
GetExchangeInfoResponseUnmarshaler: unmarshaler.UnmarshalGetExchangeInfoResponse,
GetFundingRateResponseUnmarshaler: unmarshaler.UnmarshalGetFundingRateResponse,
GetFundingRateHistoryResponseUnmarshaler: unmarshaler.UnmarshalGetFundingRateHistoryResponse,
},
}

Expand Down
36 changes: 22 additions & 14 deletions options/response_unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,24 @@ type GetPositionsResponseUnmarshaler func([]byte) ([]model.FuturesPosition, erro
type GetFuturesAccountResponseUnmarshaler func([]byte) (map[string]model.FuturesAccount, error)
type GetExchangeInfoResponseUnmarshaler func([]byte) (map[string]model.CurrencyPair, error)
type GetFundingRateResponseUnmarshaler func([]byte) (*model.FundingRate, error)
type GetFundingRateHistoryResponseUnmarshaler func([]byte) ([]model.FundingRate, error)

type UnmarshalerOptions struct {
ResponseUnmarshaler ResponseUnmarshaler
TickerUnmarshaler GetTickerResponseUnmarshaler
DepthUnmarshaler GetDepthResponseUnmarshaler
KlineUnmarshaler GetKlineResponseUnmarshaler
CreateOrderResponseUnmarshaler CreateOrderResponseUnmarshaler
GetOrderInfoResponseUnmarshaler GetOrderInfoResponseUnmarshaler
GetPendingOrdersResponseUnmarshaler GetPendingOrdersResponseUnmarshaler
GetHistoryOrdersResponseUnmarshaler GetHistoryOrdersResponseUnmarshaler
CancelOrderResponseUnmarshaler CancelOrderResponseUnmarshaler
GetAccountResponseUnmarshaler GetAccountResponseUnmarshaler
GetPositionsResponseUnmarshaler GetPositionsResponseUnmarshaler
GetFuturesAccountResponseUnmarshaler GetFuturesAccountResponseUnmarshaler
GetExchangeInfoResponseUnmarshaler GetExchangeInfoResponseUnmarshaler
GetFundingRateResponseUnmarshaler GetFundingRateResponseUnmarshaler
ResponseUnmarshaler ResponseUnmarshaler
TickerUnmarshaler GetTickerResponseUnmarshaler
DepthUnmarshaler GetDepthResponseUnmarshaler
KlineUnmarshaler GetKlineResponseUnmarshaler
CreateOrderResponseUnmarshaler CreateOrderResponseUnmarshaler
GetOrderInfoResponseUnmarshaler GetOrderInfoResponseUnmarshaler
GetPendingOrdersResponseUnmarshaler GetPendingOrdersResponseUnmarshaler
GetHistoryOrdersResponseUnmarshaler GetHistoryOrdersResponseUnmarshaler
CancelOrderResponseUnmarshaler CancelOrderResponseUnmarshaler
GetAccountResponseUnmarshaler GetAccountResponseUnmarshaler
GetPositionsResponseUnmarshaler GetPositionsResponseUnmarshaler
GetFuturesAccountResponseUnmarshaler GetFuturesAccountResponseUnmarshaler
GetExchangeInfoResponseUnmarshaler GetExchangeInfoResponseUnmarshaler
GetFundingRateResponseUnmarshaler GetFundingRateResponseUnmarshaler
GetFundingRateHistoryResponseUnmarshaler GetFundingRateHistoryResponseUnmarshaler
}

type UnmarshalerOption func(options *UnmarshalerOptions)
Expand Down Expand Up @@ -118,3 +120,9 @@ func WithGetFundingRateResponseUnmarshaler(unmarshaler GetFundingRateResponseUnm
options.GetFundingRateResponseUnmarshaler = unmarshaler
}
}

func WithGetFundingRateHistoryResponseUnmarshaler(unmarshaler GetFundingRateHistoryResponseUnmarshaler) UnmarshalerOption {
return func(options *UnmarshalerOptions) {
options.GetFundingRateHistoryResponseUnmarshaler = unmarshaler
}
}
33 changes: 20 additions & 13 deletions options/uri_options.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package options

type UriOptions struct {
Endpoint string
TickerUri string
DepthUri string
KlineUri string
GetOrderUri string
GetPendingOrdersUri string
GetHistoryOrdersUri string
CancelOrderUri string
NewOrderUri string
GetAccountUri string
GetPositionsUri string
GetExchangeInfoUri string
GetFundingRateUri string
Endpoint string
TickerUri string
DepthUri string
KlineUri string
GetOrderUri string
GetPendingOrdersUri string
GetHistoryOrdersUri string
CancelOrderUri string
NewOrderUri string
GetAccountUri string
GetPositionsUri string
GetExchangeInfoUri string
GetFundingRateUri string
GetFundingRateHistoryUri string
}

type UriOption func(*UriOptions)
Expand Down Expand Up @@ -95,3 +96,9 @@ func WithGetFundingRateUri(uri string) UriOption {
c.GetFundingRateUri = uri
}
}

func WithGetFundingRateHistoryUri(uri string) UriOption {
return func(c *UriOptions) {
c.GetFundingRateHistoryUri = uri
}
}

0 comments on commit 0c34c16

Please sign in to comment.