Skip to content

Commit

Permalink
Merge pull request #11639 from vegaprotocol/fix-retrograde-amm-volume
Browse files Browse the repository at this point in the history
fix: prevent overflow when calculating tradable volume outside of sid…
  • Loading branch information
jeremyletang authored Sep 3, 2024
2 parents 1368c15 + a62dfac commit 0a157dc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions core/execution/amm/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
16 changes: 16 additions & 0 deletions core/execution/amm/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 0a157dc

Please sign in to comment.