keeper

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2025 License: AGPL-3.0 Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Deprecated: For use by the v7.x upgrade handler
	// QuotingParamsKeyPrefix is the prefix to retrieve all QuotingParams.
	// QuotingParams store: vaultId VaultId -> QuotingParams.
	QuotingParamsKeyPrefix = "QuotingParams:"

	// Deprecated: For use by the v7.x upgrade handler
	// TotalSharesKeyPrefix is the prefix to retrieve all TotalShares.
	TotalSharesKeyPrefix = "TotalShares:"

	// Deprecated: For use by the v7.x upgrade handler
	// OwnerSharesKeyPrefix is the prefix to retrieve all OwnerShares.
	// OwnerShares store: vaultId VaultId -> owner string -> shares NumShares.
	OwnerSharesKeyPrefix = "OwnerShares:"
)

Deprecated state keys

Variables

This section is empty.

Functions

func NewMsgServerImpl

func NewMsgServerImpl(keeper Keeper) types.MsgServer

NewMsgServerImpl returns an implementation of the MsgServer interface for the provided Keeper.

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,
	clobKeeper types.ClobKeeper,
	delayMsgKeeper types.DelayMsgKeeper,
	perpetualsKeeper types.PerpetualsKeeper,
	pricesKeeper types.PricesKeeper,
	sendingKeeper types.SendingKeeper,
	subaccountsKeeper types.SubaccountsKeeper,
	indexerEventManager indexer_manager.IndexerEventManager,
	authorities []string,
) *Keeper

func (Keeper) AddVaultToAddressStore

func (k Keeper) AddVaultToAddressStore(
	ctx sdk.Context,
	vaultId types.VaultId,
)

AddVaultToAddressStore adds a vault's address to the vault address store.

func (Keeper) AllVaults

func (Keeper) AllocateToVault

func (k Keeper) AllocateToVault(
	ctx sdk.Context,
	vaultId types.VaultId,
	quantums *big.Int,
) error

AllocateToVault transfers funds from main vault to a specified vault.

func (Keeper) CancelVaultClobOrder

func (k Keeper) CancelVaultClobOrder(
	ctx sdk.Context,
	vaultId types.VaultId,
	orderId *clobtypes.OrderId,
	orderExpirationSeconds uint32,
) error

CancelVaultClobOrder cancels a vault CLOB order.

func (Keeper) DecommissionNonPositiveEquityVaults

func (k Keeper) DecommissionNonPositiveEquityVaults(
	ctx sdk.Context,
)

DecommissionVaults decommissions all deactivated vaults that have non-positive equities.

func (Keeper) DecommissionVault

func (k Keeper) DecommissionVault(
	ctx sdk.Context,
	vaultId types.VaultId,
)

DecommissionVault decommissions a vault by deleting it from vault address store and vault params store.

func (Keeper) DepositToMegavault

func (k Keeper) DepositToMegavault(
	ctx sdk.Context,
	fromSubaccount satypes.SubaccountId,
	quoteQuantums *big.Int,
) (mintedShares *big.Int, err error)

DepositToMegavault deposits from a subaccount to megavault by 1. Minting shares for owner address of `fromSubaccount`. 2. Transferring `quoteQuantums` from `fromSubaccount` to megavault subaccount 0.

func (Keeper) GetAllOwnerShareUnlocks

func (k Keeper) GetAllOwnerShareUnlocks(ctx sdk.Context) []types.OwnerShareUnlocks

GetAllOwnerShareUnlocks gets all `OwnerShareUnlocks`.

func (Keeper) GetAllOwnerShares

func (k Keeper) GetAllOwnerShares(ctx sdk.Context) []types.OwnerShare

GetAllOwnerShares gets all owner shares.

func (Keeper) GetAllVaults

func (k Keeper) GetAllVaults(ctx sdk.Context) []types.Vault

GetAllVaults returns all vaults with their vault params and most recent client IDs. Note: This function is only used for exporting module state.

func (Keeper) GetDefaultQuotingParams

func (k Keeper) GetDefaultQuotingParams(
	ctx sdk.Context,
) (
	params types.QuotingParams,
)

GetDefaultQuotingParams returns `DefaultQuotingParams` in state.

func (Keeper) GetIndexerEventManager

