Skip to content

Commit

Permalink
Address nits.
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentwschau committed Mar 21, 2024
1 parent 039290b commit 72e2082
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
39 changes: 34 additions & 5 deletions protocol/x/subaccounts/keeper/isolated_subaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ func (k *Keeper) transferCollateralForIsolatedPerpetual(
return nil
}

// If there are zero quantums to transfer, don't transfer collateral.
if stateTransition.QuoteQuantums.Sign() == 0 {
return nil
}

isolatedCollateralPoolAddr, err := k.GetCollateralPoolFromPerpetualId(ctx, stateTransition.PerpetualId)
if err != nil {
return err
Expand Down Expand Up @@ -276,11 +281,6 @@ func (k *Keeper) transferCollateralForIsolatedPerpetual(
)
}

// If there are zero quantums to transfer, don't transfer collateral.
if stateTransition.QuoteQuantums.Sign() == 0 {
return nil
}

// Invalid to transfer negative quantums. This should already be caught by collateralization
// checks as well.
if stateTransition.QuoteQuantums.Sign() == -1 {
Expand Down Expand Up @@ -317,6 +317,35 @@ func (k *Keeper) transferCollateralForIsolatedPerpetual(
return nil
}

// computeAndExecuteCollateralTransfer computes collateral transfers resulting from updates to
// a subaccount and executes the collateral transfer using `x/bank`.`
// The input `settledUpdate` must have an updated subaccount (`settledUpdate.SettledSubaccount`),
// so all the updates must have been applied already to the subaccount.
// Note: This uses the `x/bank` keeper and modifies `x/bank` state.
func (k *Keeper) computeAndExecuteCollateralTransfer(
ctx sdk.Context,
settledUpdateWithUpdatedSubaccount SettledUpdate,
perpetuals []perptypes.Perpetual,
) error {
// The subaccount in `settledUpdateWithUpdatedSubaccount` already has the perpetual updates
// and asset updates applied to it.
stateTransition, err := GetIsolatedPerpetualStateTransition(
settledUpdateWithUpdatedSubaccount,
perpetuals,
)
if err != nil {
return err
}
if err := k.transferCollateralForIsolatedPerpetual(
ctx,
stateTransition,
); err != nil {
return err
}

return nil
}

func getPerpIdToMarketTypeMap(
perpetuals []perptypes.Perpetual,
) map[uint32]perptypes.PerpetualMarketType {
Expand Down
14 changes: 4 additions & 10 deletions protocol/x/subaccounts/keeper/subaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,18 +313,12 @@ func (k Keeper) UpdateSubaccounts(
// Transfer collateral between collateral pools for any isolated perpetual positions that changed
// state due to an update.
for _, settledUpdateWithUpdatedSubaccount := range settledUpdates {
// The subaccount in `settledUpdateWithUpdatedSubaccount` already has the perpetual updates
// and asset updates applied to it.
stateTransition, err := GetIsolatedPerpetualStateTransition(
if err := k.computeAndExecuteCollateralTransfer(
ctx,
// The subaccount in `settledUpdateWithUpdatedSubaccount` already has the perpetual updates
// and asset updates applied to it.
settledUpdateWithUpdatedSubaccount,
allPerps,
)
if err != nil {
return false, nil, err
}
if err := k.transferCollateralForIsolatedPerpetual(
ctx,
stateTransition,
); err != nil {
return false, nil, err
}
Expand Down

0 comments on commit 72e2082

Please sign in to comment.