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 29, 2024
1 parent 9a5206a commit 73bccbf
Show file tree
Hide file tree
Showing 15 changed files with 181 additions and 290 deletions.
10 changes: 6 additions & 4 deletions cmd/exchange_template/wrapper_file.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,24 @@ func ({{.Variable}} *{{.CapitalName}}) SetDefaults() {
// can use this example below:

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

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

err = {{.Variable}}.StoreAssetPairFormat(asset.Spot, fmt1)
err = {{.Variable}}.StoreAssetPairStore(asset.Spot, fmt1)
if err != nil {
log.Errorln(log.ExchangeSys, err)
log.Errorf(log.ExchangeSys, "%s error storing `%s` default asset formats: %s", {{.Variable}}.Name, asset.Spot, err)
}
err = {{.Variable}}.StoreAssetPairFormat(asset.Margin, fmt2)
err = {{.Variable}}.StoreAssetPairStore(asset.Margin, fmt2)
if err != nil {
log.Errorln(log.ExchangeSys, err)
log.Errorf(log.ExchangeSys, "%s error storing `%s` default asset formats: %s", {{.Variable}}.Name, asset.Margin, err)
}

// Fill out the capabilities/features that the exchange supports
Expand Down
10 changes: 6 additions & 4 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 @@ -176,14 +178,14 @@ Similar to the configs, spot support is inbuilt but other asset types will need
},
}

err := f.StoreAssetPairFormat(asset.Spot, spot)
err := f.StoreAssetPairStore(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)
err = f.StoreAssetPairStore(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), "StoreAssetPairStore 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
Loading

0 comments on commit 73bccbf

Please sign in to comment.