Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
BrendanChou committed Jun 14, 2024
1 parent 519f396 commit e578f94
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 35 deletions.
10 changes: 5 additions & 5 deletions protocol/daemons/liquidation/client/sub_task_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (c *Client) FetchApplicationStateAtBlockHeight(
liqFlags flags.LiquidationFlags,
) (
subaccounts []satypes.Subaccount,
perpInfos map[uint32]satypes.PerpInfo,
perpInfos map[uint32]perptypes.PerpInfo,
err error,
) {
defer telemetry.ModuleMeasureSince(
Expand Down Expand Up @@ -150,7 +150,7 @@ func (c *Client) FetchApplicationStateAtBlockHeight(
return l.Id
})

perpInfos = make(map[uint32]satypes.PerpInfo, len(perpetuals))
perpInfos = make(map[uint32]perptypes.PerpInfo, len(perpetuals))
for _, perp := range perpetuals {
price, ok := marketPricesMap[perp.Params.MarketId]
if !ok {
Expand All @@ -168,7 +168,7 @@ func (c *Client) FetchApplicationStateAtBlockHeight(
perp.Params.LiquidityTier,
)
}
perpInfos[perp.Params.Id] = satypes.PerpInfo{
perpInfos[perp.Params.Id] = perptypes.PerpInfo{
Perpetual: perp,
Price: price,
LiquidityTier: liquidityTier,
Expand All @@ -182,7 +182,7 @@ func (c *Client) FetchApplicationStateAtBlockHeight(
// at least one open position and returns a list of unique and potentially liquidatable subaccount ids.
func (c *Client) GetLiquidatableSubaccountIds(
subaccounts []satypes.Subaccount,
perpInfos map[uint32]satypes.PerpInfo,
perpInfos map[uint32]perptypes.PerpInfo,
) (
liquidatableSubaccountIds []satypes.SubaccountId,
negativeTncSubaccountIds []satypes.SubaccountId,
Expand Down Expand Up @@ -290,7 +290,7 @@ func (c *Client) GetSubaccountOpenPositionInfo(
// is not yet implemented.
func (c *Client) CheckSubaccountCollateralization(
unsettledSubaccount satypes.Subaccount,
perpInfos map[uint32]satypes.PerpInfo,
perpInfos map[uint32]perptypes.PerpInfo,
) (
isLiquidatable bool,
hasNegativeTnc bool,
Expand Down
8 changes: 4 additions & 4 deletions protocol/x/subaccounts/keeper/isolated_subaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
func (k Keeper) checkIsolatedSubaccountConstraints(
ctx sdk.Context,
settledUpdates []SettledUpdate,
perpInfos map[uint32]types.PerpInfo,
perpInfos map[uint32]perptypes.PerpInfo,
) (
success bool,
successPerUpdate []types.UpdateResult,
Expand Down Expand Up @@ -60,7 +60,7 @@ func (k Keeper) checkIsolatedSubaccountConstraints(
// perpetuals or a combination of isolated and non-isolated perpetuals
func isValidIsolatedPerpetualUpdates(
settledUpdate SettledUpdate,
perpInfos map[uint32]types.PerpInfo,
perpInfos map[uint32]perptypes.PerpInfo,
) (types.UpdateResult, error) {
// If there are no perpetual updates, then this update does not violate constraints for isolated
// markets.
Expand Down Expand Up @@ -142,7 +142,7 @@ func isValidIsolatedPerpetualUpdates(
// so all the updates must have been applied already to the subaccount.
func GetIsolatedPerpetualStateTransition(
settledUpdateWithUpdatedSubaccount SettledUpdate,
perpInfos map[uint32]types.PerpInfo,
perpInfos map[uint32]perptypes.PerpInfo,
) (*types.IsolatedPerpetualPositionStateTransition, error) {
// This subaccount needs to have had the updates in the `settledUpdate` already applied to it.
updatedSubaccount := settledUpdateWithUpdatedSubaccount.SettledSubaccount
Expand Down Expand Up @@ -318,7 +318,7 @@ func (k *Keeper) transferCollateralForIsolatedPerpetual(
func (k *Keeper) computeAndExecuteCollateralTransfer(
ctx sdk.Context,
settledUpdateWithUpdatedSubaccount SettledUpdate,
perpInfos map[uint32]types.PerpInfo,
perpInfos map[uint32]perptypes.PerpInfo,
) error {
// The subaccount in `settledUpdateWithUpdatedSubaccount` already has the perpetual updates
// and asset updates applied to it.
Expand Down
4 changes: 2 additions & 2 deletions protocol/x/subaccounts/keeper/isolated_subaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,9 @@ func TestGetIsolatedPerpetualStateTransition(t *testing.T) {
for name, tc := range tests {
t.Run(
name, func(t *testing.T) {
perpInfos := make(map[uint32]types.PerpInfo, len(tc.perpetuals))
perpInfos := make(map[uint32]perptypes.PerpInfo, len(tc.perpetuals))
for _, perp := range tc.perpetuals {
perpInfos[perp.Params.Id] = types.PerpInfo{
perpInfos[perp.Params.Id] = perptypes.PerpInfo{
Perpetual: perp,
}
}
Expand Down
17 changes: 8 additions & 9 deletions protocol/x/subaccounts/keeper/subaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (k Keeper) getRandomBytes(ctx sdk.Context, rand *rand.Rand) ([]byte, error)
func (k Keeper) getSettledUpdates(
ctx sdk.Context,
updates []types.Update,
perpInfos map[uint32]types.PerpInfo,
perpInfos map[uint32]perptypes.PerpInfo,
requireUniqueSubaccount bool,
) (
settledUpdates []SettledUpdate,
Expand Down Expand Up @@ -443,7 +443,7 @@ func (k Keeper) CanUpdateSubaccounts(
// Note that this is a stateless utility function.
func GetSettledSubaccountWithPerpetuals(
subaccount types.Subaccount,
perpInfos map[uint32]types.PerpInfo,
perpInfos map[uint32]perptypes.PerpInfo,
) (
settledSubaccount types.Subaccount,
fundingPayments map[uint32]dtypes.SerializableInt,
Expand Down Expand Up @@ -550,7 +550,7 @@ func (k Keeper) internalCanUpdateSubaccounts(
ctx sdk.Context,
settledUpdates []SettledUpdate,
updateType types.UpdateType,
perpInfos map[uint32]types.PerpInfo,
perpInfos map[uint32]perptypes.PerpInfo,
) (
success bool,
successPerUpdate []types.UpdateResult,
Expand Down Expand Up @@ -642,13 +642,12 @@ func (k Keeper) internalCanUpdateSubaccounts(

// Temporily apply open interest delta to perpetuals, so IMF is calculated based on open interest after the update.
// `perpOpenInterestDeltas` is only present for `Match` update type.
existingValue := new(big.Int)
if perpOpenInterestDelta != nil {
perpInfo, ok := perpInfos[perpOpenInterestDelta.PerpetualId]
if !ok {
return false, nil, errorsmod.Wrapf(types.ErrPerpetualInfoDoesNotExist, "%d", perpOpenInterestDelta.PerpetualId)
}
existingValue.SetUint64(0)
existingValue := big.NewInt(0)
if !perpInfo.Perpetual.OpenInterest.IsNil() {
existingValue.Set(perpInfo.Perpetual.OpenInterest.BigInt())
}
Expand Down Expand Up @@ -878,7 +877,7 @@ func (k Keeper) GetNetCollateralAndMarginRequirements(
func (k Keeper) internalGetNetCollateralAndMarginRequirements(
ctx sdk.Context,
settledUpdate SettledUpdate,
perpInfos map[uint32]types.PerpInfo,
perpInfos map[uint32]perptypes.PerpInfo,
) (
bigNetCollateral *big.Int,
bigInitialMargin *big.Int,
Expand Down Expand Up @@ -1025,7 +1024,7 @@ func (k Keeper) GetAllRelevantPerpetuals(
ctx sdk.Context,
updates []types.Update,
) (
map[uint32]types.PerpInfo,
map[uint32]perptypes.PerpInfo,
error,
) {
subaccountIds := make(map[types.SubaccountId]struct{})
Expand All @@ -1049,7 +1048,7 @@ func (k Keeper) GetAllRelevantPerpetuals(
}

// Get all perpetual information from state.
perpetuals := make(map[uint32]types.PerpInfo, len(perpIds))
perpetuals := make(map[uint32]perptypes.PerpInfo, len(perpIds))
for perpId := range perpIds {
perpetual,
price,
Expand All @@ -1058,7 +1057,7 @@ func (k Keeper) GetAllRelevantPerpetuals(
if err != nil {
return nil, err
}
perpetuals[perpId] = types.PerpInfo{
perpetuals[perpId] = perptypes.PerpInfo{
Perpetual: perpetual,
Price: price,
LiquidityTier: liquidityTier,
Expand Down
7 changes: 4 additions & 3 deletions protocol/x/subaccounts/keeper/subaccount_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
errorsmod "cosmossdk.io/errors"

"github.com/dydxprotocol/v4-chain/protocol/dtypes"
perptypes "github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/types"
"github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"
)

Expand Down Expand Up @@ -104,7 +105,7 @@ func getUpdatedPerpetualPositions(
// For newly created positions, use `perpIdToFundingIndex` map to populate the `FundingIndex` field.
func UpdatePerpetualPositions(
settledUpdates []SettledUpdate,
perpInfos map[uint32]types.PerpInfo,
perpInfos map[uint32]perptypes.PerpInfo,
) {
// Apply the updates.
for i, u := range settledUpdates {
Expand Down Expand Up @@ -134,8 +135,8 @@ func UpdatePerpetualPositions(
// Create the new position.
perpInfo, exists := perpInfos[pu.PerpetualId]
if !exists {
// Invariant: `perpInfos` contains all existing perpetauls,
// and perpetual position update must refer to an existing perpetual.
// Invariant: `perpInfos` should all relevant perpetuals, which includes all
// perpetuals that are updated.
panic(errorsmod.Wrapf(types.ErrPerpetualInfoDoesNotExist, "%d", pu.PerpetualId))
}
perpetualPosition := &types.PerpetualPosition{
Expand Down
12 changes: 0 additions & 12 deletions protocol/x/subaccounts/types/perpinfo.go

This file was deleted.

0 comments on commit e578f94

Please sign in to comment.