Skip to content

BinHelper ​

Git Source

Author:
Trader Joe

This library contains functions to help interaction with bins.

Functions ​

getAmountOutOfBin ​

Returns the amount of tokens that will be received when burning the given amount of liquidity

solidity
function getAmountOutOfBin(bytes32 binReserves, uint256 amountToBurn, uint256 totalSupply)
    internal
    pure
    returns (bytes32 amountsOut);

Parameters

NameTypeDescription
binReservesbytes32The reserves of the bin
amountToBurnuint256The amount of liquidity to burn
totalSupplyuint256The total supply of the liquidity book

Returns

NameTypeDescription
amountsOutbytes32The encoded amount of tokens that will be received

getSharesAndEffectiveAmountsIn ​

Returns the share and the effective amounts in when adding liquidity

solidity
function getSharesAndEffectiveAmountsIn(bytes32 binReserves, bytes32 amountsIn, uint256 price, uint256 totalSupply)
    internal
    pure
    returns (uint256 shares, bytes32 effectiveAmountsIn);

Parameters

NameTypeDescription
binReservesbytes32The reserves of the bin
amountsInbytes32The amounts of tokens to add
priceuint256The price of the bin
totalSupplyuint256The total supply of the liquidity book

Returns

NameTypeDescription
sharesuint256The share of the liquidity book that the user will receive
effectiveAmountsInbytes32The encoded effective amounts of tokens that the user will add. This is the amount of tokens that the user will actually add to the liquidity book, and will always be less than or equal to the amountsIn.

getLiquidity ​

Returns the amount of liquidity following the constant sum formula L = price * x + y

solidity
function getLiquidity(bytes32 amounts, uint256 price) internal pure returns (uint256 liquidity);

Parameters

NameTypeDescription
amountsbytes32The amounts of tokens
priceuint256The price of the bin

Returns

NameTypeDescription
liquidityuint256The amount of liquidity

getLiquidity ​

Returns the amount of liquidity following the constant sum formula L = price * x + y

solidity
function getLiquidity(uint256 x, uint256 y, uint256 price) internal pure returns (uint256 liquidity);

Parameters

NameTypeDescription
xuint256The amount of the token X
yuint256The amount of the token Y
priceuint256The price of the bin

Returns

NameTypeDescription
liquidityuint256The amount of liquidity

verifyAmounts ​

Verify that the amounts are correct and that the composition factor is not flawed

solidity
function verifyAmounts(bytes32 amounts, uint24 activeId, uint24 id) internal pure;

Parameters

NameTypeDescription
amountsbytes32The amounts of tokens
activeIduint24The id of the active bin
iduint24The id of the bin

getCompositionFees ​

Returns the composition fees when adding liquidity to the active bin with a different
composition factor than the bin's one, as it does an implicit swap

solidity
function getCompositionFees(
    bytes32 binReserves,
    bytes32 parameters,
    uint16 binStep,
    bytes32 amountsIn,
    uint256 totalSupply,
    uint256 shares
) internal pure returns (bytes32 fees);

Parameters

NameTypeDescription
binReservesbytes32The reserves of the bin
parametersbytes32The parameters of the liquidity book
binStepuint16The step of the bin
amountsInbytes32The amounts of tokens to add
totalSupplyuint256The total supply of the liquidity book
sharesuint256The share of the liquidity book that the user will receive

Returns

NameTypeDescription
feesbytes32The encoded fees that will be charged

isEmpty ​

Returns whether the bin is empty (true) or not (false)

solidity
function isEmpty(bytes32 binReserves, bool isX) internal pure returns (bool);

Parameters

NameTypeDescription
binReservesbytes32The reserves of the bin
isXboolWhether the reserve to check is the X reserve (true) or the Y reserve (false)

Returns

NameTypeDescription
<none>boolWhether the bin is empty (true) or not (false)

getAmounts ​

