SwapMath

Functions

_amountToBinNetOfProtocolFee

Internal function to calculate the amount after deducting the protocol fee.

function _amountToBinNetOfProtocolFee(uint256 deltaInErc, uint256 feeBasis, uint256 protocolFeeD3)
    private
    pure
    returns (uint256 amount);

Parameters

NameTypeDescription

deltaInErc

uint256

Input delta in ERC token.

feeBasis

uint256

Fee basis for calculating the fee amount.

protocolFeeD3

uint256

Proportion of the fee that goes to the protocol in three-decimal format. e.g. fee of 100 is 10% protocol fee rate.

Returns

NameTypeDescription

amount

uint256

The amount after deducting the protocol fee.

_remainingBinInputSpaceGivenOutput

Internal function to calculate the remaining input space given the output.

function _remainingBinInputSpaceGivenOutput(uint256 binLiquidity, uint256 output, uint256 sqrtPrice, bool tokenAIn)
    private
    pure
    returns (uint256 binAmountIn);

Parameters

NameTypeDescription

binLiquidity

uint256

The current liquidity in the bin.

output

uint256

The desired output amount.

sqrtPrice

uint256

The current square root price.

tokenAIn

bool

True if the swap input is token A.

Returns

NameTypeDescription

binAmountIn

uint256

The input amount that can be accommodated in the bin.

computeEndPrice

Compute end price of a swap as well as the approximate end price in the tick domain. The resulting output fraction tick part is written to the input newDelta object.

Ain: endSqrtP = in / L + sqrtP

Bin: endSqrtP = 1 / (in / L + 1 / sqrtP) = L / (in + L / sqrtP)

fractional Tick: (endSqrtP - lowerSqrtP) / (upperSqrtP - lowerSqrtP)

function computeEndPrice(
    Delta.Instance memory delta,
    Delta.Instance memory newDelta,
    IMaverickV2Pool.TickData memory tickData
) internal pure;

computeSwapExactIn

Calculate swap data for an exact input swap.

function computeSwapExactIn(
    uint256 sqrtPrice,
    IMaverickV2Pool.TickData memory tickData,
    uint256 amountIn,
    bool tokenAIn,
    uint256 fee,
    uint256 protocolFeeD3
) internal pure returns (Delta.Instance memory delta);

Parameters

NameTypeDescription

sqrtPrice

uint256

Current price.

tickData

IMaverickV2Pool.TickData

Reserve and liquidity values of the bin.

amountIn

uint256

Desired input amount.

tokenAIn

bool

True if the swap input is token A.

fee

uint256

Ratio that the swapper pays in D18 format.

protocolFeeD3

uint256

Proportion of the fee that goes to the protocol.

Returns

NameTypeDescription

delta

Delta.Instance

Swap data delta for the exact input swap.

computeSwapExactOut

Calculate swap data for an exact output swap.

function computeSwapExactOut(
    uint256 sqrtPrice,
    IMaverickV2Pool.TickData memory tickData,
    uint256 amountOut,
    bool tokenAIn,
    uint256 fee,
    uint256 protocolFeeD3
) internal pure returns (Delta.Instance memory delta);

Parameters

NameTypeDescription

sqrtPrice

uint256

Current price.

tickData

IMaverickV2Pool.TickData

Reserve and liquidity values of the bin.

amountOut

uint256

Desired output amount.

tokenAIn

bool

True if the swap input is token A.

fee

uint256

Ratio that the swapper pays in D18 format.

protocolFeeD3

uint256

Proportion of the fee that goes to the protocol.

Returns

NameTypeDescription

delta

Delta.Instance

Swap data delta for the exact output swap.

Last updated