diff --git a/protocol/x/subaccounts/keeper/oimf.go b/protocol/x/subaccounts/keeper/oimf.go index 8fe7f18ca3e..6a050639c2f 100644 --- a/protocol/x/subaccounts/keeper/oimf.go +++ b/protocol/x/subaccounts/keeper/oimf.go @@ -68,7 +68,10 @@ func GetDeltaOpenInterestFromPerpMatchUpdates( ) } - if settledUpdates[0].PerpetualUpdates[0].PerpetualId != settledUpdates[1].PerpetualUpdates[0].PerpetualId { + perpUpdate0 := settledUpdates[0].PerpetualUpdates[0] + perpUpdate1 := settledUpdates[1].PerpetualUpdates[0] + + if perpUpdate0.PerpetualId != perpUpdate1.PerpetualId { panic( fmt.Sprintf( types.ErrMatchUpdatesMustBeSamePerpId, @@ -79,6 +82,16 @@ func GetDeltaOpenInterestFromPerpMatchUpdates( updatedPerpId = settledUpdates[0].PerpetualUpdates[0].PerpetualId } + if (perpUpdate0.BigQuantumsDelta.Sign()*perpUpdate0.BigQuantumsDelta.Sign() < 0) || + perpUpdate0.BigQuantumsDelta.CmpAbs(perpUpdate1.BigQuantumsDelta) != 0 { + panic( + fmt.Sprintf( + types.ErrMatchUpdatesInvalidSize, + settledUpdates, + ), + ) + } + deltaOpenInterest = big.NewInt(0) for _, u := range settledUpdates { deltaLong := getDeltaLongFromSettledUpdate(u, updatedPerpId) diff --git a/protocol/x/subaccounts/keeper/subaccount_test.go b/protocol/x/subaccounts/keeper/subaccount_test.go index 19fc43de7aa..de19a3b02c3 100644 --- a/protocol/x/subaccounts/keeper/subaccount_test.go +++ b/protocol/x/subaccounts/keeper/subaccount_test.go @@ -3247,10 +3247,7 @@ func TestCanUpdateSubaccounts(t *testing.T) { }, additionalTestSubaccounts: []types.Subaccount{ { - Id: &types.SubaccountId{ - Owner: "Bob", - Number: 0, - }, + Id: &constants.Bob_Num0, AssetPositions: []*types.AssetPosition{ { AssetId: uint32(0), @@ -3314,10 +3311,7 @@ func TestCanUpdateSubaccounts(t *testing.T) { }, additionalTestSubaccounts: []types.Subaccount{ { - Id: &types.SubaccountId{ - Owner: "Bob", - Number: 0, - }, + Id: &constants.Bob_Num0, AssetPositions: []*types.AssetPosition{ { AssetId: uint32(0), @@ -3388,10 +3382,7 @@ func TestCanUpdateSubaccounts(t *testing.T) { }, additionalTestSubaccounts: []types.Subaccount{ { - Id: &types.SubaccountId{ - Owner: "Bob", - Number: 0, - }, + Id: &constants.Bob_Num0, AssetPositions: []*types.AssetPosition{ { AssetId: uint32(0), @@ -3464,10 +3455,7 @@ func TestCanUpdateSubaccounts(t *testing.T) { }, additionalTestSubaccounts: []types.Subaccount{ { - Id: &types.SubaccountId{ - Owner: "Bob", - Number: 0, - }, + Id: &constants.Bob_Num0, AssetPositions: []*types.AssetPosition{ { AssetId: uint32(0), @@ -3539,10 +3527,7 @@ func TestCanUpdateSubaccounts(t *testing.T) { }, additionalTestSubaccounts: []types.Subaccount{ { - Id: &types.SubaccountId{ - Owner: "Bob", - Number: 0, - }, + Id: &constants.Bob_Num0, AssetPositions: []*types.AssetPosition{ { AssetId: uint32(0), diff --git a/protocol/x/subaccounts/types/errors.go b/protocol/x/subaccounts/types/errors.go index 1b5315b187d..d27fb280fb0 100644 --- a/protocol/x/subaccounts/types/errors.go +++ b/protocol/x/subaccounts/types/errors.go @@ -13,8 +13,10 @@ const ( "exactly 2 updates, got settledUpdates: %+v" ErrMatchUpdatesMustUpdateOnePerp = "internalCanUpdateSubaccounts: MATCH subaccount updates must each have " + "exactly 1 PerpetualUpdate, got settledUpdates: %+v" - ErrMatchUpdatesMustBeSamePerpId = "internalCanUpdateSubaccounts: MATCH subaccount updates must consists two " + + ErrMatchUpdatesMustBeSamePerpId = "internalCanUpdateSubaccounts: MATCH subaccount updates must consists of two " + "updates on same perpetual Id, got settledUpdates: %+v" + ErrMatchUpdatesInvalidSize = "internalCanUpdateSubaccounts: MATCH subaccount updates must consists of two " + + "updates of equal absolute base quantums and opposite sign: %+v" ) // x/subaccounts module sentinel errors