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

function MAX_BPS() external view returns (uint256)

Returns

addLiquidity

function addLiquidity(uint256[] amounts, uint256 minToMint, uint256 deadline) external nonpayable returns (uint256)

Add liquidity to the pool with the given amounts of tokens

Parameters

Returns

calculateRemoveLiquidity

function calculateRemoveLiquidity(uint256 amount) external view returns (uint256[])

A simple method to calculate amount of each underlying tokens that is returned upon burning given amount of LP tokens

Parameters

Returns

calculateRemoveLiquidityOneToken

function calculateRemoveLiquidityOneToken(uint256 tokenAmount, uint8 tokenIndex) external view returns (uint256 availableTokenAmount)

Calculate the amount of underlying token available to withdraw when withdrawing via only single token

Parameters

Returns

calculateSwap

function calculateSwap(uint8 tokenIndexFrom, uint8 tokenIndexTo, uint256 dx) external view returns (uint256)

Calculate amount of tokens you receive on swap

Parameters

Returns

calculateTokenAmount

function calculateTokenAmount(uint256[] amounts, bool deposit) external view returns (uint256)

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

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

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

function flashLoanFeeBPS() external view returns (uint256)

Returns

getA

function getA() external view returns (uint256)

Return A, the amplification coefficient * n * (n - 1)

See the StableSwap paper for details

Returns

getAPrecise

function getAPrecise() external view returns (uint256)

Return A in its raw precision form

See the StableSwap paper for details

Returns

getAdminBalance

function getAdminBalance(uint256 index) external view returns (uint256)

This function reads the accumulated amount of admin fees of the token with given index

Parameters

Returns

getToken

function getToken(uint8 index) external view returns (contract IERC20)

Return address of the pooled token at given index. Reverts if tokenIndex is out of range.

Parameters

Returns

getTokenBalance

function getTokenBalance(uint8 index) external view returns (uint256)

Return current balance of the pooled token at given index

Parameters

Returns

getTokenIndex

function getTokenIndex(address tokenAddress) external view returns (uint8)

Return the index of the given token address. Reverts if no matching token is found.

Parameters

Returns

getVirtualPrice

function getVirtualPrice() external view returns (uint256)

Get the virtual price, to help calculate profit

Returns

initialize

function initialize(contract IERC20[] _pooledTokens, uint8[] decimals, string lpTokenName, string lpTokenSymbol, uint256 _a, uint256 _fee, uint256 _adminFee, address lpTokenTargetAddress) external nonpayable

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

function owner() external view returns (address)

Returns the address of the current owner.

Returns

pause

function pause() external nonpayable

Pause the contract. Revert if already paused.

paused

function paused() external view returns (bool)

Returns true if the contract is paused, and false otherwise.

Returns

protocolFeeShareBPS

function protocolFeeShareBPS() external view returns (uint256)

Returns

rampA

function rampA(uint256 futureA, uint256 futureTime) external nonpayable

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

function removeLiquidity(uint256 amount, uint256[] minAmounts, uint256 deadline) external nonpayable returns (uint256[])

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

function removeLiquidityImbalance(uint256[] amounts, uint256 maxBurnAmount, uint256 deadline) external nonpayable returns (uint256)

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

function removeLiquidityOneToken(uint256 tokenAmount, uint8 tokenIndex, uint256 minAmount, uint256 deadline) external nonpayable returns (uint256)

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

function renounceOwnership() external nonpayable

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

function setAdminFee(uint256 newAdminFee) external nonpayable

Update the admin fee. Admin fee takes portion of the swap fee.

Parameters

setFlashLoanFees

function setFlashLoanFees(uint256 newFlashLoanFeeBPS, uint256 newProtocolFeeShareBPS) external nonpayable

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

Parameters

setSwapFee

function setSwapFee(uint256 newSwapFee) external nonpayable

Update the swap fee to be applied on swaps

Parameters

stopRampA

function stopRampA() external nonpayable

Stop ramping A immediately. Reverts if ramp A is already stopped.

swap

function swap(uint8 tokenIndexFrom, uint8 tokenIndexTo, uint256 dx, uint256 minDy, uint256 deadline) external nonpayable returns (uint256)

Swap two tokens using this pool

Parameters

Returns

swapStorage

function swapStorage() external view returns (uint256 initialA, uint256 futureA, uint256 initialATime, uint256 futureATime, uint256 swapFee, uint256 adminFee, contract LPToken lpToken)

Returns

transferOwnership

function transferOwnership(address newOwner) external nonpayable

Transfers ownership of the contract to a new account (newOwner). Can only be called by the current owner.

Parameters

unpause

function unpause() external nonpayable

Unpause the contract. Revert if already unpaused.

withdrawAdminFees

function withdrawAdminFees() external nonpayable

Withdraw all admin fees to the contract owner

Events

AddLiquidity

event AddLiquidity(address indexed provider, uint256[] tokenAmounts, uint256[] fees, uint256 invariant, uint256 lpTokenSupply)

Parameters

FlashLoan

event FlashLoan(address indexed receiver, uint8 tokenIndex, uint256 amount, uint256 amountFee, uint256 protocolFee)

Parameters

NewAdminFee

event NewAdminFee(uint256 newAdminFee)

Parameters

NewSwapFee

event NewSwapFee(uint256 newSwapFee)

Parameters

OwnershipTransferred

event OwnershipTransferred(address indexed previousOwner, address indexed newOwner)

Parameters

Paused

event Paused(address account)

Parameters

RampA

event RampA(uint256 oldA, uint256 newA, uint256 initialTime, uint256 futureTime)

Parameters

RemoveLiquidity

event RemoveLiquidity(address indexed provider, uint256[] tokenAmounts, uint256 lpTokenSupply)

Parameters

RemoveLiquidityImbalance

event RemoveLiquidityImbalance(address indexed provider, uint256[] tokenAmounts, uint256[] fees, uint256 invariant, uint256 lpTokenSupply)

Parameters

RemoveLiquidityOne

event RemoveLiquidityOne(address indexed provider, uint256 lpTokenAmount, uint256 lpTokenSupply, uint256 boughtId, uint256 tokensBought)

Parameters

StopRampA

event StopRampA(uint256 currentA, uint256 time)

Parameters

TokenSwap

event TokenSwap(address indexed buyer, uint256 tokensSold, uint256 tokensBought, uint128 soldId, uint128 boughtId)

Parameters

Unpaused

event Unpaused(address account)

Parameters

Last updated