-
Notifications
You must be signed in to change notification settings - Fork 822
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
shazbert
committed
Jan 13, 2025
1 parent
6bc0bb6
commit 789da79
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package versions | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"time" | ||
|
||
"github.com/buger/jsonparser" | ||
) | ||
|
||
// Version3 is an ExchangeVersion to remove the orderbook and publishPeriod from the exchange config | ||
type Version3 struct { | ||
} | ||
|
||
func init() { | ||
Manager.registerVersion(3, &Version3{}) | ||
} | ||
|
||
// // Exchanges returns all exchanges: "*" | ||
func (v *Version3) Exchanges() []string { return []string{"*"} } | ||
|
||
// UpgradeExchange will remove the orderbook and publishPeriod from the exchange config | ||
func (v *Version3) UpgradeExchange(_ context.Context, e []byte) ([]byte, error) { | ||
e = jsonparser.Delete(e, "orderbook", "publishPeriod") | ||
return e, nil | ||
} | ||
|
||
var defaultOrderbookPublishPeriod = time.Second * 10 | ||
|
||
// DowngradeExchange will downgrade the exchange config with the default orderbook publish period | ||
func (v *Version3) DowngradeExchange(_ context.Context, e []byte) ([]byte, error) { | ||
if _, _, _, err := jsonparser.Get(e, "orderbook"); err != nil { | ||
return e, nil | ||
} | ||
out, err := json.Marshal(defaultOrderbookPublishPeriod) | ||
if err != nil { | ||
return e, err | ||
} | ||
return jsonparser.Set(e, out, "orderbook", "publishPeriod") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package versions | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestVersion3UpgradeExchange(t *testing.T) { | ||
t.Parallel() | ||
|
||
got, err := (&Version3{}).UpgradeExchange(nil, nil) | ||
require.NoError(t, err) | ||
require.Nil(t, got) | ||
|
||
payload := []byte(`{"orderbook": {"verificationBypass": false,"websocketBufferLimit": 5,"websocketBufferEnabled": false,"publishPeriod": 10000000000}}`) | ||
expected := []byte(`{"orderbook": {"verificationBypass": false,"websocketBufferLimit": 5,"websocketBufferEnabled": false}}`) | ||
got, err = (&Version3{}).UpgradeExchange(nil, payload) | ||
require.NoError(t, err) | ||
require.Equal(t, got, expected) | ||
} | ||
|
||
func TestVersion3DowngradeExchange(t *testing.T) { | ||
t.Parallel() | ||
|
||
got, err := (&Version3{}).DowngradeExchange(nil, nil) | ||
require.NoError(t, err) | ||
require.Nil(t, got) | ||
|
||
payload := []byte(`{"orderbook": {"verificationBypass": false,"websocketBufferLimit": 5,"websocketBufferEnabled": false}}`) | ||
expected := []byte(`{"orderbook": {"verificationBypass": false,"websocketBufferLimit": 5,"websocketBufferEnabled": false,"publishPeriod":10000000000}}`) | ||
got, err = (&Version3{}).DowngradeExchange(nil, payload) | ||
require.NoError(t, err) | ||
require.Equal(t, got, expected) | ||
} | ||
|
||
func TestVersion3Exchanges(t *testing.T) { | ||
t.Parallel() | ||
assert := require.New(t) | ||
assert.Equal([]string{"*"}, (&Version3{}).Exchanges()) | ||
} |