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.Deploys this Swap contract with given parameters as default values. This will also deploy a LPToken that represents users LP position. The owner of LPToken will be this contract - which means only this contract is allowed to mint new 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 with_allowlist
: address of allowlist contract for guarded launch
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
Reads and returns the address of the allowlist that is set during deployment of this contract
- the address of the allowlist contract casted to the IAllowlist interface
Return timestamp of last deposit of given address
- timestamp of the last deposit made by the given 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.
account
: address that is depositing or withdrawing tokensamounts
: 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
account
: the address that is withdrawing tokensamount
: 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
account
: the address that is withdrawing tokenstokenAmount
: the amount of LP token to burntokenIndex
: index of which token will be withdrawn
- availableTokenAmount calculated amount of underlying tokenavailable 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 given amounts during guarded launch phase. Only users with valid address and proof can successfully call this function. When this function is called after the guarded release phase is over, the merkleProof is ignored.
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 transactionmerkleProof
: data generated when constructing the allowlist merkle tree. Users can get this data off chain. Even if the address is in the allowlist, users must include a valid proof for this call to succeed. If the pool is no longer in the guarded release phase, this parameter is ignored.
- amount of LP token user minted and received
Burn LP tokens to remove liquidity from the pool.
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.
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.
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.
Disables the guarded launch phase, removing any limits on deposit amounts and addresses
Reads and returns current guarded status of the pool
- guarded_ boolean value indicating whether the deposits should be guarded
No description
No description
No description
No description
No description
No description
No description
No description
No description
No description
Last modified 1yr ago