Skip to content

Commit

Permalink
fix: ensure we hit leave auction trigger on mark-price calc, and hand…
Browse files Browse the repository at this point in the history
…le zero length auction intervals in perpetual
  • Loading branch information
wwestgarth committed Mar 11, 2024
1 parent 0fb8d1c commit 8d34e48
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
8 changes: 4 additions & 4 deletions core/execution/common/mark_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,11 @@ func (mpc *CompositePriceCalculator) updateMarkPriceIfNotInAuction(ctx context.C
return nil
}
priceMonitor.CheckPrice(ctx, as, []*types.Trade{{Price: mpcCandidate, Size: 1}}, true, true)
if !as.InAuction() {
mpc.price = mpcCandidate
return nil
if as.InAuction() || as.AuctionStart() {
return fmt.Errorf("price monitoring failed for the new mark price")
}
return fmt.Errorf("price monitoring failed for the new mark price")
mpc.price = mpcCandidate
return nil
}

// CalculateMarkPrice is called at the end of each mark price calculation interval and calculates the mark price
Expand Down
4 changes: 3 additions & 1 deletion core/execution/future/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -1580,7 +1580,6 @@ func (m *Market) leaveAuction(ctx context.Context, now time.Time) {

m.mkt.State = types.MarketStateActive
m.mkt.TradingMode = types.MarketTradingModeContinuous
m.tradableInstrument.Instrument.UpdateAuctionState(ctx, false)
m.broker.Send(events.NewMarketUpdatedEvent(ctx, *m.mkt))

m.updateLiquidityFee(ctx)
Expand Down Expand Up @@ -1612,6 +1611,9 @@ func (m *Market) leaveAuction(ctx context.Context, now time.Time) {

// update auction state, so we know what the new tradeMode ought to be
endEvt := m.as.Left(ctx, now)
// we tell the perp that we've left auction, we might re-enter just a bit down but thats fine as
// we will at least keep the in/out orders in sync
m.tradableInstrument.Instrument.UpdateAuctionState(ctx, false)

for _, uncrossedOrder := range uncrossedOrders {
updatedOrders = append(updatedOrders, uncrossedOrder.Order)
Expand Down
20 changes: 19 additions & 1 deletion core/products/perpetual.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,25 @@ func (a *auctionIntervals) update(t int64, enter bool) {
}

if enter {
a.auctionStart = t
if len(a.auctions) == 0 {
a.auctionStart = t
return
}

st, nd := a.auctions[len(a.auctions)-2], a.auctions[len(a.auctions)-1]
if t != nd {
a.auctionStart = t
return
}

a.auctions = slices.Delete(a.auctions, len(a.auctions)-2, len(a.auctions))
a.auctionStart = st
return
}

if t == a.auctionStart {
// left an auction as soon as we entered it, no need to log it
a.auctionStart = 0
return
}

Expand Down
26 changes: 26 additions & 0 deletions core/products/perpetual_auctions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,32 @@ func testPastFundingPayment(t *testing.T) {
assert.Equal(t, int64(expectedTWAP), fundingPayment.Int64())
}

func TestZeroLengthAuctionPeriods(t *testing.T) {
perp := testPerpetual(t)
defer perp.ctrl.Finish()

// set of the data points such that difference in averages is 0
points := getTestDataPoints(t)

// tell the perpetual that we are ready to accept settlement stuff
whenLeaveOpeningAuction(t, perp, points[0].t)

// enter auction
whenAuctionStateChanges(t, perp, points[0].t, true)

// send in some data points with a TWAP difference
submitDataWithDifference(t, perp, points, 10)

// leave auction
whenAuctionStateChanges(t, perp, points[len(points)-1].t, false)

// but then enter again straight away
whenAuctionStateChanges(t, perp, points[len(points)-1].t, true)

fundingPayment := whenTheFundingPeriodEnds(t, perp, points[len(points)-1].t)
assert.Equal(t, "0", fundingPayment.String())
}

func TestFairgroundPanic(t *testing.T) {
perp := testPerpetual(t)
defer perp.ctrl.Finish()
Expand Down

0 comments on commit 8d34e48

Please sign in to comment.