Adding & Removing Liquidity
Metapool Factory: Pools
Factory pools are permissionless metapools that can be deployed by anyone. New pools are deployed using Factory.deploy_metapool.
Implementation Contracts
Each pool deployed by the factory is a thin proxy contract created with Vyperβs create_forwarder_to
. The implementation contract targetted by the proxy is determined based on the base pool used. This is the same technique that was used to create pools in Uniswap V1.
The implementation contracts used for pools are deployed to the mainnet at the following addresses:
3pool: 0x61bB2F4a4763114268a47fB990e633Cb40f045F8
When interacting with a factory pool you should use the ABI at the corresponding implementation address:
Getting Pool Info
StableSwap.coins
(i: uint256) β address: view
Getter for the array of swappable coins within the pool. The last coin will always be the LP token of the base pool.
StableSwap.balances
(i: uint256) β uint256: view
Getter for the pool balances array.
StableSwap.A
() β uint256: view
The amplification coefficient or the pool.
StableSwap.get_virtual_price
() β uint256: view
The current price of the pool LP token relative to the underlying pool assets. Given as an integer with 1e18 precision.
StableSwap.fee
() β uint256: view
The pool swap fee, as an integer with 1e10 precision.
StableSwap.admin_fee
() β uint256: view
The percentage of the swap fee that is taken as an admin fee, as an integer with with 1e10 precision.
For factory pools this is hardcoded at 50% (5000000000
).
Making Exchanges
StableSwap.get_dy
(i: int128, j: int128, dx: uint256) β uint256: view
Get the amount received (βdyβ) when performing a swap between two assets within the pool.
Index values can be found using the coins public getter method, or get_coins within the factory contract.
i
: Index value of the coin to send.j
: Index value of the coin to receive.dx
: The amount ofi
being exchanged.
Returns the amount of j
received.
StableSwap.get_dy_underlying
(i: int128, j: int128, dx: uint256) β uint256: view
Get the amount received (βdyβ) when swapping between two underlying assets within the pool.
Index values can be found using get_underlyingcoins within the factory contract.
i
: Index value of the token to send.j
: Index value of the token to receive.dx
: The amount ofi
being exchanged.
Returns the amount of j
received.
StableSwap.exchange
(i: int128, j: int128, dx: uint256, min_dy: uint256, _receiver: address = msg.sender) β uint256: nonpayable
Performs an exchange between two tokens.
Index values can be found using the coins public getter method, get_coins within the factory contract.
i
: Index value of the token to send.j
: Index value of the token to receive.dx
: The amount ofi
being exchanged.min_dy
: The minimum amount ofj
to receive. If the swap would result in less, the transaction will revert._receiver
: An optional address that will receivej
. If not given, defaults to the caller.
Returns the amount of j
received in the exchange.
StableSwap.exchange_underlying
(i: int128, j: int128, dx: uint256, min_dy: uint256, _receiver: address = msg.sender) β uint256: nonpayable
Perform an exchange between two underlying coins.
Index values can be found using getunderlyingcoins within the factory contract.
i
: Index value of the underlying token to send.j
: Index value of the underlying token to receive.dx
: The amount ofi
being exchanged.min_dy
: The minimum amount ofj
to receive. If the swap would result in less, the transaction will revert._receiver
: An optional address that will receivej
. If not given, defaults to the caller.
Returns the amount of j
received in the exchange.
Adding and Removing Liquidity
Note that if you wish to add or remove liqudity using the underlying assets within the base pool, you must use a depositor contract.
StableSwap.calc_token_amount
(_amounts: uint256[2], _is_deposit: pool β uint256: view
Estimate the amount of LP tokens minted or burned based on a deposit or withdrawal.
This calculation accounts for slippage, but not fees. It should be used as a basis for determining expected amounts when calling add_liquidity but should not be considered to be precise!
_amounts
: Amount of each coin being deposited. Amounts correspond to the tokens at the same index locations within coins._is_deposit
: setTrue
for deposits,False
for withdrawals.
Returns the expected amount of LP tokens minted or burned.
StableSwap.calc_withdraw_one_coin
(_burn_amount: uint256, i: int128) β uint256: view
Calculate the amount received when withdrawing and unwrapping in a single coin. Useful for setting _max_burn_amount
when calling remove_liquidity_one_coin.
_pool
: Address of the pool to deposit into._token_amount
: Amount of LP tokens to burn in the withdrawal.i
: Index value of the underlying coin to withdraw. Can be found using the coins getter method.
Returns the expected amount of coin received.
StableSwap.add_liquidity
(_deposit_amounts: uint256[2], _min_mint_amount: uint256, _receiver: address = msg.sender) β uint256: nonpayable
Deposits coins into to the pool and mints new LP tokens.
_deposit_amounts
: List of amounts of underlying coins to deposit. Amounts correspond to the tokens at the same index locations within coins_min_mint_amount
: Minimum amount of LP tokens to mint from the deposit._receiver
: Optional address that receives the LP tokens. If not specified, they are sent to the caller.
Returns the amount of LP tokens that were minted in the deposit.
StableSwap.remove_liquidity
(_burn_amount: uint256, _min_amounts: uint256[2], _receiver: address = msg.sender) β uint256[2]: nonpayable
Withdraws coins from the pool and burns LP tokens.
Withdrawal amounts are based on current deposit ratios. Withdrawals using this method do not incur a fee.
_burn_amount
: Quantity of LP tokens to burn in the withdrawal. Amounts correspond to the tokens at the same index locations within coins_min_amounts
: Minimum amounts of coins to receive._receiver
: Optional address that receives the withdrawn coins. If not specified, the coins are sent to the caller.
Returns a list of the amounts of coins that were withdrawn.
StableSwap.remove_liquidity_imbalance
(_amounts: uint256[2], _max_burn_amount: uint256, _receiver: address = msg.sender) β uint256: nonpayable
Withdraw coins in an imbalanced amount.
_amounts
: List of amounts of underlying coins to withdraw. Amounts correspond to the tokens at the same index locations within coins_max_burn_amount
: Maximum number of LP token to burn in the withdrawal._receiver
: Optional address that receives the withdrawn coins. If not specified, the coins are sent to the caller.
Returns the amount of the LP tokens burned in the withdrawal.
StableSwap.remove_liquidity_one_coin
(_burn_amount: uint256, i: int128, _min_received: uint256, _receiver: address = msg.sender) β uint256: nonpayable
Withdraw a single asset from the pool.
_burn_amount
: Amount of LP tokens to burn in the withdrawal.i
: Index value of the coin to withdraw. Can be found using the coins getter method._min_amount
: Minimum amount of the coin to receive_receiver
: Optional address that receives the withdrawn coin. If not specified, the coin is sent to the caller.
Returns the amount of the coin received in the withdrawal.
Claiming Admin Fees
StableSwap.withdraw_admin_fees(): nonpayable
Transfer admin fees to the fee distributor, allowing the fees to be claimed by BURROW holders.
Anyone can call this method. The destination address for the fees is hardcoded. To simplify fee distribution, this method swaps the admin balance of the non-base pool LP token into the base pool LP token.
LP Tokens
Factory pools differ from traditional MMF pools in that the pool contract is also the LP token. This improves gas efficiency and simplifies the factory deployment process.
Pool contracts adhere to the CRC20 standards As such, the following methods are available:
Token Info
StableSwap.name
() β String[64]: view
The name of the pool / LP token.
StableSwap.symbol
() β String[32]: view
The token symbol.
StableSwap.decimals
() β uint256: view
The number of decimals for the token. MMF pool tokens always use 18 decimals.
StableSwap.totalSupply
() β uint256: view
Balances and Allowances
StableSwap.balanceOf
(_addr: address) β uint256: view
Getter for the current balance of an account.
StableSwap.allowance
(_owner: address, _spender: address) β uint256: view
Getter for the number of tokens _owner
has approve _spender
to transfer on their behalf.
2**256-1
it is considered infinite approval. The approval amount will not decrease when tokens are transferred.
Transfers and Approvals
StableSwap.approve
(_spender: address, _value: uint256) β bool: nonpayable
Approve _spender
to transfer up to _value
tokens on behalf of the caller.
If an approval is given for 2**256-1
it is considered infinite. The approval amount will not decrease when tokens are transferred, reducing gas costs.
_spender
Address to set the approval for_value
Amount of the callerβs tokens that_spender
is permitted to transfer
Returns True
on success. Reverts on failure.
StableSwap.transfer
(_to: address, _value: uint256) β bool: nonpayable
Transfer tokens from the caller to the given address.
_to
: Address receiving the tokens._value
: Amount of tokens to be transferred.
Returns True
on a successful call. Reverts on failure.
StableSwap.transferFrom
(_from: address, _to: address, _value: uint256) β bool: nonpayable
Transfer tokens between two addresses. The caller must have been given approval to transfer tokens on behalf of _from
or the call will revert.
_from
: The address to transfer the tokens from._to
: Address receiving the tokens._value
: mount of tokens to be transferred.
Returns
True
on a successful call. Reverts on failure.
Last updated