Skip to content

Commit

Permalink
Bybit: Fix UpdateAccountInfo and enhance GetWalletBalance
Browse files Browse the repository at this point in the history
  • Loading branch information
thrasher- committed Jul 24, 2024
1 parent 425ec0c commit f271531
Show file tree
Hide file tree
Showing 8 changed files with 308 additions and 214 deletions.
115 changes: 40 additions & 75 deletions exchanges/bybit/bybit_live_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,95 +10,60 @@ import (
"os"
"testing"

"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/sharedtestvalues"
gctlog "github.com/thrasher-corp/gocryptotrader/log"
testexch "github.com/thrasher-corp/gocryptotrader/internal/testing/exchange"
)

var mockTests = false

func TestMain(m *testing.M) {
b.SetDefaults()
cfg := config.GetConfig()
err := cfg.LoadConfig("../../testdata/configtest.json", true)
if err != nil {
b = new(Bybit)
if err := testexch.Setup(b); err != nil {
log.Fatal(err)
}

exchCfg, err := cfg.GetExchangeConfig("Bybit")
if err != nil {
log.Fatal(err)
}
exchCfg.API.Credentials.Key = apiKey
exchCfg.API.Credentials.Secret = apiSecret
exchCfg.API.AuthenticatedSupport = true
exchCfg.API.AuthenticatedWebsocketSupport = true
b.Websocket = sharedtestvalues.NewTestWebsocket()
err = b.Setup(exchCfg)
if err != nil {
log.Fatal(err)
if apiKey != "" && apiSecret != "" {
b.API.AuthenticatedSupport = true
b.API.AuthenticatedWebsocketSupport = true
b.SetCredentials(apiKey, apiSecret, "", "", "", "")
b.Websocket.SetCanUseAuthenticatedEndpoints(true)
}
err = instantiateTradablePairs()
if err != nil {
log.Fatalf("%s %v", b.Name, err)
}
err = b.RetrieveAndSetAccountType(context.Background())
if err != nil {
gctlog.Errorf(gctlog.ExchangeSys, "RetrieveAndSetAccountType: %v", err)

if b.API.AuthenticatedSupport {
if err := b.RetrieveAndSetAccountType(context.Background()); err != nil {
log.Printf("%s unable to RetrieveAndSetAccountType: %v", b.Name, err)
}
}

instantiateTradablePairs()

os.Exit(m.Run())
}

func instantiateTradablePairs() error {
err := b.UpdateTradablePairs(context.Background(), true)
if err != nil {
return err
}
tradables, err := b.GetEnabledPairs(asset.Spot)
if err != nil {
return err
}
format, err := b.GetPairFormat(asset.Spot, true)
if err != nil {
return err
}
spotTradablePair = tradables[0].Format(format)
tradables, err = b.GetEnabledPairs(asset.USDTMarginedFutures)
if err != nil {
return err
}
format, err = b.GetPairFormat(asset.USDTMarginedFutures, true)
if err != nil {
return err
func instantiateTradablePairs() {
handleError := func(msg string, err error) {
if err != nil {
log.Fatalf("Bybit %s: %v", msg, err)
}
}
usdtMarginedTradablePair = tradables[0].Format(format)
tradables, err = b.GetEnabledPairs(asset.USDCMarginedFutures)
if err != nil {
return err
}
format, err = b.GetPairFormat(asset.USDCMarginedFutures, true)
if err != nil {
return err
}
usdcMarginedTradablePair = tradables[0].Format(format)
tradables, err = b.GetEnabledPairs(asset.CoinMarginedFutures)
if err != nil {
return err
}
format, err = b.GetPairFormat(asset.CoinMarginedFutures, true)
if err != nil {
return err
}
inverseTradablePair = tradables[0].Format(format)
tradables, err = b.GetEnabledPairs(asset.Options)
if err != nil {
return err
}
format, err = b.GetPairFormat(asset.Options, true)
if err != nil {
return err

err := b.UpdateTradablePairs(context.Background(), true)
handleError("unable to UpdateTradablePairs", err)

setTradablePair := func(assetType asset.Item, p *currency.Pair) {
tradables, err := b.GetEnabledPairs(assetType)
handleError("unable to GetEnabledPairs", err)

format, err := b.GetPairFormat(assetType, true)
handleError("unable to GetPairFormat", err)

*p = tradables[0].Format(format)
}
optionsTradablePair = tradables[0].Format(format)
return nil

setTradablePair(asset.Spot, &spotTradablePair)
setTradablePair(asset.USDTMarginedFutures, &usdtMarginedTradablePair)
setTradablePair(asset.USDCMarginedFutures, &usdcMarginedTradablePair)
setTradablePair(asset.CoinMarginedFutures, &inverseTradablePair)
setTradablePair(asset.Options, &optionsTradablePair)
}
93 changes: 24 additions & 69 deletions exchanges/bybit/bybit_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,95 +10,50 @@ import (
"os"
"testing"

"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/mock"
"github.com/thrasher-corp/gocryptotrader/exchanges/sharedtestvalues"
testexch "github.com/thrasher-corp/gocryptotrader/internal/testing/exchange"
)

const mockfile = "../../testdata/http_mock/bybit/bybit.json"

var mockTests = true

func TestMain(m *testing.M) {
b.SetDefaults()
cfg := config.GetConfig()
err := cfg.LoadConfig("../../testdata/configtest.json", true)
if err != nil {
b = new(Bybit)
if err := testexch.Setup(b); err != nil {
log.Fatal(err)
}

bybitConfig, err := cfg.GetExchangeConfig("Bybit")
if err != nil {
log.Fatal("Bybit Setup() init error", err)
}
b.SetCredentials("mock", "tester", "", "", "", "") // Hack for UpdateAccountInfo test

b.SkipAuthCheck = true
bybitConfig.API.Credentials.Key = apiKey
bybitConfig.API.Credentials.Secret = apiSecret
bybitConfig.API.AuthenticatedSupport = true
bybitConfig.API.AuthenticatedWebsocketSupport = true
b.Websocket = sharedtestvalues.NewTestWebsocket()
err = b.Setup(bybitConfig)
if err != nil {
log.Fatal("Bybit setup error", err)
if err := testexch.MockHTTPInstance(b); err != nil {
log.Fatal(err)
}

serverDetails, newClient, err := mock.NewVCRServer(mockfile)
if err != nil {
log.Fatalf("Mock server error %s", err)
if err := b.UpdateTradablePairs(context.Background(), true); err != nil {
log.Fatalf("Bybit unable to UpdateTradablePairs: %s", err)
}
err = b.SetHTTPClient(newClient)
if err != nil {
log.Fatalf("Mock server error %s", err)
}
endpointMap := b.API.Endpoints.GetURLMap()
for k := range endpointMap {
err = b.API.Endpoints.SetRunning(k, serverDetails)
if err != nil {
log.Fatal(err)

setEnabledPair := func(assetType asset.Item, pair currency.Pair) {
okay, err := b.IsPairEnabled(pair, assetType)
if !okay || err != nil {
err = b.CurrencyPairs.EnablePair(assetType, pair)
if err != nil {
log.Fatal(err)
}
}
}
err = b.UpdateTradablePairs(context.Background(), true)
if err != nil {
log.Fatal("Bybit setup error", err)
}

spotTradablePair = currency.Pair{Base: currency.BTC, Quote: currency.USDT}
okay, err := b.IsPairEnabled(spotTradablePair, asset.Spot)
if !okay || err != nil {
err = b.CurrencyPairs.EnablePair(asset.Spot, spotTradablePair)
if err != nil {
log.Fatal(err)
}
}
usdtMarginedTradablePair = currency.Pair{Base: currency.NewCode("10000LADYS"), Quote: currency.USDT}
if okay, err = b.IsPairEnabled(usdtMarginedTradablePair, asset.USDTMarginedFutures); !okay || err != nil {
err = b.CurrencyPairs.EnablePair(asset.USDTMarginedFutures, usdtMarginedTradablePair)
if err != nil {
log.Fatal(err)
}
}
usdcMarginedTradablePair = currency.Pair{Base: currency.ETH, Quote: currency.PERP}
if okay, err = b.IsPairEnabled(usdcMarginedTradablePair, asset.USDCMarginedFutures); !okay || err != nil {
err = b.CurrencyPairs.EnablePair(asset.USDCMarginedFutures, usdcMarginedTradablePair)
if err != nil {
log.Fatal(err)
}
}
inverseTradablePair = currency.Pair{Base: currency.ADA, Quote: currency.USD}
if okay, err = b.IsPairEnabled(inverseTradablePair, asset.CoinMarginedFutures); !okay || err != nil {
err = b.CurrencyPairs.EnablePair(asset.CoinMarginedFutures, inverseTradablePair)
if err != nil {
log.Fatal(err)
}
}
optionsTradablePair = currency.Pair{Base: currency.BTC, Delimiter: currency.DashDelimiter, Quote: currency.NewCode("29DEC23-80000-C")}
if okay, err = b.IsPairEnabled(optionsTradablePair, asset.Options); !okay || err != nil {
err = b.CurrencyPairs.EnablePair(asset.Options, optionsTradablePair)
if err != nil {
log.Fatal(err)
}
}

setEnabledPair(asset.Spot, spotTradablePair)
setEnabledPair(asset.USDTMarginedFutures, usdtMarginedTradablePair)
setEnabledPair(asset.USDCMarginedFutures, usdcMarginedTradablePair)
setEnabledPair(asset.CoinMarginedFutures, inverseTradablePair)
setEnabledPair(asset.Options, optionsTradablePair)

os.Exit(m.Run())
}
Loading

0 comments on commit f271531

Please sign in to comment.