From 40a28e49c4300e6a17a5b0a84b134e4a4397758f Mon Sep 17 00:00:00 2001 From: roy-dydx <133032749+roy-dydx@users.noreply.github.com> Date: Thu, 2 May 2024 19:10:09 -0400 Subject: [PATCH] [bugfix][protocol] make funding payment event ordering deterministic (#921) (#1455) 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 a6fff13edd..9a43ac8cbe 100644 --- a/protocol/x/subaccounts/keeper/subaccount.go +++ b/protocol/x/subaccounts/keeper/subaccount.go @@ -277,7 +277,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,