DiverSqrtPriceMath

Contains the math that uses liquidity to compute token deltas or square root of price as a Q64.96

getSTokenDelta

Gets the delta amount of Spear or Shield tokens based on the given sqrt ratios and liquidity. Computes for Spear token delta when the sqrtPrice moves from upper to lower; or Shield token delta when the sqrtPrice moves from lower to upper. For the same sqrt ratio range and liquidity, the computed delta amounts of Spear and Shield are equal.

The formula for this is spear or shield delta amount = liquidity * (sqrtRatioBX96 - sqrtRatioAX96) * (1 + 1 /(sqrtRatioAX96 * sqrtRatioBX96))

function getSTokenDelta(uint160 sqrtRatioAX96, uint160 sqrtRatioBX96, uint128 liquidity, bool roundUp) internal pure returns (uint256 amount)

Params:

Name

Type

Description

sqrtRatioAX96

uint160

A sqrt ratio

sqrtRatioBX96

uint160

Another sqrt ratio

liquidity

uint128

The change in liquidity for which to compute the Spear or Shield token delta

roundUp

bool

Whether to round the amount up or down

Returns:

Name

Type

Description

amount

uint256

The amount of Spear or Shield token corresponding to the passed liquidityDelta between the two prices

getSTokenDelta

Helper that gets signed spear or shield token delta

function getSTokenDelta(uint160 sqrtPriceAX96, uint160 sqrtPriceBX96, int128 liquidity) internal pure returns (int256 amount)

Params:

Name

Type

Description

sqrtRatioAX96

uint160

A sqrt ratio

sqrtRatioBX96

uint160

Another sqrt ratio

liquidity

int128

The change in liquidity for which to compute the Spear or Shield token delta

Returns:

Name

Type

Description

amount

int256

The amount of Spear or Shield token corresponding to the passed liquidityDelta between the two prices

getNextSqrtPriceFromSpear

Gets the next sqrt ratio based on the given the Spear token delta and liquidity.

function getNextSqrtPriceFromSpear(
        uint160 sqrtPrice,
        uint128 liquidity,
        uint256 amount,
        uint256 unit
    )
        internal
        pure
        returns (uint160 nextSqrtPrice)

Params:

Name

Type

Description

sqrtPrice

uint160

The starting price, i.e. before accounting for the spear token delta

liquidity

uint128

The amount of usable liquidity

amount

uint256

The amount of Spear token delta to mint for the trade

unit

uint256

The token decimal unit, e.g. a token with 18 decimals has a unit of 10**18

Returns:

Name

Type

Description

nextSqrtPrice

uint160

The next sqrt ratio after minting the given amount of Spear tokens

getNextSqrtPriceFromShield

Gets the next sqrt ratio based on the given the Shield token delta and liquidity.

    function getNextSqrtPriceFromShield(
        uint160 sqrtPrice,
        uint128 liquidity,
        uint256 amount,
        uint256 unit
    )
        internal
        pure
        returns (uint160 nextSqrtPrice)

Params:

Name

Type

Description

sqrtPrice

uint160

The starting price, i.e. before accounting for the Shield token delta

liquidity

uint128

The amount of usable liquidity

amount

uint256

The amount of Shield token delta to mint for the trade

unit

uint256

The token decimal unit, e.g. a token with 18 decimals has a unit of 10**18

Returns:

Name

Type

Description

nextSqrtPrice

uint160

The next sqrt ratio after minting the given amount of Shield tokens

Last updated