Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove panic when getting quantums from zero position #1964

Merged
merged 4 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading