ButtonswapLibrary
Functions
sortTokens
Returns sorted token addresses, used to handle return values from pairs sorted in this order
function sortTokens(address tokenA, address tokenB) internal pure returns (address token0, address token1);
Parameters
Name | Type | Description |
---|---|---|
tokenA | address | First token address |
tokenB | address | Second token address |
Returns
Name | Type | Description |
---|---|---|
token0 | address | First sorted token address |
token1 | address | Second sorted token address |
pairFor
Predicts the address that the Pair contract for given tokens would have been deployed to
Specifically, this calculates the CREATE2 address for a Pair contract.
It's done this way to avoid making any external calls, and thus saving on gas versus other approaches.
function pairFor(address factory, address tokenA, address tokenB) internal pure returns (address pair);
Parameters
Name | Type | Description |
---|---|---|
factory | address | The address of the ButtonswapFactory used to create the pair |
tokenA | address | First token address |
tokenB | address | Second token address |
Returns
Name | Type | Description |
---|---|---|
pair | address | The pair address |
getPools
Fetches and sorts the pools for a pair. Pools are the current token balances in the pair contract serving as liquidity.
function getPools(address factory, address tokenA, address tokenB)
internal
view
returns (uint256 poolA, uint256 poolB);
Parameters
Name | Type | Description |
---|---|---|
factory | address | The address of the ButtonswapFactory |
tokenA | address | First token address |
tokenB | address | Second token address |
Returns
Name | Type | Description |
---|---|---|
poolA | uint256 | Pool corresponding to tokenA |
poolB | uint256 | Pool corresponding to tokenB |
getReservoirs
Fetches and sorts the reservoirs for a pair. Reservoirs are the current token balances in the pair contract not actively serving as liquidity.
function getReservoirs(address factory, address tokenA, address tokenB)
internal
view
returns (uint256 reservoirA, uint256 reservoirB);
Parameters
Name | Type | Description |
---|---|---|
factory | address | The address of the ButtonswapFactory |
tokenA | address | First token address |
tokenB | address | Second token address |
Returns
Name | Type | Description |
---|---|---|
reservoirA | uint256 | Reservoir corresponding to tokenA |
reservoirB | uint256 | Reservoir corresponding to tokenB |
getLiquidityBalances
*Fetches and sorts the pools and reservoirs for a pair.
- Pools are the current token balances in the pair contract serving as liquidity.
- Reservoirs are the current token balances in the pair contract not actively serving as liquidity.*
function getLiquidityBalances(address factory, address tokenA, address tokenB)
internal
view
returns (uint256 poolA, uint256 poolB, uint256 reservoirA, uint256 reservoirB);
Parameters
Name | Type | Description |
---|---|---|
factory | address | The address of the ButtonswapFactory |
tokenA | address | First token address |
tokenB | address | Second token address |
Returns
Name | Type | Description |
---|---|---|
poolA | uint256 | Pool corresponding to tokenA |
poolB | uint256 | Pool corresponding to tokenB |
reservoirA | uint256 | Reservoir corresponding to tokenA |
reservoirB | uint256 | Reservoir corresponding to tokenB |
quote
Given some amount of an asset and pair pools, returns an equivalent amount of the other asset
function quote(uint256 amountA, uint256 poolA, uint256 poolB) internal pure returns (uint256 amountB);
Parameters
Name | Type | Description |
---|---|---|
amountA | uint256 | The amount of token A |
poolA | uint256 | The balance of token A in the pool |
poolB | uint256 | The balance of token B in the pool |
Returns
Name | Type | Description |
---|---|---|
amountB | uint256 | The amount of token B |
getMintSwappedAmounts
Given a factory, two tokens, and a mintAmount of the first, returns how much of the much of the mintAmount will be swapped for the other token and for how much during a mintWithReservoir operation.
The logic is a condensed version of PairMath.getSingleSidedMintLiquidityOutAmountA and PairMath.getSingleSidedMintLiquidityOutAmountB
function getMintSwappedAmounts(address factory, address tokenA, address tokenB, uint256 mintAmountA)
internal
view
returns (uint256 tokenAToSwap, uint256 swappedReservoirAmountB);
Parameters
Name | Type | Description |
---|---|---|
factory | address | The address of the ButtonswapFactory that created the pairs |
tokenA | address | First token address |
tokenB | address | Second token address |
mintAmountA | uint256 | The amount of tokenA to be minted |
Returns
Name | Type | Description |
---|---|---|
tokenAToSwap | uint256 | The amount of tokenA to be exchanged for tokenB from the reservoir |
swappedReservoirAmountB | uint256 | The amount of tokenB returned from the reservoir |
getBurnSwappedAmounts
Given a factory, two tokens, and a liquidity amount, returns how much of the first token will be withdrawn from the pair and how much of it came from the reservoir during a burnFromReservoir operation.
The logic is a condensed version of PairMath.getSingleSidedBurnOutputAmountA and PairMath.getSingleSidedBurnOutputAmountB
function getBurnSwappedAmounts(address factory, address tokenA, address tokenB, uint256 liquidity)
internal
view
returns (uint256 tokenOutA, uint256 swappedReservoirAmountA);
Parameters
Name | Type | Description |
---|---|---|
factory | address | The address of the ButtonswapFactory that created the pairs |
tokenA | address | First token address |
tokenB | address | Second token address |
liquidity | uint256 | The amount of liquidity to be burned |
Returns
Name | Type | Description |
---|---|---|
tokenOutA | uint256 | The amount of tokenA to be withdrawn from the pair |
swappedReservoirAmountA | uint256 | The amount of tokenA returned from the reservoir |
getAmountOut
Given an input amount of an asset and pair pools, returns the maximum output amount of the other asset Factors in the fee on the input amount.
function getAmountOut(uint256 amountIn, uint256 poolIn, uint256 poolOut) internal pure returns (uint256 amountOut);
Parameters
Name | Type | Description |
---|---|---|
amountIn | uint256 | The input amount of the asset |
poolIn | uint256 | The balance of the input asset in the pool |
poolOut | uint256 | The balance of the output asset in the pool |
Returns
Name | Type | Description |
---|---|---|
amountOut | uint256 | The output amount of the other asset |
getAmountIn
Given an output amount of an asset and pair pools, returns a required input amount of the other asset
function getAmountIn(uint256 amountOut, uint256 poolIn, uint256 poolOut) internal pure returns (uint256 amountIn);
Parameters
Name | Type | Description |
---|---|---|
amountOut | uint256 | The output amount of the asset |
poolIn | uint256 | The balance of the input asset in the pool |
poolOut | uint256 | The balance of the output asset in the pool |
Returns
Name | Type | Description |
---|---|---|
amountIn | uint256 | The required input amount of the other asset |
getAmountsOut
Given an ordered array of tokens and an input amount of the first asset, performs chained getAmountOut calculations to calculate the output amount of the final asset
function getAmountsOut(address factory, uint256 amountIn, address[] memory path)
internal
view
returns (uint256[] memory amounts);
Parameters
Name | Type | Description |
---|---|---|
factory | address | The address of the ButtonswapFactory that created the pairs |
amountIn | uint256 | The input amount of the first asset |
path | address[] | An array of token addresses [tokenA, tokenB, tokenC, ...] representing the path the input token takes to get to the output token |
Returns
Name | Type | Description |
---|---|---|
amounts | uint256[] | The output amounts of each asset in the path |
getAmountsIn
Given an ordered array of tokens and an output amount of the final asset, performs chained getAmountIn calculations to calculate the input amount of the first asset
function getAmountsIn(address factory, uint256 amountOut, address[] memory path)
internal
view
returns (uint256[] memory amounts);
Parameters
Name | Type | Description |
---|---|---|
factory | address | The address of the ButtonswapFactory that created the pairs |
amountOut | uint256 | The output amount of the final asset |
path | address[] | An array of token addresses [tokenA, tokenB, tokenC, ...] representing the path the input token takes to get to the output token |
Returns
Name | Type | Description |
---|---|---|
amounts | uint256[] | The input amounts of each asset in the path |
Errors
IdenticalAddresses
Identical addresses provided
error IdenticalAddresses();
ZeroAddress
Zero address provided
error ZeroAddress();
InsufficientAmount
Insufficient amount provided
error InsufficientAmount();
InsufficientLiquidity
Insufficient liquidity provided
error InsufficientLiquidity();
InsufficientInputAmount
Insufficient input amount provided
error InsufficientInputAmount();
InsufficientOutputAmount
Insufficient output amount provided
error InsufficientOutputAmount();
InvalidPath
Invalid path provided
error InvalidPath();