keeper

package
v0.20.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: May 11, 2023 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func From2To3

func From2To3(perpKeeper Keeper, perpammKeeper types.PerpAmmKeeper) module.MigrationHandler

func NewMsgServerImpl

func NewMsgServerImpl(keeper Keeper) types.MsgServer

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

func NewQuerier added in v0.4.0

func NewQuerier(k Keeper) types.QueryServer

func SetPairMetadata added in v0.19.0

func SetPairMetadata(k Keeper, ctx sdk.Context, pm types.PairMetadata)

func SetPosition added in v0.19.0

func SetPosition(k Keeper, ctx sdk.Context, pos types.Position)

Types

type Hooks added in v0.5.1

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

Hooks wrapper struct for perps keeper.

func (Hooks) AfterEpochEnd added in v0.5.1

func (h Hooks) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumber uint64)

func (Hooks) BeforeEpochStart added in v0.5.1

func (h Hooks) BeforeEpochStart(ctx sdk.Context, epochIdentifier string, epochNumber uint64)

BeforeEpochStart epochs hooks.

type Keeper

type Keeper struct {
	ParamSubspace paramtypes.Subspace

	BankKeeper    types.BankKeeper
	AccountKeeper types.AccountKeeper
	OracleKeeper  types.OracleKeeper
	PerpAmmKeeper types.PerpAmmKeeper
	EpochKeeper   types.EpochKeeper

	Positions      collections.Map[collections.Pair[asset.Pair, sdk.AccAddress], types.Position]
	PairsMetadata  collections.Map[asset.Pair, types.PairMetadata]
	PrepaidBadDebt collections.Map[string, types.PrepaidBadDebt]

	Metrics collections.Map[asset.Pair, types.Metrics]
	// contains filtered or unexported fields
}

func NewKeeper

func NewKeeper(
	cdc codec.BinaryCodec,
	storeKey sdk.StoreKey,
	paramSubspace paramtypes.Subspace,

	accountKeeper types.AccountKeeper,
	bankKeeper types.BankKeeper,
	oracleKeeper types.OracleKeeper,
	perpammKeeper types.PerpAmmKeeper,
	epochKeeper types.EpochKeeper,
) Keeper

NewKeeper Creates a new x/perp Keeper instance.

func (Keeper) AddMargin

func (k Keeper) AddMargin(
	ctx sdk.Context, pair asset.Pair, traderAddr sdk.AccAddress, margin sdk.Coin,
) (res *types.MsgAddMarginResponse, err error)
AddMargin deleverages an existing position by adding margin (collateral)

to it. Adding margin increases the margin ratio of the corresponding position.

func (Keeper) AfterEpochEnd added in v0.5.1

func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, _ uint64)

func (Keeper) BeforeEpochStart added in v0.5.1

func (k Keeper) BeforeEpochStart(ctx sdk.Context, epochIdentifier string, epochNumber uint64)

func (Keeper) CalcRemainMarginWithFundingPayment

func (k Keeper) CalcRemainMarginWithFundingPayment(
	ctx sdk.Context,
	currentPosition types.Position,
	marginDelta sdk.Dec,
) (remaining RemainingMarginWithFundingPayment, err error)

func (Keeper) ClosePosition added in v0.4.20

func (k Keeper) ClosePosition(ctx sdk.Context, pair asset.Pair, traderAddr sdk.AccAddress) (*types.PositionResp, error)

ClosePosition closes a position entirely and transfers the remaining margin back to the user. Errors if the position has bad debt.

args:

  • ctx: the cosmos-sdk context
  • pair: the trading pair
  • traderAddr: the trader's address

ret:

  • positionResp: the response containing the updated position and applied funding payment, bad debt, PnL
  • err: error if any

func (Keeper) DecrementPrepaidBadDebt added in v0.15.0

func (k Keeper) DecrementPrepaidBadDebt(ctx sdk.Context, denom string, amount sdk.Int) sdk.Int

DecrementPrepaidBadDebt decrements the amount of bad debt prepaid by denom. // The lowest it can be decremented to is zero. Trying to decrement a prepaid bad // debt balance to below zero will clip it at zero.

func (Keeper) EditPoolPegMultiplier

func (k Keeper) EditPoolPegMultiplier(
	ctx sdk.Context,
	sender sdk.AccAddress,
	pair asset.Pair,
	pegMultiplier sdk.Dec,
) (err error)

