@huuduynvc/v3-periphery 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/LICENSE +339 -0
  2. package/README.md +52 -0
  3. package/artifacts/contracts/NonfungiblePositionManager.sol/NonfungiblePositionManager.json +1230 -0
  4. package/artifacts/contracts/NonfungibleTokenPositionDescriptor.sol/NonfungibleTokenPositionDescriptor.json +161 -0
  5. package/artifacts/contracts/SwapRouter.sol/SwapRouter.json +574 -0
  6. package/artifacts/contracts/V3Migrator.sol/V3Migrator.json +360 -0
  7. package/artifacts/contracts/examples/PairFlash.sol/PairFlash.json +196 -0
  8. package/artifacts/contracts/interfaces/IERC20Metadata.sol/IERC20Metadata.json +233 -0
  9. package/artifacts/contracts/interfaces/IERC721Permit.sol/IERC721Permit.json +360 -0
  10. package/artifacts/contracts/interfaces/IMulticall.sol/IMulticall.json +30 -0
  11. package/artifacts/contracts/interfaces/INonfungiblePositionManager.sol/INonfungiblePositionManager.json +998 -0
  12. package/artifacts/contracts/interfaces/INonfungibleTokenPositionDescriptor.sol/INonfungibleTokenPositionDescriptor.json +35 -0
  13. package/artifacts/contracts/interfaces/IPeripheryImmutableState.sol/IPeripheryImmutableState.json +37 -0
  14. package/artifacts/contracts/interfaces/IPeripheryPayments.sol/IPeripheryPayments.json +59 -0
  15. package/artifacts/contracts/interfaces/IPeripheryPaymentsWithFee.sol/IPeripheryPaymentsWithFee.json +120 -0
  16. package/artifacts/contracts/interfaces/IPoolInitializer.sol/IPoolInitializer.json +45 -0
  17. package/artifacts/contracts/interfaces/IQuoter.sol/IQuoter.json +137 -0
  18. package/artifacts/contracts/interfaces/IQuoterV2.sol/IQuoterV2.json +211 -0
  19. package/artifacts/contracts/interfaces/ISelfPermit.sol/ISelfPermit.json +163 -0
  20. package/artifacts/contracts/interfaces/ISwapRouter.sol/ISwapRouter.json +248 -0
  21. package/artifacts/contracts/interfaces/ITickLens.sol/ITickLens.json +52 -0
  22. package/artifacts/contracts/interfaces/IV3Migrator.sol/IV3Migrator.json +296 -0
  23. package/artifacts/contracts/interfaces/external/IERC1271.sol/IERC1271.json +35 -0
  24. package/artifacts/contracts/interfaces/external/IERC20PermitAllowed.sol/IERC20PermitAllowed.json +59 -0
  25. package/artifacts/contracts/interfaces/external/IWETH9.sol/IWETH9.json +214 -0
  26. package/artifacts/contracts/lens/Quoter.sol/Quoter.json +202 -0
  27. package/artifacts/contracts/lens/QuoterV2.sol/QuoterV2.json +276 -0
  28. package/artifacts/contracts/lens/TickLens.sol/TickLens.json +52 -0
  29. package/artifacts/contracts/lens/UniswapInterfaceMulticall.sol/UniswapInterfaceMulticall.json +101 -0
  30. package/artifacts/contracts/libraries/BytesLib.sol/BytesLib.json +10 -0
  31. package/artifacts/contracts/libraries/CallbackValidation.sol/CallbackValidation.json +10 -0
  32. package/artifacts/contracts/libraries/ChainId.sol/ChainId.json +10 -0
  33. package/artifacts/contracts/libraries/HexStrings.sol/HexStrings.json +10 -0
  34. package/artifacts/contracts/libraries/LiquidityAmounts.sol/LiquidityAmounts.json +10 -0
  35. package/artifacts/contracts/libraries/NFTDescriptor.sol/NFTDescriptor.json +102 -0
  36. package/artifacts/contracts/libraries/NFTSVG.sol/NFTSVG.json +10 -0
  37. package/artifacts/contracts/libraries/OracleLibrary.sol/OracleLibrary.json +10 -0
  38. package/artifacts/contracts/libraries/Path.sol/Path.json +10 -0
  39. package/artifacts/contracts/libraries/PoolAddress.sol/PoolAddress.json +10 -0
  40. package/artifacts/contracts/libraries/PoolTicksCounter.sol/PoolTicksCounter.json +10 -0
  41. package/artifacts/contracts/libraries/PositionKey.sol/PositionKey.json +10 -0
  42. package/artifacts/contracts/libraries/PositionValue.sol/PositionValue.json +10 -0
  43. package/artifacts/contracts/libraries/TokenRatioSortOrder.sol/TokenRatioSortOrder.json +10 -0
  44. package/artifacts/contracts/libraries/TransferHelper.sol/TransferHelper.json +10 -0
  45. package/contracts/base/BlockTimestamp.sol +12 -0
  46. package/contracts/base/ERC721Permit.sol +86 -0
  47. package/contracts/base/LiquidityManagement.sol +90 -0
  48. package/contracts/base/Multicall.sol +28 -0
  49. package/contracts/base/PeripheryImmutableState.sol +18 -0
  50. package/contracts/base/PeripheryPayments.sol +70 -0
  51. package/contracts/base/PeripheryPaymentsWithFee.sol +55 -0
  52. package/contracts/base/PeripheryValidation.sol +11 -0
  53. package/contracts/base/PoolInitializer.sol +32 -0
  54. package/contracts/base/SelfPermit.sol +63 -0
  55. package/contracts/interfaces/IERC20Metadata.sol +18 -0
  56. package/contracts/interfaces/IERC721Permit.sol +32 -0
  57. package/contracts/interfaces/IMulticall.sol +13 -0
  58. package/contracts/interfaces/INonfungiblePositionManager.sol +180 -0
  59. package/contracts/interfaces/INonfungibleTokenPositionDescriptor.sol +17 -0
  60. package/contracts/interfaces/IPeripheryImmutableState.sol +12 -0
  61. package/contracts/interfaces/IPeripheryPayments.sol +28 -0
  62. package/contracts/interfaces/IPeripheryPaymentsWithFee.sol +29 -0
  63. package/contracts/interfaces/IPoolInitializer.sol +22 -0
  64. package/contracts/interfaces/IQuoter.sol +51 -0
  65. package/contracts/interfaces/IQuoterV2.sol +98 -0
  66. package/contracts/interfaces/ISelfPermit.sol +76 -0
  67. package/contracts/interfaces/ISwapRouter.sol +67 -0
  68. package/contracts/interfaces/ITickLens.sol +25 -0
  69. package/contracts/interfaces/IV3Migrator.sol +34 -0
  70. package/contracts/interfaces/external/IERC1271.sol +16 -0
  71. package/contracts/interfaces/external/IERC20PermitAllowed.sol +27 -0
  72. package/contracts/interfaces/external/IWETH9.sol +13 -0
  73. package/contracts/libraries/BytesLib.sol +101 -0
  74. package/contracts/libraries/CallbackValidation.sol +36 -0
  75. package/contracts/libraries/ChainId.sol +13 -0
  76. package/contracts/libraries/HexStrings.sol +29 -0
  77. package/contracts/libraries/LiquidityAmounts.sol +137 -0
  78. package/contracts/libraries/NFTDescriptor.sol +477 -0
  79. package/contracts/libraries/NFTSVG.sol +406 -0
  80. package/contracts/libraries/OracleLibrary.sol +161 -0
  81. package/contracts/libraries/Path.sol +69 -0
  82. package/contracts/libraries/PoolAddress.sol +48 -0
  83. package/contracts/libraries/PoolTicksCounter.sol +97 -0
  84. package/contracts/libraries/PositionKey.sol +13 -0
  85. package/contracts/libraries/PositionValue.sol +167 -0
  86. package/contracts/libraries/TokenRatioSortOrder.sol +12 -0
  87. package/contracts/libraries/TransferHelper.sol +60 -0
  88. package/package.json +68 -0