Returns the amounts of tokens that will be added and removed from the bin during a swap
along with the fees that will be charged

solidity
function getAmounts(
    bytes32 binReserves,
    bytes32 parameters,
    uint16 binStep,
    bool swapForY,
    uint24 activeId,
    bytes32 amountsInLeft
) internal pure returns (bytes32 amountsInWithFees, bytes32 amountsOutOfBin, bytes32 totalFees);

Parameters

NameTypeDescription
binReservesbytes32The reserves of the bin
parametersbytes32The parameters of the liquidity book
binStepuint16The step of the bin
swapForYboolWhether the swap is for Y (true) or for X (false)
activeIduint24The id of the active bin
amountsInLeftbytes32The amounts of tokens left to swap

Returns

NameTypeDescription
amountsInWithFeesbytes32The encoded amounts of tokens that will be added to the bin, including fees
amountsOutOfBinbytes32The encoded amounts of tokens that will be removed from the bin
totalFeesbytes32The encoded fees that will be charged

received ​

Returns the encoded amounts that were transferred to the contract

solidity
function received(bytes32 reserves, IERC20 tokenX, IERC20 tokenY) internal view returns (bytes32 amounts);

Parameters

NameTypeDescription
reservesbytes32The reserves
tokenXIERC20The token X
tokenYIERC20The token Y

Returns

NameTypeDescription
amountsbytes32The amounts, encoded as follows: [0 - 128[: amountX [128 - 256[: amountY

receivedX ​

Returns the encoded amounts that were transferred to the contract, only for token X

solidity
function receivedX(bytes32 reserves, IERC20 tokenX) internal view returns (bytes32);

Parameters

NameTypeDescription
reservesbytes32The reserves
tokenXIERC20The token X

Returns

NameTypeDescription
<none>bytes32amounts The amounts, encoded as follows: [0 - 128[: amountX [128 - 256[: empty

receivedY ​

Returns the encoded amounts that were transferred to the contract, only for token Y

solidity
function receivedY(bytes32 reserves, IERC20 tokenY) internal view returns (bytes32);

Parameters

NameTypeDescription
reservesbytes32The reserves
tokenYIERC20The token Y

Returns

NameTypeDescription
<none>bytes32amounts The amounts, encoded as follows: [0 - 128[: empty [128 - 256[: amountY

transfer ​

Transfers the encoded amounts to the recipient

solidity
function transfer(bytes32 amounts, IERC20 tokenX, IERC20 tokenY, address recipient) internal;

Parameters

NameTypeDescription
amountsbytes32The amounts, encoded as follows: [0 - 128[: amountX [128 - 256[: amountY
tokenXIERC20The token X
tokenYIERC20The token Y
recipientaddressThe recipient

transferX ​

Transfers the encoded amounts to the recipient, only for token X

solidity
function transferX(bytes32 amounts, IERC20 tokenX, address recipient) internal;

Parameters

NameTypeDescription
amountsbytes32The amounts, encoded as follows: [0 - 128[: amountX [128 - 256[: empty
tokenXIERC20The token X
recipientaddressThe recipient

transferY ​

Transfers the encoded amounts to the recipient, only for token Y

solidity
function transferY(bytes32 amounts, IERC20 tokenY, address recipient) internal;

Parameters

NameTypeDescription
amountsbytes32The amounts, encoded as follows: [0 - 128[: empty [128 - 256[: amountY
tokenYIERC20The token Y
recipientaddressThe recipient

_balanceOf ​

solidity
function _balanceOf(IERC20 token) private view returns (uint128);

Errors ​

BinHelper__CompositionFactorFlawed ​

solidity
error BinHelper__CompositionFactorFlawed(uint24 id);

BinHelper__LiquidityOverflow ​

solidity
error BinHelper__LiquidityOverflow();

BinHelper__MaxLiquidityPerBinExceeded ​

solidity
error BinHelper__MaxLiquidityPerBinExceeded();