keeper

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: May 22, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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

func (k Keeper) CalcPerpTxFee(ctx sdk.Context, pair common.TokenPair, quoteAmt sdk.Int) (toll sdk.Int, spread sdk.Int, err error)

CalcPerpTxFee calculates the total tx fee for exchanging 'quoteAmt' of tokens on the exchange.

Args:

quoteAmt (sdk.Int):

Returns:

toll (sdk.Int): Amount of tokens transferred to the the fee pool.
spread (sdk.Int): Amount of tokens transferred to the PerpEF.

func (Keeper) CreatePool

func (k Keeper) CreatePool(
	ctx sdk.Context,
	pair string,
	tradeLimitRatio sdk.Dec,
	quoteAssetReserve sdk.Dec,
	baseAssetReserve sdk.Dec,
	fluctuationLimitRatio sdk.Dec,
	maxOracleSpreadRatio sdk.Dec,
)

CreatePool creates a pool for a specific pair.

func (Keeper) ExistsPool

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

ExistsPool returns true if pool exists, false if not.

func (Keeper) GetBaseAssetPrice

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

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.TokenPair,
	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) GetQuoteAssetPrice

func (k Keeper) GetQuoteAssetPrice(
	ctx sdk.Context,
	pair common.TokenPair,
	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.TokenPair,
	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.TokenPair) (sdk.Dec, error)

TODO(mercilex): implement

func (Keeper) GetSpotPrice

func (k Keeper) GetSpotPrice(ctx sdk.Context, pair common.TokenPair) (
	price sdk.Dec, err 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.TokenPair) (
	price sdk.Dec, err 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.TokenPair) (isIt bool)

func (Keeper) SwapBaseForQuote

func (k Keeper) SwapBaseForQuote(
	ctx sdk.Context,
	pair common.TokenPair,
	dir types.Direction,
	baseAssetAmount sdk.Dec,
	quoteAmountLimit sdk.Dec,
) (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

ret:

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

func (Keeper) SwapQuoteForBase

func (k Keeper) SwapQuoteForBase(
	ctx sdk.Context,
	pair common.TokenPair,
	dir types.Direction,
	quoteAssetAmount sdk.Dec,
	baseAmountLimit sdk.Dec,
) (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

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