Skip to content

Commit

Permalink
remove panic when getting quantums from zero position (#1964)
Browse files Browse the repository at this point in the history
  • Loading branch information
jayy04 authored Jul 26, 2024
1 parent 7542acc commit cd9380f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 51 deletions.
1 change: 1 addition & 0 deletions protocol/x/subaccounts/lib/updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func GetSettledSubaccountWithPerpetuals(
newPerpetualPositions, &types.PerpetualPosition{
PerpetualId: p.PerpetualId,
Quantums: p.Quantums,
QuoteBalance: p.QuoteBalance,
FundingIndex: dtypes.NewIntFromBigInt(newFundingIndex),
},
)
Expand Down
16 changes: 4 additions & 12 deletions protocol/x/subaccounts/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"math/big"
"testing"

errorsmod "cosmossdk.io/errors"

"github.com/dydxprotocol/v4-chain/protocol/testutil/sample"
testutil "github.com/dydxprotocol/v4-chain/protocol/testutil/util"
"github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"
Expand Down Expand Up @@ -173,11 +171,8 @@ func TestGenesisState_Validate(t *testing.T) {
},
},
},
shouldPanic: true,
expectedError: errorsmod.Wrapf(
types.ErrAssetPositionZeroQuantum,
"asset position (asset Id: 0) has zero quantum",
),
shouldPanic: false,
expectedError: types.ErrAssetPositionZeroQuantum,
},
"invalid: perpetual positions out of order": {
genState: &types.GenesisState{
Expand Down Expand Up @@ -225,11 +220,8 @@ func TestGenesisState_Validate(t *testing.T) {
},
},
},
shouldPanic: true,
expectedError: errorsmod.Wrapf(
types.ErrPerpPositionZeroQuantum,
"perpetual position (perpetual Id: 0) has zero quantum",
),
shouldPanic: false,
expectedError: types.ErrPerpPositionZeroQuantum,
},
}

Expand Down
26 changes: 4 additions & 22 deletions protocol/x/subaccounts/types/position_size.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package types
import (
"math/big"

errorsmod "cosmossdk.io/errors"

"github.com/dydxprotocol/v4-chain/protocol/dtypes"
)

Expand Down Expand Up @@ -43,20 +41,12 @@ func (m *AssetPosition) GetId() uint32 {
return m.GetAssetId()
}

// Get the asset position quantum size in big.Int. Panics if the size is zero.
// Get the asset position quantum size in big.Int.
func (m *AssetPosition) GetBigQuantums() *big.Int {
if m == nil {
if m == nil || m.Quantums.IsNil() {
return new(big.Int)
}

if m.Quantums.Sign() == 0 {
panic(errorsmod.Wrapf(
ErrAssetPositionZeroQuantum,
"asset position (asset Id: %v) has zero quantum",
m.AssetId,
))
}

return m.Quantums.BigInt()
}

Expand All @@ -75,20 +65,12 @@ func (m *PerpetualPosition) SetQuantums(sizeQuantums int64) {
m.Quantums = dtypes.NewInt(sizeQuantums)
}

// Get the perpetual position quantum size in big.Int. Panics if the size is zero.
// Get the perpetual position quantum size in big.Int.
func (m *PerpetualPosition) GetBigQuantums() *big.Int {
if m == nil {
if m == nil || m.Quantums.IsNil() {
return new(big.Int)
}

if m.Quantums.Sign() == 0 {
panic(errorsmod.Wrapf(
ErrPerpPositionZeroQuantum,
"perpetual position (perpetual Id: %v) has zero quantum",
m.PerpetualId,
))
}

return m.Quantums.BigInt()
}

Expand Down
21 changes: 4 additions & 17 deletions protocol/x/subaccounts/types/position_size_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package types_test

import (
errorsmod "cosmossdk.io/errors"
"math/big"
"testing"

Expand Down Expand Up @@ -43,14 +42,8 @@ func TestPerpetualPosition_GetIsLong(t *testing.T) {
require.True(t,
longPosition.GetIsLong(),
)
require.PanicsWithError(t,
errorsmod.Wrapf(
types.ErrPerpPositionZeroQuantum,
"perpetual position (perpetual Id: 0) has zero quantum",
).Error(),
func() {
zeroPosition.GetIsLong()
},
require.False(t,
zeroPosition.GetIsLong(),
)
require.False(t,
shortPosition.GetIsLong(),
Expand All @@ -75,14 +68,8 @@ func TestAssetPosition_GetIsLong(t *testing.T) {
require.True(t,
longPosition.GetIsLong(),
)
require.PanicsWithError(t,
errorsmod.Wrapf(
types.ErrAssetPositionZeroQuantum,
"asset position (asset Id: 0) has zero quantum",
).Error(),
func() {
zeroPosition.GetIsLong()
},
require.False(t,
zeroPosition.GetIsLong(),
)
require.False(t,
shortPosition.GetIsLong(),
Expand Down

0 comments on commit cd9380f

Please sign in to comment.