Comment on page
SwapUtils
A library to be used within Swap.sol. Contains functions responsible for custody and AMM functionalities.
Contracts relying on this library must initialize SwapUtils.Swap struct then use this library for SwapUtils.Swap struct. Note that this library contains both functions called by users and admins. Admin functions should be protected within contracts using this library.
Calculate the dy, the amount of selected token that user receives and the fee of withdrawing in one token
tokenAmount
: the amount to withdraw in the pool's precisiontokenIndex
: which token will be withdrawnself
: Swap struct to read from
- the amount of token user will receive
Get the virtual price, to help calculate profit
self
: Swap struct to read from
- the virtual price, scaled to precision of POOL_PRECISION_DECIMALS
Externally calculates a swap between two tokens.
self
: Swap struct to read fromtokenIndexFrom
: the token to selltokenIndexTo
: the token to buydx
: the number of tokens to sell. If the token charges a fee on transfers, use the amount that gets transferred after the fee.
- dy the number of tokens the user will get
A simple method to calculate amount of each underlying tokens that is returned upon burning given amount of LP tokens
amount
: the amount of LP tokens that would to be burned on withdrawal
- array of amounts of tokens user will receive
A simple method to calculate prices from deposits or withdrawals, excluding fees but including slippage. This is helpful as an input into the various "min" parameters on calls to fight front-running
This shouldn't be used outside frontends for user estimates.
self
: Swap struct to read fromamounts
: an array of token amounts to deposit or withdrawal, corresponding to pooledTokens. The amount should be in each pooled token's native precision. If a token charges a fee on transfers, use the amount that gets transferred after the fee.deposit
: whether this is a deposit or a withdrawal
- if deposit was true, total amount of lp token that will be minted and if deposit was false, total amount of lp token that will be burned
return accumulated amount of admin fees of the token with given index
self
: Swap struct to read fromindex
: Index of the pooled token
- admin balance in the token's precision
swap two tokens in the pool
self
: Swap struct to read from and write totokenIndexFrom
: the token the user wants to selltokenIndexTo
: the token the user wants to buydx
: the amount of tokens the user wants to sellminDy
: the min amount the user would like to receive, or revert.
- amount of token user received on swap
Add liquidity to the pool
self
: Swap struct to read from and write toamounts
: the amounts of each token to add, in their native precisionminToMint
: the minimum LP tokens adding this amount of liquidity should mint, otherwise revert. Handy for front-running mitigation allowed addresses. If the pool is not in the guarded launch phase, this parameter will be ignored.
- amount of LP token user received
Burn LP tokens to remove liquidity from the pool.
Liquidity can always be removed, even when the pool is paused.
self
: Swap struct to read from and write toamount
: the amount of LP tokens to burnminAmounts
: the minimum amounts of each token in the pool acceptable for this burn. Useful as a front-running mitigation
- amounts of tokens the user received
Remove liquidity from the pool all in one token.
self
: Swap struct to read from and write totokenAmount
: the amount of the lp tokens to burntokenIndex
: the index of the token you want to receiveminAmount
: the minimum amount to withdraw, otherwise revert
- amount chosen token that user received
Remove liquidity from the pool, weighted differently than the pool's current balances.
self
: Swap struct to read from and write toamounts
: how much of each token to withdrawmaxBurnAmount
: the max LP token provider is willing to pay to remove liquidity. Useful as a front-running mitigation.
- actual amount of LP tokens burned in the withdrawal
withdraw all admin fees to a given address
self
: Swap struct to withdraw fees fromto
: Address to send the fees to
Sets the admin fee
adminFee cannot be higher than 100% of the swap fee
self
: Swap struct to updatenewAdminFee
: new admin fee to be applied on future transactions
update the swap fee
fee cannot be higher than 1% of each swap
self
: Swap struct to updatenewSwapFee
: new swap fee to be applied on future transactions
No description
No description
No description
No description
No description
No description
No description
Last modified 2yr ago