Skip to content

Commit

Permalink
Exchanges: Rename StoreAssetPairStore
Browse files Browse the repository at this point in the history
Previously we were calling it "Format", but accepting everything from
the PairStore.
We were also defaulting to turning the Asset on.

Now callers need to get their AssetEnabled set as they want it, so
there's no magic

This change also moves responsibility for error wrapping outside to the
caller.
  • Loading branch information
gbjk committed Nov 28, 2024
1 parent 230a04b commit 0bffb67
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 284 deletions.
6 changes: 4 additions & 2 deletions docs/ADD_NEW_EXCHANGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ Similar to the configs, spot support is inbuilt but other asset types will need

```go
spot := currency.PairStore{
AssetEnabled: true,
RequestFormat: &currency.PairFormat{
Uppercase: true,
Delimiter: "/",
Expand All @@ -166,6 +167,7 @@ Similar to the configs, spot support is inbuilt but other asset types will need
},
}
futures := currency.PairStore{
AssetEnabled: true,
RequestFormat: &currency.PairFormat{
Uppercase: true,
Delimiter: "-",
Expand All @@ -178,12 +180,12 @@ Similar to the configs, spot support is inbuilt but other asset types will need

err := f.StoreAssetPairFormat(asset.Spot, spot)
if err != nil {
log.Errorln(log.ExchangeSys, err)
log.Errorf(log.ExchangeSys, "%s error storing `spot` default asset formats: %s", bi.Name, err)
}

err = f.StoreAssetPairFormat(asset.Futures, futures)
if err != nil {
log.Errorln(log.ExchangeSys, err)
log.Errorf(log.ExchangeSys, "%s error storing `futures` default asset formats: %s", bi.Name, err)
}
```

