Skip to content

Commit

Permalink
Post-merge improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
cranktakular committed Dec 3, 2024
1 parent ed60827 commit a993fb9
Show file tree
Hide file tree
Showing 5 changed files with 689 additions and 436 deletions.
113 changes: 109 additions & 4 deletions exchanges/bitget/bitget.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@ const (
bitgetReviseHistory = "/revise-history"
bitgetDebts = "/debts"
bitgetReduces = "/reduces"
bitgetInsLoan = "ins-loan/"
bitgetProductInfos = "product-infos"
bitgetEnsureCoinsConvert = "ensure-coins-convert"
bitgetLTVConvert = "ltv-convert"
bitgetTransferred = "transfered" // sic

Check failure on line 211 in exchanges/bitget/bitget.go

View workflow job for this annotation

GitHub Actions / Spell checker

transfered ==> transferred
bitgetLoanOrder = "loan-order"
bitgetRepaidHistory = "repaid-history"

// Websocket endpoints
// Unauthenticated
Expand Down Expand Up @@ -752,7 +759,7 @@ func (bi *Bitget) GetVirtualSubaccounts(ctx context.Context, limit, pagination i
var resp struct {
GetVirSubResp `json:"data"`
}
return &resp.GetVirSubResp, bi.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, Rate5, http.MethodGet, path, vals, nil, &resp)
return &resp.GetVirSubResp, bi.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, Rate2, http.MethodGet, path, vals, nil, &resp)
}

// CreateAPIKey creates an API key for the selected virtual sub-account
Expand Down Expand Up @@ -3784,7 +3791,7 @@ func (bi *Bitget) GetCrossLiquidationOrders(ctx context.Context, orderType, from
}

// GetIsolatedRepayHistory returns the repayment history for isolated margin
func (bi *Bitget) GetIsolatedRepayHistory(ctx context.Context, pair currency.Pair, currency string, repayID, limit, pagination int64, startTime, endTime time.Time) (*RepayHistResp, error) {
func (bi *Bitget) GetIsolatedRepayHistory(ctx context.Context, pair currency.Pair, currency currency.Code, repayID, limit, pagination int64, startTime, endTime time.Time) (*RepayHistResp, error) {
if pair.IsEmpty() {
return nil, errPairEmpty
}
Expand All @@ -3804,8 +3811,8 @@ func (bi *Bitget) GetIsolatedRepayHistory(ctx context.Context, pair currency.Pai
params.Values.Set("idLessThan", strconv.FormatInt(pagination, 10))
}
params.Values.Set("symbol", pair.String())
if currency != "" {
params.Values.Set("coin", currency)
if !currency.IsEmpty() {
params.Values.Set("coin", currency.String())
}
path := bitgetMargin + bitgetIsolated + bitgetRepayHistory
var resp struct {
Expand Down Expand Up @@ -4855,6 +4862,104 @@ func (bi *Bitget) GetLiquidationRecords(ctx context.Context, orderID, pagination
return resp.LiquidRecs, bi.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, Rate10, http.MethodGet, path, params.Values, nil, &resp)
}

// GetLoanInfo returns information on an offered institutional loan
func (bi *Bitget) GetLoanInfo(ctx context.Context, productID string) (*LoanInfo, error) {
if productID == "" {
return nil, errProductIDEmpty
}
vals := url.Values{}
vals.Set("productId", productID)
path := bitgetSpot + bitgetInsLoan + bitgetProductInfos
var resp struct {
LoanInfo `json:"data"`
}
return &resp.LoanInfo, bi.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, Rate5, http.MethodGet, path, vals, nil, &resp)
}

// GetMarginCoinRatio returns the conversion rate various margin coins have for a particular loan
func (bi *Bitget) GetMarginCoinRatio(ctx context.Context, productID string) (*MarginCoinRatio, error) {
if productID == "" {
return nil, errProductIDEmpty
}
vals := url.Values{}
vals.Set("productId", productID)
path := bitgetSpot + bitgetInsLoan + bitgetEnsureCoinsConvert
var resp struct {
MarginCoinRatio `json:"data"`
}
return &resp.MarginCoinRatio, bi.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, Rate5, http.MethodGet, path, vals, nil, &resp)
}

// GetSpotSymbols returns spot trading pairs that meet ======A CERTAIN CRITERIA CURRENTLY UNCLEAR TO ME======
func (bi *Bitget) GetSpotSymbols(ctx context.Context, productID string) (*SpotSymbols, error) {
if productID == "" {
return nil, errProductIDEmpty
}
vals := url.Values{}
vals.Set("productId", productID)
path := bitgetSpot + bitgetInsLoan + bitgetSymbols
var resp struct {
SpotSymbols `json:"data"`
}
return &resp.SpotSymbols, bi.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, Rate5, http.MethodGet, path, vals, nil, &resp)
}

