Skip to main content

Stableswap

This section contains a full API reference of all public functions & events related to Connext's stableswap contracts.


Events

TokenSwap

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

AddLiquidity

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

RemoveLiquidity

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

RemoveLiquidityOne

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

Getters

getSwapStorage

function getSwapStorage(bytes32 key) external view returns (struct SwapUtils.Swap)

Return Stable swap storage

Parameters

NameTypeDescription
keybytes32Hash of the canonical id + domain

Return Values

NameTypeDescription
[0]struct SwapUtils.SwapSwapUtils.Swap

getSwapLPToken

function getSwapLPToken(bytes32 key) external view returns (address)

Return LP token for canonical Id

Parameters

NameTypeDescription
keybytes32Hash of the canonical id + domain

Return Values

NameTypeDescription
[0]addressLPToken

getSwapA

function getSwapA(bytes32 key) external view returns (uint256)

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

See the StableSwap paper for details

Parameters

NameTypeDescription
keybytes32Hash of the canonical id + domain

Return Values

NameTypeDescription
[0]uint256A parameter

getSwapAPrecise

function getSwapAPrecise(bytes32 key) external view returns (uint256)

Return A in its raw precision form

See the StableSwap paper for details

Parameters

NameTypeDescription
keybytes32Hash of the canonical id + domain

Return Values

NameTypeDescription
[0]uint256A parameter in its raw precision form

getSwapToken

function getSwapToken(bytes32 key, uint8 index) public view returns (contract IERC20)

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

Parameters

NameTypeDescription
keybytes32Hash of the canonical id + domain
indexuint8the index of the token

Return Values

NameTypeDescription
[0]contract IERC20address of the token at given index

getSwapTokenIndex

function getSwapTokenIndex(bytes32 key, address tokenAddress) public view returns (uint8)

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

Parameters

NameTypeDescription
keybytes32Hash of the canonical id + domain
tokenAddressaddressaddress of the token

Return Values

NameTypeDescription
[0]uint8the index of the given token address

getSwapTokenBalance

function getSwapTokenBalance(bytes32 key, uint8 index) external view returns (uint256)

Return current balance of the pooled token at given index

Parameters

NameTypeDescription
keybytes32Hash of the canonical id + domain
indexuint8the index of the token

Return Values

NameTypeDescription
[0]uint256current balance of the pooled token at given index with token's native precision

getSwapVirtualPrice

function getSwapVirtualPrice(bytes32 key) external view returns (uint256)

Get the virtual price, to help calculate profit

Parameters

NameTypeDescription
keybytes32Hash of the canonical id + domain

Return Values

NameTypeDescription
[0]uint256the virtual price, scaled to the POOL_PRECISION_DECIMALS

calculateSwap

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

Calculate amount of tokens you receive on swap

Parameters

NameTypeDescription
keybytes32Hash of the canonical id + domain
tokenIndexFromuint8the token the user wants to sell
tokenIndexTouint8the token the user wants to buy
dxuint256the 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.

Return Values

NameTypeDescription
[0]uint256amount of tokens the user will receive

calculateSwapTokenAmount

function calculateSwapTokenAmount(bytes32 key, 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

NameTypeDescription
keybytes32Hash of the canonical id + domain
amountsuint256[]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.
depositboolwhether this is a deposit or a withdrawal

Return Values

NameTypeDescription
[0]uint256token amount the user will receive

calculateRemoveSwapLiquidity

function calculateRemoveSwapLiquidity(bytes32 key, 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

NameTypeDescription
keybytes32Hash of the canonical id + domain
amountuint256the amount of LP tokens that would be burned on withdrawal

Return Values

NameTypeDescription
[0]uint256[]array of token balances that the user will receive

calculateRemoveSwapLiquidityOneToken

function calculateRemoveSwapLiquidityOneToken(bytes32 key, 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

NameTypeDescription
keybytes32Hash of the canonical id + domain
tokenAmountuint256the amount of LP token to burn
tokenIndexuint8index of which token will be withdrawn

Return Values

NameTypeDescription
availableTokenAmountuint256calculated amount of underlying token available to withdraw

Functions

swap

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

Swap two tokens using this pool

Parameters

NameTypeDescription
keybytes32Hash of the canonical id + domain
tokenIndexFromuint8the token the user wants to swap from
tokenIndexTouint8the token the user wants to swap to
dxuint256the amount of tokens the user wants to swap from
minDyuint256the min amount the user would like to receive, or revert.
deadlineuint256latest timestamp to accept this transaction

swapExact

function swapExact(bytes32 key, uint256 amountIn, address assetIn, address assetOut, uint256 minAmountOut, uint256 deadline) external returns (uint256)

Swap two tokens using this pool

Parameters

NameTypeDescription
keybytes32Hash of the canonical id + domain
amountInuint256the amount of tokens the user wants to swap from
assetInaddressthe token the user wants to swap from
assetOutaddressthe token the user wants to swap to
minAmountOutuint256
deadlineuint256

swapExactOut

function swapExactOut(bytes32 key, uint256 amountOut, address assetIn, address assetOut, uint256 maxAmountIn, uint256 deadline) external returns (uint256)

Swap two tokens using this pool

Parameters

NameTypeDescription
keybytes32Hash of the canonical id + domain
amountOutuint256the amount of tokens the user wants to swap to
assetInaddressthe token the user wants to swap from
assetOutaddressthe token the user wants to swap to
maxAmountInuint256
deadlineuint256

addSwapLiquidity

function addSwapLiquidity(bytes32 key, uint256[] amounts, uint256 minToMint, uint256 deadline) external returns (uint256)

Add liquidity to the pool with the given amounts of tokens

Parameters

NameTypeDescription
keybytes32Hash of the canonical id + domain
amountsuint256[]the amounts of each token to add, in their native precision
minToMintuint256the minimum LP tokens adding this amount of liquidity should mint, otherwise revert. Handy for front-running mitigation
deadlineuint256latest timestamp to accept this transaction

Return Values

NameTypeDescription
[0]uint256amount of LP token user minted and received

removeSwapLiquidity

function removeSwapLiquidity(bytes32 key, uint256 amount, uint256[] minAmounts, uint256 deadline) external 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

NameTypeDescription
keybytes32Hash of the canonical id + domain
amountuint256the amount of LP tokens to burn
minAmountsuint256[]the minimum amounts of each token in the pool acceptable for this burn. Useful as a front-running mitigation
deadlineuint256latest timestamp to accept this transaction

Return Values

NameTypeDescription
[0]uint256[]amounts of tokens user received

removeSwapLiquidityOneToken

function removeSwapLiquidityOneToken(bytes32 key, uint256 tokenAmount, uint8 tokenIndex, uint256 minAmount, uint256 deadline) external 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

NameTypeDescription
keybytes32Hash of the canonical id + domain
tokenAmountuint256the amount of the token you want to receive
tokenIndexuint8the index of the token you want to receive
minAmountuint256the minimum amount to withdraw, otherwise revert
deadlineuint256latest timestamp to accept this transaction

Return Values

NameTypeDescription
[0]uint256amount of chosen token user received