keeper

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2023 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllInvariants

func AllInvariants(k Keeper) sdk.Invariant

AllInvariants runs all invariants of the swap module

func NewMsgServerImpl

func NewMsgServerImpl(keeper Keeper) types.MsgServer

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

func NewQuerier

func NewQuerier(k Keeper, legacyQuerierCdc *codec.LegacyAmino) sdk.Querier

NewQuerier is the module level router for state queries

func NewQueryServerImpl

func NewQueryServerImpl(k Keeper) types.QueryServer

NewQueryServerImpl creates a new server for handling gRPC queries.

func PoolRecordsInvariant

func PoolRecordsInvariant(k Keeper) sdk.Invariant

PoolRecordsInvariant iterates all pool records and asserts that they are valid

func PoolReservesInvariant

func PoolReservesInvariant(k Keeper) sdk.Invariant

PoolReservesInvariant iterates all pools and ensures the total reserves matches the module account coins

func PoolSharesInvariant

func PoolSharesInvariant(k Keeper) sdk.Invariant

PoolSharesInvariant iterates all pools and shares and ensures the total pool shares match the sum of depositor shares

func RegisterInvariants

func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper)

RegisterInvariants registers the swap module invariants

func ShareRecordsInvariant

func ShareRecordsInvariant(k Keeper) sdk.Invariant

ShareRecordsInvariant iterates all share records and asserts that they are valid

Types

type Keeper

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

Keeper keeper for the swap module

func NewKeeper

func NewKeeper(
	cdc codec.Codec,
	key storetypes.StoreKey,
	paramstore paramtypes.Subspace,
	accountKeeper types.AccountKeeper,
	bankKeeper types.BankKeeper,
) Keeper

NewKeeper creates a new keeper

func (Keeper) AfterPoolDepositCreated

func (k Keeper) AfterPoolDepositCreated(ctx sdk.Context, poolID string, depositor sdk.AccAddress, sharesOwned sdkmath.Int)

AfterPoolDepositCreated - call hook if registered

func (Keeper) BeforePoolDepositModified

func (k Keeper) BeforePoolDepositModified(ctx sdk.Context, poolID string, depositor sdk.AccAddress, sharesOwned sdkmath.Int)

BeforePoolDepositModified - call hook if registered

func (*Keeper) ClearHooks

func (k *Keeper) ClearHooks()

ClearHooks clears the hooks on the keeper

func (Keeper) DeleteDepositorShares

func (k Keeper) DeleteDepositorShares(ctx sdk.Context, depositor sdk.AccAddress, poolID string)

DeleteDepositorShares deletes a share record from the store

func (Keeper) DeletePool

func (k Keeper) DeletePool(ctx sdk.Context, poolID string)

DeletePool deletes a pool record from the store

func (Keeper) Deposit

func (k Keeper) Deposit(ctx sdk.Context, depositor sdk.AccAddress, coinA sdk.Coin, coinB sdk.Coin, slippageLimit sdk.Dec) error

Deposit creates a new pool or adds liquidity to an existing pool. For a pool to be created, a pool for the coin denominations must not exist yet, and it must be allowed by the swap module parameters.

When adding liquidity to an existing pool, the provided coins are considered to be the desired deposit amount, and the actual deposited coins may be less than or equal to the provided coins. A deposit will never be exceed the coinA and coinB amounts.

The slippage is calculated using both the price and inverse price of the provided coinA and coinB. Since adding liquidity is not directional, like a swap would be, using both the price (coinB/coinA), and the inverse price (coinA/coinB), protects the depositor from a large deviation in their deposit.

The amount deposited may only change by B' < B or A' < A -- either B depreciates, or A depreciates. Therefore, slippage can be written as a function of this depreciation d. Where the new price is B*(1-d)/A or A*(1-d)/B, and the inverse of each, and is A/(B*(1-d)) and B/(A*(1-d)) respectively.

Since 1/(1-d) >= (1-d) for d <= 1, the maximum slippage is always in the appreciating price A/(B*(1-d)) and B/(A*(1-d)). In other words, when the price of an asset depreciates, the inverse price -- or the price of the other pool asset, appreciates by a larger amount. It's this percent change we calculate and compare to the slippage limit provided.

For example, if we have a pool with 100e6 ufury and 400e6 usdx. The ufury price is 4 usdx and the usdx price is 0.25 ufury. If a depositor adds liquidity of 4e6 ufury and 14e6 usdx, a nemo price of 3.50 usdx and a usdx price of 0.29 ufury. This is a -12.5% slippage is the ufury price, and a 14.3% slippage in the usdx price.

These slippages can be calculated by S_B = ((A/B')/(A/B) - 1) and S_A ((B/A')/(B/A) - 1), simplifying to S_B = (A/A' - 1), and S_B = (B/B' - 1). An error is returned when max(S_A, S_B) > slippageLimit.

func (Keeper) GetAllDepositorShares

func (k Keeper) GetAllDepositorShares(ctx sdk.Context) (records types.ShareRecords)

