bbgo_origin/pkg/exchange/okex/okexapi/public.go

68 lines
1.5 KiB
Go
Raw Normal View History

package okexapi
import (
"context"
2023-09-06 11:14:21 +00:00
"encoding/json"
"net/url"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
2021-05-24 18:22:08 +00:00
"github.com/pkg/errors"
)
2023-09-06 11:14:21 +00:00
func (s *RestClient) NewGetFundingRate() *GetFundingRateRequest {
2021-05-24 18:22:08 +00:00
return &GetFundingRateRequest{
2023-09-06 11:14:21 +00:00
client: s,
2021-05-24 18:22:08 +00:00
}
}
type FundingRate struct {
InstrumentType string `json:"instType"`
InstrumentID string `json:"instId"`
FundingRate fixedpoint.Value `json:"fundingRate"`
NextFundingRate fixedpoint.Value `json:"nextFundingRate"`
FundingTime types.MillisecondTimestamp `json:"fundingTime"`
}
type GetFundingRateRequest struct {
client *RestClient
instId string
}
func (r *GetFundingRateRequest) InstrumentID(instId string) *GetFundingRateRequest {
r.instId = instId
return r
}
func (r *GetFundingRateRequest) Do(ctx context.Context) (*FundingRate, error) {
// SPOT, SWAP, FUTURES, OPTION
var params = url.Values{}
params.Add("instId", string(r.instId))
2023-09-06 11:14:21 +00:00
req, err := r.client.NewRequest(ctx, "GET", "/api/v5/public/funding-rate", params, nil)
2021-05-24 18:22:08 +00:00
if err != nil {
return nil, err
}
2023-09-06 11:14:21 +00:00
response, err := r.client.SendRequest(req)
2021-05-24 18:22:08 +00:00
if err != nil {
return nil, err
}
2023-09-06 11:14:21 +00:00
var apiResponse APIResponse
2021-05-24 18:22:08 +00:00
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
2023-09-06 11:14:21 +00:00
var data []FundingRate
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
2021-05-24 18:22:08 +00:00
2023-09-06 11:14:21 +00:00
if len(data) == 0 {
2021-05-24 18:22:08 +00:00
return nil, errors.New("empty funding rate data")
}
2023-09-06 11:14:21 +00:00
return &data[0], nil
2021-05-24 18:22:08 +00:00
}