func (k Keeper) GetIndexerEventManager() indexer_manager.IndexerEventManager

func (Keeper) GetMegavaultEquity

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

GetMegavaultEquity returns the equity of the megavault (in quote quantums), which consists of - equity of the megavault main subaccount - equity of all vaults (if not-deactivated and positive)

func (Keeper) GetMostRecentClientIds

func (k Keeper) GetMostRecentClientIds(
	ctx sdk.Context,
	vaultId types.VaultId,
) []uint32

GetMostRecentClientIds returns the most recent client IDs for a vault.

func (Keeper) GetOperatorParams

func (k Keeper) GetOperatorParams(
	ctx sdk.Context,
) (
	params types.OperatorParams,
)

GetOperatorParams returns `OperatorParams` in state.

func (Keeper) GetOwnerShareUnlocks

func (k Keeper) GetOwnerShareUnlocks(
	ctx sdk.Context,
	owner string,
) (val types.OwnerShareUnlocks, exists bool)

GetOwnerShareUnlocks gets share unlocks for an owner.

func (Keeper) GetOwnerShares

func (k Keeper) GetOwnerShares(
	ctx sdk.Context,
	owner string,
) (val types.NumShares, exists bool)

GetOwnerShares gets owner shares for an owner.

func (Keeper) GetSubaccountEquity

func (k Keeper) GetSubaccountEquity(
	ctx sdk.Context,
	subaccountId satypes.SubaccountId,
) (*big.Int, error)

GetSubaccountEquity returns the equity of a subaccount (in quote quantums).

func (Keeper) GetTotalShares

func (k Keeper) GetTotalShares(
	ctx sdk.Context,
) (
	totalShares types.NumShares,
)

GetTotalShares gets total shares.

func (Keeper) GetVaultAndQuotingParams

func (k Keeper) GetVaultAndQuotingParams(
	ctx sdk.Context,
	vaultId types.VaultId,
) (
	vaultParams types.VaultParams,
	quotingParams types.QuotingParams,
	exists bool,
)

GetVaultAndQuotingParams returns vault params and quoting parameters for a given vault. Quoting parameters is - `VaultParams.QuotingParams` if set - `DefaultQuotingParams` otherwise `exists` is false if `VaultParams` does not exist for the given vault.

func (Keeper) GetVaultClobOrderIds

func (k Keeper) GetVaultClobOrderIds(
	ctx sdk.Context,
	vaultId types.VaultId,
) (orderIds []*clobtypes.OrderId)

GetVaultClobOrderIds returns a list of order IDs for a given CLOB vault. Let n be number of layers, then the function returns order IDs [a_0, b_0, a_1, b_1, ..., a_{n-1}, b_{n-1}] where a_i and b_i are respectively ask and bid order IDs at the i-th layer.

func (Keeper) GetVaultClobOrders

func (k Keeper) GetVaultClobOrders(
	ctx sdk.Context,
	vaultId types.VaultId,
) (orders []*clobtypes.Order, err error)

GetVaultClobOrders returns a list of long term orders for a given CLOB vault. Let n be number of layers, then the function returns orders at [a_0, b_0, a_1, b_1, ..., a_{n-1}, b_{n-1}] where a_i and b_i are the ask price and bid price at i-th layer. To compute a_i and b_i: - a_i = oraclePrice * (1 + ask_spread_i) - b_i = oraclePrice * (1 - bid_spread_i)

- ask_spread_i = (1 + skew_i) * spread - bid_spread_i = (1 - skew_i) * spread

- skew_i is computed differently based on order side and leverage_i:

  • ask, leverage_i < 0: (skew_factor * leverage_i - 1)^2 - 1
  • bid, leverage_i < 0: -skew_factor * leverage_i
  • ask, leverage_i >= 0: -skew_factor * leverage_i
  • bid, leverage_i >= 0: -((skew_factor * leverage_i + 1)^2 - 1)

- leverage_i = leverage +/- i * order_size_pct\ (- for ask and + for bid) - leverage = open notional / equity

- spread = max(spread_min, spread_buffer + min_price_change)

size of each order is calculated as `order_size_pct * equity / oraclePrice`.

func (Keeper) GetVaultClobPerpAndMarket

