LFJ Developer Docs
  • Liquidity Book
  • Introduction
  • LB V2.2 Key Changes
  • Guides
    • Swap Tokens
    • Add/Remove Liquidity
    • Tracking Volume
    • Tracking Pool Balances
    • Finding The Best Quote
    • Byte32 Decoding
    • Price From Bin Id
    • Bin Id From Price
    • Finding Liquidity Depth
    • User Balances
  • Concepts
    • Concentrated Liquidity
    • Bin Math
    • Bin Liquidity
    • Swaps
    • Fees
    • Oracle
  • Contracts
    • Interfaces
      • ILBLegacyFactory
      • ILBLegacyToken
      • ILBLegacyPair
      • ILBLegacyRouter
      • ILBFlashLoanCallback
      • IPendingOwnable
      • IJoeFactory
      • IJoePair
      • IJoeRouter01
      • IJoeRouter02
      • IWNATIVE
      • ILBFactory
      • ILBHooks
      • ILBPair
      • ILBRouter
      • ILBToken
    • Libraries
      • Math
        • BitMath
        • Encoded
        • LiquidityConfigurations
        • PackedUint128Math
        • SafeCast
        • SampleMath
        • TreeMath
        • Uint128x128Math
        • Uint256x256Math
      • BinHelper
      • Clone
      • Constants
      • FeeHelper
      • Hooks
      • ImmutableClone
      • JoeLibrary
      • OracleHelper
      • PairParameterHelper
      • PriceHelper
      • ReentrancyGuardUpgradeable
      • TokenHelper
    • LBBaseHooks
    • LBFactory
    • LBPair
    • LBQuoter
    • LBRouter
    • LBToken
  • Deployment Addresses
    • Avalanche C-Chain
    • Fuji Testnet
    • Arbitrum One
    • Binance Smart Chain
    • Binance Smart Chain Testnet
    • Ethereum Mainnet
    • Monad Testnet
  • SDK
    • Introduction
    • Making a Trade
    • Adding Liquidity
    • Removing Liquidity
  • Audits
  • AMM
    • Joe V1 Contracts
    • Joe V1 Audits
  • LFJ DEX API
    • Dex Analytics
    • Pools
    • Rewards
    • User
    • User Lifetime Stats
    • Vaults
    • Models
  • LFJ Aggregator API
    • Default
    • Models
Powered by GitBook
On this page
  • Functions
  • mulDivRoundDown
  • mulDivRoundUp
  • mulShiftRoundDown
  • mulShiftRoundUp
  • shiftDivRoundDown
  • shiftDivRoundUp
  • _getMulProds
  • _getEndOfDivRoundDown
  • sqrt
  • Errors
  • Uint256x256Math__MulShiftOverflow
  • Uint256x256Math__MulDivOverflow
  1. Contracts
  2. Libraries
  3. Math

Uint256x256Math

PreviousUint128x128MathNextBinHelper

Last updated 8 days ago

Author: Trader Joe

Helper contract used for full precision calculations

Functions

mulDivRoundDown

Calculates floor(x*y/denominator) with full precision The result will be rounded down

*Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv Requirements:

  • The denominator cannot be zero

  • The result must fit within uint256 Caveats:

  • This function does not work with fixed-point numbers*

function mulDivRoundDown(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result);

Parameters

Name
Type
Description

x

uint256

The multiplicand as an uint256

y

uint256

The multiplier as an uint256

denominator

uint256

The divisor as an uint256

Returns

Name
Type
Description

result

uint256

The result as an uint256

mulDivRoundUp

Calculates ceil(x*y/denominator) with full precision The result will be rounded up

*Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv Requirements:

  • The denominator cannot be zero

  • The result must fit within uint256 Caveats:

  • This function does not work with fixed-point numbers*

function mulDivRoundUp(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result);

Parameters

Name
Type
Description

x

uint256

The multiplicand as an uint256

y

uint256

The multiplier as an uint256

denominator

uint256

The divisor as an uint256

Returns

