Files
gocryptotrader/exchange/order/limits/store_test.go
Scott 85403fe801 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>
2025-08-26 12:30:21 +10:00

226 lines
5.5 KiB
Go

package limits
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/thrasher-corp/gocryptotrader/common/key"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
)
var happyKey = key.NewExchangeAssetPair("test", asset.Spot, currency.NewBTCUSDT())
func TestLoadLimits(t *testing.T) {
t.Parallel()
e := store{}
err := e.load(nil)
require.ErrorIs(t, err, ErrEmptyLevels)
badKeyNoExchange := []MinMaxLevel{
{
Key: key.NewExchangeAssetPair("", asset.Spot, currency.NewBTCUSDT()),
MinPrice: 100000,
MaxPrice: 1000000,
MinimumBaseAmount: 1,
MaximumBaseAmount: 10,
},
}
err = e.load(badKeyNoExchange)
assert.ErrorIs(t, err, errExchangeNameEmpty)
badKeyNoAsset := []MinMaxLevel{
{
Key: key.NewExchangeAssetPair("hi", 0, currency.NewBTCUSDT()),
MinPrice: 100000,
MaxPrice: 1000000,
MinimumBaseAmount: 1,
MaximumBaseAmount: 10,
},
}
err = e.load(badKeyNoAsset)
assert.ErrorIs(t, err, errAssetInvalid)
badKeyNoPair := []MinMaxLevel{
{
Key: key.NewExchangeAssetPair("hi", asset.Spot, currency.EMPTYPAIR),
MinPrice: 100000,
MaxPrice: 1000000,
MinimumBaseAmount: 1,
MaximumBaseAmount: 10,
},
}
err = e.load(badKeyNoPair)
assert.ErrorIs(t, err, errPairNotSet)
happyLimit := []MinMaxLevel{
{
Key: happyKey,
MinPrice: 100000,
MaxPrice: 1000000,
MinimumBaseAmount: 1,
MaximumBaseAmount: 10,
},
}
err = e.load(happyLimit)
assert.NoError(t, err)
badLimit := []MinMaxLevel{
{
Key: happyKey,
MinPrice: 2,
MaxPrice: 1,
MinimumBaseAmount: 1,
MaximumBaseAmount: 10,
},
}
err = e.load(badLimit)
assert.ErrorIs(t, err, errInvalidPriceLevels)
badLimit = []MinMaxLevel{
{
Key: happyKey,
MinPrice: 1,
MaxPrice: 2,
MinimumBaseAmount: 10,
MaximumBaseAmount: 9,
},
}
err = e.load(badLimit)
assert.ErrorIs(t, err, errInvalidAmountLevels)
badLimit = []MinMaxLevel{
{
Key: happyKey,
MinimumQuoteAmount: 100,
MaximumQuoteAmount: 10,
},
}
err = e.load(badLimit)
assert.ErrorIs(t, err, errInvalidQuoteLevels)
}
func TestGetOrderExecutionLimits(t *testing.T) {
t.Parallel()
e := store{}
_, err := e.getOrderExecutionLimits(happyKey)
require.ErrorIs(t, err, ErrExchangeLimitNotLoaded)
newLimits := []MinMaxLevel{
{
Key: happyKey,
MinPrice: 100000,
MaxPrice: 1000000,
MinimumBaseAmount: 1,
MaximumBaseAmount: 10,
},
}
err = e.load(newLimits)
require.NoError(t, err)
_, err = e.getOrderExecutionLimits(key.NewExchangeAssetPair("hi", asset.Futures, currency.NewBTCUSDT()))
assert.ErrorIs(t, err, ErrOrderLimitNotFound)
tt, err := e.getOrderExecutionLimits(happyKey)
require.NoError(t, err)
require.Equal(t, newLimits[0], tt)
}
func TestCheckLimit(t *testing.T) {
t.Parallel()
e := store{}
err := e.checkOrderExecutionLimits(happyKey, 1337, 1337, order.Limit)
require.ErrorIs(t, err, ErrExchangeLimitNotLoaded)
newLimits := []MinMaxLevel{
{
Key: happyKey,
MinPrice: 100000,
MaxPrice: 1000000,
MinimumBaseAmount: 1,
MaximumBaseAmount: 10,
},
}
err = e.load(newLimits)
assert.NoError(t, err)
err = e.checkOrderExecutionLimits(key.NewExchangeAssetPair("test", asset.Futures, currency.NewBTCUSDT()), 1337, 1337, order.Limit)
assert.ErrorIs(t, err, ErrOrderLimitNotFound)
err = e.checkOrderExecutionLimits(happyKey, 1337, 9, order.Limit)
assert.ErrorIs(t, err, ErrPriceBelowMin)
err = e.checkOrderExecutionLimits(happyKey, 1000001, 9, order.Limit)
assert.ErrorIs(t, err, ErrPriceExceedsMax)
err = e.checkOrderExecutionLimits(happyKey, 999999, .5, order.Limit)
assert.ErrorIs(t, err, ErrAmountBelowMin)
err = e.checkOrderExecutionLimits(happyKey, 999999, 11, order.Limit)
assert.ErrorIs(t, err, ErrAmountExceedsMax)
err = e.checkOrderExecutionLimits(happyKey, 999999, 7, order.Limit)
assert.NoError(t, err)
err = e.checkOrderExecutionLimits(happyKey, 999999, 7, order.Market)
assert.NoError(t, err)
}
// No parallel, working with global var
func TestPublicLoadLimits(t *testing.T) {
err := Load(nil)
assert.ErrorIs(t, err, ErrEmptyLevels)
newLimits := []MinMaxLevel{
{
Key: happyKey,
MinPrice: 100000,
MaxPrice: 1000000,
MinimumBaseAmount: 1,
MaximumBaseAmount: 10,
},
}
err = Load(newLimits)
require.NoError(t, err)
}
// No parallel, working with global var
func TestPublicGetOrderExecutionLimits(t *testing.T) {
newLimits := []MinMaxLevel{
{
Key: happyKey,
MinPrice: 100000,
MaxPrice: 1000000,
MinimumBaseAmount: 1,
MaximumBaseAmount: 10,
},
}
err := Load(newLimits)
require.NoError(t, err)
resp, err := GetOrderExecutionLimits(happyKey)
assert.NoError(t, err)
assert.Equal(t, newLimits[0], resp)
}
// No parallel, working with global var
func TestPublicCheckOrderExecutionLimits(t *testing.T) {
newLimits := []MinMaxLevel{
{
Key: happyKey,
MinPrice: 100000,
MaxPrice: 1000000,
MinimumBaseAmount: 1,
MaximumBaseAmount: 10,
},
}
err := Load(newLimits)
require.NoError(t, err)
err = CheckOrderExecutionLimits(happyKey, 1, 1, order.Market)
assert.NoError(t, err)
}