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

@aperture_finance/uni-v3-lib

v3.0.3

Published

A suite of Solidity libraries that have been imported and rewritten from Uniswap's v3-core and v3-periphery

Downloads

622

Readme

Uni V3 Lib

Lint Test npm version

The uni-v3-lib by Aperture Finance consists of a suite of Solidity libraries that have been imported and rewritten from Uniswap's v3-core and v3-periphery repositories. This project aims to equip external integrators with a set of libraries crucial for interaction with the Uniswap V3 protocol.

Overview

This repository focuses on certain libraries from Uniswap's repositories that are considered to be most useful for external integrators, e.g. SqrtPriceMath and TickMath. We have modified these libraries to make them compatible with modern Solidity compilers (versions greater than 0.8.0). Instead of simply changing the pragma and accepting the default safe math behavior, most functions have been optimized using inline assembly for less gas. Additional libraries PoolCaller and NPMCaller have been added to interact with the Uniswap V3 protocol more efficiently by omitting the extcodesize check and manipulating the stack and memory directly. Additional helpers, like nextInitializedTick which searches for the next initialized tick beyond one word, are also included. Optimization heuristics and techniques are documented by extensive annotations in the source code.

Gas Usage Comparison

| Library | Test | Original | Optimized | Gas Efficiency | |---------------|----------------------------|----------|-----------|----------------| | BitMath | testGas_LSB | 291088 | 193393 | 33.56% | | BitMath | testGas_MSB | 274437 | 213472 | 22.21% | | SqrtPriceMath | testGas_GetAmount0Delta | 280254 | 256572 | 8.45% | | SqrtPriceMath | testGas_GetAmount1Delta | 268385 | 209212 | 22.05% | | SwapMath | testGas_ComputeSwapStep | 526558 | 386504 | 26.60% | | TickMath | testGas_GetSqrtRatioAtTick | 168547 | 146478 | 13.09% | | TickMath | testGas_GetTickAtSqrtRatio | 307790 | 252577 | 17.94% |

The gas measured is the total gas used by the test transaction, including the gas used to call the test wrapper contract. The actual percentage difference in gas for the internal library functions is higher than the numbers shown above.

Libraries

The following libraries are included:

BitMath — "Functionality for computing bit properties of an unsigned integer"
CallbackValidation — "Provides validation for callbacks from Uniswap V3 Pools"
FullMath — "Contains 512-bit math functions"
LiquidityAmounts — "Provides functions for computing liquidity amounts from token amounts and prices"
LiquidityMath — "Math library for adding a signed liquidity delta to liquidity"
NPMCaller — "Gas efficient library to call `INonfungiblePositionManager` assuming it exists"
PoolAddress — "Provides functions for deriving a pool address from the factory, tokens, and the fee"
PoolCaller — "Gas efficient library to call `IUniswapV3Pool` assuming the pool exists"
SafeCast — "Library for safely casting between types"
SqrtPriceMath — "Functions based on Q64.96 sqrt price and liquidity"
SwapMath — "Computes the result of a swap within ticks"
TernaryLib — "Library for efficient ternary operations"
TickBitmap — "Packed tick initialized state library"
TickMath — "Math library for computing sqrt prices from ticks and vice versa"
UnsafeMath — "Math functions that do not check inputs or outputs"

Installation

We've utilized Foundry as our testing framework. Please follow the Foundry's installation instructions if you want to run the tests. Otherwise, you can simply install the dependencies using Yarn:

yarn install

Testing

The libraries in this repository have undergone rigorous fuzz testing using Foundry to ensure that they are equivalent to the original libraries and that the optimizations have been effective. For testing against the exact same bytecode compiled with Solidity 0.7.6, we adopt the following approach:

  1. The original Uniswap libraries are exposed by test wrapper contracts in src/test and compiled using the Solidity 0.7.6 compiler.
  2. We use the Forge cheatcode deployCode to create these test wrappers using the bytecode from the artifacts.
  3. We verify the equivalence between the modified libraries and the original ones using Foundry's fuzz testing.
  4. The effectiveness of optimizations can be assessed by tests prefixed by testGas in the gas snapshot.

To run the tests:

forge test

Inline Assembly

The libraries in this repository make use of in-line assembly for fine-grained control and optimizations. Knowledge of ABI encoding is required to understand how to calculate calldata length and parameter offsets. Helpful links:

Contributions

Contributions are welcome. Please ensure that any modifications pass all tests before submitting a pull request.

Acknowledgements

This repository is either inspired by or directly modified from:

Disclaimer

The uni-v3-lib is experimental software and is provided on an "as is" and "as available" basis. Aperture Finance does not offer any warranties and will not be responsible for any loss incurred through any use of this library.

While uni-v3-lib has undergone extensive testing, it may still have parts that could exhibit unexpected behavior when used in conjunction with other code or might not operate correctly in future versions of Solidity.

Users are strongly urged to conduct their own comprehensive tests when incorporating uni-v3-lib into their projects to ensure its correct operation within their specific code context. Use of this library is at the user's own risk.

License

This project is licensed under the terms of the GNU General Public License v2.0 (GPL-2.0).