Skip to content

Commit

Permalink
client/{core,mm}: Rebalance on EpochMatchSummary instead of new epoch
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
martonp committed Sep 11, 2023
1 parent ece6c3a commit 0f5923d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
17 changes: 10 additions & 7 deletions client/core/bookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(&note.Candle)
if !ok {
Expand Down
5 changes: 5 additions & 0 deletions client/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 5 additions & 7 deletions client/mm/mm_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion client/webserver/site/src/html/bodybuilder.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
{{end}}

{{define "bottom"}}
<script src="/js/entry.js?v=0a518229|5c5461e5"></script>
<script src="/js/entry.js?v=faadd923|2e83f47d"></script>
</body>
</html>
{{end}}
2 changes: 1 addition & 1 deletion client/webserver/site/src/js/markets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1812,7 +1812,7 @@ export default class MarketsPage extends BasePage {
}

handleEpochMatchSummary (data: BookUpdate) {
this.addRecentMatches(data.payload)
this.addRecentMatches(data.payload.matchSummaries)
this.refreshRecentMatchesTable()
}

Expand Down

0 comments on commit 0f5923d

Please sign in to comment.