Documentation ¶
Index ¶
- func NewMsgServerImpl(keeper Keeper) types.MsgServer
- func NewQuerier(k Keeper) types.QueryServer
- type Hooks
- type Keeper
- func (k Keeper) AddMargin(ctx sdk.Context, pair common.AssetPair, traderAddr sdk.AccAddress, ...) (res *types.MsgAddMarginResponse, err error)
- func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, _ uint64)
- func (k Keeper) BeforeEpochStart(ctx sdk.Context, epochIdentifier string, epochNumber uint64)
- func (k Keeper) CalcRemainMarginWithFundingPayment(ctx sdk.Context, currentPosition types.Position, marginDelta sdk.Dec) (remaining RemainingMarginWithFundingPayment, err error)
- func (k Keeper) ClosePosition(ctx sdk.Context, pair common.AssetPair, traderAddr sdk.AccAddress) (*types.PositionResp, error)
- func (k Keeper) DecrementPrepaidBadDebt(ctx sdk.Context, denom string, amount sdk.Int) sdk.Int
- func (k Keeper) ExecuteFullLiquidation(ctx sdk.Context, liquidator sdk.AccAddress, position *types.Position) (liquidationResp types.LiquidateResp, err error)
- func (k Keeper) ExecutePartialLiquidation(ctx sdk.Context, liquidator sdk.AccAddress, currentPosition *types.Position) (types.LiquidateResp, error)
- func (k Keeper) GetMarginRatio(ctx sdk.Context, position types.Position, ...) (marginRatio sdk.Dec, err error)
- func (k Keeper) GetParams(ctx sdk.Context) (params types.Params)
- func (k Keeper) GetPreferencePositionNotionalAndUnrealizedPnL(ctx sdk.Context, position types.Position, ...) (positionNotional sdk.Dec, unrealizedPnl sdk.Dec, err error)
- func (k Keeper) Hooks() Hooks
- func (k Keeper) IncrementPrepaidBadDebt(ctx sdk.Context, denom string, amount sdk.Int) sdk.Int
- func (k Keeper) Liquidate(ctx sdk.Context, liquidatorAddr sdk.AccAddress, pair common.AssetPair, ...) (feeToLiquidator sdk.Coin, feeToFund sdk.Coin, err error)
- func (k Keeper) Logger(ctx sdk.Context) log.Logger
- func (k Keeper) MultiLiquidate(ctx sdk.Context, liquidator sdk.AccAddress, ...) []MultiLiquidationResponse
- func (k Keeper) OpenPosition(ctx sdk.Context, pair common.AssetPair, side types.Side, ...) (positionResp *types.PositionResp, err error)
- func (k Keeper) RemoveMargin(ctx sdk.Context, pair common.AssetPair, traderAddr sdk.AccAddress, ...) (marginOut sdk.Coin, fundingPayment sdk.Dec, position types.Position, err error)
- func (k Keeper) SetParams(ctx sdk.Context, params types.Params)
- func (k Keeper) SettlePosition(ctx sdk.Context, currentPosition types.Position) (transferredCoins sdk.Coins, err error)
- func (k Keeper) Withdraw(ctx sdk.Context, denom string, receiver sdk.AccAddress, ...) (err error)
- type MultiLiquidationRequest
- type MultiLiquidationResponse
- type RemainingMarginWithFundingPayment
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewMsgServerImpl ¶
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
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
type Keeper ¶
type Keeper struct { ParamSubspace paramtypes.Subspace BankKeeper types.BankKeeper AccountKeeper types.AccountKeeper PricefeedKeeper types.PricefeedKeeper VpoolKeeper types.VpoolKeeper EpochKeeper types.EpochKeeper Positions collections.Map[collections.Pair[common.AssetPair, sdk.AccAddress], types.Position] PairsMetadata collections.Map[common.AssetPair, types.PairMetadata] PrepaidBadDebt collections.Map[string, types.PrepaidBadDebt] // contains filtered or unexported fields }
func NewKeeper ¶
func NewKeeper( cdc codec.BinaryCodec, storeKey sdk.StoreKey, paramSubspace paramtypes.Subspace, accountKeeper types.AccountKeeper, bankKeeper types.BankKeeper, priceKeeper types.PricefeedKeeper, vpoolKeeper types.VpoolKeeper, epochKeeper types.EpochKeeper, ) Keeper
NewKeeper Creates a new x/perp Keeper instance.
func (Keeper) AddMargin ¶
func (k Keeper) AddMargin( ctx sdk.Context, pair common.AssetPair, 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 (Keeper) BeforeEpochStart ¶ added in v0.5.1
func (Keeper) CalcRemainMarginWithFundingPayment ¶
func (Keeper) ClosePosition ¶ added in v0.4.20
func (k Keeper) ClosePosition(ctx sdk.Context, pair common.AssetPair, 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
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) ExecuteFullLiquidation ¶ added in v0.2.3
func (k Keeper) ExecuteFullLiquidation( ctx sdk.Context, liquidator sdk.AccAddress, position *types.Position, ) (liquidationResp types.LiquidateResp, err error)
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, position types.Position, priceOption types.MarginCalculationPriceOption, ) (marginRatio sdk.Dec, err error)
GetMarginRatio calculates the MarginRatio from a Position
func (Keeper) GetPreferencePositionNotionalAndUnrealizedPnL ¶ added in v0.15.0
func (k Keeper) GetPreferencePositionNotionalAndUnrealizedPnL( ctx sdk.Context, position types.Position, pnLPreferenceOption types.PnLPreferenceOption, ) (positionNotional sdk.Dec, unrealizedPnl sdk.Dec, err error)
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) IncrementPrepaidBadDebt ¶ added in v0.15.0
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, liquidatorAddr sdk.AccAddress, pair common.AssetPair, traderAddr sdk.AccAddress, ) (feeToLiquidator sdk.Coin, feeToFund sdk.Coin, err error)
Liquidate allows to liquidate the trader position if the margin is below the
required margin maintenance ratio.
args:
- liquidatorAddr: the liquidator who is executing the liquidation
- pair: the asset pair
- traderAddr: the trader who owns the position being liquidated
ret:
- feeToLiquidator: the amount of coins given to the liquidator
- feeToFund: the amount of coins given to the ecosystem fund
- err: error
func (Keeper) MultiLiquidate ¶ added in v0.15.0
func (k Keeper) MultiLiquidate(ctx sdk.Context, liquidator sdk.AccAddress, positions []MultiLiquidationRequest) []MultiLiquidationResponse
func (Keeper) OpenPosition ¶
func (k Keeper) OpenPosition( ctx sdk.Context, pair common.AssetPair, side types.Side, 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 common.AssetPair, 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) 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 MultiLiquidationRequest ¶ added in v0.15.0
type MultiLiquidationRequest struct {
// contains filtered or unexported fields
}
type MultiLiquidationResponse ¶ added in v0.15.0
type MultiLiquidationResponse struct {
// contains filtered or unexported fields
}
func (MultiLiquidationResponse) IntoMultiLiquidateResponse ¶ added in v0.15.0
func (m MultiLiquidationResponse) IntoMultiLiquidateResponse() *types.MsgMultiLiquidateResponse_MultiLiquidateResponse
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
func (r RemainingMarginWithFundingPayment) String() string