At launch, Astroport will only support two pool types:
Depending on your use-cases and needs as well as the types of assets you want to integrate, one of these pools will be better than the other.
The contract code for the constant product pool can be found here and the code for the stableswap invariant pool is here.
A user can provide liquidity to a pool by calling
provide_liquidity. Users can also withdraw liquidity by calling
Whenever liquidity is deposited into a pool, special tokens known as "liquidity tokens" are minted to the provider’s address, in proportion to how much liquidity they contributed to the pool. These tokens are a representation of a liquidity provider’s contribution to a pool. Whenever a trade occurs, the lp_commission is distributed pro-rata to all LPs in the pool at the moment of the trade. To receive the underlying liquidity back plus accrued LP fees, LPs must burn their liquidity tokens.
When providing liquidity from a smart contract, the most important thing to keep in mind is that the amount of tokens deposited into a pool and the amount of tokens withdrawn later from the pool will most likely not be the same (even if stableswap encourages a constant 1:1 ratio between all assets in the pool). In the case of constant product pools, as token prices in the pool change, so do the respective token amounts that a LP can withdraw.
As an example for stableswap pools, let's say the global ratio between two tokens x:y is 1.01:1 (1 x = 0.99 y), but the current ratio between the tokens in an Astroport pair is 1:1.01 (1 x = 1.01 y). Let's also say that someone may decide to LP in the x:y Astroport pool at the current 1:1.01 ratio. As the Astroport pool gets arbitraged to the global ratio, the amount of x & y tokens that the LP can withdraw changes because the total amounts of x & y tokens in the pool also change.
As an example for constant product pools, let's say the global ratio between two tokens x:y is 10:2 (i.e. 1 x = 0.2 y), but the current ratio between the tokens in an Astroport pair is 5:2 (1 x = 0.4 y). Let's also say that someone may decide to LP in the x:y Astroport pool at the current 5:2 ratio. As the Astroport pool gets arbitraged to the global ratio, the amount of x & y tokens that the LP can withdraw changes because the total amounts of x & y tokens in the pool also change.
Note that before executing the
provide_liqudityoperation, a user must allow the pool contract to take tokens from their wallet
Slippage Tolerance for Providing Liquidity
If an LP specifies a slippage tolerance when they provide liquidity, the pool contract makes sure that the transaction goes through only if the pool price does not change more than tolerance.
As an example, let's say someone LPs in a constant product pool and specifies a 1% slippage tolerance. The user LPs 200 axlUSDC and 1 ASSET. A similar example could be set with a stableswap pool and a pair of 200 axlUSDC and 200 ASSET.
With a 1% slippage tolerance, amountaxlUSDCMin (the minimum amount of axlUSDC to LP) should be set to 198 axlUSDC, and amountASSETMin (the minimum amount of ASSET to LP) should be set to .99 ASSET. This means that, in a worst case scenario, liquidity will be added at a pool rate of 198 ASSET/1 axlUSDC or 202.02 axlUSDC/1 ASSET (200 axlUSDC + .99 ASSET). If the contract cannot add liquidity within these bounds (because the pool ratio changed more than the tolerance), the transaction will revert.
If the contract cannot add liquidity within these bounds (because the pool ratio changed more than the tolerance), the transaction will revert.
Slippage Tolerance for Swaps
Astroport has two options to protect traders against slippage during swaps:
max_spread- The spread is calculated as the difference between the ask amount (using the constant pool price) before and after the swap operation. Once max_spread is set, it will be compared against the actual swap spread. In case the swap spread exceeds the provided max limit, the swap will fail.
Note that the spread is calculated before commission deduction in order to properly represent the pool's ratio change.
belief_priceis provided in combination with
max_spread, the pool will check the difference between the return amount (using
belief_price) and the real pool price.
Note that Astroport has the default value for the spread set to 0.5% and the max allowed spread set to 50%.