From fd45567f06b9da7c0199ed026e03da01d3e109e1 Mon Sep 17 00:00:00 2001 From: wwestgarth Date: Mon, 2 Sep 2024 17:01:12 +0100 Subject: [PATCH] fix: prevent overflow when calculating tradable volume outside of sided range --- core/execution/amm/pool.go | 6 ++++++ core/execution/amm/pool_test.go | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/core/execution/amm/pool.go b/core/execution/amm/pool.go index e4c89e673e..70a754f684 100644 --- a/core/execution/amm/pool.go +++ b/core/execution/amm/pool.go @@ -629,11 +629,17 @@ func (p *Pool) TradableVolumeInRange(side types.Side, price1 *num.Uint, price2 * if side == types.SideSell { // want all buy volume so everything below fair price, where the AMM is long + if pos > stP { + return 0 + } ndP = num.MaxV(pos, ndP) } if side == types.SideBuy { // want all sell volume so everything above fair price, where the AMM is short + if pos < ndP { + return 0 + } stP = num.MinV(pos, stP) } diff --git a/core/execution/amm/pool_test.go b/core/execution/amm/pool_test.go index 0444f469e8..a46d83876d 100644 --- a/core/execution/amm/pool_test.go +++ b/core/execution/amm/pool_test.go @@ -113,6 +113,22 @@ func testTradeableVolumeInRange(t *testing.T) { expectedVolume: 1052, position: -350, }, + { + name: "AMM is long and price range is fully in short section", + price1: num.NewUint(1900), + price2: num.NewUint(2200), + side: types.SideSell, + expectedVolume: 0, + position: 700, + }, + { + name: "AMM is short and price range is fully in long section", + price1: num.NewUint(1900), + price2: num.NewUint(2100), + side: types.SideBuy, + expectedVolume: 0, + position: -700, + }, } for _, tt := range tests {