Expand Down
4 changes: 2 additions & 2 deletions engine/rpcserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1548,6 +1548,7 @@ func TestCheckVars(t *testing.T) {

for _, a := range []asset.Item{asset.Spot, asset.Margin, asset.CoinMarginedFutures, asset.USDTMarginedFutures} {
fmt := currency.PairStore{
AssetEnabled: true,
RequestFormat: &currency.PairFormat{Uppercase: true},
ConfigFormat: &currency.PairFormat{Delimiter: currency.DashDelimiter, Uppercase: true},
}
Expand All @@ -1558,8 +1559,7 @@ func TestCheckVars(t *testing.T) {
case asset.USDTMarginedFutures:
fmt.ConfigFormat = &currency.PairFormat{Uppercase: true, Delimiter: currency.DashDelimiter}
}
require.NoError(t, b.StoreAssetPairFormat(a, fmt), "StoreAssetPairFormat must not error")
require.NoError(t, b.CurrencyPairs.SetAssetEnabled(a, true), "SetAssetEnabled must not error")
require.NoError(t, b.StoreAssetPairStore(a, fmt), "StoreAssetPairFormat must not error")
}

err = checkParams("Binance", e, asset.Spot, currency.NewPair(currency.BTC, currency.USDT))
Expand Down
86 changes: 34 additions & 52 deletions exchanges/binance/binance_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,29 @@ import (
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
)

var defaultAssetPairStores = map[asset.Item]currency.PairStore{
asset.Spot: {
AssetEnabled: true,
RequestFormat: &currency.PairFormat{Uppercase: true},
ConfigFormat: &currency.PairFormat{Delimiter: currency.DashDelimiter, Uppercase: true},
},
asset.Margin: {
AssetEnabled: true,
RequestFormat: &currency.PairFormat{Uppercase: true},
ConfigFormat: &currency.PairFormat{Delimiter: currency.DashDelimiter, Uppercase: true},
},
asset.CoinMarginedFutures: {
AssetEnabled: true,
RequestFormat: &currency.PairFormat{Uppercase: true, Delimiter: currency.UnderscoreDelimiter},
ConfigFormat: &currency.PairFormat{Uppercase: true, Delimiter: currency.UnderscoreDelimiter},
},
asset.USDTMarginedFutures: {
AssetEnabled: true,
RequestFormat: &currency.PairFormat{Uppercase: true},
ConfigFormat: &currency.PairFormat{Uppercase: true, Delimiter: currency.UnderscoreDelimiter},
},
}

// SetDefaults sets the basic defaults for Binance
func (b *Binance) SetDefaults() {
b.Name = "Binance"
Expand All @@ -45,60 +68,18 @@ func (b *Binance) SetDefaults() {
b.API.CredentialsValidator.RequiresSecret = true
b.SetValues()

fmt1 := currency.PairStore{
RequestFormat: &currency.PairFormat{Uppercase: true},
ConfigFormat: &currency.PairFormat{
Delimiter: currency.DashDelimiter,
Uppercase: true,
},
}
coinFutures := currency.PairStore{
RequestFormat: &currency.PairFormat{
Uppercase: true,
Delimiter: currency.UnderscoreDelimiter,
},
ConfigFormat: &currency.PairFormat{
Uppercase: true,
Delimiter: currency.UnderscoreDelimiter,
},
}
usdtFutures := currency.PairStore{
RequestFormat: &currency.PairFormat{
Uppercase: true,
},
ConfigFormat: &currency.PairFormat{
Uppercase: true,
Delimiter: currency.UnderscoreDelimiter,
},
}
err := b.StoreAssetPairFormat(asset.Spot, fmt1)
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
err = b.StoreAssetPairFormat(asset.Margin, fmt1)
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
err = b.DisableAssetWebsocketSupport(asset.Margin)
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
err = b.StoreAssetPairFormat(asset.CoinMarginedFutures, coinFutures)
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
err = b.DisableAssetWebsocketSupport(asset.CoinMarginedFutures)
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
err = b.StoreAssetPairFormat(asset.USDTMarginedFutures, usdtFutures)
if err != nil {
log.Errorln(log.ExchangeSys, err)
for a, ps := range defaultAssetPairStores {
if err := b.StoreAssetPairStore(a, ps); err != nil {
log.Errorf(log.ExchangeSys, "%s error storing `%s` default asset formats: %s", b.Name, a, err)
}
}
err = b.DisableAssetWebsocketSupport(asset.USDTMarginedFutures)
if err != nil {
log.Errorln(log.ExchangeSys, err)

for _, a := range []asset.Item{asset.Margin, asset.CoinMarginedFutures, asset.USDTMarginedFutures} {
if err := b.DisableAssetWebsocketSupport(a); err != nil {
log.Errorf(log.ExchangeSys, "%s error disabling `%s` asset type websocket support: %s", b.Name, a, err)
}
}

b.Features = exchange.Features{
Supports: exchange.FeaturesSupported{
REST: true,
Expand Down Expand Up @@ -196,6 +177,7 @@ func (b *Binance) SetDefaults() {
},
}

var err error
b.Requester, err = request.New(b.Name,
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout),
request.WithLimiter(GetRateLimits()))
Expand Down
8 changes: 5 additions & 3 deletions exchanges/binanceus/binanceus_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ func (bi *Binanceus) SetDefaults() {
bi.SetValues()

fmt1 := currency.PairStore{
AssetEnabled: true,
RequestFormat: &currency.PairFormat{Uppercase: true},
ConfigFormat: &currency.PairFormat{
Delimiter: currency.DashDelimiter,
Uppercase: true,
},
}
err := bi.StoreAssetPairFormat(asset.Spot, fmt1)
if err != nil {
log.Errorln(log.ExchangeSys, err)
if err := bi.StoreAssetPairStore(asset.Spot, fmt1); err != nil {
log.Errorf(log.ExchangeSys, "%s error storing `spot` default asset formats: %s", bi.Name, err)
}

bi.Features = exchange.Features{
Expand Down Expand Up @@ -122,6 +122,8 @@ func (bi *Binanceus) SetDefaults() {
},
},
}

var err error
bi.Requester, err = request.New(bi.Name,
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout),
request.WithLimiter(GetRateLimit()))
Expand Down
39 changes: 15 additions & 24 deletions exchanges/bitfinex/bitfinex_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,23 @@ func (b *Bitfinex) SetDefaults() {
b.API.CredentialsValidator.RequiresKey = true
b.API.CredentialsValidator.RequiresSecret = true

fmt1 := currency.PairStore{
RequestFormat: &currency.PairFormat{Uppercase: true},
ConfigFormat: &currency.PairFormat{Uppercase: true, Delimiter: currency.DashDelimiter},
}

fmt2 := currency.PairStore{
RequestFormat: &currency.PairFormat{Uppercase: true},
ConfigFormat: &currency.PairFormat{Uppercase: true, Delimiter: ":"},
}

err := b.StoreAssetPairFormat(asset.Spot, fmt1)
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
err = b.StoreAssetPairFormat(asset.Margin, fmt2)
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
err = b.StoreAssetPairFormat(asset.MarginFunding, fmt1)
if err != nil {
log.Errorln(log.ExchangeSys, err)
for _, a := range []asset.Item{asset.Spot, asset.Margin, asset.MarginFunding} {
ps := currency.PairStore{
AssetEnabled: true,
RequestFormat: &currency.PairFormat{Uppercase: true},
ConfigFormat: &currency.PairFormat{Uppercase: true, Delimiter: currency.DashDelimiter},
}
if a == asset.Margin {
ps.ConfigFormat.Delimiter = ":"
}
if err := b.StoreAssetPairStore(a, ps); err != nil {
log.Errorf(log.ExchangeSys, "%s error storing `%s` default asset formats: %s", b.Name, a, err)
}
}

// Margin WS Currently not fully implemented and causes subscription collisions with spot
err = b.DisableAssetWebsocketSupport(asset.Margin)
if err != nil {
log.Errorln(log.ExchangeSys, err)
if err := b.DisableAssetWebsocketSupport(asset.Margin); err != nil {
log.Errorf(log.ExchangeSys, "%s error disabling `%s` asset type websocket support: %s", b.Name, asset.Margin, err)
}

// TODO: Implement Futures and Securities asset types.
Expand Down Expand Up @@ -162,6 +152,7 @@ func (b *Bitfinex) SetDefaults() {
Subscriptions: defaultSubscriptions.Clone(),
}

var err error
b.Requester, err = request.New(b.Name,
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout),
request.WithLimiter(GetRateLimit()))
Expand Down
44 changes: 15 additions & 29 deletions exchanges/bitmex/bitmex_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,22 @@ func (b *Bitmex) SetDefaults() {
b.API.CredentialsValidator.RequiresKey = true
b.API.CredentialsValidator.RequiresSecret = true

configFmt := &currency.PairFormat{Uppercase: true, Delimiter: currency.DashDelimiter}
standardRequestFmt := &currency.PairFormat{Uppercase: true}
spotRequestFormat := &currency.PairFormat{Uppercase: true, Delimiter: currency.UnderscoreDelimiter}

spot := currency.PairStore{RequestFormat: spotRequestFormat, ConfigFormat: configFmt}
err := b.StoreAssetPairFormat(asset.Spot, spot)
if err != nil {
log.Errorln(log.ExchangeSys, err)
}

perp := currency.PairStore{RequestFormat: standardRequestFmt, ConfigFormat: configFmt}
err = b.StoreAssetPairFormat(asset.PerpetualContract, perp)
if err != nil {
log.Errorln(log.ExchangeSys, err)
}

futures := currency.PairStore{RequestFormat: standardRequestFmt, ConfigFormat: configFmt}
err = b.StoreAssetPairFormat(asset.Futures, futures)
if err != nil {
log.Errorln(log.ExchangeSys, err)
}

index := currency.PairStore{RequestFormat: standardRequestFmt, ConfigFormat: configFmt}
err = b.StoreAssetPairFormat(asset.Index, index)
if err != nil {
log.Errorln(log.ExchangeSys, err)
for _, a := range []asset.Item{asset.Spot, asset.PerpetualContract, asset.Futures, asset.Index} {
ps := currency.PairStore{
AssetEnabled: true,
RequestFormat: &currency.PairFormat{Uppercase: true},
ConfigFormat: &currency.PairFormat{Uppercase: true, Delimiter: currency.DashDelimiter},
}
if a == asset.Spot {
ps.RequestFormat = &currency.PairFormat{Uppercase: true, Delimiter: currency.UnderscoreDelimiter}
}
if err := b.StoreAssetPairStore(a, ps); err != nil {
log.Errorf(log.ExchangeSys, "%s error storing `%s` default asset formats: %s", b.Name, a, err)
}
}

err = b.DisableAssetWebsocketSupport(asset.Index)
if err != nil {
log.Errorln(log.ExchangeSys, err)
if err := b.DisableAssetWebsocketSupport(asset.Index); err != nil {
log.Errorf(log.ExchangeSys, "%s error disabling `%s` asset type websocket support: %s", b.Name, asset.Index, err)
}

b.Features = exchange.Features{
Expand Down Expand Up @@ -139,6 +124,7 @@ func (b *Bitmex) SetDefaults() {
Subscriptions: defaultSubscriptions.Clone(),
}

var err error
b.Requester, err = request.New(b.Name,
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout),
request.WithLimiter(GetRateLimit()))
Expand Down
40 changes: 13 additions & 27 deletions exchanges/btse/btse_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,18 @@ func (b *BTSE) SetDefaults() {
b.API.CredentialsValidator.RequiresKey = true
b.API.CredentialsValidator.RequiresSecret = true

fmt1 := currency.PairStore{
RequestFormat: &currency.PairFormat{
Uppercase: true,
Delimiter: currency.DashDelimiter,
},
ConfigFormat: &currency.PairFormat{
Uppercase: true,
Delimiter: currency.DashDelimiter,
},
}
err := b.StoreAssetPairFormat(asset.Spot, fmt1)
if err != nil {
log.Errorln(log.ExchangeSys, err)
}

fmt2 := currency.PairStore{
RequestFormat: &currency.PairFormat{
Uppercase: true,
},
ConfigFormat: &currency.PairFormat{
Uppercase: true,
Delimiter: currency.DashDelimiter,
},
}
err = b.StoreAssetPairFormat(asset.Futures, fmt2)
if err != nil {
log.Errorln(log.ExchangeSys, err)
for _, a := range []asset.Item{asset.Spot, asset.Futures} {
ps := currency.PairStore{
AssetEnabled: true,
RequestFormat: &currency.PairFormat{Uppercase: true},
ConfigFormat: &currency.PairFormat{Uppercase: true, Delimiter: currency.DashDelimiter},
}
if a == asset.Spot {
ps.RequestFormat.Delimiter = currency.DashDelimiter
}
if err := b.StoreAssetPairStore(a, ps); err != nil {
log.Errorf(log.ExchangeSys, "%s error storing `%s` default asset formats: %s", b.Name, a, err)
}
}

b.Features = exchange.Features{
Expand Down Expand Up @@ -142,6 +127,7 @@ func (b *BTSE) SetDefaults() {
},
}

var err error
b.Requester, err = request.New(b.Name,
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout),
request.WithLimiter(GetRateLimit()))
Expand Down
8 changes: 4 additions & 4 deletions exchanges/bybit/bybit_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ func (by *Bybit) SetDefaults() {
by.API.CredentialsValidator.RequiresSecret = true

for _, n := range assetPairFmts {
ps := currency.PairStore{RequestFormat: n.reqFmt, ConfigFormat: n.cfgFmt}
if err := by.StoreAssetPairFormat(n.asset, ps); err != nil {
log.Errorf(log.ExchangeSys, "%v %v", n.asset, err)
ps := currency.PairStore{AssetEnabled: true, RequestFormat: n.reqFmt, ConfigFormat: n.cfgFmt}
if err := by.StoreAssetPairStore(n.asset, ps); err != nil {
log.Errorf(log.ExchangeSys, "%s error storing `%s` default asset formats: %s", by.Name, n.asset, err)
}
}

for _, a := range []asset.Item{asset.CoinMarginedFutures, asset.USDTMarginedFutures, asset.USDCMarginedFutures, asset.Options} {
if err := by.DisableAssetWebsocketSupport(a); err != nil {
log.Errorln(log.ExchangeSys, err)
log.Errorf(log.ExchangeSys, "%s error disabling `%s` asset type websocket support: %s", b.Name, a, err)

Check failure on line 72 in exchanges/bybit/bybit_wrapper.go

View workflow job for this annotation

GitHub Actions / lint

undefined: b) (typecheck)

Check failure on line 72 in exchanges/bybit/bybit_wrapper.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (ubuntu-latest, amd64, true, false)

undefined: b

Check failure on line 72 in exchanges/bybit/bybit_wrapper.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (ubuntu-latest, 386, true, true)

undefined: b

Check failure on line 72 in exchanges/bybit/bybit_wrapper.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (macos-latest, amd64, true, true)

undefined: b

Check failure on line 72 in exchanges/bybit/bybit_wrapper.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (macos-13, amd64, true, true)

undefined: b

Check failure on line 72 in exchanges/bybit/bybit_wrapper.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (windows-latest, amd64, true, true)

undefined: b
}
}

Expand Down
Loading

0 comments on commit 0bffb67

Please sign in to comment.