EditPoolPegMultiplier edits the peg multiplier of an amm pool after making sure there's enough money in the perp EF fund to pay for the repeg. These funds get send to the vault to pay for trader's new net margin.

func (Keeper) EditPoolSwapInvariant

func (k Keeper) EditPoolSwapInvariant(ctx sdk.Context, sender sdk.AccAddress, pair asset.Pair, multiplier sdk.Dec) (err error)

func (Keeper) ExecuteFullLiquidation added in v0.2.3

func (k Keeper) ExecuteFullLiquidation(
	ctx sdk.Context, liquidator sdk.AccAddress, position *types.Position,
) (liquidationResp types.LiquidateResp, err error)

ExecuteFullLiquidation Fully liquidates a position. It is assumed that the margin ratio has already been checked prior to calling this method.

args:

  • ctx: cosmos-sdk context
  • liquidator: the liquidator's address
  • position: the position to liquidate

ret:

  • liquidationResp: a response object containing the results of the liquidation
  • err: error

func (Keeper) ExecutePartialLiquidation added in v0.2.3

func (k Keeper) ExecutePartialLiquidation(
	ctx sdk.Context, liquidator sdk.AccAddress, currentPosition *types.Position,
) (types.LiquidateResp, error)

ExecutePartialLiquidation partially liquidates a position

func (Keeper) GetMarginRatio

func (k Keeper) GetMarginRatio(
	ctx sdk.Context, market perpammtypes.Market, position types.Position, priceOption types.MarginCalculationPriceOption,
) (marginRatio sdk.Dec, err error)

GetMarginRatio calculates the MarginRatio from a Position

func (Keeper) GetParams

func (k Keeper) GetParams(ctx sdk.Context) (params types.Params)

GetParams get all parameters as types.Params

func (Keeper) GetPreferencePositionNotionalAndUnrealizedPnL added in v0.15.0

func (k Keeper) GetPreferencePositionNotionalAndUnrealizedPnL(
	ctx sdk.Context,
	market perpammtypes.Market,
	position types.Position,
	pnLPreferenceOption types.PnLPreferenceOption,
) (positionNotional sdk.Dec, unrealizedPnl sdk.Dec, err error)

GetPreferencePositionNotionalAndUnrealizedPnL Calculates both position notional value and unrealized PnL based on both spot price and TWAP, and lets the caller pick which one based on MAX or MIN.

args:

  • ctx: cosmos-sdk context
  • position: the trader's position
  • pnlPreferenceOption: MAX or MIN

Returns:

  • positionNotional: the position's notional value as sdk.Dec (signed)
  • unrealizedPnl: the position's unrealized profits and losses (PnL) as sdk.Dec (signed) For LONG positions, this is positionNotional - openNotional For SHORT positions, this is openNotional - positionNotional

func (Keeper) Hooks added in v0.5.1

func (k Keeper) Hooks() Hooks

Hooks Return the wrapper struct.

func (Keeper) IncrementPrepaidBadDebt added in v0.15.0

func (k Keeper) IncrementPrepaidBadDebt(ctx sdk.Context, denom string, amount sdk.Int) sdk.Int

IncrementPrepaidBadDebt increases the bad debt for the provided denom. And returns the newest bad-debt amount. If no prepaid bad debt for the given denom was recorded before then it is set using the provided amount and the provided amount is returned.

func (Keeper) Liquidate added in v0.2.3

func (k Keeper) Liquidate(
	ctx sdk.Context,
	liquidator sdk.AccAddress,
	pair asset.Pair,
	trader sdk.AccAddress,
) (liquidatorFee sdk.Coin, perpEcosystemFundFee sdk.Coin, err error)
Liquidate allows to liquidate the trader position if the margin is below the

required margin maintenance ratio.

args:

  • liquidator: the liquidator who is executing the liquidation
  • pair: the asset pair
  • trader: the trader who owns the position being liquidated

ret:

  • liquidatorFee: the amount of coins given to the liquidator
  • perpEcosystemFundFee: the amount of coins given to the ecosystem fund
  • err: error

func (Keeper) Logger

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

func (Keeper) MultiLiquidate added in v0.15.0

