Skip to main content

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

NameTypeDescription
tokenAaddressFirst token address
tokenBaddressSecond token address

Returns

NameTypeDescription
token0addressFirst sorted token address
token1addressSecond 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

NameTypeDescription
factoryaddressThe address of the ButtonswapFactory used to create the pair
tokenAaddressFirst token address
tokenBaddressSecond token address

Returns

NameTypeDescription
pairaddressThe 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

NameTypeDescription
factoryaddressThe address of the ButtonswapFactory
tokenAaddressFirst token address
tokenBaddressSecond token address

Returns

NameTypeDescription
poolAuint256Pool corresponding to tokenA
poolBuint256Pool 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

NameTypeDescription
factoryaddressThe address of the ButtonswapFactory
tokenAaddressFirst token address
tokenBaddressSecond token address

Returns

NameTypeDescription
reservoirAuint256Reservoir corresponding to tokenA
reservoirBuint256Reservoir 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

NameTypeDescription
amountAuint256The amount of token A
poolAuint256The balance of token A in the pool
poolBuint256The balance of token B in the pool

Returns

NameTypeDescription
amountBuint256The 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

NameTypeDescription
amountInuint256The input amount of the asset
poolInuint256The balance of the input asset in the pool
poolOutuint256The balance of the output asset in the pool

Returns

NameTypeDescription
amountOutuint256The 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

NameTypeDescription
amountOutuint256The output amount of the asset
poolInuint256The balance of the input asset in the pool
poolOutuint256The balance of the output asset in the pool

Returns

NameTypeDescription
amountInuint256The 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

NameTypeDescription
factoryaddressThe address of the ButtonswapFactory that created the pairs
amountInuint256The input amount of the first asset
pathaddress[]An array of token addresses [tokenA, tokenB, tokenC, ...] representing the path the input token takes to get to the output token

Returns

NameTypeDescription
amountsuint256[]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

NameTypeDescription
factoryaddressThe address of the ButtonswapFactory that created the pairs
amountOutuint256The output amount of the final asset
pathaddress[]An array of token addresses [tokenA, tokenB, tokenC, ...] representing the path the input token takes to get to the output token

Returns

NameTypeDescription
amountsuint256[]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();