Skip to content

Commit

Permalink
[refactor] Remove errors from subaccounts keeper functions that don't…
Browse files Browse the repository at this point in the history
… return errors (#678)
  • Loading branch information
lucas-dydx authored Oct 20, 2023
1 parent dcd6ecb commit 31281e2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 44 deletions.
16 changes: 4 additions & 12 deletions protocol/x/subaccounts/keeper/subaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,19 +283,13 @@ func (k Keeper) UpdateSubaccounts(
}

// Apply the updates to perpetual positions.
success, err = UpdatePerpetualPositions(
UpdatePerpetualPositions(
settledUpdates,
perpIdToFundingIndex,
)
if !success || err != nil {
return success, successPerUpdate, err
}

// Apply the updates to asset positions.
success, err = UpdateAssetPositions(settledUpdates)
if !success || err != nil {
return success, successPerUpdate, err
}
UpdateAssetPositions(settledUpdates)

// Apply all updates, including a subaccount update event in the Indexer block message
// per update and emit a cometbft event for each settled funding payment.
Expand Down Expand Up @@ -433,10 +427,8 @@ func (k Keeper) getSettledSubaccount(
// division result always rounds towards negative infinity.
totalNetSettlementPpm.Div(totalNetSettlementPpm, lib.BigIntOneMillion()),
)
err = newSubaccount.SetUsdcAssetPosition(newUsdcPosition)
if err != nil {
return types.Subaccount{}, nil, err
}
// TODO(CLOB-993): Remove this function and use `UpdateAssetPositions` instead.
newSubaccount.SetUsdcAssetPosition(newUsdcPosition)
return newSubaccount, fundingPayments, nil
}

Expand Down
8 changes: 0 additions & 8 deletions protocol/x/subaccounts/keeper/subaccount_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ func getUpdatedPerpetualPositions(
func UpdatePerpetualPositions(
settledUpdates []settledUpdate,
perpIdToFundingIndex map[uint32]dtypes.SerializableInt,
) (
success bool,
err error,
) {
// Apply the updates.
for i, u := range settledUpdates {
Expand Down Expand Up @@ -164,16 +161,12 @@ func UpdatePerpetualPositions(

settledUpdates[i].SettledSubaccount.PerpetualPositions = perpetualPositions
}
return true, nil
}

// For each settledUpdate in settledUpdates, updates its SettledSubaccount.AssetPositions
// to reflect settledUpdate.AssetUpdates.
func UpdateAssetPositions(
settledUpdates []settledUpdate,
) (
success bool,
err error,
) {
// Apply the updates.
for i, u := range settledUpdates {
Expand Down Expand Up @@ -226,5 +219,4 @@ func UpdateAssetPositions(

settledUpdates[i].SettledSubaccount.AssetPositions = assetPositions
}
return true, nil
}
33 changes: 16 additions & 17 deletions protocol/x/subaccounts/types/subaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,25 @@ func (m *Subaccount) GetUsdcPosition() *big.Int {
}

// SetUsdcAssetPosition sets the balance of the USDC asset position to `newUsdcPosition`.
// If the absolute value of `newUsdcPosition` cannot be represented in a uint64,
// an error is returned.
func (m *Subaccount) SetUsdcAssetPosition(newUsdcPosition *big.Int) error {
if m != nil {
usdcAssetPosition := m.getUsdcAssetPosition()
if newUsdcPosition == nil || newUsdcPosition.Sign() == 0 {
if usdcAssetPosition != nil {
m.AssetPositions = m.AssetPositions[1:]
}
} else {
if usdcAssetPosition == nil {
usdcAssetPosition = &AssetPosition{
AssetId: assettypes.AssetUsdc.Id,
}
m.AssetPositions = append([]*AssetPosition{usdcAssetPosition}, m.AssetPositions...)
func (m *Subaccount) SetUsdcAssetPosition(newUsdcPosition *big.Int) {
if m == nil {
return
}

usdcAssetPosition := m.getUsdcAssetPosition()
if newUsdcPosition == nil || newUsdcPosition.Sign() == 0 {
if usdcAssetPosition != nil {
m.AssetPositions = m.AssetPositions[1:]
}
} else {
if usdcAssetPosition == nil {
usdcAssetPosition = &AssetPosition{
AssetId: assettypes.AssetUsdc.Id,
}
usdcAssetPosition.Quantums = dtypes.NewIntFromBigInt(newUsdcPosition)
m.AssetPositions = append([]*AssetPosition{usdcAssetPosition}, m.AssetPositions...)
}
usdcAssetPosition.Quantums = dtypes.NewIntFromBigInt(newUsdcPosition)
}
return nil
}

func (m *Subaccount) getUsdcAssetPosition() *AssetPosition {
Expand Down
8 changes: 1 addition & 7 deletions protocol/x/subaccounts/types/subaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ func TestSetSubaccountQuoteBalance(t *testing.T) {
subaccount *types.Subaccount
newQuoteBalance *big.Int
expectedAssetPositions []*types.AssetPosition
expectedError error
}{
"can set nil subaccount": {
subaccount: nil,
Expand Down Expand Up @@ -311,12 +310,7 @@ func TestSetSubaccountQuoteBalance(t *testing.T) {
// Run tests.
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
err := tc.subaccount.SetUsdcAssetPosition(tc.newQuoteBalance)
if tc.expectedError == nil {
require.NoError(t, err)
} else {
require.ErrorIs(t, tc.expectedError, err)
}
tc.subaccount.SetUsdcAssetPosition(tc.newQuoteBalance)
if tc.subaccount != nil {
require.Equal(
t,
Expand Down

0 comments on commit 31281e2

Please sign in to comment.