Skip to content

Commit

Permalink
reuse Uniswap BitMath
Browse files Browse the repository at this point in the history
  • Loading branch information
ZumZoom committed Oct 18, 2023
1 parent fe61224 commit a9c78c6
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 123 deletions.
49 changes: 4 additions & 45 deletions contracts/AlgebraHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@

pragma solidity 0.8.19;

import "@uniswap/v3-core/contracts/libraries/BitMath.sol";
import "./interfaces/IAlgebra.sol";

/// @title AlgebraHelper
/// @dev This contract includes helper functions for the Algebra protocol.
contract AlgebraHelper {
/// @dev Minimum allowed tick value.
int24 private constant _MIN_TICK = -887272;

/// @dev Maximum allowed tick value.
int24 private constant _MAX_TICK = -_MIN_TICK;

/// @dev Base fee for transactions.
uint16 internal constant _BASE_FEE = 100;

/// @dev Spacing between ticks.
int24 internal constant _TICK_SPACING = 60;

Expand Down Expand Up @@ -64,7 +65,7 @@ contract AlgebraHelper {
uint256 bm = pool.tickTable(pos);

while (bm != 0) {
uint8 bit = _leastSignificantBit(bm);
uint8 bit = BitMath.leastSignificantBit(bm);
bm ^= 1 << bit;
int24 extractedTick = ((int24(pos) << 8) | int24(uint24(bit))) * _TICK_SPACING;
if (extractedTick >= fromTick && extractedTick <= toTick) {
Expand Down Expand Up @@ -98,46 +99,4 @@ contract AlgebraHelper {
);
}
}

/**
* @notice Determines the position of the least significant bit in the given number.
* @dev The function works by repeatedly halving the number and keeping track of the number of operations
* performed until the number is less than 2.
* @param x The input number for which the least significant bit position is to be found.
* @return r The position of the least significant bit in the given number.
*/
function _leastSignificantBit(uint256 x) private pure returns (uint8 r) {
require(x > 0, "x is 0");
x = x & (~x + 1);

if (x >= 0x100000000000000000000000000000000) {
x >>= 128;
r += 128;
}
if (x >= 0x10000000000000000) {
x >>= 64;
r += 64;
}
if (x >= 0x100000000) {
x >>= 32;
r += 32;
}
if (x >= 0x10000) {
x >>= 16;
r += 16;
}
if (x >= 0x100) {
x >>= 8;
r += 8;
}
if (x >= 0x10) {
x >>= 4;
r += 4;
}
if (x >= 0x4) {
x >>= 2;
r += 2;
}
if (x >= 0x2) r += 1;
}
}
36 changes: 2 additions & 34 deletions contracts/SolidlyV3Helper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

pragma solidity 0.8.19;

import "@uniswap/v3-core/contracts/libraries/BitMath.sol";
import "./interfaces/IUniswapV3.sol";

contract SolidlyV3Helper {
Expand Down Expand Up @@ -29,7 +30,7 @@ contract SolidlyV3Helper {
uint256 bm = pool.tickBitmap(pos);

while (bm != 0) {
uint8 bit = _mostSignificantBit(bm);
uint8 bit = BitMath.mostSignificantBit(bm);
initTicks[counter] = (int24(pos) * 256 + int24(int256(uint256(bit)))) * tickSpacing;
counter += 1;
bm ^= 1 << bit;
Expand All @@ -41,37 +42,4 @@ contract SolidlyV3Helper {
ticks[i] = abi.encodePacked(initTicks[i]);
}
}

// @dev Parameter `x` should be greater than 0, but it is never equal to 0 in this contract.
function _mostSignificantBit(uint256 x) private pure returns (uint8 r) {
if (x >= 0x100000000000000000000000000000000) {
x >>= 128;
r += 128;
}
if (x >= 0x10000000000000000) {
x >>= 64;
r += 64;
}
if (x >= 0x100000000) {
x >>= 32;
r += 32;
}
if (x >= 0x10000) {
x >>= 16;
r += 16;
}
if (x >= 0x100) {
x >>= 8;
r += 8;
}
if (x >= 0x10) {
x >>= 4;
r += 4;
}
if (x >= 0x4) {
x >>= 2;
r += 2;
}
if (x >= 0x2) r += 1;
}
}
45 changes: 2 additions & 43 deletions contracts/UniV3Helper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

pragma solidity 0.8.19;

import "@uniswap/v3-core/contracts/libraries/BitMath.sol";
import "./interfaces/IUniswapV3.sol";

/// @title UniV3Helper
Expand Down Expand Up @@ -44,7 +45,7 @@ contract UniV3Helper {
uint256 bm = pool.tickBitmap(pos);

while (bm != 0) {
uint8 bit = _leastSignificantBit(bm);
uint8 bit = BitMath.leastSignificantBit(bm);
bm ^= 1 << bit;
int24 extractedTick = ((int24(pos) << 8) | int24(uint24(bit))) * tickSpacing;
if (extractedTick >= fromTick && extractedTick <= toTick) {
Expand Down Expand Up @@ -78,46 +79,4 @@ contract UniV3Helper {
);
}
}

/**
* @notice Determines the position of the least significant bit in the given number.
* @dev The function works by repeatedly halving the number and keeping track of the number of operations
* performed until the number is less than 2.
* @param x The input number for which the least significant bit position is to be found.
* @return r The position of the least significant bit in the given number.
*/
function _leastSignificantBit(uint256 x) private pure returns (uint8 r) {
require(x > 0, "x is 0");
x = x & (~x + 1);

if (x >= 0x100000000000000000000000000000000) {
x >>= 128;
r += 128;
}
if (x >= 0x10000000000000000) {
x >>= 64;
r += 64;
}
if (x >= 0x100000000) {
x >>= 32;
r += 32;
}
if (x >= 0x10000) {
x >>= 16;
r += 16;
}
if (x >= 0x100) {
x >>= 8;
r += 8;
}
if (x >= 0x10) {
x >>= 4;
r += 4;
}
if (x >= 0x4) {
x >>= 2;
r += 2;
}
if (x >= 0x2) r += 1;
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"license": "MIT",
"dependencies": {
"@1inch/solidity-utils": "2.4.0",
"@openzeppelin/contracts": "4.9.1"
"@openzeppelin/contracts": "4.9.1",
"@uniswap/v3-core": "1.0.1"
},
"devDependencies": {
"@matterlabs/hardhat-zksync-deploy": "0.6.3",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,11 @@
ethers "^5.3.1"
tiny-invariant "^1.3.1"

"@uniswap/v3-core@1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@uniswap/v3-core/-/v3-core-1.0.1.tgz#b6d2bdc6ba3c3fbd610bdc502395d86cd35264a0"
integrity sha512-7pVk4hEm00j9tc71Y9+ssYpO6ytkeI0y7WE9P6UcmNzhxPePwyAxImuhVsTqWK9YFvzgtvzJHi64pBl4jUzKMQ==

JSONStream@1.3.2:
version "1.3.2"
resolved "https://registry.yarnpkg.com/JSONStream/-/JSONStream-1.3.2.tgz#c102371b6ec3a7cf3b847ca00c20bb0fce4c6dea"
Expand Down

0 comments on commit a9c78c6

Please sign in to comment.