Skip to content

Commit

Permalink
Config: Add Version2 to rename GDAX
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Nov 10, 2024
1 parent d858758 commit c12ef71
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
3 changes: 0 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -842,9 +842,6 @@ func (c *Config) CheckExchangeConfigValues() error {
exchanges := 0
for i := range c.Exchanges {
e := &c.Exchanges[i]
if strings.EqualFold(e.Name, "GDAX") {
e.Name = "CoinbasePro"
}

// Check to see if the old API storage format is used
if e.APIKey != nil {
Expand Down
2 changes: 2 additions & 0 deletions config/versions/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package versions
import (
v0 "github.com/thrasher-corp/gocryptotrader/config/versions/v0"
v1 "github.com/thrasher-corp/gocryptotrader/config/versions/v1"
v2 "github.com/thrasher-corp/gocryptotrader/config/versions/v2"
)

func init() {
Manager.registerVersion(&v0.Version{})
Manager.registerVersion(&v1.Version{})
Manager.registerVersion(&v2.Version{})
}
36 changes: 36 additions & 0 deletions config/versions/v2/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// v2 is an ExchangeVersion to change the name of GDAX to CoinbasePro
package v2

import (
"context"

"github.com/buger/jsonparser"
)

// Version implements ExchangeVersion
type Version struct{}

const (
from = "GDAX"
to = "CoinbasePro"
)

// Exchanges returns just GDAX and CoinbasePro
func (v *Version) Exchanges() []string { return []string{from, to} }

// UpgradeExchange will change the exchange name from GDAX to CoinbasePro
func (v *Version) UpgradeExchange(_ context.Context, e []byte) ([]byte, error) {
if n, err := jsonparser.GetString(e, "name"); err == nil && n == from {
return jsonparser.Set(e, []byte(`"`+to+`"`), "name")
}
return e, nil

}

// DowngradeExchange will change the exchange name from CoinbasePro to GDAX
func (v *Version) DowngradeExchange(_ context.Context, e []byte) ([]byte, error) {
if n, err := jsonparser.GetString(e, "name"); err == nil && n == to {
return jsonparser.Set(e, []byte(`"`+from+`"`), "name")
}
return e, nil
}
37 changes: 37 additions & 0 deletions config/versions/v2/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package v2

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestUpgrade(t *testing.T) {
t.Parallel()
for _, tt := range [][]string{
{"GDAX", "CoinbasePro"},
{"Kraken", "Kraken"},
{"CoinbasePro", "CoinbasePro"},
} {
out, err := new(Version).UpgradeExchange(context.Background(), []byte(`{"name":"`+tt[0]+`"}`))
require.NoError(t, err)
require.NotEmpty(t, out)
assert.Equalf(t, `{"name":"`+tt[1]+`"}`, string(out), "Test exchange name %s", tt[0])
}
}

func TestDowngrade(t *testing.T) {
t.Parallel()
for _, tt := range [][]string{
{"GDAX", "GDAX"},
{"Kraken", "Kraken"},
{"CoinbasePro", "GDAX"},
} {
out, err := new(Version).DowngradeExchange(context.Background(), []byte(`{"name":"`+tt[0]+`"}`))
require.NoError(t, err)
require.NotEmpty(t, out)
assert.Equalf(t, `{"name":"`+tt[1]+`"}`, string(out), "Test exchange name %s", tt[0])
}
}

0 comments on commit c12ef71

Please sign in to comment.