Skip to content

Commit

Permalink
Merge pull request #11492 from vegaprotocol/11491-missing-nil-check
Browse files Browse the repository at this point in the history
fix: add nil check on AMM range partition for case where aggressive o…
  • Loading branch information
wwestgarth authored Jul 24, 2024
2 parents 2ba50fa + d822ac3 commit 6691a3d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
6 changes: 4 additions & 2 deletions core/execution/amm/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,9 @@ func (e *Engine) submit(active []*Pool, agg *types.Order, inner, outer *num.Uint
}

// partition takes the given price range and returns which pools have volume in that region, and
// divides that range into sub-levels where AMM boundaries end.
// divides that range into sub-levels where AMM boundaries end. Note that `outer` can be nil for the case
// where the incoming order is a market order (so we have no bound on the price), and we've already consumed
// all volume on the orderbook.
func (e *Engine) partition(agg *types.Order, inner, outer *num.Uint) ([]*Pool, []*num.Uint) {
active := []*Pool{}
bounds := map[string]*num.Uint{}
Expand Down Expand Up @@ -503,7 +505,7 @@ func (e *Engine) partition(agg *types.Order, inner, outer *num.Uint) ([]*Pool, [
// This is because to get the BUY volume an AMM has at price P, we need to calculate the difference
// in its position between prices P -> P + 1. For SELL volume its the other way around and we
// need the difference in position from P - 1 -> P.
if inner.EQ(outer) {
if inner != nil && outer != nil && inner.EQ(outer) {
if agg.Side == types.SideSell {
outer = num.UintZero().Add(outer, e.oneTick)
} else {
Expand Down
26 changes: 26 additions & 0 deletions core/execution/amm/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func TestAMMTrading(t *testing.T) {
t.Run("test basic submit order", testBasicSubmitOrder)
t.Run("test submit order at best price", testSubmitOrderAtBestPrice)
t.Run("test submit market order", testSubmitMarketOrder)
t.Run("test submit market order unbounded", testSubmitMarketOrderUnbounded)
t.Run("test submit order pro rata", testSubmitOrderProRata)
t.Run("test best prices and volume", testBestPricesAndVolume)

Expand Down Expand Up @@ -269,6 +270,31 @@ func testSubmitMarketOrder(t *testing.T) {
assert.Equal(t, 126420, int(orders[0].Size))
}

func testSubmitMarketOrderUnbounded(t *testing.T) {
tst := getTestEngine(t)

party, subAccount := getParty(t, tst)
submit := getPoolSubmission(t, party, tst.marketID)

expectSubaccountCreation(t, tst, party, subAccount)
whenAMMIsSubmitted(t, tst, submit)

// now submit an order against it
agg := &types.Order{
Size: 1000000,
Remaining: 1000000,
Side: types.SideSell,
Price: num.NewUint(0),
Type: types.OrderTypeMarket,
}

ensurePosition(t, tst.pos, 0, num.NewUint(0))
orders := tst.engine.SubmitOrder(agg, num.NewUint(1980), nil)
require.Len(t, orders, 1)
assert.Equal(t, "1960", orders[0].Price.String())
assert.Equal(t, 1000000, int(orders[0].Size))
}

func testSubmitOrderProRata(t *testing.T) {
tst := getTestEngine(t)

Expand Down

0 comments on commit 6691a3d

Please sign in to comment.