Skip to content

Commit

Permalink
nits
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyding committed Mar 21, 2024
1 parent 3164b81 commit 2c9f80a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 22 deletions.
15 changes: 14 additions & 1 deletion protocol/x/subaccounts/keeper/oimf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -79,6 +82,16 @@ func GetDeltaOpenInterestFromPerpMatchUpdates(
updatedPerpId = settledUpdates[0].PerpetualUpdates[0].PerpetualId
}

if (perpUpdate0.BigQuantumsDelta.Sign()*perpUpdate1.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)
Expand Down
54 changes: 54 additions & 0 deletions protocol/x/subaccounts/keeper/oimf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,60 @@ func TestGetDeltaOpenInterestFromPerpMatchUpdates(t *testing.T) {
},
panicErr: types.ErrMatchUpdatesMustBeSamePerpId,
},
"Invalid: updates don't have opposite signs": {
settledUpdates: []keeper.SettledUpdate{
{
SettledSubaccount: types.Subaccount{
Id: aliceSubaccountId,
},
PerpetualUpdates: []types.PerpetualUpdate{
{
PerpetualId: 1,
BigQuantumsDelta: big.NewInt(500),
},
},
},
{
SettledSubaccount: types.Subaccount{
Id: bobSubaccountId,
},
PerpetualUpdates: []types.PerpetualUpdate{
{
PerpetualId: 1,
BigQuantumsDelta: big.NewInt(500),
},
},
},
},
panicErr: types.ErrMatchUpdatesInvalidSize,
},
"Invalid: updates don't have equal absolute base quantums": {
settledUpdates: []keeper.SettledUpdate{
{
SettledSubaccount: types.Subaccount{
Id: aliceSubaccountId,
},
PerpetualUpdates: []types.PerpetualUpdate{
{
PerpetualId: 1,
BigQuantumsDelta: big.NewInt(500),
},
},
},
{
SettledSubaccount: types.Subaccount{
Id: bobSubaccountId,
},
PerpetualUpdates: []types.PerpetualUpdate{
{
PerpetualId: 1,
BigQuantumsDelta: big.NewInt(-499),
},
},
},
},
panicErr: types.ErrMatchUpdatesInvalidSize,
},
"Valid: 0 -> -500, 0 -> 500, delta = 500": {
settledUpdates: []keeper.SettledUpdate{
{
Expand Down
25 changes: 5 additions & 20 deletions protocol/x/subaccounts/keeper/subaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
4 changes: 3 additions & 1 deletion protocol/x/subaccounts/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2c9f80a

Please sign in to comment.