ButtonwoodLibrary
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 |
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 |
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();