🏊‍♀️Constant Product Pool

The famous xy=k pool implementation

1. Overview

The constant product pool uses the widely known xy=k formula. More details around how the pool functions can be found here.

2. Variables & Functions

Constants

Name
Description

CONTRACT_NAME

The constant product contract name

CONTRACT_VERSION

The contract version

INSTANTIATE_TOKEN_REPLY_ID

A reply call code ID used for sub-messages

DEFAULT_SLIPPAGE

Default slippage tolerance enforced on swaps

MAX_ALLOWED_SLIPPAGE

The maximum possible slippage tolerance

TWAP_PRECISION

Decimal precision for TWAP results

Structs

Name
Description
Contains

Config

This struct holds general contract parameters

  • pair_info - General pair information (e.g pair type)

  • factory_addr - The Astroport factory contract address

  • block_time_last - The last timestamp when the pair contract update the asset cumulative prices

  • price0_cumulative_last - The last cumulative price for asset 0

  • price1_cumulative_last - The last cumulative price for asset 1

InstantiateMsg

This struct holds parameters used to instantiate the pair contract

  • asset_infos - Information about the two assets in the pool

  • token_code_id - The token contract code ID used for the tokens in the pool

  • factory_addr - The factory contract address

  • init_params - Optional binary serialised parameters for custom pool types

PoolResponse

This struct is used to return a query result with the total amount of LP tokens and the two assets in a specific pool

  • assets - The assets in the pool together with asset amounts

  • total_share - The total amount of LP tokens currently issued

ConfigResponse

This struct is used to return a query result with the general contract configuration

  • block_time_last - Last timestamp when the cumulative prices in the pool were updated

  • params - The pool's parameters

SimulationResponse

This structure holds the parameters that are returned from a swap simulation response

  • return_amount - The amount of ask assets returned by the swap

  • spread_amount - The spread used in the swap operation

  • commission_amount - The amount of fees charged by the transaction

ReverseSimulationResponse

This structure holds the parameters that are returned from a reverse swap simulation response

  • offer_amount - The amount of offer assets returned by the reverse swap

  • spread_amount - The spread used in the swap operation

  • commission_amount - The amount of fees charged by the transaction

CumulativePricesResponse

This structure is used to return a cumulative prices query response

  • assets - The two assets in the pool to query

  • total_share - The total amount of LP tokens currently issued

  • price0_cumulative_last - The last value for the token0 cumulative price

  • price1_cumulative_last - The last value for the token1 cumulative price

Functions

Name
Params
Description

instantiate

(deps: DepsMut, _env: Env, _info: MessageInfo, msg: InstantiateMsg) -> Result<Response, ContractError>

Instantiate the pair contract

reply

(deps: DepsMut, _env: Env, msg: Reply) -> Result<Response, ContractError>

The entry point to the contract for processing replies from submessages

execute

(deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg) -> Result<Response, ContractError>

Execute a function from the contract

receive_cw20

(deps: DepsMut, env: Env, info: MessageInfo, cw20_msg: Cw20ReceiveMsg) -> Result<Response, ContractError>

Receives a message of type [Cw20ReceiveMsg] and processes it depending on the received template

provide_liquidity

(deps: DepsMut, env: Env, info: MessageInfo, assets: [Asset; 2], slippage_tolerance: Option<Decimal>, auto_stake: Option<bool>, receiver: Option<String>) -> Result<Response, ContractError>

Provide liquidity in the pair. Asset is imported from the asset file

mint_liquidity_token_message

(deps: Deps, config: &Config, env: Env, recipient: Addr, amount: Uint128, auto_stake: bool) -> Result<Vec, ContractError>

Internal function used to mint LP tokens for a beneficiary and auto stake the tokens in the Generator contract (if auto staking is specified)

withdraw_liquidity

(deps: DepsMut, env: Env, info: MessageInfo, sender: Addr, amount: Uint128) -> Result<Response, ContractError>

Withdraw liquidity from the pool

get_share_in_assets

(pools: &[Asset; 2], amount: Uint128, total_share: Uint128) -> Vec<Asset>

Returns the amount of pool assets that correspond to an amount of LP tokens. Asset is imported from the asset file

swap

(deps: DepsMut, env: Env, info: MessageInfo, sender: Addr, offer_asset: Asset, belief_price: Option<Decimal>, max_spread: Option<Decimal>, to: Option<Addr>) -> Result<Response, ContractError>

Performs an swap operation with the specified parameters. Asset is imported from the asset file

accumulate_prices

(env: Env, config: &Config, x: Uint128, y: Uint128) -> StdResult<Option<(Uint128, Uint128, u64)>>

Internal function used to accumulate token prices for the assets in the pool. Note that this function shifts block_time when any of the token prices is zero in order to not fill an accumulator with a null price for that period

calculate_maker_fee

(pool_info: AssetInfo, commission_amount: Uint128, maker_commission_rate: Decimal) -> Option<Asset>

Internal function used to calculate the amount of fees the Maker contract gets according to specified pair parameters. AssetInfo is imported from the asset file

migrate

(_deps: DepsMut, _env: Env, _msg: MigrateMsg) -> StdResult<Response>

Used for the contract migration

query

(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary>

Query the contract

query_pair_info

(deps: Deps) -> StdResult<PairInfo>

Returns information about the pair contract. PairInfo is imported from the asset file

query_pool

(deps: Deps) -> StdResult<PoolResponse>

Returns the amounts of assets in the pair contract as well as the amount of LP tokens currently issued

query_share

(deps: Deps, amount: Uint128) -> StdResult<Vec<Asset>>

Returns the amount of assets that could be withdrawn from the pool using a specific amount of LP tokens. Asset is imported from the asset file

query_simulation

(deps: Deps, offer_asset: Asset) -> StdResult<SimulationResponse>

Returns information about a swap simulation. Asset is imported from the asset file

query_reverse_simulation

(deps: Deps, ask_asset: Asset) -> StdResult<ReverseSimulationResponse>

Returns information about a reverse swap simulation. Asset is imported from the asset file

query_cumulative_prices

(deps: Deps, env: Env) -> StdResult<CumulativePricesResponse>

Returns information about cumulative prices for the assets in the pool

query_config

(deps: Deps) -> StdResult<ConfigResponse>

Returns the pair contract configuration

amount_of

(coins: &[Coin], denom: String) -> Uint128

Returns an amount of coins

compute_swap

(offer_pool: Uint128, ask_pool: Uint128, offer_amount: Uint128, commission_rate: Decimal) -> StdResult<(Uint128, Uint128, Uint128)>

Returns the result of a swap

compute_offer_amount

(offer_pool: Uint128, ask_pool: Uint128, ask_amount: Uint128, commission_rate: Decimal) -> StdResult<(Uint128, Uint128, Uint128)>

Returns an amount of offer assets for a specified amount of ask assets

assert_max_spread

(belief_price: Option<Decimal>, max_spread: Option<Decimal>, offer_amount: Uint128, return_amount: Uint128, spread_amount: Uint128) -> Result<(), ContractError>

This is an internal function; if belief_price and max_spread are both specified, we compute a new spread, otherwise we just use the swap spread to check max_spread

assert_slippage_tolerance

(slippage_tolerance: Option<Decimal>, deposits: &[Uint128; 2], pools: &[Asset; 2]) -> Result<(), ContractError>

This is an internal function that enforces slippage tolerance for swaps. Asset is imported from the asset file

pool_info

(deps: Deps, config: Config) -> StdResult<([Asset; 2], Uint128)>

Returns the total amount of assets in the pool as well as

Last updated

Was this helpful?