func (k Keeper) MultiLiquidate(
	ctx sdk.Context, liquidator sdk.AccAddress, liquidationRequests []*types.MsgMultiLiquidate_Liquidation,
) ([]*types.MsgMultiLiquidateResponse_LiquidationResponse, error)

func (Keeper) OnSwapEnd added in v0.16.1

func (k Keeper) OnSwapEnd(
	ctx sdk.Context,
	pair asset.Pair,
	quoteAssetAmount sdk.Dec,
	baseAssetAmount sdk.Dec,
)

OnSwapEnd recalculates perp metrics for a particular pair.

func (Keeper) OpenPosition

func (k Keeper) OpenPosition(
	ctx sdk.Context,
	pair asset.Pair,
	side perpammtypes.Direction,
	traderAddr sdk.AccAddress,
	quoteAssetAmount sdk.Int,
	leverage sdk.Dec,
	baseAmtLimit sdk.Dec,
) (positionResp *types.PositionResp, err error)

OpenPosition opens a position on the selected pair.

args:

  • ctx: cosmos-sdk context
  • pair: the pair where the position will be opened
  • side: whether the position in the BUY or SELL direction
  • traderAddr: the address of the trader who opens the position
  • quoteAssetAmount: the amount of quote asset
  • leverage: the amount of leverage to take, as sdk.Dec
  • baseAmtLimit: the limit on the base asset amount to make sure the trader doesn't get screwed, in base asset units

ret:

  • positionResp: contains the result of the open position and the new position
  • err: error

func (Keeper) RemoveMargin

func (k Keeper) RemoveMargin(
	ctx sdk.Context, pair asset.Pair, traderAddr sdk.AccAddress, margin sdk.Coin,
) (marginOut sdk.Coin, fundingPayment sdk.Dec, position types.Position, err error)
RemoveMargin further leverages an existing position by directly removing

the margin (collateral) that backs it from the vault. This also decreases the margin ratio of the position.

Fails if the position goes underwater.

args:

  • ctx: the cosmos-sdk context
  • pair: the asset pair
  • traderAddr: the trader's address
  • margin: the amount of margin to withdraw. Must be positive.

ret:

  • marginOut: the amount of margin removed
  • fundingPayment: the funding payment that was applied with this position interaction
  • err: error if any

func (Keeper) SetParams

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

SetParams set the params

func (Keeper) SettlePosition

func (k Keeper) SettlePosition(
	ctx sdk.Context,
	currentPosition types.Position,
) (transferredCoins sdk.Coins, err error)

SettlePosition settles a trader position

func (Keeper) Withdraw added in v0.2.0

func (k Keeper) Withdraw(
	ctx sdk.Context,
	denom string,
	receiver sdk.AccAddress,
	amountToWithdraw sdk.Int,
) (err error)

Withdraws coins from the vault to the receiver. If the total amount of coins to withdraw is greater than the vault's amount, then withdraw the shortage from the PerpEF and mark it as prepaid bad debt.

Prepaid bad debt will count towards realized bad debt from negative PnL positions when those are closed/liquidated.

An example of this happening is when a long position has really high PnL and closes their position, realizing their profits. There is a counter party short position with really negative PnL, but their position hasn't been closed/liquidated yet. We must pay the long trader first, which results in funds being taken from the EF. when the short position is closed, it also realizes some bad debt but because we have already withdrawn from the EF, we don't need to withdraw more from the EF.

type RemainingMarginWithFundingPayment

type RemainingMarginWithFundingPayment struct {
	// Margin: amount of quote token (y) backing the position.
	Margin sdk.Dec

	/* BadDebt: Bad debt (margin units) cleared by the PerpEF during the tx.
	   Bad debt is negative net margin past the liquidation point of a position. */
	BadDebt sdk.Dec

	/* FundingPayment: A funding payment (margin units) made or received by the trader on
	    the current position. 'fundingPayment' is positive if 'owner' is the sender
		and negative if 'owner' is the receiver of the payment. Its magnitude is
		abs(vSize * fundingRate). Funding payments act to converge the mark price
		(vPrice) and index price (average price on major exchanges).
	*/
	FundingPayment sdk.Dec

	/* LatestCumulativePremiumFraction: latest cumulative funding rate from state. Units are (margin units)/(position units). */
	LatestCumulativePremiumFraction sdk.Dec
}

func (RemainingMarginWithFundingPayment) String added in v0.5.1

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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