keeper

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2023 License: AGPL-3.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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. TODO(DEC-1310): Rename to reflect new premium sampling design.

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) GetFundingRateClampFactorPpm

func (k Keeper) GetFundingRateClampFactorPpm(ctx sdk.Context) uint32

`GetFundingRateClampFactorPpm` returns funding rate clamp factor (in parts-per-million).

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` by `marginAdjustmentPpm`, then limited to a maximum of 100%. `marginAdjustmentPpm“ is given by the equation `sqrt(notionalValue / liquidityTier.BasePositionNotional)` limited to a minimum of 100%. `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.

func (Keeper) GetMinNumVotesPerSample

func (k Keeper) GetMinNumVotesPerSample(ctx sdk.Context) uint32

`GetMinNumVotesPerSample` returns minimum number of votes per sample.

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.

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) types.Params
=== PARAMETERS FUNCTIONS ===

`GetParams` returns all 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.

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) GetPremiumVoteClampFactorPpm

func (k Keeper) GetPremiumVoteClampFactorPpm(ctx sdk.Context) uint32

`GetPremiumVoteClampFactorPpm` returns premium vote clamp factor (in parts-per-million).

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 added in v0.3.0

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 added in v0.3.0

func (k Keeper) HasAuthority(authority string) bool

func (Keeper) HasLiquidityTier added in v0.3.0

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 added in v0.3.0

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 satifies:

  • 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) ModifyOpenInterest

func (k Keeper) ModifyOpenInterest(
	ctx sdk.Context,
	id uint32,
	isIncrease bool,
	deltaBaseQuantums uint64,
) (newOpenInterestBaseQuantums uint64, 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)

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) SetFundingRateClampFactorPpm

func (k Keeper) SetFundingRateClampFactorPpm(ctx sdk.Context, num uint32) error

`SetFundingRateClampFactorPpm` sets funding rate clamp factor (in parts-per-million) in store and returns error instead if parameter value fails validation.

func (Keeper) SetLiquidityTier added in v0.3.0

func (k Keeper) SetLiquidityTier(
	ctx sdk.Context,
	id uint32,
	name string,
	initialMarginPpm uint32,
	maintenanceFractionPpm uint32,
	basePositionNotional uint64,
	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) SetMinNumVotesPerSample

func (k Keeper) SetMinNumVotesPerSample(ctx sdk.Context, num uint32) error

`SetMinNumVotesPerSample` sets minimum number of votes per sample in store.

func (Keeper) SetParams added in v0.3.0

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

`SetParams` sets all perpetuals module parameters in store.

func (Keeper) SetPremiumSamples

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

func (Keeper) SetPremiumVoteClampFactorPpm

func (k Keeper) SetPremiumVoteClampFactorPpm(ctx sdk.Context, num uint32) error

`SetPremiumVoteClampFactorPpm` sets premium vote clamp factor (in parts-per-million) in store and returns error instead if parameter value fails validation.

func (Keeper) SetPremiumVotes

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

func (Keeper) UnsafeDeleteNumLiquidityTiersKey added in v0.3.0

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

func (Keeper) UnsafeSetPerpetual added in v0.3.0

func (k Keeper) UnsafeSetPerpetual(
	ctx sdk.Context,
	perpetual types.Perpetual,
)

UnsafeSetPerpetual sets a specific `Perpetual` in the store from its index.

Jump to

Keyboard shortcuts

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