// GetLoanToValue returns the loan to value ratio for all loans on the user's account
func (bi *Bitget) GetLoanToValue(ctx context.Context) (*LoanToValue, error) {
path := bitgetSpot + bitgetInsLoan + bitgetLTVConvert
var resp struct {
LoanToValue `json:"data"`
}
return &resp.LoanToValue, bi.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, Rate5, http.MethodGet, path, nil, nil, &resp)
}

// GetTransferableAmount returns the amount of a currency that can be transferred
func (bi *Bitget) GetTransferableAmount(ctx context.Context, accountID string, coin currency.Code) (*TransferableAmount, error) {
if coin.IsEmpty() {
return nil, errCurrencyEmpty
}
vals := url.Values{}
vals.Set("userId", accountID)
vals.Set("coin", coin.String())
path := bitgetSpot + bitgetInsLoan + bitgetTransferred
var resp struct {
TransferableAmount `json:"data"`
}
return &resp.TransferableAmount, bi.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, Rate5, http.MethodGet, path, vals, nil, &resp)
}

// GetLoanOrders returns a list of loan orders taken out on the user's account
func (bi *Bitget) GetLoanOrders(ctx context.Context, orderID string, startTime, endTime time.Time) ([]LoanOrders, error) {
var params Params
params.Values = make(url.Values)
params.Values.Set("orderId", orderID)
err := params.prepareDateString(startTime, endTime, true, true)
if err != nil {
return nil, err
}
path := bitgetSpot + bitgetInsLoan + bitgetLoanOrder
var resp struct {
LoanOrders []LoanOrders `json:"data"`
}
return resp.LoanOrders, bi.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, Rate5, http.MethodGet, path, params.Values, nil, &resp)
}

// GetRepaymentOrders returns a list of repayment orders taken out on the user's account
func (bi *Bitget) GetRepaymentOrders(ctx context.Context, orderID string, startTime, endTime time.Time) ([]RepaymentOrders, error) {
var params Params
params.Values = make(url.Values)
params.Values.Set("orderId", orderID)
err := params.prepareDateString(startTime, endTime, true, true)
if err != nil {
return nil, err
}
path := bitgetSpot + bitgetInsLoan + bitgetRepaidHistory
var resp struct {
RepaymentOrders []RepaymentOrders `json:"data"`
}
return resp.RepaymentOrders, bi.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, Rate5, http.MethodGet, path, params.Values, nil, &resp)
}

