binance: fix and add loan/repay history test

This commit is contained in:
c9s 2022-05-29 00:52:22 +08:00
parent aec9de8dd6
commit 1ab10eb574
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
11 changed files with 672 additions and 45 deletions

View File

@ -14,6 +14,7 @@ import (
"github.com/c9s/requestgen"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/types"
)
@ -21,14 +22,16 @@ import (
const defaultHTTPTimeout = time.Second * 15
const RestBaseURL = "https://api.binance.com"
const SandboxRestBaseURL = "https://testnet.binance.vision"
const DebugRequestResponse = false
var defaultHttpClient = &http.Client{
Timeout: defaultHTTPTimeout,
}
type RestClient struct {
BaseURL *url.URL
requestgen.BaseAPIClient
client *http.Client
Key, Secret, Passphrase string
KeyVersion string
Key, Secret string
recvWindow int
timeOffset int64
@ -41,10 +44,9 @@ func NewClient() *RestClient {
}
client := &RestClient{
BaseURL: u,
KeyVersion: "2",
client: &http.Client{
Timeout: defaultHTTPTimeout,
BaseAPIClient: requestgen.BaseAPIClient{
BaseURL: u,
HttpClient: defaultHttpClient,
},
}
@ -77,27 +79,6 @@ func (c *RestClient) NewRequest(ctx context.Context, method, refURL string, para
return http.NewRequestWithContext(ctx, method, pathURL.String(), bytes.NewReader(body))
}
// sendRequest sends the request to the API server and handle the response
func (c *RestClient) SendRequest(req *http.Request) (*requestgen.Response, error) {
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
// newResponse reads the response body and return a new Response object
response, err := requestgen.NewResponse(resp)
if err != nil {
return response, err
}
// Check error, if there is an error, return the ErrorResponse struct type
if response.IsError() {
return response, errors.New(string(response.Body))
}
return response, nil
}
func (c *RestClient) SetTimeOffsetFromServer(ctx context.Context) error {
req, err := c.NewRequest(ctx, "GET", "/api/v3/time", nil, nil)
if err != nil {
@ -122,6 +103,17 @@ func (c *RestClient) SetTimeOffsetFromServer(ctx context.Context) error {
return nil
}
func (c *RestClient) SendRequest(req *http.Request) (*requestgen.Response, error) {
if DebugRequestResponse {
logrus.Debugf("-> request: %+v", req)
response, err := c.BaseAPIClient.SendRequest(req)
logrus.Debugf("<- response: %s", string(response.Body))
return response, err
}
return c.BaseAPIClient.SendRequest(req)
}
// newAuthenticatedRequest creates new http request for authenticated routes.
func (c *RestClient) NewAuthenticatedRequest(ctx context.Context, method, refURL string, params url.Values, payload interface{}) (*http.Request, error) {
if len(c.Key) == 0 {

View File

@ -0,0 +1,54 @@
package binanceapi
import (
"time"
"github.com/c9s/requestgen"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
// one of PENDING (pending execution), CONFIRMED (successfully loaned), FAILED (execution failed, nothing happened to your account);
type LoanStatus string
const (
LoanStatusPending LoanStatus = "PENDING"
LoanStatusConfirmed LoanStatus = "CONFIRMED"
LoanStatusFailed LoanStatus = "FAILED"
)
type MarginLoanRecord struct {
IsolatedSymbol string `json:"isolatedSymbol"`
TxId int64 `json:"txId"`
Asset string `json:"asset"`
Principal fixedpoint.Value `json:"principal"`
Timestamp types.MillisecondTimestamp `json:"timestamp"`
Status LoanStatus `json:"status"`
}
// GetMarginLoanHistoryRequest
//
// txId or startTime must be sent. txId takes precedence.
// Response in descending order
// If isolatedSymbol is not sent, crossed margin data will be returned
// The max interval between startTime and endTime is 30 days.
// If startTime and endTime not sent, return records of the last 7 days by default
// Set archived to true to query data from 6 months ago
//
//go:generate requestgen -method GET -url "/sapi/v1/margin/loan" -type GetMarginLoanHistoryRequest -responseType .RowsResponse -responseDataField Rows -responseDataType []MarginLoanRecord
type GetMarginLoanHistoryRequest struct {
client requestgen.AuthenticatedAPIClient
asset string `param:"asset"`
startTime *time.Time `param:"startTime,milliseconds"`
endTime *time.Time `param:"endTime,milliseconds"`
isolatedSymbol *string `param:"isolatedSymbol"`
archived *bool `param:"archived"`
size *int `param:"size"`
current *int `param:"current"`
}
func (c *RestClient) NewGetMarginLoanHistoryRequest() *GetMarginLoanHistoryRequest {
return &GetMarginLoanHistoryRequest{client: c}
}

View File

@ -0,0 +1,234 @@
// Code generated by "requestgen -method GET -url /sapi/v1/margin/loan -type GetMarginLoanHistoryRequest -responseType .RowsResponse -responseDataField Rows -responseDataType []MarginLoanRecord"; DO NOT EDIT.
package binanceapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
"strconv"
"time"
)
func (g *GetMarginLoanHistoryRequest) Asset(asset string) *GetMarginLoanHistoryRequest {
g.asset = asset
return g
}
func (g *GetMarginLoanHistoryRequest) StartTime(startTime time.Time) *GetMarginLoanHistoryRequest {
g.startTime = &startTime
return g
}
func (g *GetMarginLoanHistoryRequest) EndTime(endTime time.Time) *GetMarginLoanHistoryRequest {
g.endTime = &endTime
return g
}
func (g *GetMarginLoanHistoryRequest) IsolatedSymbol(isolatedSymbol string) *GetMarginLoanHistoryRequest {
g.isolatedSymbol = &isolatedSymbol
return g
}
func (g *GetMarginLoanHistoryRequest) Archived(archived bool) *GetMarginLoanHistoryRequest {
g.archived = &archived
return g
}
func (g *GetMarginLoanHistoryRequest) Size(size int) *GetMarginLoanHistoryRequest {
g.size = &size
return g
}
func (g *GetMarginLoanHistoryRequest) Current(current int) *GetMarginLoanHistoryRequest {
g.current = &current
return g
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (g *GetMarginLoanHistoryRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
query := url.Values{}
for _k, _v := range params {
query.Add(_k, fmt.Sprintf("%v", _v))
}
return query, nil
}
// GetParameters builds and checks the parameters and return the result in a map object
func (g *GetMarginLoanHistoryRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check asset field -> json key asset
asset := g.asset
// assign parameter of asset
params["asset"] = asset
// check startTime field -> json key startTime
if g.startTime != nil {
startTime := *g.startTime
// assign parameter of startTime
// convert time.Time to milliseconds time stamp
params["startTime"] = strconv.FormatInt(startTime.UnixNano()/int64(time.Millisecond), 10)
} else {
}
// check endTime field -> json key endTime
if g.endTime != nil {
endTime := *g.endTime
// assign parameter of endTime
// convert time.Time to milliseconds time stamp
params["endTime"] = strconv.FormatInt(endTime.UnixNano()/int64(time.Millisecond), 10)
} else {
}
// check isolatedSymbol field -> json key isolatedSymbol
if g.isolatedSymbol != nil {
isolatedSymbol := *g.isolatedSymbol
// assign parameter of isolatedSymbol
params["isolatedSymbol"] = isolatedSymbol
} else {
}
// check archived field -> json key archived
if g.archived != nil {
archived := *g.archived
// assign parameter of archived
params["archived"] = archived
} else {
}
// check size field -> json key size
if g.size != nil {
size := *g.size
// assign parameter of size
params["size"] = size
} else {
}
// check current field -> json key current
if g.current != nil {
current := *g.current
// assign parameter of current
params["current"] = current
} else {
}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (g *GetMarginLoanHistoryRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := g.GetParameters()
if err != nil {
return query, err
}
for _k, _v := range params {
if g.isVarSlice(_v) {
g.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (g *GetMarginLoanHistoryRequest) GetParametersJSON() ([]byte, error) {
params, err := g.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
func (g *GetMarginLoanHistoryRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (g *GetMarginLoanHistoryRequest) applySlugsToUrl(url string, slugs map[string]string) string {
for _k, _v := range slugs {
needleRE := regexp.MustCompile(":" + _k + "\\b")
url = needleRE.ReplaceAllString(url, _v)
}
return url
}
func (g *GetMarginLoanHistoryRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (g *GetMarginLoanHistoryRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (g *GetMarginLoanHistoryRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{}
params, err := g.GetSlugParameters()
if err != nil {
return slugs, nil
}
for _k, _v := range params {
slugs[_k] = fmt.Sprintf("%v", _v)
}
return slugs, nil
}
func (g *GetMarginLoanHistoryRequest) Do(ctx context.Context) ([]MarginLoanRecord, error) {
// empty params for GET operation
var params interface{}
query, err := g.GetParametersQuery()
if err != nil {
return nil, err
}
apiURL := "/sapi/v1/margin/loan"
req, err := g.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params)
if err != nil {
return nil, err
}
response, err := g.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse RowsResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
var data []MarginLoanRecord
if err := json.Unmarshal(apiResponse.Rows, &data); err != nil {
return nil, err
}
return data, nil
}

View File

@ -0,0 +1,29 @@
package binanceapi
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func Test_GetMarginLoanHistoryRequest(t *testing.T) {
client := getTestClientOrSkip(t)
ctx := context.Background()
err := client.SetTimeOffsetFromServer(ctx)
assert.NoError(t, err)
req := client.NewGetMarginLoanHistoryRequest()
req.Asset("USDT")
req.IsolatedSymbol("DOTUSDT")
req.StartTime(time.Date(2022, time.February, 1, 0, 0, 0, 0, time.UTC))
req.EndTime(time.Date(2022, time.March, 1, 0, 0, 0, 0, time.UTC))
req.Size(100)
records, err := req.Do(ctx)
assert.NoError(t, err)
assert.NotEmpty(t, records)
t.Logf("loans: %+v", records)
}

View File

@ -0,0 +1,44 @@
package binanceapi
import (
"time"
"github.com/c9s/requestgen"
)
// RepayStatus one of PENDING (pending execution), CONFIRMED (successfully loaned), FAILED (execution failed, nothing happened to your account);
type RepayStatus string
const (
RepayStatusPending LoanStatus = "PENDING"
RepayStatusConfirmed LoanStatus = "CONFIRMED"
RepayStatusFailed LoanStatus = "FAILED"
)
type MarginRepayRecord 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 int64 `json:"timestamp"`
TxId int64 `json:"txId"`
}
//go:generate requestgen -method GET -url "/sapi/v1/margin/repay" -type GetMarginRepayHistoryRequest -responseType .RowsResponse -responseDataField Rows -responseDataType []MarginRepayRecord
type GetMarginRepayHistoryRequest struct {
client requestgen.AuthenticatedAPIClient
asset string `param:"asset"`
startTime *time.Time `param:"startTime,milliseconds"`
endTime *time.Time `param:"endTime,milliseconds"`
isolatedSymbol *string `param:"isolatedSymbol"`
archived *bool `param:"archived"`
size *int `param:"size"`
current *int `param:"current"`
}
func (c *RestClient) NewGetMarginRepayHistoryRequest() *GetMarginRepayHistoryRequest {
return &GetMarginRepayHistoryRequest{client: c}
}

View File

@ -0,0 +1,234 @@
// Code generated by "requestgen -method GET -url /sapi/v1/margin/repay -type GetMarginRepayHistoryRequest -responseType .RowsResponse -responseDataField Rows -responseDataType []MarginRepayRecord"; DO NOT EDIT.
package binanceapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
"strconv"
"time"
)
func (g *GetMarginRepayHistoryRequest) Asset(asset string) *GetMarginRepayHistoryRequest {
g.asset = asset
return g
}
func (g *GetMarginRepayHistoryRequest) StartTime(startTime time.Time) *GetMarginRepayHistoryRequest {
g.startTime = &startTime
return g
}
func (g *GetMarginRepayHistoryRequest) EndTime(endTime time.Time) *GetMarginRepayHistoryRequest {
g.endTime = &endTime
return g
}
func (g *GetMarginRepayHistoryRequest) IsolatedSymbol(isolatedSymbol string) *GetMarginRepayHistoryRequest {
g.isolatedSymbol = &isolatedSymbol
return g
}
func (g *GetMarginRepayHistoryRequest) Archived(archived bool) *GetMarginRepayHistoryRequest {
g.archived = &archived
return g
}
func (g *GetMarginRepayHistoryRequest) Size(size int) *GetMarginRepayHistoryRequest {
g.size = &size
return g
}
func (g *GetMarginRepayHistoryRequest) Current(current int) *GetMarginRepayHistoryRequest {
g.current = &current
return g
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (g *GetMarginRepayHistoryRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
query := url.Values{}
for _k, _v := range params {
query.Add(_k, fmt.Sprintf("%v", _v))
}
return query, nil
}
// GetParameters builds and checks the parameters and return the result in a map object
func (g *GetMarginRepayHistoryRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check asset field -> json key asset
asset := g.asset
// assign parameter of asset
params["asset"] = asset
// check startTime field -> json key startTime
if g.startTime != nil {
startTime := *g.startTime
// assign parameter of startTime
// convert time.Time to milliseconds time stamp
params["startTime"] = strconv.FormatInt(startTime.UnixNano()/int64(time.Millisecond), 10)
} else {
}
// check endTime field -> json key endTime
if g.endTime != nil {
endTime := *g.endTime
// assign parameter of endTime
// convert time.Time to milliseconds time stamp
params["endTime"] = strconv.FormatInt(endTime.UnixNano()/int64(time.Millisecond), 10)
} else {
}
// check isolatedSymbol field -> json key isolatedSymbol
if g.isolatedSymbol != nil {
isolatedSymbol := *g.isolatedSymbol
// assign parameter of isolatedSymbol
params["isolatedSymbol"] = isolatedSymbol
} else {
}
// check archived field -> json key archived
if g.archived != nil {
archived := *g.archived
// assign parameter of archived
params["archived"] = archived
} else {
}
// check size field -> json key size
if g.size != nil {
size := *g.size
// assign parameter of size
params["size"] = size
} else {
}
// check current field -> json key current
if g.current != nil {
current := *g.current
// assign parameter of current
params["current"] = current
} else {
}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (g *GetMarginRepayHistoryRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := g.GetParameters()
if err != nil {
return query, err
}
for _k, _v := range params {
if g.isVarSlice(_v) {
g.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (g *GetMarginRepayHistoryRequest) GetParametersJSON() ([]byte, error) {
params, err := g.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
func (g *GetMarginRepayHistoryRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (g *GetMarginRepayHistoryRequest) applySlugsToUrl(url string, slugs map[string]string) string {
for _k, _v := range slugs {
needleRE := regexp.MustCompile(":" + _k + "\\b")
url = needleRE.ReplaceAllString(url, _v)
}
return url
}
func (g *GetMarginRepayHistoryRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (g *GetMarginRepayHistoryRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (g *GetMarginRepayHistoryRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{}
params, err := g.GetSlugParameters()
if err != nil {
return slugs, nil
}
for _k, _v := range params {
slugs[_k] = fmt.Sprintf("%v", _v)
}
return slugs, nil
}
func (g *GetMarginRepayHistoryRequest) Do(ctx context.Context) ([]MarginRepayRecord, error) {
// empty params for GET operation
var params interface{}
query, err := g.GetParametersQuery()
if err != nil {
return nil, err
}
apiURL := "/sapi/v1/margin/repay"
req, err := g.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params)
if err != nil {
return nil, err
}
response, err := g.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse RowsResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
var data []MarginRepayRecord
if err := json.Unmarshal(apiResponse.Rows, &data); err != nil {
return nil, err
}
return data, nil
}

View File

@ -0,0 +1,29 @@
package binanceapi
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func Test_GetMarginRepayHistoryRequest(t *testing.T) {
client := getTestClientOrSkip(t)
ctx := context.Background()
err := client.SetTimeOffsetFromServer(ctx)
assert.NoError(t, err)
req := client.NewGetMarginRepayHistoryRequest()
req.Asset("USDT")
req.IsolatedSymbol("DOTUSDT")
req.StartTime(time.Date(2022, time.February, 1, 0, 0, 0, 0, time.UTC))
req.EndTime(time.Date(2022, time.March, 1, 0, 0, 0, 0, time.UTC))
req.Size(100)
records, err := req.Do(ctx)
assert.NoError(t, err)
assert.NotEmpty(t, records)
t.Logf("loans: %+v", records)
}

View File

@ -1,7 +1,6 @@
package binanceapi
import (
"encoding/json"
"time"
"github.com/c9s/requestgen"
@ -25,23 +24,12 @@ type SpotRebate struct {
UpdateTime types.MillisecondTimestamp `json:"updateTime"`
}
type PagedResponse struct {
Status string `json:"status"`
Type string `json:"type"`
Code string `json:"code"`
Data struct {
Page int `json:"page"`
TotalRecords int `json:"totalRecords"`
TotalPageNum int `json:"totalPageNum"`
Data json.RawMessage `json:"data"`
} `json:"data"`
}
// GetSpotRebateHistoryRequest
// 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.
// The earliest startTime is supported on June 10, 2020
//go:generate requestgen -method GET -url "/sapi/v1/rebate/taxQuery" -type GetSpotRebateHistoryRequest -responseType PagedResponse -responseDataField Data.Data -responseDataType []SpotRebate
//go:generate requestgen -method GET -url "/sapi/v1/rebate/taxQuery" -type GetSpotRebateHistoryRequest -responseType PagedDataResponse -responseDataField Data.Data -responseDataType []SpotRebate
type GetSpotRebateHistoryRequest struct {
client requestgen.AuthenticatedAPIClient

View File

@ -1,4 +1,4 @@
// Code generated by "requestgen -method GET -url /sapi/v1/rebate/taxQuery -type GetSpotRebateHistoryRequest -responseType PagedResponse -responseDataField Data.Data -responseDataType []SpotRebate"; DO NOT EDIT.
// Code generated by "requestgen -method GET -url /sapi/v1/rebate/taxQuery -type GetSpotRebateHistoryRequest -responseType PagedDataResponse -responseDataField Data.Data -responseDataType []SpotRebate"; DO NOT EDIT.
package binanceapi
@ -160,7 +160,7 @@ func (g *GetSpotRebateHistoryRequest) Do(ctx context.Context) ([]SpotRebate, err
return nil, err
}
var apiResponse PagedResponse
var apiResponse PagedDataResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}

View File

@ -0,0 +1,15 @@
package binanceapi
import "encoding/json"
type PagedDataResponse struct {
Status string `json:"status"`
Type string `json:"type"`
Code string `json:"code"`
Data struct {
Page int `json:"page"`
TotalRecords int `json:"totalRecords"`
TotalPageNum int `json:"totalPageNum"`
Data json.RawMessage `json:"data"`
} `json:"data"`
}

View File

@ -0,0 +1,8 @@
package binanceapi
import "encoding/json"
type RowsResponse struct {
Rows json.RawMessage `json:"rows"`
Total int `json:"total"`
}