Swap
This contract is responsible for custody of closely pegged assets (eg. group of stablecoins) and automatic market making system. Users become an LP (Liquidity Provider) by depositing their tokens in desired ratios for an exchange of the pool token that represents their share of the pool. Users can burn pool tokens and withdraw their share of token(s).
Each time a swap between the pooled tokens happens, a set fee incurs which effectively gets distributed to the LPs.
In case of emergencies, admin can pause additional deposits, swaps, or single-asset withdraws - which stops the ratio of the tokens in the pool from changing. Users can always withdraw their tokens via multi-asset withdraws.
Most of the logic is stored as a library
SwapUtils
for the sake of reducing contract's deployment size.Initializes this Swap contract with the given parameters. This will also clone a LPToken contract that represents users' LP positions. The owner of LPToken will be this contract - which means only this contract is allowed to mint/burn tokens.
_pooledTokens
: an array of ERC20s this pool will acceptdecimals
: the decimals to use for each pooled token, eg 8 for WBTC. Cannot be larger than POOL_PRECISION_DECIMALSlpTokenName
: the long-form name of the token to be deployedlpTokenSymbol
: the short symbol for the token to be deployed_a
: the amplification coefficient _ n _ (n - 1). See the StableSwap paper for details_fee
: default swap fee to be initialized with_adminFee
: default adminFee to be initialized withlpTokenTargetAddress
: the address of an existing LPToken contract to use as a target
Return A, the amplification coefficient _ n _ (n - 1)
See the StableSwap paper for details
- A parameter
Return A in its raw precision form
See the StableSwap paper for details
- A parameter in its raw precision form
Return address of the pooled token at given index. Reverts if tokenIndex is out of range.
index
: the index of the token
- address of the token at given index
Return the index of the given token address. Reverts if no matching token is found.
tokenAddress
: address of the token
- the index of the given token address
Return current balance of the pooled token at given index
index
: the index of the token
- current balance of the pooled token at given index with token's native precision
Get the virtual price, to help calculate profit
- the virtual price, scaled to the POOL_PRECISION_DECIMALS
Calculate amount of tokens you receive on swap
tokenIndexFrom
: the token the user wants to selltokenIndexTo
: the token the user wants to buydx
: the amount of tokens the user wants to sell. If the token charges a fee on transfers, use the amount that gets transferred after the fee.
- amount of tokens the 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.
amounts
: 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
- token amount the user will receive
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 be burned on withdrawal
- array of token balances that the user will receive
Calculate the amount of underlying token available to withdraw when withdrawing via only single token
tokenAmount
: the amount of LP token to burntokenIndex
: index of which token will be withdrawn
- availableTokenAmount calculated amount of underlying token available to withdraw
This function reads the accumulated amount of admin fees of the token with given index
index
: Index of the pooled token
- s token balance in the token's precision
Swap two tokens using this pool
tokenIndexFrom
: the token the user wants to swap fromtokenIndexTo
: the token the user wants to swap todx
: the amount of tokens the user wants to swap fromminDy
: the min amount the user would like to receive, or revert.deadline
: latest timestamp to accept this transaction
Add liquidity to the pool with the given amounts of tokens
amounts
: 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 mitigationdeadline
: latest timestamp to accept this transaction
- amount of LP token user minted and received
Burn LP tokens to remove liquidity from the pool. Withdraw fee that decays linearly over period of 4 weeks since last deposit will apply.
Liquidity can always be removed, even when the pool is paused.
amount
: 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 mitigationdeadline
: latest timestamp to accept this transaction
- amounts of tokens user received
Remove liquidity from the pool all in one token. Withdraw fee that decays linearly over period of 4 weeks since last deposit will apply.
tokenAmount
: the amount of the token you want to receivetokenIndex
: the index of the token you want to receiveminAmount
: the minimum amount to withdraw, otherwise revertdeadline
: latest timestamp to accept this transaction
- amount of chosen token user received
Remove liquidity from the pool, weighted differently than the pool's current balances. Withdraw fee that decays linearly over period of 4 weeks since last deposit will apply.
amounts
: 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.deadline
: latest timestamp to accept this transaction
- amount of LP tokens burned
Withdraw all admin fees to the contract owner
Update the admin fee. Admin fee takes portion of the swap fee.
newAdminFee
: new admin fee to be applied on future transactions
Update the swap fee to be applied on swaps
newSwapFee
: new swap fee to be applied on future transactions
Start ramping up or down A parameter towards given futureA and futureTime Checks if the change is too rapid, and commits the new A value only when it falls under the limit range.
futureA
: the new A to ramp towardsfutureTime
: timestamp when the new A should be reached
Stop ramping A immediately. Reverts if ramp A is already stopped.
No description
No description
No description
No description
No description
No description
No description
No description
No description
No description
Last modified 1yr ago