๐ฑPair
The pair contract is the pool. Creating a new pool should be done via the pool factory, so that the pool is indexed in the pool registry stored by the factory. A pool can be created with native, ibc or cw20 tokens.
Constant Product
For now, White Whale pools make prices based on a constant product invariant:
Where X and Y are the reserves for each token in the pool, while k is a constant. The product of the number of tokens on each side of the pool should remain constant across trading operations. With X being the current balance of the pool's source asset and and Y being that of the target asset:
To determine the value of the ask asset โy given the trader's offered asset โx:
The market price is calculated by dividing the number of pool's target token into the source asset (also called the pool ratio).
The spread between the executed and the expected trade is:
The deeper the pool the smaller the spread.
Liquidity Provision
A user or bot can provide liquidity by sending a provide_liquidity
message and withdraw liquidity with withdraw_liquidity
. Whenever liquidity is deposited into a pool, special tokens known as liquidity (LP) tokens are minted to the provider's address, in proportion to how much liquidity it contributed to the pool. These tokens are a representation of a liquidity provider's contribution to a pool. Whenever a trade occurs, the swap_fee
percentage, which is a parameter specified by the PoolFee
, is distributed pro-rata to all LPs in the pool at the moment of the trade. To receive the underlying liquidity back, plus commission fees that were accrued while their liquidity was locked, LPs must burn their liquidity tokens, which is done via withdraw_liquidity
.
Fees
There are three types of fees associated to the pools, namely swap_fee
, protocol_fee
and burn_fee
. The swap_fee
remains in the pool, causing a permanent increase in the constant product K. The value of this permanently increased pool goes to all LPs.
The protocol_fee
goes to the protocol, and it is to be collected by the Fee Collector contract of the Liquidity Hub. The protocol revenue is then distributed to WHALE stakers in the form of token buybacks.
The burn_fee
is a percentage of the tokens that is burned whenever a swap takes place.
Feature toggle
Pools contain a feature toggle, containing the following properties: withdrawals_enabled
, deposits_enabled
and swaps_enabled
. Those features are `ON by default, but could be changed via governance. They could be changed in different scenarios, the ones we could envision include for example if a critical bug is found in a given feature, it could be shut down while is being fixed to avoid exploits. Additionally, if for example liquidity is desired to be migrated to another pool, deposits could be disabled to prevent bots or users to deposit liquidity in the pool.
The code for the pair contract can be found here.
The following are the messages that can be executed on the pair contract:
Instantiate
Instantiates the pair.
Key | Type | Description |
---|---|---|
| [AssetInfo; 2] | Information about the two assets |
| u64 | Code if for the token contract. Used by the factory to create the LP token contract |
| [u8; 2] | Decimal places for the given assets |
| PoolFee | Pool fees for the given pair |
| String | The variant of pair to create, it can be ConstantProduct or StableSwap |
| String | If true, the pair will use the token factory to create the LP token. If false, it will use a cw20 token instead |
Migrate
Migrates a pair. This is to be triggered by the owner of the contract, i.e. the pool factory, via the migrate_pair
message.
ExecuteMsg
Collect protocol fees
Sends the accrued protocol fees to the Fee Collector. This action can be triggered by anyone.
Provide liquidity
Provides liquidity to the pool.
Note: in case of providing liquidity with a cw20 token, the message increase_allowance should be called on the cw20 token contract * before* cw20 tokens can be transferred to the pool.
Key | Type | Description |
---|---|---|
| [Asset; 2] | Assets to provide liquidity with |
| Option<Decimal> | If set, the transaction will succeed if the price in the pool moves less than the given slippage tolerance |
| Option<String> | Receiver address for the LP tokens in case it is different from the sender |
Swap
Swap a token, either native/ibc/factory token or cw20 token.
Key | Type | Description |
---|---|---|
| Asset | Asset to swap |
| Option<Decimal> | Belief price of the asset |
| Option<Decimal> | Max desired spread to perform the swap with |
| Option<String> | Receiver address for ask asset in case it is different from the sender |
Update config
Updates the configuration of the pool.
Key | Type | Description |
---|---|---|
| Option<String> | New owner of the contract |
| Option<String> | New fee collector address |
| Option<PoolFee> | New pool fees |
| Option<FeatureToggle> | If set, toggles features on/off based on the parameters specified in |
Withdraw
Withdraws liquidity from the pool. In case the LP token is a cw20 token, the message needs to be sent to the cw20 token.
The LP amount to be withdrawn needs to be sent as funds
together with this transaction.
Queries
Config
Retrieves the configuration of the pool.
Pair
Retrieves information of the pool pair.
Pool
Retrieves information about the pool, i.e. liquidity provided, asset infos and total LP shares.
Protocol fees
Retrieves the protocol fees on the pool. If all_time
is true
, it will return the fees collected since the inception of the pool. On the other hand, if all_time
is set to false
, only the fees that has been accrued by the pool but not collected by the fee collector will be returned.
Alternatively, if asset_id
is set the accrued fees (non collected) for that specific asset will be returned.
Key | Type | Description |
---|---|---|
| Option<String> | Asset id to return the protocol fees for |
| Option<bool> | Defines whether to return all time fees or not |
Burned fees
Retrieves the fees that have been burned by pool. If asset_id
is set, only the burned fees for that specific asset will be returned, otherwise it returns burned fees for both assets in the pool.
Key | Type | Description |
---|---|---|
| Option<String> | Asset id to return the burned fees for |
Simulation
Performs a swap simulation for the given token.
Key | Type | Description |
---|---|---|
| Asset | Asset to simulate swap for |
Reserve simulation
Performs a reverse swap simulation, i.e. given the ask asset, how much of the offer asset is needed to perform the swap.
Key | Type | Description |
---|---|---|
| Asset | Desired asset to get after the swap |
Last updated