npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cfmm-math-libraries

v1.0.9

Published

Comprehensive CFMM Solidity Math libraries for price simulation, fixed point conversion, and swap simulation on v2/v3 pools.

Downloads

31

Readme

 ________  ________ _____ ______   _____ ______           _____ ______   ________  _________  ___  ___     
|\   ____\|\  _____\\   _ \  _   \|\   _ \  _   \        |\   _ \  _   \|\   __  \|\___   ___\\  \|\  \    
\ \  \___|\ \  \__/\ \  \\\__\ \  \ \  \\\__\ \  \       \ \  \\\__\ \  \ \  \|\  \|___ \  \_\ \  \\\  \   
 \ \  \    \ \   __\\ \  \\|__| \  \ \  \\|__| \  \       \ \  \\|__| \  \ \   __  \   \ \  \ \ \   __  \  
  \ \  \____\ \  \_| \ \  \    \ \  \ \  \    \ \  \       \ \  \    \ \  \ \  \ \  \   \ \  \ \ \  \ \  \ 
   \ \_______\ \__\   \ \__\    \ \__\ \__\    \ \__\       \ \__\    \ \__\ \__\ \__\   \ \__\ \ \__\ \__\
    \|_______|\|__|    \|__|     \|__|\|__|     \|__|        \|__|     \|__|\|__|\|__|    \|__|  \|__|\|__|
   

CFMM Math Solidity library & contract for price simulation, fixed point conversion, and swap simulation on v2/v3 pools. The idea behind CFMM math is to provide an out of the box solidity tool suite that allows anyone to simulate, quote, compare, and convert v2/v3 prices seemlessly in a standardized fixed point representation.

Installation

To install as an npm package run:

npm i cfmm-math-libraries

Test Instructions

From the project route. Run Test Suite:

forge test -f <RPC_ENDPOINT>  --fork-block-number 15233771

Run Gas Snapshot:

forge snapshot -f <RPC_ENDPOINT> --fork-block-number 15233771

Features

CFMMQuoter

CFMMQuoter.sol offers a comprehensive tool suite for quoting amounts yielded from swaps, simulating price changes from input/output quantities, and inferring input/output quantities introduced/removed from a pool based on price changes. CFMMQuoter is a contract not a library. So, to cheaply utilize all of its core functions in your contract you can simply inherit CFMMQuoter.

V3 Features

Precisely quotes the amount yielded from a swap on a v3 pool. This function can be used to determine a precise amountOutMin for a v3 swap on chain, and eliminates the need to use the v3 quoter saving a significant amount of gas.

function simulateAmountOutOnSqrtPriceX96(
    address token0,
    address tokenIn,
    address lpAddressAToWeth,
    uint256 amountIn,
    int24 tickSpacing,
    uint128 liquidity,
    uint24 fee
) internal returns (int256 amountOut

Simulates tick state of a v3 pool after swapping amountIn of tokenIn.

function simulateNewTickOnInputAmountPrecise(
        address token0,
        address tokenIn,
        address lpAddressAToWeth,
        uint256 amountIn,
        int24 tickSpacing,
        uint128 liquidity,
        uint24 fee
    ) internal returns (int24 limitTick)

Calculates the exact amount introduced/removed from a v3 pool to move the price from sqrtRatioAX96 to sqrtRatioBX96.

function calculateDeltaAddedOnSqrtPriceChange(
        uint160 sqrtRatioAX96,
        uint160 sqrtRatioBX96,
        uint128 liquidity
    ) internal pure returns (uint256 amountDelta) {}
    
function calculateDeltaRemovedOnSqrtPriceChange(
        uint160 sqrtRatioAX96,
        uint160 sqrtRatioBX96,
        uint128 liquidity
    ) internal pure returns (uint256 amountDelta) {}

V2 Features

Precisely Quotes the amount1 received on a v2 swap.

function calculateAmountOutV2(
        uint256 amount0,
        uint256 reserve0,
        uint256 reserve1
    ) internal pure returns (uint256 amount1) {}

Precisely calculates the spot price in a v2 pool after swapping amount0.

function v2SimulateNewSpotFromInput(
        uint112 reserve0,
        uint112 reserve1,
        uint112 amount0
    ) internal pure returns (uint128) {}

CFMMMath

CFMMMath provdes a tool suite for mathematical operations in 64.64 fixed point.

V3 Functions

Converts sqrtRatioX96 (sqrt(spot price)) of form Q96.64 to 64.64 fixed point representation of the spot price normalized by token decimals, and directionally representative of the base/quote token.

function fromSqrtX96ToX64(
        uint8 decimals0,
        uint8 decimals1,
        uint160 sqrtPriceX96,
        bool token0IsReserve0
    ) internal pure returns (uint128 priceX64) {}

Converts 64.64 fixed point representation of the spotPrice into sqrt(spotPrice) as Q96.64 unnormalized on token decimals.

function from64XSpotToSqrtRatioX96(
        uint8 decimals0,
        uint8 decimals1,
        uint128 spot64X,
        bool zeroForOne
    ) internal pure returns (uint160 sqrtRatioX96) {}

V2 Functions

Calculates a v2 spot price from the pool reserves as a 64.64 fixed point number.

function v2SpotPrice64x64(
        uint112 reserve0,
        uint112 reserve1,
        uint8 decimals0,
        uint8 decimals1
    ) internal pure returns (uint128) {}

Normalizes v2 reserves to a target decimal.

function v2NormalizeReserves(
        uint112 _reserve0,
        uint112 _reserve1,
        uint8 decimals0,
        uint8 decimals1,
        uint8 _targetDecimals
    ) internal pure returns (uint112 reserve0, uint112 reserve1) {}

Extra CFMM Helpers

function proportionalPriceDifferenceUnsigned(uint128 x, uint128 y)
        internal
        pure
        returns (uint128)
    {}
function proportionalPriceDifferenceSigned(uint128 x, uint128 y)
        internal
        pure
        returns (int128)
    {}

Standard Operation Functions

Converts unsigned integer to 64.64 fixed point.

fromUInt256(uint256 x)

Converts 64.64 fixed point to unsigned integer.

toUInt(uint128 x)

Converts 128.128 fixed point to 64.64 fixed point.

from128x128(uint256 x)

Adds two 64.64 fixed point numbers.

add64x64(uint128 x, uint128 y)

Computes a - b where a is 64.64 fixed point, and b is an unsigned integer.

sub64x64U(int128 x, int128 y)

Computes a - b where a is 64.64 fixed point, and b is an nsigned integer.

sub64x64I(int128 x, int128 y)

Computes the product of two 64.64 fixed point numbers.

mul64x64(uint128 x, uint128 y)

Computes a/b where a & b are both 64.64 fixed point numbers.

div64x64(uint128 x, uint128 y)

Computes a/b where a & b are both unsigned integers and returns the result as 64.64 fixed point number.

divuu(uint256 x, uint256 y)

Computes the absolute value of a signed integer.

abs(int256 x)

Computes the binary exponent of a 64.64 fixed point number.

exp_2(uint128 x)

Computes the square root of an unsigned integer and returns the result as 64.64 fixed point number.

sqrtu(uint256 x)

Additional Features Coming Soon

1.) Derive input quantity from 2 v2 prices. 2.) Simulate Tick Change on output V3 3.) Simulate amountIn on exact output sqrtPriceX96 V3. 4.) DODO Architecture Integration 5.) Curve Architecture Integration

Additionally feel free to open an issue on any features you would like integrated!