Socket
Socket
Sign inDemoInstall

@aperture_finance/uni-v3-lib

Package Overview
Dependencies
Maintainers
3
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@aperture_finance/uni-v3-lib

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


Version published
Weekly downloads
175
increased by12.9%
Maintainers
3
Weekly downloads
 
Created
Source

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

LibraryTestOriginalOptimizedGas Efficiency
BitMathtestGas_LSB29108819339333.56%
BitMathtestGas_MSB27443721347222.21%
SqrtPriceMathtestGas_GetAmount0Delta2802542565728.45%
SqrtPriceMathtestGas_GetAmount1Delta26838520921222.05%
SwapMathtestGas_ComputeSwapStep52655838650426.60%
TickMathtestGas_GetSqrtRatioAtTick16854714647813.09%
TickMathtestGas_GetTickAtSqrtRatio30779025257717.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

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).

Keywords

FAQs

Package last updated on 23 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc