Skip to content

Commit

Permalink
Merge pull request #11736 from vegaprotocol/11715-amm-crossing
Browse files Browse the repository at this point in the history
fix: when uncrossing after auction refine approximately expanded AMMs…
  • Loading branch information
jeremyletang authored Oct 9, 2024
2 parents 85a3438 + 4527359 commit 2a40697
Show file tree
Hide file tree
Showing 17 changed files with 529 additions and 261 deletions.
16 changes: 7 additions & 9 deletions core/execution/amm/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -970,32 +970,30 @@ func (e *Engine) UpdateSubAccountBalance(

// OrderbookShape expands all registered AMM's into orders between the given prices. If `ammParty` is supplied then just the pool
// with that party id is expanded.
func (e *Engine) OrderbookShape(st, nd *num.Uint, ammParty *string) ([]*types.Order, []*types.Order) {
func (e *Engine) OrderbookShape(st, nd *num.Uint, ammParty *string) []*types.OrderbookShapeResult {
if ammParty == nil {
// no party give, expand all registered
buys, sells := []*types.Order{}, []*types.Order{}
res := make([]*types.OrderbookShapeResult, 0, len(e.poolsCpy))
for _, p := range e.poolsCpy {
b, s := p.OrderbookShape(st, nd, e.idgen)
buys = append(buys, b...)
sells = append(sells, s...)
res = append(res, p.OrderbookShape(st, nd, e.idgen))
}
return buys, sells
return res
}

// asked to expand just one AMM, lets find it, first amm-party -> owning party
owner, ok := e.ammParties[*ammParty]
if !ok {
return nil, nil
return nil
}

// now owning party -> pool
p, ok := e.pools[owner]
if !ok {
return nil, nil
return nil
}

// expand it
return p.OrderbookShape(st, nd, e.idgen)
return []*types.OrderbookShapeResult{p.OrderbookShape(st, nd, e.idgen)}
}

func (e *Engine) GetAMMPoolsBySubAccount() map[string]common.AMMPool {
Expand Down
21 changes: 14 additions & 7 deletions core/execution/amm/shape.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,10 @@ func (sm *shapeMaker) adjustRegion() bool {
return true
}

func (sm *shapeMaker) makeShape() ([]*types.Order, []*types.Order) {
func (sm *shapeMaker) makeShape() {
if !sm.adjustRegion() {
// if there is no overlap between the input region and the AMM's bounds then there are no orders
return sm.buys, sm.sells
return
}

// create accurate orders at the boundary of the adjusted region (even if we are going to make approximate internal steps)
Expand Down Expand Up @@ -481,15 +481,22 @@ func (sm *shapeMaker) makeShape() ([]*types.Order, []*types.Order) {
logging.Int("sells", len(sm.sells)),
)
}
return sm.buys, sm.sells
}

func (p *Pool) OrderbookShape(from, to *num.Uint, idgen *idgeneration.IDGenerator) ([]*types.Order, []*types.Order) {
return newShapeMaker(
func (p *Pool) OrderbookShape(from, to *num.Uint, idgen *idgeneration.IDGenerator) *types.OrderbookShapeResult {
sm := newShapeMaker(
p.log,
p,
from,
to,
idgen).
makeShape()
idgen)

sm.makeShape()

return &types.OrderbookShapeResult{
AmmParty: sm.pool.AMMParty,
Buys: sm.buys,
Sells: sm.sells,
Approx: sm.approx,
}
}
Loading

0 comments on commit 2a40697

Please sign in to comment.