2022-05-28 17:42:08 +00:00
|
|
|
package binance
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2022-05-29 03:52:25 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/exchange/binance/binanceapi"
|
2022-05-28 17:42:08 +00:00
|
|
|
"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)
|
2022-05-29 03:52:25 +00:00
|
|
|
req.Size(100)
|
2022-05-28 17:42:08 +00:00
|
|
|
|
|
|
|
if startTime != nil {
|
|
|
|
req.StartTime(*startTime)
|
2022-05-29 03:52:25 +00:00
|
|
|
|
|
|
|
// 6 months
|
|
|
|
if time.Since(*startTime) > time.Hour*24*30*6 {
|
|
|
|
req.Archived(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if startTime != nil && endTime != nil {
|
|
|
|
duration := endTime.Sub(*startTime)
|
|
|
|
if duration > time.Hour*24*30 {
|
|
|
|
t := startTime.Add(time.Hour * 24 * 30)
|
|
|
|
endTime = &t
|
|
|
|
}
|
2022-05-28 17:42:08 +00:00
|
|
|
}
|
2022-05-29 03:52:25 +00:00
|
|
|
|
2022-05-28 17:42:08 +00:00
|
|
|
if endTime != nil {
|
|
|
|
req.EndTime(*endTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.MarginSettings.IsIsolatedMargin {
|
|
|
|
req.IsolatedSymbol(e.MarginSettings.IsolatedMarginSymbol)
|
|
|
|
}
|
|
|
|
|
2022-05-29 03:52:25 +00:00
|
|
|
records, err := req.Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var loans []types.MarginLoanRecord
|
|
|
|
for _, record := range records {
|
|
|
|
loans = append(loans, toGlobalLoan(record))
|
|
|
|
}
|
|
|
|
|
|
|
|
return loans, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func toGlobalLoan(record binanceapi.MarginLoanRecord) types.MarginLoanRecord {
|
|
|
|
return types.MarginLoanRecord{
|
|
|
|
TransactionID: uint64(record.TxId),
|
|
|
|
Asset: record.Asset,
|
|
|
|
Principle: record.Principal,
|
|
|
|
Time: types.Time(record.Timestamp),
|
|
|
|
IsolatedSymbol: record.IsolatedSymbol,
|
|
|
|
}
|
2022-05-28 17:42:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) QueryRepayHistory(ctx context.Context, asset string, startTime, endTime *time.Time) ([]types.MarginRepayRecord, error) {
|
|
|
|
req := e.client2.NewGetMarginRepayHistoryRequest()
|
|
|
|
req.Asset(asset)
|
2022-05-29 03:52:25 +00:00
|
|
|
req.Size(100)
|
2022-05-28 17:42:08 +00:00
|
|
|
|
|
|
|
if startTime != nil {
|
|
|
|
req.StartTime(*startTime)
|
2022-05-29 03:52:25 +00:00
|
|
|
|
|
|
|
// 6 months
|
|
|
|
if time.Since(*startTime) > time.Hour*24*30*6 {
|
|
|
|
req.Archived(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if startTime != nil && endTime != nil {
|
|
|
|
duration := endTime.Sub(*startTime)
|
|
|
|
if duration > time.Hour*24*30 {
|
|
|
|
t := startTime.Add(time.Hour * 24 * 30)
|
|
|
|
endTime = &t
|
|
|
|
}
|
2022-05-28 17:42:08 +00:00
|
|
|
}
|
2022-05-29 03:52:25 +00:00
|
|
|
|
2022-05-28 17:42:08 +00:00
|
|
|
if endTime != nil {
|
|
|
|
req.EndTime(*endTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.MarginSettings.IsIsolatedMargin {
|
|
|
|
req.IsolatedSymbol(e.MarginSettings.IsolatedMarginSymbol)
|
|
|
|
}
|
|
|
|
|
2022-05-29 03:52:25 +00:00
|
|
|
records, err := req.Do(ctx)
|
|
|
|
|
|
|
|
var repays []types.MarginRepayRecord
|
|
|
|
for _, record := range records {
|
|
|
|
repays = append(repays, toGlobalRepay(record))
|
|
|
|
}
|
|
|
|
|
|
|
|
return repays, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func toGlobalRepay(record binanceapi.MarginRepayRecord) types.MarginRepayRecord {
|
|
|
|
return types.MarginRepayRecord{
|
|
|
|
TransactionID: record.TxId,
|
|
|
|
Asset: record.Asset,
|
|
|
|
Principle: record.Principal,
|
|
|
|
Time: types.Time(record.Timestamp),
|
|
|
|
IsolatedSymbol: record.IsolatedSymbol,
|
|
|
|
}
|
2022-05-28 17:42:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|