From 31c2a937fc87bb9b7617443e4e5db2b1a2e9bda9 Mon Sep 17 00:00:00 2001 From: roy-dydx <133032749+roy-dydx@users.noreply.github.com> Date: Tue, 12 Mar 2024 11:28:33 -0700 Subject: [PATCH] [bugfix][protocol] make funding payment event ordering deterministic (#921) (#1166) Co-authored-by: lucas-dydx <76970939+lucas-dydx@users.noreply.github.com> --- protocol/x/subaccounts/keeper/subaccount.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/protocol/x/subaccounts/keeper/subaccount.go b/protocol/x/subaccounts/keeper/subaccount.go index 1763818ed4..7a43b7c29b 100644 --- a/protocol/x/subaccounts/keeper/subaccount.go +++ b/protocol/x/subaccounts/keeper/subaccount.go @@ -324,7 +324,10 @@ func (k Keeper) UpdateSubaccounts( // Emit an event indicating a funding payment was paid / received for each settled funding // payment. Note that `fundingPaid` is positive if the subaccount paid funding, // and negative if the subaccount received funding. - for perpetualId, fundingPaid := range fundingPayments { + // Note the perpetual IDs are sorted first to ensure event emission determinism. + sortedPerpIds := lib.GetSortedKeys[lib.Sortable[uint32]](fundingPayments) + for _, perpetualId := range sortedPerpIds { + fundingPaid := fundingPayments[perpetualId] ctx.EventManager().EmitEvent( types.NewCreateSettledFundingEvent( *u.SettledSubaccount.Id,