Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
@aperture_finance/uni-v3-lib
Advanced tools
A suite of Solidity libraries that have been imported and rewritten from Uniswap's v3-core and v3-periphery
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.
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.
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"
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
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:
src/test
and compiled using the Solidity
0.7.6 compiler.deployCode
to create these test wrappers using the bytecode from the artifacts.testGas
in the gas snapshot.To run the tests:
forge test
Contributions are welcome. Please ensure that any modifications pass all tests before submitting a pull request.
This repository is either inspired by or directly modified from:
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.
This project is licensed under the terms of the GNU General Public License v2.0 (GPL-2.0).
FAQs
A suite of Solidity libraries that have been imported and rewritten from Uniswap's v3-core and v3-periphery
The npm package @aperture_finance/uni-v3-lib receives a total of 146 weekly downloads. As such, @aperture_finance/uni-v3-lib popularity was classified as not popular.
We found that @aperture_finance/uni-v3-lib demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
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.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.