GetAllDepositorShares returns all depositor share records from the store

func (Keeper) GetAllDepositorSharesByOwner

func (k Keeper) GetAllDepositorSharesByOwner(ctx sdk.Context, owner sdk.AccAddress) (records types.ShareRecords)

GetAllDepositorSharesByOwner returns all depositor share records from the store for a specific address

func (Keeper) GetAllPools

func (k Keeper) GetAllPools(ctx sdk.Context) (records types.PoolRecords)

GetAllPools returns all pool records from the store

func (Keeper) GetDepositorShares

func (k Keeper) GetDepositorShares(ctx sdk.Context, depositor sdk.AccAddress, poolID string) (types.ShareRecord, bool)

GetDepositorShares gets a share record from the store

func (Keeper) GetDepositorSharesAmount

func (k Keeper) GetDepositorSharesAmount(ctx sdk.Context, depositor sdk.AccAddress, poolID string) (sdkmath.Int, bool)

GetDepositorSharesAmount gets a depositor's shares in a pool from the store

func (Keeper) GetParams

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

GetParams returns the params from the store

func (Keeper) GetPool

func (k Keeper) GetPool(ctx sdk.Context, poolID string) (types.PoolRecord, bool)

GetPool retrieves a pool record from the store

func (Keeper) GetPoolShares

func (k Keeper) GetPoolShares(ctx sdk.Context, poolID string) (sdkmath.Int, bool)

GetPoolShares gets the total shares in a pool from the store

func (Keeper) GetSwapFee

func (k Keeper) GetSwapFee(ctx sdk.Context) sdk.Dec

GetSwapFee returns the swap fee set in the module parameters

func (Keeper) GetSwapModuleAccount

func (k Keeper) GetSwapModuleAccount(ctx sdk.Context) authtypes.ModuleAccountI

GetSwapModuleAccount returns the swap ModuleAccount

func (Keeper) IterateDepositorShares

func (k Keeper) IterateDepositorShares(ctx sdk.Context, cb func(record types.ShareRecord) (stop bool))

IterateDepositorShares iterates over all pool objects in the store and performs a callback function

func (Keeper) IterateDepositorSharesByOwner

func (k Keeper) IterateDepositorSharesByOwner(ctx sdk.Context, owner sdk.AccAddress, cb func(record types.ShareRecord) (stop bool))

IterateDepositorSharesByOwner iterates over share records for a specific address and performs a callback function

func (Keeper) IteratePools

func (k Keeper) IteratePools(ctx sdk.Context, cb func(record types.PoolRecord) (stop bool))

IteratePools iterates over all pool objects in the store and performs a callback function

func (Keeper) SetDepositorShares

func (k Keeper) SetDepositorShares(ctx sdk.Context, record types.ShareRecord)

SetDepositorShares saves a share record to the store and panics if the record is invalid

func (Keeper) SetDepositorShares_Raw

func (k Keeper) SetDepositorShares_Raw(ctx sdk.Context, record types.ShareRecord)

SetDepositorShares_Raw saves a share record to the store without validation

func (*Keeper) SetHooks

func (k *Keeper) SetHooks(sh types.SwapHooks) *Keeper

SetHooks adds hooks to the keeper.

func (Keeper) SetParams

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

SetParams sets params on the store

func (Keeper) SetPool

func (k Keeper) SetPool(ctx sdk.Context, record types.PoolRecord)

SetPool saves a pool to the store and panics if the record is invalid

func (Keeper) SetPool_Raw

func (k Keeper) SetPool_Raw(ctx sdk.Context, record types.PoolRecord)

SetPool_Raw saves a pool record to the store without any validation

func (*Keeper) SwapExactForTokens

func (k *Keeper) SwapExactForTokens(ctx sdk.Context, requester sdk.AccAddress, exactCoinA, coinB sdk.Coin, slippageLimit sdk.Dec) error

SwapExactForTokens swaps an exact coin a input for a coin b output

func (*Keeper) SwapForExactTokens

func (k *Keeper) SwapForExactTokens(ctx sdk.Context, requester sdk.AccAddress, coinA, exactCoinB sdk.Coin, slippageLimit sdk.Dec) error

SwapForExactTokens swaps a coin a input for an exact coin b output

func (Keeper) Withdraw

func (k Keeper) Withdraw(ctx sdk.Context, owner sdk.AccAddress, shares sdkmath.Int, minCoinA, minCoinB sdk.Coin) error

Withdraw removes liquidity from an existing pool from an owners deposit, converting the provided shares for the returned pool liquidity.

If 100% of the owners shares are removed, then the deposit is deleted. In addition, if all the pool shares are removed then the pool is deleted.

The number of shares must be large enough to result in at least 1 unit of the smallest reserve in the pool. If the share input is below the minimum required for positive liquidity to be remove from both reserves, a insufficient error is returned.

In addition, if the withdrawn liquidity for each reserve is below the provided minimum, a slippage exceeded error is returned.

Jump to

Keyboard shortcuts

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