keeper

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewQuerier added in v0.3.0

func NewQuerier(k Keeper) queryServer

Types

type Keeper

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

func NewKeeper

func NewKeeper(
	codec codec.BinaryCodec,
	storeKey sdk.StoreKey,
	pricefeedKeeper types.PricefeedKeeper,
) Keeper

func (Keeper) CreatePool

func (k Keeper) CreatePool(
	ctx sdk.Context,
	pair common.AssetPair,
	tradeLimitRatio sdk.Dec,
	quoteAssetReserve sdk.Dec,
	baseAssetReserve sdk.Dec,
	fluctuationLimitRatio sdk.Dec,
	maxOracleSpreadRatio sdk.Dec,
	maintenanceMarginRatio sdk.Dec,
	maxLeverage sdk.Dec,
)

CreatePool creates a pool for a specific pair.

func (Keeper) ExistsPool

func (k Keeper) ExistsPool(ctx sdk.Context, pair common.AssetPair) bool

ExistsPool returns true if pool exists, false if not.

func (Keeper) GetAllPools added in v0.2.10

func (k Keeper) GetAllPools(ctx sdk.Context) []*types.Pool

GetAllPools returns all pools that exist.

func (Keeper) GetBaseAssetPrice

func (k Keeper) GetBaseAssetPrice(
	ctx sdk.Context,
	pair common.AssetPair,
	dir types.Direction,
	baseAssetAmount sdk.Dec,
) (quoteAmount sdk.Dec, err error)

So how much stablecoin you would get if you sold baseAssetAmount amount of perpetual contracts.

Returns the amount of quote assets required to achieve a move of baseAssetAmount in a direction. e.g. if removing <baseAssetAmount> base assets from the pool, returns the amount of quote assets do so.

args:

  • ctx: cosmos-sdk context
  • pair: the trading token pair
  • dir: add or remove
  • baseAssetAmount: the amount of base asset

ret:

  • quoteAmount: the amount of quote assets required to make the desired swap
  • err: error

func (Keeper) GetBaseAssetTWAP

func (k Keeper) GetBaseAssetTWAP(
	ctx sdk.Context,
	pair common.AssetPair,
	direction types.Direction,
	baseAssetAmount sdk.Dec,
	lookbackInterval time.Duration,
) (quoteAssetAmount sdk.Dec, err error)

Returns the amount of quote assets required to achieve a move of baseAssetAmount in a direction, based on historical snapshots. e.g. if removing <baseAssetAmount> base assets from the pool, returns the amount of quote assets do so.

args:

  • ctx: cosmos-sdk context
  • pair: the token pair
  • direction: add or remove
  • baseAssetAmount: amount of base asset to add or remove
  • lookbackInterval: how far back to calculate TWAP

ret:

  • quoteAssetAmount: the amount of quote asset to make the desired move, as sdk.Dec
  • err: error

func (Keeper) GetCurrentTWAP added in v0.9.2

func (k Keeper) GetCurrentTWAP(
	ctx sdk.Context, pair common.AssetPair,
) (types.CurrentTWAP, error)

GetCurrentTWAP fetches the instantaneous time-weighted average (mark) price for the given asset pair.

func (Keeper) GetMaintenanceMarginRatio added in v0.11.0

func (k Keeper) GetMaintenanceMarginRatio(ctx sdk.Context, pair common.AssetPair) sdk.Dec

* GetMaintenanceMarginRatio returns the maintenance margin ratio for the pool from the asset pair.

args:

  • ctx: the cosmos-sdk context
  • pair: the asset pair

ret:

  • sdk.Dec: The maintenance margin ratio for the pool

func (Keeper) GetMaxLeverage added in v0.13.0

func (k Keeper) GetMaxLeverage(ctx sdk.Context, pair common.AssetPair) sdk.Dec

* GetMaxLeverage returns the maximum leverage required to open a position in the pool.

args:

  • ctx: the cosmos-sdk context
  • pair: the asset pair

ret:

  • sdk.Dec: The maintenance margin ratio for the pool

func (Keeper) GetPoolPrices added in v0.13.0

func (k Keeper) GetPoolPrices(ctx sdk.Context, pool types.Pool) types.PoolPrices

GetPoolPrices returns the mark price, twap (mark) price, and index price for a vpool.

func (Keeper) GetQuoteAssetPrice

func (k Keeper) GetQuoteAssetPrice(
	ctx sdk.Context,
	pair common.AssetPair,
	dir types.Direction,
	quoteAmount sdk.Dec,
) (baseAssetAmount sdk.Dec, err error)

Returns the amount of base assets required to achieve a move of quoteAmount in a direction. e.g. if removing <quoteAmount> quote assets from the pool, returns the amount of base assets do so.

args:

  • ctx: cosmos-sdk context
  • pair: the trading token pair
  • dir: add or remove
  • quoteAmount: the amount of quote asset

