BinHelper

Git Sourcearrow-up-right

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

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

Parameters

Name
Type
Description

binReserves

bytes32

The reserves of the bin

amountToBurn

uint256

The amount of liquidity to burn

totalSupply

uint256

The total supply of the liquidity book

Returns

Name
Type
Description

amountsOut

bytes32

The encoded amount of tokens that will be received

getSharesAndEffectiveAmountsIn

Returns the share and the effective amounts in when adding liquidity

Parameters

Name
Type
Description

binReserves

bytes32

The reserves of the bin

amountsIn

bytes32

The amounts of tokens to add

price

uint256

The price of the bin

totalSupply

uint256

The total supply of the liquidity book

Returns

Name
Type
Description

shares

uint256

The share of the liquidity book that the user will receive

effectiveAmountsIn

bytes32

The 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

Parameters

Name
Type
Description

amounts

bytes32

The amounts of tokens

price

uint256

The price of the bin

Returns

Name
Type
Description

liquidity

uint256

The amount of liquidity

getLiquidity

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

Parameters

Name
Type
Description

x

uint256

The amount of the token X

y

uint256

The amount of the token Y

price

uint256

The price of the bin

Returns

Name
Type
Description

liquidity

uint256

The amount of liquidity

verifyAmounts

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

Parameters

Name
Type
Description

amounts

bytes32

The amounts of tokens

activeId

uint24

The id of the active bin

id

uint24

The 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

Parameters

Name
Type
Description

binReserves

bytes32

The reserves of the bin

parameters

bytes32

The parameters of the liquidity book

binStep

uint16

The step of the bin

amountsIn

bytes32

The amounts of tokens to add

totalSupply

uint256

The total supply of the liquidity book

shares

uint256

The share of the liquidity book that the user will receive

Returns

Name
Type
Description

fees

bytes32

The encoded fees that will be charged

isEmpty

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

Parameters

Name
Type
Description

binReserves

bytes32

The reserves of the bin

isX

bool

Whether the reserve to check is the X reserve (true) or the Y reserve (false)

Returns

Name
Type
Description

<none>

bool

Whether 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

Parameters

Name
Type
Description

binReserves

bytes32

The reserves of the bin

parameters

bytes32

The parameters of the liquidity book

binStep

uint16

The step of the bin

swapForY

bool

Whether the swap is for Y (true) or for X (false)

activeId

uint24

The id of the active bin

amountsInLeft

bytes32

The amounts of tokens left to swap

Returns

Name
Type
Description

amountsInWithFees

bytes32

The encoded amounts of tokens that will be added to the bin, including fees

amountsOutOfBin

bytes32

The encoded amounts of tokens that will be removed from the bin

totalFees

bytes32

The encoded fees that will be charged

received

Returns the encoded amounts that were transferred to the contract

Parameters

Name
Type
Description

reserves

bytes32

The reserves

tokenX

IERC20

The token X

tokenY

IERC20

The token Y

Returns

Name
Type
Description

amounts

bytes32

The 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

Parameters

Name
Type
Description

reserves

bytes32

The reserves

tokenX

IERC20

The token X

Returns

Name
Type
Description

<none>

bytes32

amounts 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

Parameters

Name
Type
Description

reserves

bytes32

The reserves

tokenY

IERC20

The token Y

Returns

Name
Type
Description

<none>

bytes32

amounts The amounts, encoded as follows: [0 - 128[: empty [128 - 256[: amountY

transfer

Transfers the encoded amounts to the recipient

Parameters

Name
Type
Description

amounts

bytes32

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

tokenX

IERC20

The token X

tokenY

IERC20

The token Y

recipient

address

The recipient

transferX

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

Parameters

Name
Type
Description

amounts

bytes32

The amounts, encoded as follows: [0 - 128[: amountX [128 - 256[: empty

tokenX

IERC20

The token X

recipient

address

The recipient

transferY

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

Parameters

Name
Type
Description

amounts

bytes32

The amounts, encoded as follows: [0 - 128[: empty [128 - 256[: amountY

tokenY

IERC20

The token Y

recipient

address

The recipient

_balanceOf

Errors

BinHelper__CompositionFactorFlawed

BinHelper__LiquidityOverflow

BinHelper__MaxLiquidityPerBinExceeded

Last updated