exchange/order/limits: Migrate to new package and integrate with exchanges (#1860)

* move limits, transition to key gen

* rollout NewExchangePairAssetKey everywhere

* test improvements

* self-review fixes

* ok, lets go

* fix merge issue

* slower value func,assertify,drop IsValidPairString

* remove binance reference for backtesting test

* Redundant nil checks removed due to redundancy

* Update order_test.go

* Move limits back into /exchanges/

* puts limits in a different box again

* SHAZBERT SPECIAL SUGGESTIONS

* Update gateio_wrapper.go

* fixes all build issues

* Many niteroos!

* something has gone awry

* bugfix

* gk's everywhere nits

* lint

* extra lint

* re-remove IsValidPairString

* lint fix

* standardise test

* revert some bads

* dupe rm

* another revert 360 mcgee

* un-in-revertify

* Update exchange/order/limits/levels_test.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* fix

* Update exchanges/binance/binance_test.go

HERE'S HOPING GITHUB FORMATS THIS CORRECTLY!

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* update text

* rn func, same line err gk4202000

---------

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>
This commit is contained in:
Scott
2025-08-26 12:30:21 +10:00
committed by GitHub
parent fc0f262c42
commit 85403fe801
103 changed files with 1751 additions and 2168 deletions

View File

@@ -1,18 +1,48 @@
package key
import (
"strings"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
)
// ExchangePairAsset is a unique map key signature for exchange, currency pair and asset
type ExchangePairAsset struct {
// ExchangeAssetPair is a unique map key signature for exchange, currency pair and asset
type ExchangeAssetPair struct {
Exchange string
Asset asset.Item
Base *currency.Item
Quote *currency.Item
Asset asset.Item
}
// NewExchangeAssetPair is a helper function to expand a Pair into an ExchangeAssetPair
func NewExchangeAssetPair(exch string, a asset.Item, cp currency.Pair) ExchangeAssetPair {
return ExchangeAssetPair{
Exchange: exch,
Base: cp.Base.Item,
Quote: cp.Quote.Item,
Asset: a,
}
}
// Pair combines the base and quote into a pair
func (k ExchangeAssetPair) Pair() currency.Pair {
return currency.NewPair(k.Base.Currency(), k.Quote.Currency())
}
// MatchesExchangeAsset checks if the key matches the exchange and asset
func (k ExchangeAssetPair) MatchesExchangeAsset(exch string, item asset.Item) bool {
return k.Exchange == exch && k.Asset == item
}
// MatchesPairAsset checks if the key matches the pair and asset
func (k ExchangeAssetPair) MatchesPairAsset(pair currency.Pair, item asset.Item) bool {
return k.Base == pair.Base.Item &&
k.Quote == pair.Quote.Item &&
k.Asset == item
}
// MatchesExchange checks if the exchange matches
func (k ExchangeAssetPair) MatchesExchange(exch string) bool {
return k.Exchange == exch
}
// ExchangeAsset is a unique map key signature for exchange and asset
@@ -28,50 +58,13 @@ type PairAsset struct {
Asset asset.Item
}
// Pair combines the base and quote into a pair
func (k PairAsset) Pair() currency.Pair {
return currency.NewPair(k.Base.Currency(), k.Quote.Currency())
}
// SubAccountAsset is a unique map key signature for subaccount and asset
type SubAccountAsset struct {
SubAccount string
Asset asset.Item
}
// Pair combines the base and quote into a pair
func (k *PairAsset) Pair() currency.Pair {
if k == nil || (k.Base == nil && k.Quote == nil) {
return currency.EMPTYPAIR
}
return currency.NewPair(k.Base.Currency(), k.Quote.Currency())
}
// Pair combines the base and quote into a pair
func (k *ExchangePairAsset) Pair() currency.Pair {
if k == nil || (k.Base == nil && k.Quote == nil) {
return currency.EMPTYPAIR
}
return currency.NewPair(k.Base.Currency(), k.Quote.Currency())
}
// MatchesExchangeAsset checks if the key matches the exchange and asset
func (k *ExchangePairAsset) MatchesExchangeAsset(exch string, item asset.Item) bool {
if k == nil {
return false
}
return strings.EqualFold(k.Exchange, exch) && k.Asset == item
}
// MatchesPairAsset checks if the key matches the pair and asset
func (k *ExchangePairAsset) MatchesPairAsset(pair currency.Pair, item asset.Item) bool {
if k == nil {
return false
}
return k.Base == pair.Base.Item &&
k.Quote == pair.Quote.Item &&
k.Asset == item
}
// MatchesExchange checks if the exchange matches
func (k *ExchangePairAsset) MatchesExchange(exch string) bool {
if k == nil {
return false
}
return strings.EqualFold(k.Exchange, exch)
}

View File

@@ -11,84 +11,55 @@ import (
func TestMatchesExchangeAsset(t *testing.T) {
t.Parallel()
cp := currency.NewBTCUSD()
k := ExchangePairAsset{
k := ExchangeAssetPair{
Exchange: "test",
Base: cp.Base.Item,
Quote: cp.Quote.Item,
Asset: asset.Spot,
}
if !k.MatchesExchangeAsset("test", asset.Spot) {
t.Error("expected true")
}
if k.MatchesExchangeAsset("TEST", asset.Futures) {
t.Error("expected false")
}
if k.MatchesExchangeAsset("test", asset.Futures) {
t.Error("expected false")
}
if !k.MatchesExchangeAsset("TEST", asset.Spot) {
t.Error("expected true")
}
assert.True(t, k.MatchesExchangeAsset("test", asset.Spot))
assert.False(t, k.MatchesExchangeAsset("TEST", asset.Futures))
assert.False(t, k.MatchesExchangeAsset("test", asset.Futures))
assert.False(t, k.MatchesExchangeAsset("TEST", asset.Spot))
}
func TestMatchesPairAsset(t *testing.T) {
t.Parallel()
cp := currency.NewBTCUSD()
k := ExchangePairAsset{
k := ExchangeAssetPair{
Base: cp.Base.Item,
Quote: cp.Quote.Item,
Asset: asset.Spot,
}
if !k.MatchesPairAsset(cp, asset.Spot) {
t.Error("expected true")
}
if k.MatchesPairAsset(cp, asset.Futures) {
t.Error("expected false")
}
if k.MatchesPairAsset(currency.EMPTYPAIR, asset.Futures) {
t.Error("expected false")
}
if k.MatchesPairAsset(currency.NewBTCUSDT(), asset.Spot) {
t.Error("expected false")
}
assert.True(t, k.MatchesPairAsset(cp, asset.Spot))
assert.False(t, k.MatchesPairAsset(cp, asset.Futures))
assert.False(t, k.MatchesPairAsset(currency.EMPTYPAIR, asset.Futures))
assert.False(t, k.MatchesPairAsset(currency.NewBTCUSDT(), asset.Spot))
}
func TestMatchesExchange(t *testing.T) {
t.Parallel()
k := ExchangePairAsset{
k := ExchangeAssetPair{
Exchange: "test",
}
if !k.MatchesExchange("test") {
t.Error("expected true")
}
if !k.MatchesExchange("TEST") {
t.Error("expected true")
}
if k.MatchesExchange("tèst") {
t.Error("expected false")
}
if k.MatchesExchange("") {
t.Error("expected false")
}
assert.True(t, k.MatchesExchange("test"))
assert.False(t, k.MatchesExchange("TEST"))
assert.False(t, k.MatchesExchange("tèst"))
assert.False(t, k.MatchesExchange(""))
}
func TestExchangePairAsset_Pair(t *testing.T) {
t.Parallel()
cp := currency.NewBTCUSD()
k := ExchangePairAsset{
k := ExchangeAssetPair{
Base: currency.BTC.Item,
Quote: currency.USD.Item,
Asset: asset.Spot,
}
assert.Equal(t, cp, k.Pair())
cp = currency.NewPair(currency.BTC, currency.EMPTYCODE)
k.Quote = currency.EMPTYCODE.Item
assert.Equal(t, cp, k.Pair())
cp = currency.EMPTYPAIR
var epa *ExchangePairAsset
assert.Equal(t, cp, epa.Pair())
}
func TestPairAsset_Pair(t *testing.T) {
@@ -100,12 +71,25 @@ func TestPairAsset_Pair(t *testing.T) {
Asset: asset.Spot,
}
assert.Equal(t, cp, k.Pair())
cp = currency.NewPair(currency.BTC, currency.EMPTYCODE)
k.Quote = currency.EMPTYCODE.Item
assert.Equal(t, cp, k.Pair())
cp = currency.EMPTYPAIR
var pa *PairAsset
assert.Equal(t, cp, pa.Pair())
}
func TestNewExchangePairAssetKey(t *testing.T) {
t.Parallel()
e := "test"
a := asset.Spot
p := currency.NewBTCUSDT()
k := NewExchangeAssetPair(e, a, p)
assert.Equal(t, e, k.Exchange)
assert.Equal(t, p.Base.Item, k.Base)
assert.Equal(t, p.Quote.Item, k.Quote)
assert.Equal(t, a, k.Asset)
e = ""
a = 0
p = currency.EMPTYPAIR
k = NewExchangeAssetPair(e, a, p)
assert.Equal(t, a, k.Asset, "NewExchangeAssetPair should not alter an invalid asset")
}