Skip to content

Commit

Permalink
Fix comment.
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentwschau committed Mar 12, 2024
1 parent f4a7413 commit 733c4c3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
11 changes: 2 additions & 9 deletions protocol/x/subaccounts/keeper/isolated_subaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

// checkIsolatedSubaccountConstaints will validate all `updates` to the relevant subaccounts against
// isolated subaccount constraints.
// This function checks each update in isolation, so if multiple updates for the same subaccount id
// are passed in, they are evaluated together
// The input subaccounts must be settled.
//
// Returns a `success` value of `true` if all updates are valid.
Expand All @@ -22,28 +24,20 @@ func (k Keeper) checkIsolatedSubaccountConstraints(
ctx sdk.Context,
settledUpdates []settledUpdate,
perpetuals []perptypes.Perpetual,
uniqueSubaccounts bool,
) (
success bool,
successPerUpdate []types.UpdateResult,
err error,
) {
success = true
successPerUpdate = make([]types.UpdateResult, len(settledUpdates))
var idOfSettledUpdates = make(map[types.SubaccountId]struct{})
var perpIdToMarketType = make(map[uint32]perptypes.PerpetualMarketType)

for _, perpetual := range perpetuals {
perpIdToMarketType[perpetual.GetId()] = perpetual.Params.MarketType
}

for i, u := range settledUpdates {
if uniqueSubaccounts {
if _, exists := idOfSettledUpdates[*u.SettledSubaccount.Id]; exists {
return false, nil, types.ErrNonUniqueUpdatesSubaccount
}
}

result, err := isValidIsolatedPerpetualUpdates(u, perpIdToMarketType)
if err != nil {
return false, nil, err
Expand All @@ -53,7 +47,6 @@ func (k Keeper) checkIsolatedSubaccountConstraints(
}

successPerUpdate[i] = result
idOfSettledUpdates[*u.SettledSubaccount.Id] = struct{}{}
}

return success, successPerUpdate, nil
Expand Down
38 changes: 18 additions & 20 deletions protocol/x/subaccounts/keeper/subaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,23 +280,17 @@ func (k Keeper) UpdateSubaccounts(
return false, nil, err
}

// Check if the updates satisfy the isolated perpetual constraints.
allPerps := k.perpetualsKeeper.GetAllPerpetuals(ctx)
success, successPerUpdate, err = k.checkIsolatedSubaccountConstraints(
success, successPerUpdate, err = k.internalCanUpdateSubaccounts(
ctx,
settledUpdates,
updateType,
allPerps,
true, // uniqueSubaccounts
)
if !success || err != nil {
return success, successPerUpdate, err
}

success, successPerUpdate, err = k.internalCanUpdateSubaccounts(ctx, settledUpdates, updateType)
if !success || err != nil {
return success, successPerUpdate, err
}

// Get a mapping from perpetual Id to current perpetual funding index.
perpIdToFundingIndex := make(map[uint32]dtypes.SerializableInt)
for _, perp := range allPerps {
Expand Down Expand Up @@ -389,19 +383,8 @@ func (k Keeper) CanUpdateSubaccounts(
return false, nil, err
}

// Check if the updates satisfy the isolated perpetual constraints.
allPerps := k.perpetualsKeeper.GetAllPerpetuals(ctx)
success, successPerUpdate, err = k.checkIsolatedSubaccountConstraints(
ctx,
settledUpdates,
allPerps,
false, // uniqueSubaccounts
)
if !success || err != nil {
return success, successPerUpdate, err
}

return k.internalCanUpdateSubaccounts(ctx, settledUpdates, updateType)
return k.internalCanUpdateSubaccounts(ctx, settledUpdates, updateType, allPerps)
}

// getSettledSubaccount returns 1. a new settled subaccount given an unsettled subaccount,
Expand Down Expand Up @@ -534,6 +517,7 @@ func checkPositionUpdatable(

// internalCanUpdateSubaccounts will validate all `updates` to the relevant subaccounts.
// The `updates` do not have to contain `Subaccounts` with unique `SubaccountIds`.
// The `updates` do not have to contain `Subaccounts` with unique `SubaccountIds`.
// Each update is considered in isolation. Thus if two updates are provided
// with the same `Subaccount`, they are validated without respect to each
// other.
Expand All @@ -547,6 +531,7 @@ func (k Keeper) internalCanUpdateSubaccounts(
ctx sdk.Context,
settledUpdates []settledUpdate,
updateType types.UpdateType,
perpetuals []perptypes.Perpetual,
) (
success bool,
successPerUpdate []types.UpdateResult,
Expand All @@ -555,6 +540,19 @@ func (k Keeper) internalCanUpdateSubaccounts(
success = true
successPerUpdate = make([]types.UpdateResult, len(settledUpdates))

// Check if the updates satisfy the isolated perpetual constraints.
success, successPerUpdate, err = k.checkIsolatedSubaccountConstraints(
ctx,
settledUpdates,
perpetuals,
)
if err != nil {
return false, nil, err
}
if !success {
return success, successPerUpdate, nil
}

// Block all withdrawals and transfers if either of the following is true within the last
// `WITHDRAWAL_AND_TRANSFERS_BLOCKED_AFTER_NEGATIVE_TNC_SUBACCOUNT_SEEN_BLOCKS`:
// - There was a negative TNC subaccount seen.
Expand Down

0 comments on commit 733c4c3

Please sign in to comment.