keeper

package
v0.0.0-...-baf6593 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 21, 2023 License: AGPL-3.0 Imports: 31 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetSettledSubaccountWithPerpetuals

func GetSettledSubaccountWithPerpetuals(
	subaccount types.Subaccount,
	perpetuals map[uint32]perptypes.Perpetual,
) (
	settledSubaccount types.Subaccount,
	fundingPayments map[uint32]dtypes.SerializableInt,
	err error,
)

GetSettledSubaccountWithPerpetuals returns 1. a new settled subaccount given an unsettled subaccount, updating the USDC AssetPosition, FundingIndex, and LastFundingPayment fields accordingly (does not persist any changes) and 2. a map with perpetual ID as key and last funding payment as value (for emitting funding payments to indexer).

Note that this is a stateless utility function.

func IsValidStateTransitionForUndercollateralizedSubaccount

func IsValidStateTransitionForUndercollateralizedSubaccount(
	bigCurNetCollateral *big.Int,
	bigCurInitialMargin *big.Int,
	bigCurMaintenanceMargin *big.Int,
	bigNewNetCollateral *big.Int,
	bigNewMaintenanceMargin *big.Int,
) types.UpdateResult

IsValidStateTransitionForUndercollateralizedSubaccount returns an `UpdateResult` denoting whether this state transition is valid. This function accepts the collateral and margin requirements of a subaccount before and after an update ("cur" and "new", respectively).

This function should only be called if the account is undercollateralized after the update.

A state transition is valid if the subaccount enters a "less-or-equally-risky" state after an update. i.e.`newNetCollateral / newMaintenanceMargin >= curNetCollateral / curMaintenanceMargin`.

Otherwise, the state transition is invalid. If the account was previously undercollateralized, `types.StillUndercollateralized` is returned. If the account was previously collateralized and is now undercollateralized, `types.NewlyUndercollateralized` is returned.

Note that the inequality `newNetCollateral / newMaintenanceMargin >= curNetCollateral / curMaintenanceMargin` has divide-by-zero issue when margin requirements are zero. To make sure the state transition is valid, we special case this scenario and only allow state transition that improves net collateral.

func UpdateAssetPositions

func UpdateAssetPositions(
	settledUpdates []settledUpdate,
)

For each settledUpdate in settledUpdates, updates its SettledSubaccount.AssetPositions to reflect settledUpdate.AssetUpdates.

func UpdatePerpetualPositions

func UpdatePerpetualPositions(
	settledUpdates []settledUpdate,
	perpIdToFundingIndex map[uint32]dtypes.SerializableInt,
)

For each settledUpdate in settledUpdates, updates its SettledSubaccount.PerpetualPositions to reflect settledUpdate.PerpetualUpdates. For newly created positions, use `perpIdToFundingIndex` map to populate the `FundingIndex` field.

Types

type Keeper

type Keeper struct {
	// contains filtered or unexported fields
}

func NewKeeper

func NewKeeper(
	cdc codec.BinaryCodec,
	storeKey storetypes.StoreKey,
	assetsKeeper types.AssetsKeeper,
	bankKeeper types.BankKeeper,
	perpetualsKeeper types.PerpetualsKeeper,
	indexerEventManager indexer_manager.IndexerEventManager,
) *Keeper

func (Keeper) CanUpdateSubaccounts

func (k Keeper) CanUpdateSubaccounts(
	ctx sdk.Context,
	updates []types.Update,
) (
	success bool,
	successPerUpdate []types.UpdateResult,
	err error,
)

CanUpdateSubaccounts will validate all `updates` to the relevant subaccounts. The `updates` do not have to contain unique `SubaccountIds`. Each update is considered in isolation. Thus if two updates are provided with the same `SubaccountId`, they are validated without respect to each other.

Returns a `success` value of `true` if all updates are valid. Returns a `successPerUpdates` value, which is a slice of `UpdateResult`. These map to the updates and are used to indicate which of the updates caused a failure, if any.

func (Keeper) DepositFundsFromAccountToSubaccount

func (k Keeper) DepositFundsFromAccountToSubaccount(
	ctx sdk.Context,
	fromAccount sdk.AccAddress,
	toSubaccountId types.SubaccountId,
	assetId uint32,
	quantums *big.Int,
) error

DepositFundsFromAccountToSubaccount returns an error if the call to `k.CanUpdateSubaccounts()` fails. Otherwise, increases the asset quantums in the subaccount, translates the `assetId` and `quantums` into a `sdk.Coin`, and calls `bankKeeper.SendCoinsFromAccountToModule()`. TODO(CORE-168): Change function interface to accept `denom` and `amount` instead of `assetId` and `quantums`.

func (Keeper) ForEachSubaccount

func (k Keeper) ForEachSubaccount(ctx sdk.Context, callback func(types.Subaccount) (finished bool))

ForEachSubaccount performs a callback across all subaccounts. The callback function should return a boolean if we should end iteration or not. This is more performant than GetAllSubaccount because it does not fetch all at once. and you do not need to iterate through all the subaccounts.

func (Keeper) ForEachSubaccountRandomStart

func (k Keeper) ForEachSubaccountRandomStart(
	ctx sdk.Context,
	callback func(types.Subaccount) (finished bool),
	rand *rand.Rand,
)

ForEachSubaccountRandomStart performs a callback across all subaccounts. The callback function should return a boolean if we should end iteration or not. Note that this function starts at a random subaccount using the passed in `rand` and iterates from there. `rand` should be seeded for determinism if used in ways that affect consensus. TODO(CLOB-823): improve how random bytes are selected since bytes distribution might not be uniform.

