Skip to content

Commit

Permalink
Another Bitget commit deployed expertly
Browse files Browse the repository at this point in the history
  • Loading branch information
cranktakular committed Aug 7, 2024
1 parent 4d8657b commit ded7a3c
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 36 deletions.
12 changes: 8 additions & 4 deletions exchanges/bitget/bitget.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"net/url"
"strconv"
"strings"
"time"

"github.com/thrasher-corp/gocryptotrader/common"
Expand Down Expand Up @@ -271,6 +272,7 @@ var (
errUnknownPairQuote = errors.New("unknown pair quote; pair can't be split due to lack of delimiter and unclear base length")
errStrategyMutex = errors.New("only one of immediate or cancel, fill or kill, and post only can be set to true")
errOrderNotFound = errors.New("order not found")
errReturnEmpty = errors.New("returned data unexpectedly empty")

prodTypes = []string{"USDT-FUTURES", "COIN-FUTURES", "USDC-FUTURES"}
planTypes = []string{"normal_plan", "track_plan", "profit_loss"}
Expand Down Expand Up @@ -1999,9 +2001,6 @@ func (bi *Bitget) GetFundingCurrent(ctx context.Context, pair, productType strin

// GetContractConfig returns details for a given contract
func (bi *Bitget) GetContractConfig(ctx context.Context, pair, productType string) (*ContractConfigResp, error) {
if pair == "" {
return nil, errPairEmpty
}
if productType == "" {
return nil, errProductTypeEmpty
}
Expand Down Expand Up @@ -4494,8 +4493,13 @@ func (bi *Bitget) SendAuthenticatedHTTPRequest(ctx context.Context, ep exchange.
return nil, err
}
}
// $ gets escaped in URLs, but the exchange reverses this before checking the signature; if we don't
// reverse it ourselves, they'll consider it invalid. This technically applies to other escape characters
// too, but $ is one we need to worry about, since it's included in some currencies supported by the
// exchange
unescapedPath := strings.ReplaceAll(path, "%24", "$")
t := strconv.FormatInt(time.Now().UnixMilli(), 10)
message := t + method + "/api/v2/" + path + string(payload)
message := t + method + "/api/v2/" + unescapedPath + string(payload)
// The exchange also supports user-generated RSA keys, but we haven't implemented that yet
var hmac []byte
hmac, err = crypto.GetHMAC(crypto.HashSHA256, []byte(message), []byte(creds.Secret))
Expand Down
70 changes: 53 additions & 17 deletions exchanges/bitget/bitget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/thrasher-corp/gocryptotrader/currency"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/fundingrate"
"github.com/thrasher-corp/gocryptotrader/exchanges/futures"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/sharedtestvalues"
Expand Down Expand Up @@ -1061,7 +1063,10 @@ func TestGetFundingCurrent(t *testing.T) {

func TestGetContractConfig(t *testing.T) {
t.Parallel()
testGetTwoArgs(t, bi.GetContractConfig)
_, err := bi.GetContractConfig(context.Background(), "", "")
assert.ErrorIs(t, err, errProductTypeEmpty)
_, err = bi.GetContractConfig(context.Background(), "", prodTypes[0])
assert.NoError(t, err)
}

func TestGetOneFuturesAccount(t *testing.T) {
Expand Down Expand Up @@ -2657,7 +2662,6 @@ func TestGetActiveOrders(t *testing.T) {
assert.NoError(t, err)
req.AssetType = asset.Spot
_, err = bi.GetActiveOrders(context.Background(), req)
// This is failing since the String() method on these novel pairs returns them with a delimiter for some reason
assert.NoError(t, err)
req.Pairs = []currency.Pair{testPair}
_, err = bi.GetActiveOrders(context.Background(), req)
Expand Down Expand Up @@ -2727,28 +2731,59 @@ func TestGetHistoricCandles(t *testing.T) {
t.Parallel()
_, err := bi.GetHistoricCandles(context.Background(), currency.Pair{}, asset.Spot, kline.Raw, time.Time{}, time.Time{})
assert.ErrorIs(t, err, currency.ErrCurrencyPairEmpty)
_, err = bi.GetHistoricCandles(context.Background(), testPair, asset.Spot, kline.OneDay, time.Time{}, time.Time{})
_, err = bi.GetHistoricCandles(context.Background(), testPair, asset.Spot, kline.OneDay,
time.Now().Add(-time.Hour*24*20), time.Now())
assert.NoError(t, err)
_, err = bi.GetHistoricCandles(context.Background(), testPair, asset.Futures, kline.OneDay, time.Time{}, time.Time{})
_, err = bi.GetHistoricCandles(context.Background(), testPair, asset.Futures, kline.OneDay,
time.Now().Add(-time.Hour*24*20), time.Now())
assert.NoError(t, err)

// _, err = bi.GetHistoricCandles(context.Background(), testPair, asset.Binary, kline.OneMin, time.Now().Add(-time.Hour),
// time.Now())
// assert.ErrorIs(t, err, asset.ErrNotSupported)
// _, err = bi.GetHistoricCandles(context.Background(), testPair, asset.Spot, kline.OneMin, time.Now().Add(-time.Hour),
// time.Now())
// assert.NoError(t, err)
// _, err = bi.GetHistoricCandles(context.Background(), testPair, asset.Futures, kline.OneMin, time.Now().Add(-time.Hour),
// time.Now())
// assert.NoError(t, err)
}

func TestGetHistoricCandlesExtended(t *testing.T) {
t.Parallel()
_, err := bi.GetHistoricCandlesExtended(context.Background(), currency.Pair{}, asset.Spot, kline.Raw, time.Time{},
time.Time{})
assert.ErrorIs(t, err, currency.ErrCurrencyPairEmpty)
// Rest of this is being put on ice until the issue with the previous test has been figured out
_, err = bi.GetHistoricCandlesExtended(context.Background(), testPair, asset.Spot, kline.OneDay,
time.Now().Add(-time.Hour*24*20), time.Now())
assert.NoError(t, err)
_, err = bi.GetHistoricCandlesExtended(context.Background(), testPair, asset.Futures, kline.OneDay,
time.Now().Add(-time.Hour*24*20), time.Now())
assert.NoError(t, err)
}

func TestGetFuturesContractDetails(t *testing.T) {
t.Parallel()
testGetOneArg(t, bi.GetFuturesContractDetails, asset.Empty, asset.Empty, nil, false, false, true)
}

func TestGetLatestFundingRates(t *testing.T) {
t.Parallel()
req1 := new(fundingrate.LatestRateRequest)
req1.Pair = currency.Pair{}
req2 := new(fundingrate.LatestRateRequest)
req2.Pair = testPair
testGetOneArg(t, bi.GetLatestFundingRates, req1, req2, errPairEmpty, false, false, true)
}

func TestUpdateOrderExecutionLimits(t *testing.T) {
t.Parallel()
err := bi.UpdateOrderExecutionLimits(context.Background(), asset.Empty)
assert.ErrorIs(t, err, asset.ErrNotSupported)
err = bi.UpdateOrderExecutionLimits(context.Background(), asset.Spot)
assert.NoError(t, err)
err = bi.UpdateOrderExecutionLimits(context.Background(), asset.Futures)
assert.NoError(t, err)
err = bi.UpdateOrderExecutionLimits(context.Background(), asset.Margin)
assert.NoError(t, err)
}

func TestGetAvailableTransferChains(t *testing.T) {
t.Parallel()
testGetOneArg(t, bi.GetAvailableTransferChains, currency.EMPTYCODE, testCrypto, errCurrencyEmpty, false, false, true)
_, err := bi.GetAvailableTransferChains(context.Background(), currency.NewCode("fakecurrencynotrealmeowmeow"))
assert.NoError(t, err)
// See if there's an established fake currency you can use instead of reinventing this one
}

// The following 3 tests aren't parallel due to collisions with each other, and some other plan order-related tests
Expand Down Expand Up @@ -3126,11 +3161,12 @@ type getOneArgResp interface {
*SubOrderResp | *BatchOrderResp | *BoolData | *FutureTickerResp | *AllAccResp | *SubaccountFuturesResp |
*CrossAssetResp | *MaxBorrowCross | *MaxTransferCross | *IntRateMaxBorrowCross | *TierConfigCross |
*FlashRepayCross | *IsoAssetResp | *IntRateMaxBorrowIso | *MaxBorrowIso | *MaxTransferIso | *FlashRepayIso |
*EarnAssets | *LoanCurList | currency.Pairs | time.Time
*EarnAssets | *LoanCurList | currency.Pairs | time.Time | []futures.Contract | []fundingrate.LatestRateResponse |
[]string
}

type getOneArgParam interface {
string | []string | bool | asset.Item
string | []string | bool | asset.Item | *fundingrate.LatestRateRequest | currency.Code
}

type getOneArgGen[R getOneArgResp, P getOneArgParam] func(context.Context, P) (R, error)
Expand Down
10 changes: 5 additions & 5 deletions exchanges/bitget/bitget_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1186,11 +1186,11 @@ type ContractConfigResp struct {
SymbolStatus string `json:"symbolStatus"`
OffTime int64 `json:"offTime,string"`
LimitOpenTime int64 `json:"limitOpenTime,string"`
DeliveryTime EmptyInt `json:"deliveryTime"`
DeliveryStartTime EmptyInt `json:"deliveryStartTime"`
DeliveryPeriod EmptyInt `json:"deliveryPeriod"`
LaunchTime EmptyInt `json:"launchTime"`
FundInterval uint16 `json:"fundInterval,string"`
DeliveryTime UnixTimestamp `json:"deliveryTime"`
DeliveryStartTime UnixTimestamp `json:"deliveryStartTime"`
DeliveryPeriod string `json:"deliveryPeriod"`
LaunchTime UnixTimestamp `json:"launchTime"`
FundInterval EmptyInt `json:"fundInterval"`
MinLever float64 `json:"minLever,string"`
MaxLever float64 `json:"maxLever,string"`
PosLimit float64 `json:"posLimit,string"`
Expand Down
Loading

0 comments on commit ded7a3c

Please sign in to comment.