func (k Keeper) GetVaultClobPerpAndMarket(
	ctx sdk.Context,
	vaultId types.VaultId,
) (
	clobPair clobtypes.ClobPair,
	perpetual perptypes.Perpetual,
	marketParam pricestypes.MarketParam,
	marketPrice pricestypes.MarketPrice,
	err error,
)

GetVaultClobPerpAndMarket returns the clob pair, perpetual, market param, and market price that correspond to a vault.

func (Keeper) GetVaultEquity

func (k Keeper) GetVaultEquity(
	ctx sdk.Context,
	vaultId types.VaultId,
) (*big.Int, error)

GetVaultEquity returns the equity of a vault (in quote quantums).

func (Keeper) GetVaultInventoryInPerpetual

func (k Keeper) GetVaultInventoryInPerpetual(
	ctx sdk.Context,
	vaultId types.VaultId,
	perpId uint32,
) *big.Int

GetVaultInventory returns the inventory of a vault in a given perpeutal (in base quantums).

func (Keeper) GetVaultLeverageAndEquity

func (k Keeper) GetVaultLeverageAndEquity(
	ctx sdk.Context,
	vaultId types.VaultId,
	perpetual *perptypes.Perpetual,
	marketPrice *pricestypes.MarketPrice,
) (
	leverage *big.Rat,
	equity *big.Int,
	err error,
)

GetVaultLeverageAndEquity returns a vault's leverage and equity. - leverage = open notional / equity. Note that an error is returned if equity is non-positive.

func (Keeper) GetVaultParams

func (k Keeper) GetVaultParams(
	ctx sdk.Context,
	vaultId types.VaultId,
) (
	vaultParams types.VaultParams,
	exists bool,
)

GetVaultParams returns `VaultParams` in state for a given vault.

func (Keeper) GetVaultWithdrawalSlippage

func (k Keeper) GetVaultWithdrawalSlippage(
	ctx sdk.Context,
	vaultId types.VaultId,
	sharesToWithdraw *big.Int,
	totalShares *big.Int,
	leverage *big.Rat,
	perpetual *perptypes.Perpetual,
	marketParam *pricestypes.MarketParam,
) (*big.Rat, error)

GetVaultWithdrawalSlippage returns the slippage that should be incurred from the specified vault on withdrawing `sharesToWithdraw` shares. For example, if `sharesToWithdraw = 100` and `0.2` is returned, it means that withdrawing 100 shares has a 20% slippage for the given `vaultId`.

Slippage is calculated as `min(simple_slippage, estimated_slippage)` where: - simple_slippage = leverage * initial_margin - estimated_slippage = spread * (1 + average_skew) * leverage

  • average_skew = integral / (posterior_leverage - leverage)
  • integral = skew_antiderivative(skew_factor, posterior_leverage) - skew_antiderivative(skew_factor, leverage)
  • posterior_leverage = leverage / (1 - withdrawal_portion) = leverage / (1 - shares_to_withdraw / total_shares) = leverage * total_shares / (total_shares - shares_to_withdraw)

To simplify above formula, let l = leverage, n = total_shares, m = shares_to_withdraw

estimated_slippage
= spread * (1 + integral / (posterior_leverage - leverage)) * leverage
= spread * (1 + integral * (n - m) / (ln - l(n - m))) * l
= spread * (1 + integral * (n - m) / lm) * l
= spread * (l + integral * (n - m) / m)

func (Keeper) HasAuthority

func (k Keeper) HasAuthority(authority string) bool

func (Keeper) InitializeForGenesis

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

func (Keeper) IsVault

func (k Keeper) IsVault(
	ctx sdk.Context,
	address string,
) bool

IsVault checks if a given address is the address of an existing vault.

func (Keeper) LockShares

func (k Keeper) LockShares(
	ctx sdk.Context,
	ownerAddress string,
	sharesToLock types.NumShares,
	tilBlock uint32,
) error

LockShares locks `sharesToLock` for `ownerAddress` until height `tilBlock`. Note: cannot lock more than the total number of shares that owner has.

func (Keeper) Logger

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

func (Keeper) MintShares

func (k Keeper) MintShares(
	ctx sdk.Context,
	owner string,
	quantumsToDeposit *big.Int,
) (mintedShares *big.Int, err error)

