binance: adjust margin history interface

This commit is contained in:
c9s 2022-05-29 01:42:08 +08:00
parent f58f44ffd8
commit 409ad9b75c
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
6 changed files with 111 additions and 16 deletions

View File

@ -24,7 +24,7 @@ const RestBaseURL = "https://api.binance.com"
const SandboxRestBaseURL = "https://testnet.binance.vision" const SandboxRestBaseURL = "https://testnet.binance.vision"
const DebugRequestResponse = false const DebugRequestResponse = false
var defaultHttpClient = &http.Client{ var DefaultHttpClient = &http.Client{
Timeout: defaultHTTPTimeout, Timeout: defaultHTTPTimeout,
} }
@ -37,8 +37,12 @@ type RestClient struct {
timeOffset int64 timeOffset int64
} }
func NewClient() *RestClient { func NewClient(baseURL string) *RestClient {
u, err := url.Parse(RestBaseURL) if len(baseURL) == 0 {
baseURL = RestBaseURL
}
u, err := url.Parse(baseURL)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -46,7 +50,7 @@ func NewClient() *RestClient {
client := &RestClient{ client := &RestClient{
BaseAPIClient: requestgen.BaseAPIClient{ BaseAPIClient: requestgen.BaseAPIClient{
BaseURL: u, BaseURL: u,
HttpClient: defaultHttpClient, HttpClient: DefaultHttpClient,
}, },
} }

View File

@ -35,7 +35,7 @@ func getTestClientOrSkip(t *testing.T) *RestClient {
return nil return nil
} }
client := NewClient() client := NewClient("")
client.Auth(key, secret) client.Auth(key, secret)
return client return client
} }
@ -124,7 +124,7 @@ func TestClient_privateCall(t *testing.T) {
t.SkipNow() t.SkipNow()
} }
client := NewClient() client := NewClient("")
client.Auth(key, secret) client.Auth(key, secret)
ctx := context.Background() ctx := context.Background()
@ -154,7 +154,7 @@ func TestClient_privateCall(t *testing.T) {
} }
func TestClient_setTimeOffsetFromServer(t *testing.T) { func TestClient_setTimeOffsetFromServer(t *testing.T) {
client := NewClient() client := NewClient("")
err := client.SetTimeOffsetFromServer(context.Background()) err := client.SetTimeOffsetFromServer(context.Background())
assert.NoError(t, err) assert.NoError(t, err)
} }

View File

@ -24,7 +24,6 @@ type SpotRebate struct {
UpdateTime types.MillisecondTimestamp `json:"updateTime"` UpdateTime types.MillisecondTimestamp `json:"updateTime"`
} }
// GetSpotRebateHistoryRequest // GetSpotRebateHistoryRequest
// The max interval between startTime and endTime is 30 days. // The max interval between startTime and endTime is 30 days.
// If startTime and endTime are not sent, the recent 7 days' data will be returned. // If startTime and endTime are not sent, the recent 7 days' data will be returned.

View File