Name
Type
Description

result

uint256

The result as an uint256

mulShiftRoundDown

Calculates floor(x * y / 2**offset) with full precision The result will be rounded down

*Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv Requirements:

  • The offset needs to be strictly lower than 256

  • The result must fit within uint256 Caveats:

  • This function does not work with fixed-point numbers*

function mulShiftRoundDown(uint256 x, uint256 y, uint8 offset) internal pure returns (uint256 result);

Parameters

Name
Type
Description

x

uint256

The multiplicand as an uint256

y

uint256

The multiplier as an uint256

offset

uint8

The offset as an uint256, can't be greater than 256

Returns

Name
Type
Description

result

uint256

The result as an uint256

mulShiftRoundUp

Calculates floor(x * y / 2**offset) with full precision The result will be rounded down

*Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv Requirements:

  • The offset needs to be strictly lower than 256

  • The result must fit within uint256 Caveats:

  • This function does not work with fixed-point numbers*

function mulShiftRoundUp(uint256 x, uint256 y, uint8 offset) internal pure returns (uint256 result);

Parameters

Name
Type
Description

x

uint256

The multiplicand as an uint256

y

uint256

The multiplier as an uint256

offset

uint8

The offset as an uint256, can't be greater than 256

Returns

Name
Type
Description

result

uint256

The result as an uint256

shiftDivRoundDown

Calculates floor(x << offset / y) with full precision The result will be rounded down

*Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv Requirements:

  • The offset needs to be strictly lower than 256

  • The result must fit within uint256 Caveats:

  • This function does not work with fixed-point numbers*

function shiftDivRoundDown(uint256 x, uint8 offset, uint256 denominator) internal pure returns (uint256 result);

Parameters

Name
Type
Description

x

uint256

The multiplicand as an uint256

offset

uint8

The number of bit to shift x as an uint256

denominator

uint256

The divisor as an uint256

Returns

Name
Type
Description

result

uint256

The result as an uint256

shiftDivRoundUp

Calculates ceil(x << offset / y) with full precision The result will be rounded up

*Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv Requirements:

  • The offset needs to be strictly lower than 256

  • The result must fit within uint256 Caveats:

  • This function does not work with fixed-point numbers*

function shiftDivRoundUp(uint256 x, uint8 offset, uint256 denominator) internal pure returns (uint256 result);

Parameters

Name
Type
Description

x

uint256

The multiplicand as an uint256

offset

uint8

The number of bit to shift x as an uint256

denominator

uint256

The divisor as an uint256

Returns

Name
Type
Description

result

uint256

The result as an uint256

_getMulProds

Helper function to return the result of x * y as 2 uint256

function _getMulProds(uint256 x, uint256 y) private pure returns (uint256 prod0, uint256 prod1);

Parameters

Name
Type
Description

x

uint256

The multiplicand as an uint256

y

uint256

The multiplier as an uint256

Returns

Name
Type
Description

prod0

uint256

The least significant 256 bits of the product

prod1

uint256

The most significant 256 bits of the product

_getEndOfDivRoundDown

Helper function to return the result of x * y / denominator with full precision

function _getEndOfDivRoundDown(uint256 x, uint256 y, uint256 denominator, uint256 prod0, uint256 prod1)
    private
    pure
    returns (uint256 result);

Parameters

Name
Type
Description

x

uint256

The multiplicand as an uint256

y

uint256

The multiplier as an uint256

denominator

uint256

The divisor as an uint256

prod0

uint256

The least significant 256 bits of the product

prod1

uint256

The most significant 256 bits of the product

Returns

Name
Type
Description

result

uint256

The result as an uint256

sqrt

Calculates the square root of x

Credit to OpenZeppelin's Math library under MIT license

function sqrt(uint256 x) internal pure returns (uint256 sqrtX);

Errors

Uint256x256Math__MulShiftOverflow

error Uint256x256Math__MulShiftOverflow();

Uint256x256Math__MulDivOverflow

error Uint256x256Math__MulDivOverflow();
Git Source