Skip to content

Commit

Permalink
Update Margining Flows
Browse files Browse the repository at this point in the history
  • Loading branch information
BrendanChou committed Jun 24, 2024
1 parent 502d937 commit 766a887
Show file tree
Hide file tree
Showing 6 changed files with 335 additions and 264 deletions.
4 changes: 1 addition & 3 deletions protocol/daemons/liquidation/client/sub_task_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,7 @@ func (c *Client) CheckSubaccountCollateralization(
)

risk, err := salib.GetRiskForSubaccount(
satypes.SettledUpdate{
SettledSubaccount: settledSubaccount,
},
settledSubaccount,
perpInfos,
)

Expand Down
65 changes: 40 additions & 25 deletions protocol/x/subaccounts/keeper/subaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,19 @@ func (k Keeper) UpdateSubaccounts(
}
}

// Apply the updates to perpetual positions.
salib.UpdatePerpetualPositions(
settledUpdates,
perpInfos,
)

// Apply the updates to asset positions.
salib.UpdateAssetPositions(settledUpdates)
for _, sa := range settledUpdates {
// Apply the updates to asset positions.
salib.UpdateAssetPositions(
&sa.SettledSubaccount,
sa.AssetUpdates,
)
// Apply the updates to perpetual positions.
salib.UpdatePerpetualPositions(
&sa.SettledSubaccount,
sa.PerpetualUpdates,
perpInfos,
)
}

// Transfer collateral between collateral pools for any isolated perpetual positions that changed
// state due to an update.
Expand All @@ -349,21 +354,21 @@ func (k Keeper) UpdateSubaccounts(
// 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.
for _, u := range settledUpdates {
// Update the subaccount in state.
k.SetSubaccount(ctx, u.SettledSubaccount)

// Below access is safe because for all updated subaccounts' IDs, this map
// is populated as GetSettledSubaccountWithPerpetuals() is called in getSettledUpdates().
fundingPayments := subaccountIdToFundingPayments[*u.SettledSubaccount.Id]
updatedPositions := salib.GetUpdatedPerpetualPositions(u, fundingPayments)
k.GetIndexerEventManager().AddTxnEvent(
ctx,
indexerevents.SubtypeSubaccountUpdate,
indexerevents.SubaccountUpdateEventVersion,
indexer_manager.GetBytes(
indexerevents.NewSubaccountUpdateEvent(
u.SettledSubaccount.Id,
salib.GetUpdatedPerpetualPositions(
u,
fundingPayments,
),
updatedPositions,
salib.GetUpdatedAssetPositions(u),
fundingPayments,
),
Expand Down Expand Up @@ -602,8 +607,18 @@ func (k Keeper) internalCanUpdateSubaccounts(
}

// Get the new collateralization and margin requirements with the update applied.
updatedSubaccount := u.SettledSubaccount.DeepCopy()
salib.UpdateAssetPositions(
&updatedSubaccount,
u.AssetUpdates,
)
salib.UpdatePerpetualPositions(
&updatedSubaccount,
u.PerpetualUpdates,
perpInfos,
)
riskNew, err := salib.GetRiskForSubaccount(
u,
updatedSubaccount,
perpInfos,
)
if err != nil {
Expand All @@ -615,11 +630,6 @@ func (k Keeper) internalCanUpdateSubaccounts(
// The subaccount is not well-collateralized after the update.
// We must now check if the state transition is valid.
if !riskNew.IsInitialCollateralized() {
// Get the current collateralization and margin requirements without the update applied.
emptyUpdate := types.SettledUpdate{
SettledSubaccount: u.SettledSubaccount,
}

bytes, err := proto.Marshal(u.SettledSubaccount.Id)
if err != nil {
return false, nil, err
Expand All @@ -629,7 +639,7 @@ func (k Keeper) internalCanUpdateSubaccounts(
// Cache the current collateralization and margin requirements for the subaccount.
if _, ok := riskCurMap[saKey]; !ok {
riskCurMap[saKey], err = salib.GetRiskForSubaccount(
emptyUpdate,
u.SettledSubaccount,
perpInfos,
)
if err != nil {
Expand Down Expand Up @@ -680,14 +690,19 @@ func (k Keeper) GetNetCollateralAndMarginRequirements(
}
settledSubaccount, _ := salib.GetSettledSubaccountWithPerpetuals(subaccount, perpInfos)

settledUpdate := types.SettledUpdate{
SettledSubaccount: settledSubaccount,
AssetUpdates: update.AssetUpdates,
PerpetualUpdates: update.PerpetualUpdates,
}
updatedSubaccount := settledSubaccount.DeepCopy()
salib.UpdateAssetPositions(
&updatedSubaccount,
update.AssetUpdates,
)
salib.UpdatePerpetualPositions(
&updatedSubaccount,
update.PerpetualUpdates,
perpInfos,
)

return salib.GetRiskForSubaccount(
settledUpdate,
updatedSubaccount,
perpInfos,
)
}
Expand Down
Loading

0 comments on commit 766a887

Please sign in to comment.