🤽‍♂️Stableswap Pool

The clever 4A(Rx+Ry) + D pool implementation allowing for efficient swaps between like assets

1. Overview

The stableswap pool uses the 4A(Rx+Ry) + D formula, resulting in a constant price ∆x / ∆y = 1. More details about 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

  • init_amp - This is the current amplification used in the pool

  • init_amp_time - This is the start time when amplification starts to scale up or down

  • next_amp - This is the target amplification to reach at next_amp_time

  • next_amp_time - This is the target amplification to reach at next_amp_time

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

StablePoolParams

This structure holds stableswap pool parameters

  • amp - The current stableswap pool amplification

StablePoolConfig

This structure stores a stableswap pool's configuration

  • amp - The stableswap pool amplification

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 a swap operation with the specified parameters. Asset is imported from the asset file

accumulate_prices

(env: Env, config: &Config, x: Uint128, x_precision: u8, y: Uint128, y_precision: u8) -> 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

update_config

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

Updates the pool configuration

start_changing_amp

(mut config: Config, deps: DepsMut, env: Env, next_amp: u64, next_amp_time: u64) -> Result<(), ContractError>

Start changing the pool amplification. Only the contract owner can call this

stop_changing_amp

(mut config: Config, deps: DepsMut, env: Env) -> StdResult<()>

Stop changing the pool amplification. Only the contract owner can call this

compute_current_amp

(config: &Config, env: &Env) -> StdResult<u64>

Internal function used to determine the current pool amplification as it's being scaled

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, env: Env, offer_asset: Asset) -> StdResult<SimulationResponse>

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

query_reverse_simulation

deps: Deps, env: Env, 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, env: Env) -> StdResult<ConfigResponse>

Returns the pair contract configuration

amount_of

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

Returns an amount of coins

compute_swap

(offer_pool: Uint128, offer_precision: u8, ask_pool: Uint128, ask_precision: u8, offer_amount: Uint128, commission_rate: Decimal, amp: u64) -> StdResult<(Uint128, Uint128, Uint128)>

Return the result of a swap

compute_offer_amount

(offer_pool: Uint128, offer_precision: u8, ask_pool: Uint128, ask_precision: u8, ask_amount: Uint128, commission_rate: Decimal, amp: u64) -> StdResult<(Uint128, Uint128, Uint128)>

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

adjust_precision

(value: Uint128, current_precision: u8, new_precision: u8) -> StdResult<Uint128>

Internal function used to return a value using a newly specified precision

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 the total amount of LP tokens currently minted. Asset is imported from the asset file

Last updated

Was this helpful?