ret:

  • baseAssetAmount: the amount of base assets required to make the desired swap
  • err: error

func (Keeper) GetQuoteAssetTWAP

func (k Keeper) GetQuoteAssetTWAP(
	ctx sdk.Context,
	pair common.AssetPair,
	direction types.Direction,
	quoteAssetAmount sdk.Dec,
	lookbackInterval time.Duration,
) (baseAssetAmount sdk.Dec, err error)

Returns the amount of base assets required to achieve a move of quoteAssetAmount in a direction, based on historical snapshots. e.g. if removing <quoteAssetAmount> quote assets from the pool, returns the amount of base assets do so.

args:

  • ctx: cosmos-sdk context
  • pair: the token pair
  • direction: add or remove
  • quoteAssetAmount: amount of base asset to add or remove
  • lookbackInterval: how far back to calculate TWAP

ret:

  • baseAssetAmount: the amount of quote asset to make the desired move, as sdk.Dec
  • err: error

func (Keeper) GetSettlementPrice

func (k Keeper) GetSettlementPrice(ctx sdk.Context, pair common.AssetPair) (sdk.Dec, error)

TODO(mercilex): implement

func (Keeper) GetSpotPrice

func (k Keeper) GetSpotPrice(ctx sdk.Context, pair common.AssetPair) (sdk.Dec, error)

GetSpotPrice retrieves the price of the base asset denominated in quote asset.

The convention is the amount of quote assets required to buy one base asset.

e.g. If the tokenPair is BTC:NUSD, the method would return sdk.Dec(40,000.00) because the instantaneous tangent slope on the vpool curve is 40,000.00, so it would cost ~40,000.00 to buy one BTC:NUSD perp.

args:

  • ctx: cosmos-sdk context
  • pair: the token pair to get price for

ret:

  • price: the price of the token pair as sdk.Dec
  • err: error

func (Keeper) GetUnderlyingPrice

func (k Keeper) GetUnderlyingPrice(ctx sdk.Context, pair common.AssetPair) (sdk.Dec, error)

Retrieves the base asset's price from PricefeedKeeper (oracle). The price is denominated in quote asset, so # of quote asset to buy one base asset.

args:

  • ctx: cosmos-sdk context
  • pair: token pair

ret:

  • price: price as sdk.Dec
  • err: error

func (Keeper) IsOverSpreadLimit

func (k Keeper) IsOverSpreadLimit(ctx sdk.Context, pair common.AssetPair) bool

* IsOverSpreadLimit compares the current spot price of the vpool (given by pair) to the underlying's index price (given by an oracle). It panics if you provide it with a pair that doesn't exist in the state.

args:

  • ctx: the cosmos-sdk context
  • pair: the asset pair

ret:

  • bool: whether or not the price has deviated from the oracle price beyond a spread ratio

func (Keeper) Logger added in v0.13.0

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

func (Keeper) SwapBaseForQuote

func (k Keeper) SwapBaseForQuote(
	ctx sdk.Context,
	pair common.AssetPair,
	dir types.Direction,
	baseAssetAmount sdk.Dec,
	quoteAmountLimit sdk.Dec,
	skipFluctuationLimitCheck bool,
) (quoteAssetAmount sdk.Dec, err error)

Trades baseAssets in exchange for quoteAssets. The base asset is a crypto asset like BTC. The quote asset is a stablecoin like NUSD.

args:

  • ctx: cosmos-sdk context
  • pair: a token pair like BTC:NUSD
  • dir: either add or remove from pool
  • baseAssetAmount: the amount of quote asset being traded
  • quoteAmountLimit: a limiter to ensure the trader doesn't get screwed by slippage
  • skipFluctuationLimitCheck: whether or not to skip the fluctuation limit check

ret:

  • quoteAssetAmount: the amount of quote asset swapped
  • err: error

func (Keeper) SwapQuoteForBase

func (k Keeper) SwapQuoteForBase(
	ctx sdk.Context,
	pair common.AssetPair,
	dir types.Direction,
	quoteAssetAmount sdk.Dec,
	baseAmountLimit sdk.Dec,
	skipFluctuationLimitCheck bool,
) (baseAssetAmount sdk.Dec, err error)

Trades quoteAssets in exchange for baseAssets. The quote asset is a stablecoin like NUSD. The base asset is a crypto asset like BTC or ETH.

args:

  • ctx: cosmos-sdk context
  • pair: a token pair like BTC:NUSD
  • dir: either add or remove from pool
  • quoteAssetAmount: the amount of quote asset being traded
  • baseAmountLimit: a limiter to ensure the trader doesn't get screwed by slippage
  • skipFluctuationLimitCheck: whether or not to skip the fluctuation limit check

ret:

  • baseAssetAmount: the amount of base asset swapped
  • err: error

func (Keeper) UpdateTWAP added in v0.9.2

func (k Keeper) UpdateTWAP(ctx sdk.Context, pairID string) error

Jump to

Keyboard shortcuts

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