Skip to main content

SwapFlashLoan

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.

Functions#

initialize#

function initialize(
contract IERC20[] _pooledTokens,
uint8[] decimals,
string lpTokenName,
string lpTokenSymbol,
uint256 _a,
uint256 _fee,
uint256 _adminFee,
uint256 _withdrawFee
) public

Initializes this Swap contract with the given parameters. This will also deploy the 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.

Parameters:#

NameTypeDescription
_pooledTokenscontract IERC20[]an array of ERC20s this pool will accept
decimalsuint8[]the decimals to use for each pooled token,

eg 8 for WBTC. Cannot be larger than POOL_PRECISION_DECIMALS |lpTokenName | string | the long-form name of the token to be deployed |lpTokenSymbol | string | the short symbol for the token to be deployed |_a | uint256 | the amplification coefficient n (n - 1). See the StableSwap paper for details |_fee | uint256 | default swap fee to be initialized with |_adminFee | uint256 | default adminFee to be initialized with |_withdrawFee | uint256 | default withdrawFee to be initialized with

flashLoan#

function flashLoan(
address receiver,
contract IERC20 token,
uint256 amount,
bytes params
) external

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:#

NameTypeDescription
receiveraddressthe address of the receiver of the token. This address must implement the IFlashLoanReceiver

interface and the callback function executeOperation. |token | contract IERC20 | the protocol fee in bps to be applied on the total flash loan fee |amount | uint256 | the total amount to borrow in this transaction |params | bytes | optional data to pass along to the callback function

setFlashLoanFees#

function setFlashLoanFees(
uint256 newFlashLoanFeeBPS,
uint256 newProtocolFeeShareBPS
) external

Updates the flash loan fee parameters. This function can only be called by the owner.

Parameters:#

NameTypeDescription
newFlashLoanFeeBPSuint256the total fee in bps to be applied on future flash loans
newProtocolFeeShareBPSuint256the protocol fee in bps to be applied on the total flash loan fee

Events#

FlashLoan#

event FlashLoan(
)