keeper

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 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) types.QueryServer

Types

type Keeper

type Keeper struct {
	Pools            collections.Map[common.AssetPair, types.VPool]
	ReserveSnapshots collections.Map[collections.Pair[common.AssetPair, time.Time], types.ReserveSnapshot]
	// 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.VPool

GetAllPools returns an array of all the pools

args:

  • ctx: the cosmos-sdk context

ret:

  • []types.VPool: All defined vpool

func (Keeper) GetBaseAssetPrice

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

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

GetBaseAssetTWAP 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) 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) GetMarkPrice added in v0.15.0

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

GetMarkPrice 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) GetMarkPriceTWAP added in v0.15.0

func (k Keeper) GetMarkPriceTWAP(
	ctx sdk.Context,
	pair common.AssetPair,
	lookbackInterval time.Duration,
) (quoteAssetAmount sdk.Dec, err error)

GetMarkPriceTWAP Returns the twap of the spot price (y/x).

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) 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.VPool,
) (types.PoolPrices, error)

GetPoolPrices returns the mark price, twap (mark) price, and index price for a vpool. An error is returned if the pool does not exist. No error is returned if the prices don't exist, however.

func (Keeper) GetQuoteAssetPrice

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

GetQuoteAssetPrice 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) GetSettlementPrice

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

TODO(mercilex): implement

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 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,
	baseAmt sdk.Dec,
	quoteLimit sdk.Dec,
	skipFluctuationLimitCheck bool,
) (quoteAmt sdk.Dec, err error)

SwapBaseForQuote 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,
	quoteAmt sdk.Dec,
	baseLimit sdk.Dec,
	skipFluctuationLimitCheck bool,
) (baseAmt sdk.Dec, err error)

SwapQuoteForBase 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

Jump to

Keyboard shortcuts

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