Crate pallet_xyk
source ·Expand description
XYK pallet
Provides functions for token operations, swapping tokens, creating token pools, minting and burning liquidity and supporting public functions
Token operation functions:
- create_pool
- mint_liquidity
- burn_liquidity
- sell_asset
- buy_asset
- compound_rewards
- provide_liquidity_with_conversion
Supporting public functions:
- calculate_sell_price
- calculate_buy_price
- calculate_sell_price_id
- calculate_buy_price_id
- get_liquidity_token
- get_burn_amount
- account_id
- settle_treasury_buy_and_burn
- calculate_balanced_sell_amount
- get_liq_tokens_for_trading
fn create_pool
-Sets the initial ratio/price of both assets to each other depending on amounts of each assets when creating pool.
-Transfers assets from user to vault and makes appropriate entry to pools map, where are assets amounts kept.
-Issues new liquidity asset in amount corresponding to amounts creating the pool, marks them as liquidity assets corresponding to this pool and transfers them to user. first_token_amount
arguments
origin
- sender of a fn, user creating the pool
first_token_id
- id of first token which will be directly inter-tradeable in a pair of first_token_id-second_token_id
first_token_amount
- amount of first token in which the pool will be initiated, which will set their initial ratio/price
second_token_id
- id of second token which will be directly inter-tradeable in a pair of first_token_id-second_token_id
second_token_amount
- amount of second token in which the pool will be initiated, which will set their initial ratio/price
Example
create_pool(
Origin::signed(1),
0,
1000,
1,
2000,
)
Account_id 1 created pool with tokens 0 and 1, with amounts 1000, 2000. Initial ratio is 1:2. Liquidity token with new id created in an amount of 1500 and transfered to user 1.
Errors
ZeroAmount
- creating pool with 0 amount of first or second token
PoolAlreadyExists
- creating pool which already exists
NotEnoughTokens
- creating pool with amounts higher then user owns
SameToken
- creating pool with same token
fn sell_token
-Sells/exchanges set amount of sold token for corresponding amount by xyk formula of bought token
arguments
origin
- sender of a fn, user creating the pool
sold_token_id
- token which will be sold
bought_token_id
- token which will be bought
sold_token_amount
- amount of token to be sold
min_amount_out
- minimal acceptable amount of bought token received after swap
Example
sell_token (
Origin::signed(1),
0,
1,
1000,
800,
)
Account_id 1 sells/exchanges 1000 token 0 for corresponding amount of token 1, while requiring at least 800 token 1
Errors
ZeroAmount
- buying 0 tokens
NoSuchPool
- pool sold_token_id - bought_token_id does not exist
NotEnoughTokens
- selling more tokens then user owns
InsufficientOutputAmount
- bought tokens to receive amount is lower then required min_amount_out
fn buy_token
-Buys/exchanges set amount of bought token for corresponding amount by xyk formula of sold token
arguments
origin
- sender of a fn, user creating the pool
sold_token_id
- token which will be sold
bought_token_id
- token which will be bought
bought_token_amount
- amount of token to be bought
max_amount_in
- maximal acceptable amount of sold token to pay for requested bought amount
Example
buy_token (
Origin::signed(1),
0,
1,
1000,
800,
)
Account_id 1 buys/exchanges 1000 tokens 1 by paying corresponding amount by xyk formula of tokens 0
Errors
ZeroAmount
- selling 0 tokens
NoSuchPool
- pool sold_token_id - bought_token_id does not exist
NotEnoughTokens
- selling more tokens then user owns
InsufficientInputAmount
- sold tokens to pay is higher then maximum acceptable value of max_amount_in
fn mint_liquidity
-Adds liquidity to pool, providing both tokens in actual ratio -First token amount is provided by user, second token amount is calculated by function, depending on actual ratio -Mints and transfers corresponding amount of liquidity token to mintin user
arguments
origin
- sender of a fn, user creating the pool
first_token_id - first token in pair
second_token_id - second token in pair
first_token_amount - amount of first_token_id, second token amount will be calculated
Example
mint_liquidity (
Origin::signed(1),
0,
1,
1000,
)
If pool token 0 - token 1 has tokens in amounts 9000:18000 (total liquidity tokens 27000)
Account_id 1 added liquidity to pool token 0 - token 1, by providing 1000 token 0 and corresponding amount of token 1. In this case 2000, as the ratio in pool is 1:2. Account_id 1 also receives corresponding liquidity tokens in corresponding amount. In this case he gets 10% of all corresponding liquidity tokens, as he is providing 10% of all provided liquidity in pool. 3000 out of total 30000 liquidity tokens is now owned by Account_id 1
Errors
ZeroAmount
- minting with 0 tokens
NoSuchPool
- pool first_token_id - second_token_id does not exist
NotEnoughTokens
- minting with more tokens then user owns, either first_token_id or second_token_id
fn burn_liquidity
-Removes tokens from liquidity pool and transfers them to user, by burning user owned liquidity tokens -Amount of tokens is determined by their ratio in pool and amount of liq tokens burned
arguments
origin
- sender of a fn, user creating the pool
first_token_id - first token in pair
second_token_id - second token in pair
liquidity_token_amount - amount of liquidity token amount to burn
Example
burn_liquidity (
Origin::signed(1),
0,
1,
3000,
)
If pool token 0 - token 1 has tokens in amounts 10000:20000 (total liquidity tokens 30000)
Account_id 1 is burning 3000 liquidity tokens of pool token 0 - token 1 As Account_id 1 is burning 10% of total liquidity tokens for this pool, user receives in this case 1000 token 0 and 2000 token 1
Errors
ZeroAmount
- burning 0 liquidity tokens
NoSuchPool
- pool first_token_id - second_token_id does not exist
NotEnoughTokens
- burning more liquidity tokens than user owns
fn compound_rewards
- Claims a specified portion of rewards, and provides them back into the selected pool.
- Wraps claim_rewards, sell_asset and mint_liquidity, so that there is minimal surplus of reward asset left after operation.
- Current impl assumes a MGX-ASSET pool & rewards in MGX asset
arguments
origin
- sender of a fn, user claiming rewards and providing liquidity to the pool
liquidity_asset_id - the pool where we provide the liquidity
amount_permille - portion of rewards to claim
Example
compound_rewards (
Origin::signed(1),
2,
1_000,
)
Claim all of the rewards, currently in MGX, and use them to provide liquidity for the pool with asset id 2
Errors
- inherits all of the errors from
claim_rewards
,sell_asset
andmint_liquidity
NoSuchLiquidityAsset
- pool with given asset id does not exist
FunctionNotAvailableForThisToken
- not available for this asset id
NotEnoughRewardsEarned
- not enough rewards available
fn provide_liquidity_with_conversion
- Given one of the liquidity pool asset, computes balanced sell amount and provides liquidity into the pool
- Wraps sell_asset and mint_liquidity
arguments
origin
- sender of a fn, user claiming rewards and providing liquidity to the pool
liquidity_asset_id - the pool where we provide the liquidity
provided_asset_id - which asset of the pool
provided_asset_amount - amount of the provided asset to use
Example
provide_liquidity_with_conversion (
Origin::signed(1),
2,
1,
1_000_000,
)
Given the liquidity pool with asset id 2, we assume that asset id 1 is one of the pool’s pair, compute balanced swap and provide liquidity into the pool
Errors
- inherits all of the errors from
sell_asset
andmint_liquidity
NoSuchLiquidityAsset
- pool wiht given asset id does not exist
FunctionNotAvailableForThisToken
- not available for this asset id
calculate_sell_price
- Supporting public function accessible through rpc call which calculates and returns bought_token_amount while providing sold_token_amount and respective reserves
calculate_buy_price
- Supporting public function accessible through rpc call which calculates and returns sold_token_amount while providing bought_token_amount and respective reserves
calculate_sell_price_id
- Same as calculate_sell_price, but providing token_id instead of reserves. Reserves are fetched by function.
calculate_buy_price_id
- Same as calculate_buy_price, but providing token_id instead of reserves. Reserves are fetched by function.
get_liquidity_token
- Supporting public function accessible through rpc call which returns liquidity_token_id while providing pair token ids
get_burn_amount
- Supporting public function accessible through rpc call which returns amounts of tokens received by burning provided liquidity_token_amount in pool of provided token ids
account_id
- Returns palled account_id
settle_treasury_buy_and_burn
- Supporting function which takes tokens to alocate to treasury and tokens to be used to burn mangata
- First step is deciding whether we are using sold or bought token id, depending which is closer to mangata token
- In second step, if tokens are mangata, they are placed to treasury and removed from corresponding pool. If tokens are not mangata, but are available in mangata pool, they are swapped to mangata and placed to treasury and removed from corresponding pool. If token is not connected to mangata, token is temporarily placed to treasury and burn treasury.
calculate_balanced_sell_amount
- Supporting public function accessible through rpc call which calculates how much amount x we need to swap from total_amount, so that after
y = swap(x)
, the resulting balance equals(total_amount - x) / y = pool_x / pool_y
- the resulting amounts can then be used to
mint_liquidity
with minimal leftover after operation
get_liq_tokens_for_trading
- Supporting public function accessible through rpc call which lists all of the liquidity pool token ids that are available for trading
Re-exports
pub use weights::WeightInfo;
pub use pallet::*;
Modules
- The
pallet
module in each FRAME pallet hosts the most important items needed to construct this pallet. - Autogenerated weights for pallet_xyk