MintShares mints shares for `owner` based on `quantumsToDeposit` by: 1. Increasing total shares. 2. Increasing owner shares for given `owner`.

func (Keeper) Params

func (Keeper) PlaceVaultClobOrder

func (k Keeper) PlaceVaultClobOrder(
	ctx sdk.Context,
	vaultId types.VaultId,
	order *clobtypes.Order,
) error

PlaceVaultClobOrder places a vault CLOB order internal to the protocol, skipping various logs, metrics, and validations

func (Keeper) RedeemFromMainAndSubVaults

func (k Keeper) RedeemFromMainAndSubVaults(
	ctx sdk.Context,
	shares *big.Int,
	simulate bool,
) (
	redeemedQuoteQuantums *big.Int,
	megavaultEquity *big.Int,
	totalShares *big.Int,
	err error,
)

RedeemFromMainAndSubVaults redeems `shares` number of shares from main and sub vaults. If and only if `simulate` is false, logs are enabled and quote quantums redeemed from each sub vault are transferred to the main vault.

func (Keeper) RefreshAllVaultOrders

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

RefreshAllVaultOrders refreshes all orders for all vaults by 1. Cancelling all existing orders. 2. Placing new orders.

func (Keeper) RefreshVaultClobOrders

func (k Keeper) RefreshVaultClobOrders(ctx sdk.Context, vaultId types.VaultId) (err error)

RefreshVaultClobOrders refreshes orders of a CLOB vault. Note: Client IDs are deterministically constructed based on layer and side. A client ID has its last bit flipped only upon order replacement.

func (Keeper) ReplaceVaultClobOrder

func (k Keeper) ReplaceVaultClobOrder(
	ctx sdk.Context,
	vaultId types.VaultId,
	oldOrderId *clobtypes.OrderId,
	newOrder *clobtypes.Order,
) error

ReplaceVaultClobOrder replaces a vault CLOB order internal to the protocol and emits order replacement indexer event.

func (Keeper) SetDefaultQuotingParams

func (k Keeper) SetDefaultQuotingParams(
	ctx sdk.Context,
	params types.QuotingParams,
) error

SetDefaultQuotingParams updates `DefaultQuotingParams` in state. Returns an error if validation fails.

func (Keeper) SetMostRecentClientIds

func (k Keeper) SetMostRecentClientIds(
	ctx sdk.Context,
	vaultId types.VaultId,
	clientIds []uint32,
)

SetMostRecentClientIds sets the most recent client IDs for a vault.

func (Keeper) SetOperatorParams

func (k Keeper) SetOperatorParams(
	ctx sdk.Context,
	params types.OperatorParams,
) error

SetOperatorParams sets `OperatorParams` in state. Returns an error if validation fails.

func (Keeper) SetOwnerShareUnlocks

func (k Keeper) SetOwnerShareUnlocks(
	ctx sdk.Context,
	owner string,
	ownerShareUnlocks types.OwnerShareUnlocks,
) error

SetOwnerShareUnlocks sets share unlocks for an owner.

func (Keeper) SetOwnerShares

func (k Keeper) SetOwnerShares(
	ctx sdk.Context,
	owner string,
	ownerShares types.NumShares,
) error

SetOwnerShares sets owner shares for an owner. Returns error if `ownerShares` is negative.

func (Keeper) SetTotalShares

func (k Keeper) SetTotalShares(
	ctx sdk.Context,
	totalShares types.NumShares,
) error

SetTotalShares sets total shares. Returns error if `totalShares` is negative.

func (Keeper) SetVaultParams

func (k Keeper) SetVaultParams(
	ctx sdk.Context,
	vaultId types.VaultId,
	vaultParams types.VaultParams,
) error

SetVaultParams sets `VaultParams` in state for a given vault. Returns an error if validation fails.

func (Keeper) SetVaultStatus

func (k Keeper) SetVaultStatus(
	ctx sdk.Context,
	vaultId types.VaultId,
	status types.VaultStatus,
) error

SetVaultStatus sets `VaultParams.Status` in state for a given vault.

func (Keeper) SweepMainVaultBankBalance

func (k Keeper) SweepMainVaultBankBalance(
	ctx sdk.Context,
)

SweepMainVaultBankBalances deposits any usdc balance from the Megavault main vault bank balance into the Megavault main vault subaccount balance.

func (Keeper) TryToCancelVaultClobOrder

func (k Keeper) TryToCancelVaultClobOrder(
	ctx sdk.Context,
	vaultId types.VaultId,
	clientId uint32,
	orderExpirationSeconds uint32,
) (
	orderExists bool,
	err error,
)

TryToCancelVaultClobOrder tries to cancel a vault CLOB order. Returns whether the order exists and whether cancellation errors.

func (Keeper) UnlockShares

func (k Keeper) UnlockShares(
	ctx sdk.Context,
	ownerAddress string,
) (unlockedShares types.NumShares, err error)

UnlockShares unlocks all shares of an owner that are due to unlock at or before current block height.

func (Keeper) UnsafeDeleteAllVaultOwnerShares

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

UnsafeDeleteVaultOwnerShares deletes all owner shares of a given vault from state. Used for v7.x upgrade handler

func (Keeper) UnsafeDeleteAllVaultTotalShares

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

UnsafeDeleteVaultTotalShares deletes total shares of a given vault from state. Used for v7.x upgrade handler

func (Keeper) UnsafeDeleteParams

func (k Keeper) UnsafeDeleteParams(
	ctx sdk.Context,
)

UnsafeDeleteParams deletes `Params` in state. Used for v6.x upgrade handler.

func (Keeper) UnsafeDeleteQuotingParams

func (k Keeper) UnsafeDeleteQuotingParams(
	ctx sdk.Context,
	vaultId types.VaultId,
)

UnsafeDeleteQuotingParams deletes quoting parameters for a given vault from state. Used for v7.x upgrade handler

func (Keeper) UnsafeGetAllOwnerEquities

func (k Keeper) UnsafeGetAllOwnerEquities(ctx sdk.Context) map[string]*big.Rat

UnsafeGetAllOwnerEquities returns equity that belongs to each owner across all vaults using the deprecated owner shares and total shares state. Deprecated and used for v7.x upgrade handler

func (Keeper) UnsafeGetAllOwnerShares

func (k Keeper) UnsafeGetAllOwnerShares(
	ctx sdk.Context,
	vaultId types.VaultId,
) []*types.OwnerShare

UnsafeGetAllOwnerShares gets all owner shares of a given vault. Deprecated and used for v7.x upgrade handler

func (Keeper) UnsafeGetAllVaultIds

func (k Keeper) UnsafeGetAllVaultIds(ctx sdk.Context) []types.VaultId

UnsafeGetAllVaultIds returns all vault ids from state using the deprecated total shares state.

func (Keeper) UnsafeGetParams

func (k Keeper) UnsafeGetParams(
	ctx sdk.Context,
) (
	params types.QuotingParams,
)

UnsafeGetParams returns `Params` in state. Used for v6.x upgrade handler.

func (Keeper) UnsafeGetQuotingParams

func (k Keeper) UnsafeGetQuotingParams(
	ctx sdk.Context,
	vaultId types.VaultId,
) (
	quotingParams types.QuotingParams,
	exists bool,
)

UnsafeGetQuotingParams returns quoting parameters for a given vault from state. Used for v7.x upgrade handler

func (Keeper) UnsafeGetTotalShares

func (k Keeper) UnsafeGetTotalShares(
	ctx sdk.Context,
	vaultId types.VaultId,
) (val types.NumShares, exists bool)

GetTotalShares gets TotalShares for a vault. Deprecated and used for v7.x upgrade handler

func (Keeper) UnsafeSetQuotingParams

func (k Keeper) UnsafeSetQuotingParams(
	ctx sdk.Context,
	vaultId types.VaultId,
	quotingParams types.QuotingParams,
) error

UnsafeSetQuotingParams sets quoting parameters for a given vault from state. Used for v7.x upgrade handler

func (Keeper) Vault

func (Keeper) VaultParams

func (Keeper) WithdrawFromMegavault

func (k Keeper) WithdrawFromMegavault(
	ctx sdk.Context,
	toSubaccount satypes.SubaccountId,
	sharesToWithdraw *big.Int,
	minQuoteQuantums *big.Int,
) (redeemedQuoteQuantums *big.Int, err error)

WithdrawFromMegavault withdraws from megavault to a subaccount.

Jump to

Keyboard shortcuts

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