SwapFlashLoan
Swap - A StableSwap implementation in solidity.
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.
Methods
MAX_BPS
Returns
addLiquidity
Add liquidity to the pool with the given amounts of tokens
Parameters
Returns
calculateRemoveLiquidity
A simple method to calculate amount of each underlying tokens that is returned upon burning given amount of LP tokens
Parameters
Returns
calculateRemoveLiquidityOneToken
Calculate the amount of underlying token available to withdraw when withdrawing via only single token
Parameters
Returns
calculateSwap
Calculate amount of tokens you receive on swap
Parameters
Returns
calculateTokenAmount
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.
Parameters
Returns
flashLoan
Borrow the specified token from this pool for this transaction only. This function will call IFlashLoanReceiver(receiver).executeOperation
and the receiver
must return the full amount of the token and the associated fee by the end of the callback transaction. If the conditions are not met, this call is reverted.
Parameters
flashLoanFeeBPS
Returns
getA
Return A, the amplification coefficient * n * (n - 1)
See the StableSwap paper for details
Returns
getAPrecise
Return A in its raw precision form
See the StableSwap paper for details
Returns
getAdminBalance
This function reads the accumulated amount of admin fees of the token with given index
Parameters
Returns
getToken
Return address of the pooled token at given index. Reverts if tokenIndex is out of range.
Parameters
Returns
getTokenBalance
Return current balance of the pooled token at given index
Parameters
Returns
getTokenIndex
Return the index of the given token address. Reverts if no matching token is found.
Parameters
Returns
getVirtualPrice
Get the virtual price, to help calculate profit
Returns
initialize
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.
Parameters
owner
Returns the address of the current owner.
Returns
pause
Pause the contract. Revert if already paused.
paused
Returns true if the contract is paused, and false otherwise.
Returns
protocolFeeShareBPS
Returns
rampA
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.
Parameters
removeLiquidity
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.
Parameters
Returns
removeLiquidityImbalance
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.
Parameters
Returns
removeLiquidityOneToken
Remove liquidity from the pool all in one token. Withdraw fee that decays linearly over period of 4 weeks since last deposit will apply.
Parameters
Returns
renounceOwnership
Leaves the contract without owner. It will not be possible to call onlyOwner
functions anymore. Can only be called by the current owner. NOTE: Renouncing ownership will leave the contract without an owner, thereby removing any functionality that is only available to the owner.
setAdminFee
Update the admin fee. Admin fee takes portion of the swap fee.
Parameters
setFlashLoanFees
Updates the flash loan fee parameters. This function can only be called by the owner.
Parameters
setSwapFee
Update the swap fee to be applied on swaps
Parameters
stopRampA
Stop ramping A immediately. Reverts if ramp A is already stopped.
swap
Swap two tokens using this pool
Parameters
Returns
swapStorage
Returns
transferOwnership
Transfers ownership of the contract to a new account (newOwner
). Can only be called by the current owner.
Parameters
unpause
Unpause the contract. Revert if already unpaused.
withdrawAdminFees
Withdraw all admin fees to the contract owner
Events
AddLiquidity
Parameters
FlashLoan
Parameters
NewAdminFee
Parameters
NewSwapFee
Parameters
OwnershipTransferred
Parameters
Paused
Parameters
RampA
Parameters
RemoveLiquidity
Parameters
RemoveLiquidityImbalance
Parameters
RemoveLiquidityOne
Parameters
StopRampA
Parameters
TokenSwap
Parameters
Unpaused
Parameters
Last updated