@ -3,7 +3,6 @@ package binance
import ( import (
"context" "context"
"fmt" "fmt"
"net/http"
"os" "os"
"strconv" "strconv"
"strings" "strings"
@ -22,6 +21,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/exchange/binance/binanceapi"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util" "github.com/c9s/bbgo/pkg/util"
@ -77,17 +77,21 @@ type Exchange struct {
// futuresClient is used for usdt-m futures // futuresClient is used for usdt-m futures
futuresClient *futures.Client // USDT-M Futures futuresClient *futures.Client // USDT-M Futures
// deliveryClient *delivery.Client // Coin-M Futures // deliveryClient *delivery.Client // Coin-M Futures
// client2 is a newer version of the binance api client implemented by ourselves.
client2 *binanceapi.RestClient
} }
var timeSetter sync.Once var timeSetter sync.Once
func New(key, secret string) *Exchange { func New(key, secret string) *Exchange {
var client = binance.NewClient(key, secret) var client = binance.NewClient(key, secret)
client.HTTPClient = &http.Client{Timeout: 15 * time.Second} client.HTTPClient = binanceapi.DefaultHttpClient
client.Debug = viper.GetBool("debug-binance-client") client.Debug = viper.GetBool("debug-binance-client")
var futuresClient = binance.NewFuturesClient(key, secret) var futuresClient = binance.NewFuturesClient(key, secret)
futuresClient.HTTPClient = &http.Client{Timeout: 15 * time.Second} futuresClient.HTTPClient = binanceapi.DefaultHttpClient
futuresClient.Debug = viper.GetBool("debug-binance-futures-client")
if isBinanceUs() { if isBinanceUs() {
client.BaseURL = BinanceUSBaseURL client.BaseURL = BinanceUSBaseURL
@ -98,6 +102,8 @@ func New(key, secret string) *Exchange {
futuresClient.BaseURL = FutureTestBaseURL futuresClient.BaseURL = FutureTestBaseURL
} }
client2 := binanceapi.NewClient(client.BaseURL)
var err error var err error
if len(key) > 0 && len(secret) > 0 { if len(key) > 0 && len(secret) > 0 {
timeSetter.Do(func() { timeSetter.Do(func() {
@ -118,7 +124,7 @@ func New(key, secret string) *Exchange {
secret: secret, secret: secret,
client: client, client: client,
futuresClient: futuresClient, futuresClient: futuresClient,
// deliveryClient: deliveryClient, client2: client2,
} }
} }

View File

@ -0,0 +1,85 @@
package binance
import (
"context"
"time"
"github.com/c9s/bbgo/pkg/types"
)
func (e *Exchange) QueryLoanHistory(ctx context.Context, asset string, startTime, endTime *time.Time) ([]types.MarginLoanRecord, error) {
req := e.client2.NewGetMarginLoanHistoryRequest()
req.Asset(asset)
if startTime != nil {
req.StartTime(*startTime)
}
if endTime != nil {
req.EndTime(*endTime)
}
if e.MarginSettings.IsIsolatedMargin {
req.IsolatedSymbol(e.MarginSettings.IsolatedMarginSymbol)
}
loans, err := req.Do(ctx)
_ = loans
return nil, err
}
func (e *Exchange) QueryRepayHistory(ctx context.Context, asset string, startTime, endTime *time.Time) ([]types.MarginRepayRecord, error) {
req := e.client2.NewGetMarginRepayHistoryRequest()
req.Asset(asset)
if startTime != nil {
req.StartTime(*startTime)
}
if endTime != nil {
req.EndTime(*endTime)
}
if e.MarginSettings.IsIsolatedMargin {
req.IsolatedSymbol(e.MarginSettings.IsolatedMarginSymbol)
}
_, err := req.Do(ctx)
return nil, err
}
func (e *Exchange) QueryLiquidationHistory(ctx context.Context, startTime, endTime *time.Time) ([]types.MarginLiquidationRecord, error) {
req := e.client2.NewGetMarginLiquidationHistoryRequest()
if startTime != nil {
req.StartTime(*startTime)
}
if endTime != nil {
req.EndTime(*endTime)
}
if e.MarginSettings.IsIsolatedMargin {
req.IsolatedSymbol(e.MarginSettings.IsolatedMarginSymbol)
}
_, err := req.Do(ctx)
return nil, err
}
func (e *Exchange) QueryInterestHistory(ctx context.Context, asset string, startTime, endTime *time.Time) ([]types.MarginInterest, error) {
req := e.client2.NewGetMarginInterestHistoryRequest()
req.Asset(asset)
if startTime != nil {
req.StartTime(*startTime)
}
if endTime != nil {
req.EndTime(*endTime)
}
if e.MarginSettings.IsIsolatedMargin {
req.IsolatedSymbol(e.MarginSettings.IsolatedMarginSymbol)
}
_, err := req.Do(ctx)
return nil, err
}

View File

@ -2,6 +2,7 @@ package types
import ( import (
"context" "context"
"time"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
) )
@ -98,10 +99,10 @@ type MarginLiquidationRecord struct {
// MarginHistory provides the service of querying loan history and repay history // MarginHistory provides the service of querying loan history and repay history
type MarginHistory interface { type MarginHistory interface {
QueryLoanHistory() ([]MarginLoanRecord, error) QueryLoanHistory(ctx context.Context, asset string, startTime, endTime *time.Time) ([]MarginLoanRecord, error)
QueryRepayHistory() ([]MarginRepayRecord, error) QueryRepayHistory(ctx context.Context, asset string, startTime, endTime *time.Time) ([]MarginRepayRecord, error)
QueryLiquidationHistory() ([]MarginLiquidationRecord, error) QueryLiquidationHistory(ctx context.Context, startTime, endTime *time.Time) ([]MarginLiquidationRecord, error)
QueryInterestHistory() ([]MarginInterest, error) QueryInterestHistory(ctx context.Context, asset string, startTime, endTime *time.Time) ([]MarginInterest, error)
} }
type MarginSettings struct { type MarginSettings struct {