func (Keeper) GetAllSubaccount

func (k Keeper) GetAllSubaccount(ctx sdk.Context) (list []types.Subaccount)

GetAllSubaccount returns all subaccount. For more performant searching and iteration, use `ForEachSubaccount`.

func (Keeper) GetIndexerEventManager

func (k Keeper) GetIndexerEventManager() indexer_manager.IndexerEventManager

func (Keeper) GetNegativeTncSubaccountSeenAtBlock

func (k Keeper) GetNegativeTncSubaccountSeenAtBlock(
	ctx sdk.Context,
) uint32

GetNegativeTncSubaccountSeenAtBlock gets the last block height a negative TNC subaccount was seen in state. Note this defaults to 0 if it has not been set.

func (Keeper) GetNetCollateralAndMarginRequirements

func (k Keeper) GetNetCollateralAndMarginRequirements(
	ctx sdk.Context,
	update types.Update,
) (
	bigNetCollateral *big.Int,
	bigInitialMargin *big.Int,
	bigMaintenanceMargin *big.Int,
	err error,
)

GetNetCollateralAndMarginRequirements returns the total net collateral, total initial margin requirement, and total maintenance margin requirement for the subaccount as if the `update` was applied. It is used to get information about speculative changes to the subaccount.

The provided update can also be "zeroed" in order to get information about the current state of the subaccount (i.e. with no changes).

If two position updates reference the same position, an error is returned.

All return values are denoted in quote quantums.

func (Keeper) GetRandomSubaccount

func (k Keeper) GetRandomSubaccount(ctx sdk.Context, rand *rand.Rand) (types.Subaccount, error)

GetRandomSubaccount returns a random subaccount. Will return an error if there are no subaccounts.

func (Keeper) GetSubaccount

func (k Keeper) GetSubaccount(
	ctx sdk.Context,
	id types.SubaccountId,
) (val types.Subaccount)

GetSubaccount returns a subaccount from its index.

Note that this function is getting called very frequently; metrics in this function should be sampled to reduce CPU time.

func (Keeper) InitializeForGenesis

func (k Keeper) InitializeForGenesis(ctx sdk.Context)

func (Keeper) Logger

func (k Keeper) Logger(ctx sdk.Context) log.Logger

func (Keeper) SetNegativeTncSubaccountSeenAtBlock

func (k Keeper) SetNegativeTncSubaccountSeenAtBlock(
	ctx sdk.Context,
	blockHeight uint32,
)

SetNegativeTncSubaccountSeenAtBlock sets a block number in state where a negative TNC subaccount was seen. This function will overwrite previous values at this key. This function will panic if the old block height is greater than the new block height.

func (Keeper) SetSubaccount

func (k Keeper) SetSubaccount(ctx sdk.Context, subaccount types.Subaccount)

SetSubaccount set a specific subaccount in the store from its index. Note that empty subaccounts are removed from state.

func (Keeper) TransferFeesToFeeCollectorModule

func (k Keeper) TransferFeesToFeeCollectorModule(
	ctx sdk.Context,
	assetId uint32,
	quantums *big.Int,
) error

TransferFeesToFeeCollectorModule translates the assetId and quantums into a sdk.Coin, and moves the funds from subaccounts module to the `fee_collector` module account by calling bankKeeper.SendCoinsFromModuleToModule(). Does not change any individual subaccount state.

func (Keeper) TransferInsuranceFundPayments

func (k Keeper) TransferInsuranceFundPayments(
	ctx sdk.Context,
	insuranceFundDelta *big.Int,
) error

TransferInsuranceFundPayments transfers funds in and out of the insurance fund to the subaccounts module by calling `bankKeeper.SendCoinsFromModuleToModule`. This function transfers funds

  • from the insurance fund to the subaccounts module when `insuranceFundDelta` is negative.
  • from the subaccounts module to the insurance fund when `insuranceFundDelta` is positive.
  • does nothing if `insuranceFundDelta` is zero.

If the sender account does not have enough balance for the transfer, an error is returned. Note this function does not change any individual subaccount state.

func (Keeper) UpdateSubaccounts

func (k Keeper) UpdateSubaccounts(
	ctx sdk.Context,
	updates []types.Update,
) (
	success bool,
	successPerUpdate []types.UpdateResult,
	err error,
)

UpdateSubaccounts validates and applies all `updates` to the relevant subaccounts as long as this is a valid state-transition for all subaccounts involved. All `updates` are made atomically, meaning that all state-changes will either succeed or all will fail.

Returns a boolean indicating whether the update was successfully applied or not. If `false`, then no updates to any subaccount were made. A second return value returns an array of `UpdateResult` which map to the `updates` to indicate which of the updates caused a failure, if any.

Each `SubaccountId` in the `updates` must be unique or an error is returned.

func (Keeper) WithdrawFundsFromSubaccountToAccount

func (k Keeper) WithdrawFundsFromSubaccountToAccount(
	ctx sdk.Context,
	fromSubaccountId types.SubaccountId,
	toAccount sdk.AccAddress,
	assetId uint32,
	quantums *big.Int,
) error

WithdrawFundsFromSubaccountToAccount returns an error if the call to `k.CanUpdateSubaccounts()` fails. Otherwise, deducts the asset quantums from the subaccount, translates the `assetId` and `quantums` into a `sdk.Coin`, and calls `bankKeeper.SendCoinsFromModuleToAccount()`.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL