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: 29 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetMarginRequirementsInQuoteQuantums

func GetMarginRequirementsInQuoteQuantums(
	perpetual types.Perpetual,
	marketPrice pricestypes.MarketPrice,
	liquidityTier types.LiquidityTier,
	bigQuantums *big.Int,
) (
	bigInitialMarginQuoteQuantums *big.Int,
	bigMaintenanceMarginQuoteQuantums *big.Int,
)

GetMarginRequirementsInQuoteQuantums returns initial and maintenance margin requirements in quote quantums, given the position size in base quantums.

Note that this is a stateless function.

func GetNetNotionalInQuoteQuantums

func GetNetNotionalInQuoteQuantums(
	perpetual types.Perpetual,
	marketPrice pricestypes.MarketPrice,
	bigQuantums *big.Int,
) (
	bigNetNotionalQuoteQuantums *big.Int,
)

GetNetNotionalInQuoteQuantums returns the net notional in quote quantums, which can be represented by the following equation:

`quantums / 10^baseAtomicResolution * marketPrice * 10^marketExponent * 10^quoteAtomicResolution`. Note that longs are positive, and shorts are negative.

Also note that this is a stateless function.

func GetSettlementPpmWithPerpetual

func GetSettlementPpmWithPerpetual(
	perpetual types.Perpetual,
	quantums *big.Int,
	index *big.Int,
) (
	bigNetSettlementPpm *big.Int,
	newFundingIndex *big.Int,
)

GetSettlementPpm returns the net settlement amount ppm (in quote quantums) given the perpetual and position size (in base quantums).

Note that this function is a stateless utility function.

func NewMsgServerImpl

func NewMsgServerImpl(keeper types.PerpetualsKeeper) 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,
	pricesKeeper types.PricesKeeper,
	epochsKeeper types.EpochsKeeper,
	indexerEventsManager indexer_manager.IndexerEventManager,
	authorities []string,
) *Keeper

func (Keeper) AddPremiumSamples

func (k Keeper) AddPremiumSamples(
	ctx sdk.Context,
	newSamples []types.FundingPremium,
) error

AddPremiumSamples adds a list of new premium samples to state.

func (Keeper) AddPremiumVotes

func (k Keeper) AddPremiumVotes(
	ctx sdk.Context,
	newVotes []types.FundingPremium,
) error

AddPremiumVotes adds a list of new premium votes to state.

func (Keeper) CreatePerpetual

func (k Keeper) CreatePerpetual(
	ctx sdk.Context,
	id uint32,
	ticker string,
	marketId uint32,
	atomicResolution int32,
	defaultFundingPpm int32,
	liquidityTier uint32,
) (types.Perpetual, error)

CreatePerpetual creates a new perpetual in the store. Returns an error if any of the perpetual fields fail validation, or if the `marketId` does not exist.

func (Keeper) GetAddPremiumVotes

func (k Keeper) GetAddPremiumVotes(
	ctx sdk.Context,
) (
	msgAddPremiumVotes *types.MsgAddPremiumVotes,
)

GetAddPremiumVotes returns the newest premiums for all perpetuals, if the current block is the start of a new funding-sample epoch. Otherwise, does nothing and returns an empty message. Does not make any changes to state.

func (Keeper) GetAllLiquidityTiers

func (k Keeper) GetAllLiquidityTiers(ctx sdk.Context) (list []types.LiquidityTier)

`GetAllLiquidityTiers` returns all liquidity tiers, sorted by id.

func (Keeper) GetAllPerpetuals

func (k Keeper) GetAllPerpetuals(ctx sdk.Context) (list []types.Perpetual)

GetAllPerpetuals returns all perpetuals, sorted by perpetual Id.

func (Keeper) GetIndexerEventManager

func (k Keeper) GetIndexerEventManager() indexer_manager.IndexerEventManager

func (Keeper) GetLiquidityTier

func (k Keeper) GetLiquidityTier(ctx sdk.Context, id uint32) (
	liquidityTier types.LiquidityTier,
	err error,
)

`GetLiquidityTier` gets a liquidity tier given its id.

func (Keeper) GetMarginRequirements

func (k Keeper) GetMarginRequirements(
	ctx sdk.Context,
	id uint32,
	bigQuantums *big.Int,
) (
	bigInitialMarginQuoteQuantums *big.Int,
	bigMaintenanceMarginQuoteQuantums *big.Int,
	err error,
)

GetMarginRequirements returns initial and maintenance margin requirements in quote quantums, given the position size in base quantums.

Margin requirements are a function of the absolute value of the open notional of the position as well as the parameters of the relevant `LiquidityTier` of the perpetual. Initial margin requirement is determined by multiplying `InitialMarginPpm` and `notionalValue`. `notionalValue` is determined by multiplying the size of the position by the oracle price of the position. Maintenance margin requirement is then simply a fraction (`maintenanceFractionPpm`) of initial margin requirement.

Returns an error if a perpetual with `id`, `perpetual.Params.MarketId`, or `perpetual.Params.LiquidityTier` does not exist.

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

func (Keeper) GetNetCollateral

func (k Keeper) GetNetCollateral(
	ctx sdk.Context,
	id uint32,
	bigQuantums *big.Int,
) (
	bigNetCollateralQuoteQuantums *big.Int,
	err error,
)

GetNetCollateral returns the net collateral in quote quantums. The net collateral is equal to the net open notional, which can be represented by the following equation: `quantums / 10^baseAtomicResolution * marketPrice * 10^marketExponent * 10^quoteAtomicResolution`. Note that longs are positive, and shorts are negative. Returns an error if a perpetual with `id` does not exist or if the `perpetual.Params.MarketId` does not exist.

func (Keeper) GetNetNotional

func (k Keeper) GetNetNotional(
	ctx sdk.Context,
	id uint32,
	bigQuantums *big.Int,
) (
	bigNetNotionalQuoteQuantums *big.Int,
	err error,
)

GetNetNotional returns the net notional in quote quantums, which can be represented by the following equation: `quantums / 10^baseAtomicResolution * marketPrice * 10^marketExponent * 10^quoteAtomicResolution`. Note that longs are positive, and shorts are negative. Returns an error if a perpetual with `id` does not exist or if the `perpetual.Params.MarketId` does not exist.

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

func (Keeper) GetNotionalInBaseQuantums

func (k Keeper) GetNotionalInBaseQuantums(
	ctx sdk.Context,
	id uint32,
	bigQuoteQuantums *big.Int,
) (
	bigBaseQuantums *big.Int,
	err error,
)

GetNotionalInBaseQuantums returns the net notional in base quantums, which can be represented by the following equation: `quoteQuantums * 10^baseAtomicResolution / (marketPrice * 10^marketExponent * 10^quoteAtomicResolution)`. Note that longs are positive, and shorts are negative. Returns an error if a perpetual with `id` does not exist or if the `perpetual.Params.MarketId` does not exist.

func (Keeper) GetParams

func (k Keeper) GetParams(
	ctx sdk.Context,
) (params types.Params)
=== PARAMETERS FUNCTIONS ===

`GetParams` returns perpetuals module parameters as a `Params` object from store.

func (Keeper) GetPerpetual

func (k Keeper) GetPerpetual(
	ctx sdk.Context,
	id uint32,
) (val types.Perpetual, err error)

GetPerpetual returns a perpetual from its id.

func (Keeper) GetPerpetualAndMarketPrice

func (k Keeper) GetPerpetualAndMarketPrice(
	ctx sdk.Context,
	perpetualId uint32,
) (types.Perpetual, pricestypes.MarketPrice, error)

GetPerpetualAndMarketPrice retrieves a Perpetual by its id and its corresponding MarketPrice.

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

func (Keeper) GetPremiumSamples

func (k Keeper) GetPremiumSamples(ctx sdk.Context) (
	premiumStore types.PremiumStore,
)

GetPremiumSamples reads premium samples from the current `funding-tick` epoch, stored in a `PremiumStore` struct.

func (Keeper) GetPremiumVotes

func (k Keeper) GetPremiumVotes(ctx sdk.Context) (
	premiumStore types.PremiumStore,
)

GetPremiumVotes premium sample votes from the current `funding-sample` epoch, stored in a `PremiumStore` struct.

func (Keeper) GetRemoveSampleTailsFunc

func (k Keeper) GetRemoveSampleTailsFunc(
	ctx sdk.Context,
	tailRemovalRatePpm uint32,
) func(input []int32) (output []int32)

GetRemoveSampleTailsFunc returns a function that sorts the input samples (in place) and returns the sub-slice from the original slice, which removes `tailRemovalRatePpm` from top and bottom from the samples. Note the returned sub-slice is not a copy but references a sub-sequence of the original slice.

func (Keeper) GetSettlementPpm

func (k Keeper) GetSettlementPpm(
	ctx sdk.Context,
	perpetualId uint32,
	quantums *big.Int,
	index *big.Int,
) (
	bigNetSettlementPpm *big.Int,
	newFundingIndex *big.Int,
	err error,
)

GetSettlementPpm returns the net settlement amount ppm (in quote quantums) given the perpetual Id and position size (in base quantums). When handling rounding, always round positive settlement amount to zero, and negative amount to negative infinity. This ensures total amount of value does not increase after settlement. Example: For a round of funding payments, accounts A, B are to receive 102.5 quote quantums; account C is to pay 205 quote quantums. After settlement, accounts A, B are credited 102 quote quantum each; account C is debited 205 quote quantums.

func (Keeper) HasAuthority

func (k Keeper) HasAuthority(authority string) bool

func (Keeper) HasLiquidityTier

func (k Keeper) HasLiquidityTier(
	ctx sdk.Context,
	id uint32,
) (found bool)

HasLiquidityTier checks if a liquidity tier exists in the store.

func (Keeper) HasPerpetual

func (k Keeper) HasPerpetual(
	ctx sdk.Context,
	id uint32,
) (found bool)

HasPerpetual checks if a perpetual exists in the store.

func (Keeper) InitializeForGenesis

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

func (Keeper) IsPositionUpdatable

func (k Keeper) IsPositionUpdatable(
	ctx sdk.Context,
	perpetualId uint32,
) (
	updatable bool,
	err error,
)

IsPositionUpdatable returns whether position of a perptual is updatable. A perpetual is not updatable if it satisfies:

  • Perpetual has zero oracle price. Since new oracle prices are created at zero by default and valid oracle priceupdates are non-zero, this indicates the absence of a valid oracle price update.

func (Keeper) Logger

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

func (Keeper) MaybeProcessNewFundingSampleEpoch

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

MaybeProcessNewFundingSampleEpoch summarizes premium votes stored in application states into new funding samples, if the current block is the start of a new `funding-sample` epoch. Otherwise, does nothing.

func (Keeper) MaybeProcessNewFundingTickEpoch

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

MaybeProcessNewFundingTickEpoch processes funding ticks if the current block is the start of a new funding-tick epoch. Otherwise, do nothing.

func (Keeper) ModifyFundingIndex

func (k Keeper) ModifyFundingIndex(
	ctx sdk.Context,
	perpetualId uint32,
	bigFundingIndexDelta *big.Int,
) (
	err error,
)

func (Keeper) ModifyPerpetual

func (k Keeper) ModifyPerpetual(
	ctx sdk.Context,
	id uint32,
	ticker string,
	marketId uint32,
	defaultFundingPpm int32,
	liquidityTier uint32,
) (types.Perpetual, error)

ModifyPerpetual modifies an existing perpetual in the store. The new perpetual object must pass stateful and stateless validations. Upon successful modification, send an indexer event.

func (Keeper) Params

func (Keeper) PerformStatefulPremiumVotesValidation

func (k Keeper) PerformStatefulPremiumVotesValidation(
	ctx sdk.Context,
	msg *types.MsgAddPremiumVotes,
) (
	err error,
)

PerformStatefulPremiumVotesValidation performs stateful validation on `MsgAddPremiumVotes`. For each vote, it checks that: - The perpetual Id is valid. - The premium vote value is correctly clamped. This function throws an error if the associated clob pair cannot be found or is not active.

func (*Keeper) SetClobKeeper

func (k *Keeper) SetClobKeeper(getter types.PerpetualsClobKeeper)

SetClobKeeper sets the `PerpetualsClobKeeper` reference, which is a Clob Keeper, for this Perpetuals Keeper. This method is called after the Perpetuals Keeper struct is initialized. This reference is set with an explicit method call rather than during `NewKeeper` due to the bidirectional dependency between the Perpetuals Keeper and the Clob Keeper.

func (Keeper) SetEmptyPremiumSamples

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

SetEmptyPremiumSamples initializes empty premium samples for all perpetuals

func (Keeper) SetEmptyPremiumVotes

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

SetEmptyPremiumSamples initializes empty premium sample votes for all perpetuals

func (Keeper) SetLiquidityTier

func (k Keeper) SetLiquidityTier(
	ctx sdk.Context,
	id uint32,
	name string,
	initialMarginPpm uint32,
	maintenanceFractionPpm uint32,
	impactNotional uint64,
) (
	liquidityTier types.LiquidityTier,
	err error,
)

`SetLiquidityTier` sets a liquidity tier in the store (i.e. updates if `id` exists and creates otherwise). Returns an error if any of its fields fails validation.

func (Keeper) SetParams

func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error

`SetParams` sets perpetuals module parameters in store.

func (Keeper) SetPremiumSamples

func (k Keeper) SetPremiumSamples(
	ctx sdk.Context,
	premiumStore types.PremiumStore,
)

func (Keeper) SetPremiumVotes

func (k Keeper) SetPremiumVotes(
	ctx sdk.Context,
	premiumStore types.PremiumStore,
)

Jump to

Keyboard shortcuts

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