@@ -0,0 +1,16 @@
1
+ // SPDX-License-Identifier: GPL-2.0-or-later
2
+ pragma solidity >=0.5.0;
3
+
4
+ /// @title Interface for verifying contract-based account signatures
5
+ /// @notice Interface that verifies provided signature for the data
6
+ /// @dev Interface defined by EIP-1271
7
+ interface IERC1271 {
8
+ /// @notice Returns whether the provided signature is valid for the provided data
9
+ /// @dev MUST return the bytes4 magic value 0x1626ba7e when function passes.
10
+ /// MUST NOT modify state (using STATICCALL for solc < 0.5, view modifier for solc > 0.5).
11
+ /// MUST allow external calls.
12
+ /// @param hash Hash of the data to be signed
13
+ /// @param signature Signature byte array associated with _data
14
+ /// @return magicValue The bytes4 magic value 0x1626ba7e
15
+ function isValidSignature(bytes32 hash, bytes memory signature) external view returns (bytes4 magicValue);
16
+ }
@@ -0,0 +1,27 @@
1
+ // SPDX-License-Identifier: GPL-2.0-or-later
2
+ pragma solidity >=0.5.0;
3
+
4
+ /// @title Interface for permit
5
+ /// @notice Interface used by DAI/CHAI for permit
6
+ interface IERC20PermitAllowed {
7
+ /// @notice Approve the spender to spend some tokens via the holder signature
8
+ /// @dev This is the permit interface used by DAI and CHAI
9
+ /// @param holder The address of the token holder, the token owner
10
+ /// @param spender The address of the token spender
11
+ /// @param nonce The holder's nonce, increases at each call to permit
12
+ /// @param expiry The timestamp at which the permit is no longer valid
13
+ /// @param allowed Boolean that sets approval amount, true for type(uint256).max and false for 0
14
+ /// @param v Must produce valid secp256k1 signature from the holder along with `r` and `s`
15
+ /// @param r Must produce valid secp256k1 signature from the holder along with `v` and `s`
16
+ /// @param s Must produce valid secp256k1 signature from the holder along with `r` and `v`
17
+ function permit(
18
+ address holder,
19
+ address spender,
20
+ uint256 nonce,
21
+ uint256 expiry,
22
+ bool allowed,
23
+ uint8 v,
24
+ bytes32 r,
25
+ bytes32 s
26
+ ) external;
27
+ }
@@ -0,0 +1,13 @@
1
+ // SPDX-License-Identifier: GPL-2.0-or-later
2
+ pragma solidity =0.7.6;
3
+
4
+ import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
5
+
6
+ /// @title Interface for WETH9
7
+ interface IWETH9 is IERC20 {
8
+ /// @notice Deposit ether to get wrapped ether
9
+ function deposit() external payable;
10
+
11
+ /// @notice Withdraw wrapped ether to get ether
12
+ function withdraw(uint256) external;
13
+ }
@@ -0,0 +1,101 @@
1
+ // SPDX-License-Identifier: GPL-2.0-or-later
2
+ /*
3
+ * @title Solidity Bytes Arrays Utils
4
+ * @author Gonçalo Sá <goncalo.sa@consensys.net>
5
+ *
6
+ * @dev Bytes tightly packed arrays utility library for ethereum contracts written in Solidity.
7
+ * The library lets you concatenate, slice and type cast bytes arrays both in memory and storage.
8
+ */
9
+ pragma solidity >=0.5.0 <0.8.0;
10
+
11
+ library BytesLib {
12
+ function slice(
13
+ bytes memory _bytes,
14
+ uint256 _start,
15
+ uint256 _length
16
+ ) internal pure returns (bytes memory) {
17
+ require(_length + 31 >= _length, 'slice_overflow');
18
+ require(_start + _length >= _start, 'slice_overflow');
19
+ require(_bytes.length >= _start + _length, 'slice_outOfBounds');
20
+
21
+ bytes memory tempBytes;
22
+
23
+ assembly {
24
+ switch iszero(_length)
25
+ case 0 {
26
+ // Get a location of some free memory and store it in tempBytes as
27
+ // Solidity does for memory variables.
28
+ tempBytes := mload(0x40)
29
+
30
+ // The first word of the slice result is potentially a partial
31
+ // word read from the original array. To read it, we calculate
32
+ // the length of that partial word and start copying that many
33
+ // bytes into the array. The first word we copy will start with
34
+ // data we don't care about, but the last `lengthmod` bytes will
35
+ // land at the beginning of the contents of the new array. When
36
+ // we're done copying, we overwrite the full first word with
37
+ // the actual length of the slice.
38
+ let lengthmod := and(_length, 31)
39
+
40
+ // The multiplication in the next line is necessary
41
+ // because when slicing multiples of 32 bytes (lengthmod == 0)
42
+ // the following copy loop was copying the origin's length
43
+ // and then ending prematurely not copying everything it should.
44
+ let mc := add(add(tempBytes, lengthmod), mul(0x20, iszero(lengthmod)))
45
+ let end := add(mc, _length)
46
+
47
+ for {
48
+ // The multiplication in the next line has the same exact purpose
49
+ // as the one above.
50
+ let cc := add(add(add(_bytes, lengthmod), mul(0x20, iszero(lengthmod))), _start)
51
+ } lt(mc, end) {
52
+ mc := add(mc, 0x20)
53
+ cc := add(cc, 0x20)
54
+ } {
55
+ mstore(mc, mload(cc))
56
+ }
57
+
58
+ mstore(tempBytes, _length)
59
+
60
+ //update free-memory pointer
61
+ //allocating the array padded to 32 bytes like the compiler does now
62
+ mstore(0x40, and(add(mc, 31), not(31)))
63
+ }
64
+ //if we want a zero-length slice let's just return a zero-length array
65
+ default {
66
+ tempBytes := mload(0x40)
67
+ //zero out the 32 bytes slice we are about to return
68
+ //we need to do it because Solidity does not garbage collect
69
+ mstore(tempBytes, 0)
70
+
71
+ mstore(0x40, add(tempBytes, 0x20))
72
+ }
73
+ }
74
+
75
+ return tempBytes;
76
+ }
77
+
78
+ function toAddress(bytes memory _bytes, uint256 _start) internal pure returns (address) {
79
+ require(_start + 20 >= _start, 'toAddress_overflow');
80
+ require(_bytes.length >= _start + 20, 'toAddress_outOfBounds');
81
+ address tempAddress;
82
+
83
+ assembly {
84
+ tempAddress := div(mload(add(add(_bytes, 0x20), _start)), 0x1000000000000000000000000)
85
+ }
86
+
87
+ return tempAddress;
88
+ }
89
+
90
+ function toUint24(bytes memory _bytes, uint256 _start) internal pure returns (uint24) {
91
+ require(_start + 3 >= _start, 'toUint24_overflow');
92
+ require(_bytes.length >= _start + 3, 'toUint24_outOfBounds');
93
+ uint24 tempUint;
94
+
95
+ assembly {
96
+ tempUint := mload(add(add(_bytes, 0x3), _start))
97
+ }
98
+
99
+ return tempUint;
100
+ }
101
+ }
@@ -0,0 +1,36 @@
1
+ // SPDX-License-Identifier: GPL-2.0-or-later
2
+ pragma solidity =0.7.6;
3
+
4
+ import '@huuduynvc/v3-core/contracts/interfaces/IUniswapV3Pool.sol';
5
+ import './PoolAddress.sol';
6
+
7
+ /// @notice Provides validation for callbacks from Uniswap V3 Pools
8
+ library CallbackValidation {
9
+ /// @notice Returns the address of a valid Uniswap V3 Pool
10
+ /// @param factory The contract address of the Uniswap V3 factory
11
+ /// @param tokenA The contract address of either token0 or token1
12
+ /// @param tokenB The contract address of the other token
13
+ /// @param fee The fee collected upon every swap in the pool, denominated in hundredths of a bip
14
+ /// @return pool The V3 pool contract address
15
+ function verifyCallback(
16
+ address factory,
17
+ address tokenA,
18
+ address tokenB,
19
+ uint24 fee
20
+ ) internal view returns (IUniswapV3Pool pool) {
21
+ return verifyCallback(factory, PoolAddress.getPoolKey(tokenA, tokenB, fee));
22
+ }
23
+
24
+ /// @notice Returns the address of a valid Uniswap V3 Pool
25
+ /// @param factory The contract address of the Uniswap V3 factory
26
+ /// @param poolKey The identifying key of the V3 pool
27
+ /// @return pool The V3 pool contract address
28
+ function verifyCallback(address factory, PoolAddress.PoolKey memory poolKey)
29
+ internal
30
+ view
31
+ returns (IUniswapV3Pool pool)
32
+ {
33
+ pool = IUniswapV3Pool(PoolAddress.computeAddress(factory, poolKey));
34
+ require(msg.sender == address(pool));
35
+ }
36
+ }
@@ -0,0 +1,13 @@
1
+ // SPDX-License-Identifier: GPL-2.0-or-later
2
+ pragma solidity >=0.7.0;
3
+
4
+ /// @title Function for getting the current chain ID
5
+ library ChainId {
6
+ /// @dev Gets the current chain ID
7
+ /// @return chainId The current chain ID
8
+ function get() internal pure returns (uint256 chainId) {
9
+ assembly {
10
+ chainId := chainid()
11
+ }
12
+ }
13
+ }
@@ -0,0 +1,29 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity =0.7.6;
3
+
4
+ library HexStrings {
5
+ bytes16 internal constant ALPHABET = '0123456789abcdef';
6
+
7
+ /// @notice Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
8
+ /// @dev Credit to Open Zeppelin under MIT license https://github.com/OpenZeppelin/openzeppelin-contracts/blob/243adff49ce1700e0ecb99fe522fb16cff1d1ddc/contracts/utils/Strings.sol#L55
9
+ function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
10
+ bytes memory buffer = new bytes(2 * length + 2);
11
+ buffer[0] = '0';
12
+ buffer[1] = 'x';
13
+ for (uint256 i = 2 * length + 1; i > 1; --i) {
14
+ buffer[i] = ALPHABET[value & 0xf];
15
+ value >>= 4;
16
+ }
17
+ require(value == 0, 'Strings: hex length insufficient');
18
+ return string(buffer);
19
+ }
20
+
21
+ function toHexStringNoPrefix(uint256 value, uint256 length) internal pure returns (string memory) {
22
+ bytes memory buffer = new bytes(2 * length);
23
+ for (uint256 i = buffer.length; i > 0; i--) {
24
+ buffer[i - 1] = ALPHABET[value & 0xf];
25
+ value >>= 4;
26
+ }
27
+ return string(buffer);
28
+ }
29
+ }
@@ -0,0 +1,137 @@
1
+ // SPDX-License-Identifier: GPL-2.0-or-later
2
+ pragma solidity >=0.5.0;
3
+
4
+ import '@huuduynvc/v3-core/contracts/libraries/FullMath.sol';
5
+ import '@huuduynvc/v3-core/contracts/libraries/FixedPoint96.sol';
6
+
7
+ /// @title Liquidity amount functions
8
+ /// @notice Provides functions for computing liquidity amounts from token amounts and prices
9
+ library LiquidityAmounts {
10
+ /// @notice Downcasts uint256 to uint128
11
+ /// @param x The uint258 to be downcasted
12
+ /// @return y The passed value, downcasted to uint128
13
+ function toUint128(uint256 x) private pure returns (uint128 y) {
14
+ require((y = uint128(x)) == x);
15
+ }
16
+
17
+ /// @notice Computes the amount of liquidity received for a given amount of token0 and price range
18
+ /// @dev Calculates amount0 * (sqrt(upper) * sqrt(lower)) / (sqrt(upper) - sqrt(lower))
19
+ /// @param sqrtRatioAX96 A sqrt price representing the first tick boundary
20
+ /// @param sqrtRatioBX96 A sqrt price representing the second tick boundary
21
+ /// @param amount0 The amount0 being sent in
22
+ /// @return liquidity The amount of returned liquidity
23
+ function getLiquidityForAmount0(
24
+ uint160 sqrtRatioAX96,
25
+ uint160 sqrtRatioBX96,
26
+ uint256 amount0
27
+ ) internal pure returns (uint128 liquidity) {
28
+ if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
29
+ uint256 intermediate = FullMath.mulDiv(sqrtRatioAX96, sqrtRatioBX96, FixedPoint96.Q96);
30
+ return toUint128(FullMath.mulDiv(amount0, intermediate, sqrtRatioBX96 - sqrtRatioAX96));
31
+ }
32
+
33
+ /// @notice Computes the amount of liquidity received for a given amount of token1 and price range
34
+ /// @dev Calculates amount1 / (sqrt(upper) - sqrt(lower)).
35
+ /// @param sqrtRatioAX96 A sqrt price representing the first tick boundary
36
+ /// @param sqrtRatioBX96 A sqrt price representing the second tick boundary
37
+ /// @param amount1 The amount1 being sent in
38
+ /// @return liquidity The amount of returned liquidity
39
+ function getLiquidityForAmount1(
40
+ uint160 sqrtRatioAX96,
41
+ uint160 sqrtRatioBX96,
42
+ uint256 amount1
43
+ ) internal pure returns (uint128 liquidity) {
44
+ if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
45
+ return toUint128(FullMath.mulDiv(amount1, FixedPoint96.Q96, sqrtRatioBX96 - sqrtRatioAX96));
46
+ }
47
+
48
+ /// @notice Computes the maximum amount of liquidity received for a given amount of token0, token1, the current
49
+ /// pool prices and the prices at the tick boundaries
50
+ /// @param sqrtRatioX96 A sqrt price representing the current pool prices
51
+ /// @param sqrtRatioAX96 A sqrt price representing the first tick boundary
52
+ /// @param sqrtRatioBX96 A sqrt price representing the second tick boundary
53
+ /// @param amount0 The amount of token0 being sent in
54
+ /// @param amount1 The amount of token1 being sent in
55
+ /// @return liquidity The maximum amount of liquidity received
56
+ function getLiquidityForAmounts(
57
+ uint160 sqrtRatioX96,
58
+ uint160 sqrtRatioAX96,
59
+ uint160 sqrtRatioBX96,
60
+ uint256 amount0,
61
+ uint256 amount1
62
+ ) internal pure returns (uint128 liquidity) {
63
+ if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
64
+
65
+ if (sqrtRatioX96 <= sqrtRatioAX96) {
66
+ liquidity = getLiquidityForAmount0(sqrtRatioAX96, sqrtRatioBX96, amount0);
67
+ } else if (sqrtRatioX96 < sqrtRatioBX96) {
68
+ uint128 liquidity0 = getLiquidityForAmount0(sqrtRatioX96, sqrtRatioBX96, amount0);
69
+ uint128 liquidity1 = getLiquidityForAmount1(sqrtRatioAX96, sqrtRatioX96, amount1);
70
+
71
+ liquidity = liquidity0 < liquidity1 ? liquidity0 : liquidity1;
72
+ } else {
73
+ liquidity = getLiquidityForAmount1(sqrtRatioAX96, sqrtRatioBX96, amount1);
74
+ }
75
+ }
76
+
77
+ /// @notice Computes the amount of token0 for a given amount of liquidity and a price range
78
+ /// @param sqrtRatioAX96 A sqrt price representing the first tick boundary
79
+ /// @param sqrtRatioBX96 A sqrt price representing the second tick boundary
80
+ /// @param liquidity The liquidity being valued
81
+ /// @return amount0 The amount of token0
82
+ function getAmount0ForLiquidity(
83
+ uint160 sqrtRatioAX96,
84
+ uint160 sqrtRatioBX96,
85
+ uint128 liquidity
86
+ ) internal pure returns (uint256 amount0) {
87
+ if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
88
+
89
+ return
90
+ FullMath.mulDiv(
91
+ uint256(liquidity) << FixedPoint96.RESOLUTION,
92
+ sqrtRatioBX96 - sqrtRatioAX96,
93
+ sqrtRatioBX96
94
+ ) / sqrtRatioAX96;
95
+ }
96
+
97
+ /// @notice Computes the amount of token1 for a given amount of liquidity and a price range
98
+ /// @param sqrtRatioAX96 A sqrt price representing the first tick boundary
99
+ /// @param sqrtRatioBX96 A sqrt price representing the second tick boundary
100
+ /// @param liquidity The liquidity being valued
101
+ /// @return amount1 The amount of token1
102
+ function getAmount1ForLiquidity(
103
+ uint160 sqrtRatioAX96,
104
+ uint160 sqrtRatioBX96,
105
+ uint128 liquidity
106
+ ) internal pure returns (uint256 amount1) {
107
+ if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
108
+
109
+ return FullMath.mulDiv(liquidity, sqrtRatioBX96 - sqrtRatioAX96, FixedPoint96.Q96);
110
+ }
111
+
112
+ /// @notice Computes the token0 and token1 value for a given amount of liquidity, the current
113
+ /// pool prices and the prices at the tick boundaries
114
+ /// @param sqrtRatioX96 A sqrt price representing the current pool prices
115
+ /// @param sqrtRatioAX96 A sqrt price representing the first tick boundary
116
+ /// @param sqrtRatioBX96 A sqrt price representing the second tick boundary
117
+ /// @param liquidity The liquidity being valued
118
+ /// @return amount0 The amount of token0
119
+ /// @return amount1 The amount of token1
120
+ function getAmountsForLiquidity(
121
+ uint160 sqrtRatioX96,
122
+ uint160 sqrtRatioAX96,
123
+ uint160 sqrtRatioBX96,
124
+ uint128 liquidity
125
+ ) internal pure returns (uint256 amount0, uint256 amount1) {
126
+ if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
127
+
128
+ if (sqrtRatioX96 <= sqrtRatioAX96) {
129
+ amount0 = getAmount0ForLiquidity(sqrtRatioAX96, sqrtRatioBX96, liquidity);
130
+ } else if (sqrtRatioX96 < sqrtRatioBX96) {
131
+ amount0 = getAmount0ForLiquidity(sqrtRatioX96, sqrtRatioBX96, liquidity);
132
+ amount1 = getAmount1ForLiquidity(sqrtRatioAX96, sqrtRatioX96, liquidity);
133
+ } else {
134
+ amount1 = getAmount1ForLiquidity(sqrtRatioAX96, sqrtRatioBX96, liquidity);
135
+ }
136
+ }
137
+ }