From 0f5923dc2ee751a9479951b3b8107bbee9fcd62e Mon Sep 17 00:00:00 2001 From: martonp Date: Mon, 11 Sep 2023 03:06:25 -0400 Subject: [PATCH] client/{core,mm}: Rebalance on EpochMatchSummary instead of new epoch Updates the basic market maker to rebalance when the order book receives an EpochMatchSummary notification from the server instead of when an `EpochNotification` arrives. When the `EpochNotification` arrives, the orderbook has not yet been updated with the matches of the previous epoch. --- client/core/bookie.go | 17 ++++++++++------- client/core/types.go | 5 +++++ client/mm/mm_basic.go | 12 +++++------- client/webserver/site/src/html/bodybuilder.tmpl | 2 +- client/webserver/site/src/js/markets.ts | 2 +- 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/client/core/bookie.go b/client/core/bookie.go index d1bc0845e6..541b15848a 100644 --- a/client/core/bookie.go +++ b/client/core/bookie.go @@ -184,13 +184,16 @@ func (b *bookie) logEpochReport(note *msgjson.EpochReportNote) error { marketID := marketName(b.base, b.quote) matchSummaries := b.AddRecentMatches(note.MatchSummary, note.EndStamp) - if len(note.MatchSummary) > 0 { - b.send(&BookUpdate{ - Action: EpochMatchSummary, - MarketID: marketID, - Payload: matchSummaries, - }) - } + + b.send(&BookUpdate{ + Action: EpochMatchSummary, + MarketID: marketID, + Payload: &EpochMatchSummaryPayload{ + MatchSummaries: matchSummaries, + Epoch: note.Epoch, + }, + }) + for durStr, cache := range b.candleCaches { c, ok := cache.addCandle(¬e.Candle) if !ok { diff --git a/client/core/types.go b/client/core/types.go index 102b56798b..9fa94d278f 100644 --- a/client/core/types.go +++ b/client/core/types.go @@ -743,6 +743,11 @@ type CandlesPayload struct { Candles []msgjson.Candle `json:"candles"` } +type EpochMatchSummaryPayload struct { + MatchSummaries []*orderbook.MatchSummary `json:"matchSummaries"` + Epoch uint64 `json:"epoch"` +} + // dexAccount is the core type to represent the client's account information for // a DEX. type dexAccount struct { diff --git a/client/mm/mm_basic.go b/client/mm/mm_basic.go index 249128c6e0..0869a664df 100644 --- a/client/mm/mm_basic.go +++ b/client/mm/mm_basic.go @@ -716,8 +716,6 @@ func (m *basicMarketMaker) handleNotification(note core.Notification) { return } m.processTrade(ord) - case *core.EpochNotification: - go m.rebalance(n.Epoch) case *core.FiatRatesNote: go m.processFiatRates(n.FiatRates) } @@ -803,11 +801,11 @@ func (m *basicMarketMaker) run() { defer wg.Done() for { select { - case <-bookFeed.Next(): - // Really nothing to do with the updates. We just need to keep - // the subscription live in order to get a mid-gap rate when - // needed. We could use this to trigger rebalances mid-epoch - // though, which I think would provide some advantage. + case n := <-bookFeed.Next(): + if n.Action == core.EpochMatchSummary { + payload := n.Payload.(*core.EpochMatchSummaryPayload) + m.rebalance(payload.Epoch + 1) + } case <-m.ctx.Done(): return } diff --git a/client/webserver/site/src/html/bodybuilder.tmpl b/client/webserver/site/src/html/bodybuilder.tmpl index 0726783d84..ea413e7cc6 100644 --- a/client/webserver/site/src/html/bodybuilder.tmpl +++ b/client/webserver/site/src/html/bodybuilder.tmpl @@ -103,7 +103,7 @@ {{end}} {{define "bottom"}} - + {{end}} diff --git a/client/webserver/site/src/js/markets.ts b/client/webserver/site/src/js/markets.ts index c43cbae2da..46b5703c0d 100644 --- a/client/webserver/site/src/js/markets.ts +++ b/client/webserver/site/src/js/markets.ts @@ -1812,7 +1812,7 @@ export default class MarketsPage extends BasePage { } handleEpochMatchSummary (data: BookUpdate) { - this.addRecentMatches(data.payload) + this.addRecentMatches(data.payload.matchSummaries) this.refreshRecentMatchesTable() }