// SendAuthenticatedHTTPRequest sends an authenticated HTTP request
func (bi *Bitget) SendAuthenticatedHTTPRequest(ctx context.Context, ep exchange.URL, rateLim request.EndpointLimit, method, path string, queryParams url.Values, bodyParams map[string]any, result any) error {
creds, err := bi.GetCredentials(ctx)
Expand Down
116 changes: 89 additions & 27 deletions exchanges/bitget/bitget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net/http"
"net/url"
"os"
"slices"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -616,7 +615,7 @@ func TestBatchCancelAndPlaceSpotOrders(t *testing.T) {
if len(resp) == 0 {
t.Skip(skipInsufficientOrders)
}
newPair, err := currency.NewPairFromString(resp[0].Symbol)
newPair, err := pairFromStringHelper(resp[0].Symbol)
require.NoError(t, err)
req = append(req, ReplaceSpotOrderStruct{
OrderID: int64(resp[0].OrderID),
Expand Down Expand Up @@ -659,7 +658,7 @@ func TestBatchPlaceSpotOrders(t *testing.T) {
Strategy: "IOC",
Price: testPrice,
Size: testAmount,
Pair: testPair.String(),
Pair: testPair,
})
resp, err := bi.BatchPlaceSpotOrders(context.Background(), testPair, true, true, req)
require.NoError(t, err)
Expand All @@ -679,10 +678,12 @@ func TestBatchCancelOrders(t *testing.T) {
if len(resp) == 0 {
t.Skip(skipInsufficientOrders)
}
pair, err := pairFromStringHelper(resp[0].Symbol)
assert.NoError(t, err)
req = append(req, CancelSpotOrderStruct{
OrderID: int64(resp[0].OrderID),
ClientOrderID: resp[0].ClientOrderID,
Pair: resp[0].Symbol,
Pair: pair,
})
resp2, err := bi.BatchCancelOrders(context.Background(), testPair, true, req)
assert.NoError(t, err)
Expand Down Expand Up @@ -795,8 +796,7 @@ func TestBatchCancelSpotPlanOrders(t *testing.T) {
func TestGetAccountInfo(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, bi)
// Not chucked into testGetNoArgs due to checking the presence of resp.Data, refactoring that generic for that
// would waste too many lines to do so just for this
// Not chucked into testGetNoArgs due to checking the presence of resp.Data, refactoring that generic for that would waste too many lines to do so just for this
resp, err := bi.GetAccountInfo(context.Background())
require.NoError(t, err)
assert.NotEmpty(t, resp)
Expand Down Expand Up @@ -1787,17 +1787,17 @@ func TestGetCrossLiquidationOrders(t *testing.T) {

func TestGetIsolatedRepayHistory(t *testing.T) {
t.Parallel()
_, err := bi.GetIsolatedRepayHistory(context.Background(), currency.Pair{}, "", 0, 0, 0, time.Time{}, time.Time{})
_, err := bi.GetIsolatedRepayHistory(context.Background(), currency.Pair{}, currency.Code{}, 0, 0, 0, time.Time{}, time.Time{})
assert.ErrorIs(t, err, errPairEmpty)
_, err = bi.GetIsolatedRepayHistory(context.Background(), testPair, "", 0, 0, 0, time.Time{}, time.Time{})
_, err = bi.GetIsolatedRepayHistory(context.Background(), testPair, currency.Code{}, 0, 0, 0, time.Time{}, time.Time{})
assert.ErrorIs(t, err, common.ErrDateUnset)
sharedtestvalues.SkipTestIfCredentialsUnset(t, bi)
resp, err := bi.GetIsolatedRepayHistory(context.Background(), testPair, "", 0, 5, 1<<62, time.Now().Add(-time.Hour*24*85), time.Time{})
resp, err := bi.GetIsolatedRepayHistory(context.Background(), testPair, currency.Code{}, 0, 5, 1<<62, time.Now().Add(-time.Hour*24*85), time.Time{})
require.NoError(t, err)
if len(resp.ResultList) == 0 {
t.Skip(skipInsufficientOrders)
}
_, err = bi.GetIsolatedRepayHistory(context.Background(), testPair, "", resp.ResultList[0].RepayID, 5, 1<<62, time.Now().Add(-time.Hour*24*85), time.Time{})
_, err = bi.GetIsolatedRepayHistory(context.Background(), testPair, testFiat, resp.ResultList[0].RepayID, 5, 1<<62, time.Now().Add(-time.Hour*24*85), time.Time{})
assert.NoError(t, err)
}

Expand Down Expand Up @@ -2351,6 +2351,68 @@ func TestGetLiquidationRecords(t *testing.T) {
assert.NoError(t, err)
}

// Try to put these into those get one arg functions
func TestGetLoanInfo(t *testing.T) {
t.Parallel()
// bi.Verbose = true

Check failure on line 2357 in exchanges/bitget/bitget_test.go

View workflow job for this annotation

GitHub Actions / lint

commentedOutCode: may want to remove commented-out code (gocritic)
_, err := bi.GetLoanInfo(context.Background(), "")
assert.ErrorIs(t, err, errProductIDEmpty)
_, err = bi.GetLoanInfo(context.Background(), "1")
assert.NoError(t, err)
}

func TestGetMarginCoinRatio(t *testing.T) {
t.Parallel()
// bi.Verbose = true
_, err := bi.GetMarginCoinRatio(context.Background(), "")
assert.ErrorIs(t, err, errProductIDEmpty)
_, err = bi.GetMarginCoinRatio(context.Background(), "1")
assert.NoError(t, err)
}

func TestGetSpotSymbols(t *testing.T) {
t.Parallel()
// bi.Verbose = true
_, err := bi.GetSpotSymbols(context.Background(), "")
assert.ErrorIs(t, err, errProductIDEmpty)
_, err = bi.GetSpotSymbols(context.Background(), "1")
assert.NoError(t, err)
}

func TestGetLoanToValue(t *testing.T) {
t.Parallel()
// bi.Verbose = true
_, err := bi.GetLoanToValue(context.Background())
assert.NoError(t, err)
}

func TestGetTransferableAmount(t *testing.T) {
t.Parallel()
// bi.Verbose = true
_, err := bi.GetTransferableAmount(context.Background(), "", currency.Code{})
assert.ErrorIs(t, err, errCurrencyEmpty)
_, err = bi.GetTransferableAmount(context.Background(), "", testFiat)
assert.NoError(t, err)
}

func TestGetLoanOrders(t *testing.T) {
t.Parallel()
// bi.Verbose = true
_, err := bi.GetLoanOrders(context.Background(), "", time.Now().Add(time.Minute), time.Time{})
assert.ErrorIs(t, err, common.ErrStartAfterTimeNow)
_, err = bi.GetLoanOrders(context.Background(), "", time.Time{}, time.Time{})
assert.NoError(t, err)
}

func TestGetRepaymentOrders(t *testing.T) {
t.Parallel()
// bi.Verbose = true
_, err := bi.GetRepaymentOrders(context.Background(), "", time.Now().Add(time.Minute), time.Time{})
assert.ErrorIs(t, err, common.ErrStartAfterTimeNow)
_, err = bi.GetRepaymentOrders(context.Background(), "", time.Time{}, time.Time{})
assert.NoError(t, err)
}

func TestFetchTradablePairs(t *testing.T) {
t.Parallel()
testGetOneArg(t, bi.FetchTradablePairs, asset.Empty, asset.Spot, asset.ErrNotSupported, false, false, false)
Expand Down Expand Up @@ -3812,14 +3874,14 @@ func aBenchmarkHelper(a, pag int64) {
// 763 1819054 ns/op 14336 B/op 1 allocs/op
// 87 13602672 ns/op 0 B/op 0 allocs/op
func BenchmarkGen(b *testing.B) {
pairs, err := bi.GetSupportedCurrencies(context.Background())
if err != nil {
panic(err)
}
check, err := bi.GetSymbolInfo(context.Background(), currency.Pair{})
if err != nil {
panic(err)
}
// pairs, err := bi.GetSupportedCurrencies(context.Background())
// if err != nil {
// panic(err)
// }
// check, err := bi.GetSymbolInfo(context.Background(), currency.Pair{})
// if err != nil {
// panic(err)
// }
b.ResetTimer()
// for j := 0; j < b.N; j++ {
// checkSlice := make([]string, len(check))
Expand All @@ -3833,13 +3895,13 @@ func BenchmarkGen(b *testing.B) {
// }
// }

for j := 0; j < b.N; j++ {
for x := range pairs {
if !slices.ContainsFunc(check, func(s SymbolInfoResp) bool {
return s.Symbol == pairs[x].Symbol
}) {
continue
}
}
}
// for j := 0; j < b.N; j++ {
// for x := range pairs {
// if !slices.ContainsFunc(check, func(s SymbolInfoResp) bool {
// return s.Symbol == pairs[x].Symbol
// }) {
// continue
// }
// }
// }
}
Loading

0 comments on commit a993fb9

Please sign in to comment.