diff --git a/.gitmodules b/.gitmodules index 7818cc4..ae1116d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,4 @@ [submodule "lib/joe-v2"] path = lib/joe-v2 url = https://github.com/traderjoe-xyz/joe-v2 + branch = v2.1.0 diff --git a/lib/forge-std b/lib/forge-std index 2a2ce36..0837133 160000 --- a/lib/forge-std +++ b/lib/forge-std @@ -1 +1 @@ -Subproject commit 2a2ce3692b8c1523b29de3ec9d961ee9fbbc43a6 +Subproject commit 0837133d126989f244cd11b444a3e9775dabc171 diff --git a/lib/joe-v2 b/lib/joe-v2 index d87bc67..8eeb42d 160000 --- a/lib/joe-v2 +++ b/lib/joe-v2 @@ -1 +1 @@ -Subproject commit d87bc670bcd7e96efae14759e1f5f3449183907f +Subproject commit 8eeb42d73b5fe473f5b65d544d27827065a83e32 diff --git a/src/JoeV2PeripheryErrors.sol b/src/JoeV2PeripheryErrors.sol deleted file mode 100644 index 602045e..0000000 --- a/src/JoeV2PeripheryErrors.sol +++ /dev/null @@ -1,9 +0,0 @@ -// SPDX-License-Identifier: MIT - -pragma solidity ^0.8.0; - -import "joe-v2/LBErrors.sol"; - -/** LiquidityAmounts Errors */ - -error LiquidityAmounts__LengthMismatch(); diff --git a/src/PendingFeesContract.sol b/src/PendingFeesContract.sol deleted file mode 100644 index e758f6e..0000000 --- a/src/PendingFeesContract.sol +++ /dev/null @@ -1,45 +0,0 @@ -// SPDX-License-Identifier: MIT - -pragma solidity ^0.8.0; - -import "./periphery/PendingFees.sol"; - -/// @title Liquidity Book periphery contract for Pending Fees -/// @author Trader Joe -/// @notice Periphery contract to help compute pending fees from ids. -contract PendingFeesContract { - using PendingFees for address; - - /// @notice Return the fees amounts that are cached for a given user - /// @param LBPair the address of the pair - /// @param user the address of the user - /// @return amountX the amount of tokenX that are cached - /// @return amountY the amount of tokenY that are cached - function getCachedFees(address LBPair, address user) external view returns (uint256 amountX, uint256 amountY) { - return LBPair.getCachedFees(user); - } - - /// @notice Return the ids and amounts that have fees for a given user in the given list of ids - /// @dev The returned arrays will be equal or smaller than the given arrays - /// @param LBPair the address of the pair - /// @param user the address of the user - /// @param ids the list of ids where the user want to know if there are pending fees - /// @return cachedX the amount of tokenX that are cached - /// @return cachedY the amount of tokenY that are cached - /// @return idsWithFees the list of ids that have pending fees - /// @return amountsX the list of amount of tokenX that are pending for each id - /// @return amountsY the list of amount of tokenY that are pending for each id - function getIdsWithFees(address LBPair, address user, uint256[] memory ids) - external - view - returns ( - uint256 cachedX, - uint256 cachedY, - uint256[] memory idsWithFees, - uint256[] memory amountsX, - uint256[] memory amountsY - ) - { - return LBPair.getIdsWithFees(user, ids); - } -} diff --git a/src/periphery/LiquidityAmounts.sol b/src/periphery/LiquidityAmounts.sol index 3f28bfe..8336159 100644 --- a/src/periphery/LiquidityAmounts.sol +++ b/src/periphery/LiquidityAmounts.sol @@ -2,23 +2,23 @@ pragma solidity ^0.8.0; -import "joe-v2/libraries/Math512Bits.sol"; -import "joe-v2/libraries/BinHelper.sol"; +import "joe-v2/libraries/math/Uint256x256Math.sol"; +import "joe-v2/libraries/PriceHelper.sol"; import "joe-v2/libraries/Constants.sol"; -import "joe-v2/libraries/SafeCast.sol"; +import "joe-v2/libraries/math/SafeCast.sol"; import "joe-v2/interfaces/ILBPair.sol"; import "joe-v2/interfaces/ILBToken.sol"; -import "../JoeV2PeripheryErrors.sol"; - /// @title Liquidity Book periphery library for Liquidity Amount /// @author Trader Joe /// @notice Periphery library to help compute liquidity amounts from amounts and ids. /// @dev The caller must ensure that the parameters are valid following the comments. library LiquidityAmounts { - using Math512Bits for uint256; + using Uint256x256Math for uint256; using SafeCast for uint256; + error LiquidityAmounts__LengthMismatch(); + /// @notice Return the liquidities amounts received for a given amount of tokenX and tokenY /// @dev The caller needs to ensure that the ids are unique, if not, the result will be wrong. /// @param ids the list of ids where the user want to add liquidity @@ -26,16 +26,15 @@ library LiquidityAmounts { /// @param amountX the amount of tokenX /// @param amountY the amount of tokenY /// @return liquidities the amounts of liquidity received - function getLiquiditiesForAmounts( - uint256[] memory ids, - uint16 binStep, - uint112 amountX, - uint112 amountY - ) internal pure returns (uint256[] memory liquidities) { + function getLiquiditiesForAmounts(uint256[] memory ids, uint16 binStep, uint112 amountX, uint112 amountY) + internal + pure + returns (uint256[] memory liquidities) + { liquidities = new uint256[](ids.length); for (uint256 i; i < ids.length; ++i) { - uint256 price = BinHelper.getPriceFromId(ids[i].safe24(), binStep); + uint256 price = PriceHelper.getPriceFromId(ids[i].safe24(), binStep); liquidities[i] = price.mulShiftRoundDown(amountX, Constants.SCALE_OFFSET) + amountY; } @@ -56,9 +55,8 @@ library LiquidityAmounts { uint112[] memory binReservesY ) internal pure returns (uint256 amountX, uint256 amountY) { if ( - liquidities.length != totalSupplies.length && - liquidities.length != binReservesX.length && - liquidities.length != binReservesY.length + liquidities.length != totalSupplies.length && liquidities.length != binReservesX.length + && liquidities.length != binReservesY.length ) revert LiquidityAmounts__LengthMismatch(); for (uint256 i; i < liquidities.length; ++i) { @@ -73,11 +71,11 @@ library LiquidityAmounts { /// @param ids the list of ids where the user have liquidity /// @param LBPair The address of the LBPair /// @return liquidities the list of amount of liquidity of the user - function getLiquiditiesOf( - address user, - uint256[] memory ids, - address LBPair - ) internal view returns (uint256[] memory liquidities) { + function getLiquiditiesOf(address user, uint256[] memory ids, address LBPair) + internal + view + returns (uint256[] memory liquidities) + { liquidities = new uint256[](ids.length); for (uint256 i; i < ids.length; ++i) { @@ -92,11 +90,11 @@ library LiquidityAmounts { /// @param LBPair The address of the LBPair /// @return amountX the amount of tokenX received by the user /// @return amountY the amount of tokenY received by the user - function getAmountsOf( - address user, - uint256[] memory ids, - address LBPair - ) internal view returns (uint256 amountX, uint256 amountY) { + function getAmountsOf(address user, uint256[] memory ids, address LBPair) + internal + view + returns (uint256 amountX, uint256 amountY) + { for (uint256 i; i < ids.length; ++i) { uint24 id = ids[i].safe24(); diff --git a/src/periphery/PendingFees.sol b/src/periphery/PendingFees.sol deleted file mode 100644 index 029f3c4..0000000 --- a/src/periphery/PendingFees.sol +++ /dev/null @@ -1,76 +0,0 @@ -// SPDX-License-Identifier: MIT - -pragma solidity ^0.8.0; - -import "joe-v2/interfaces/ILBPair.sol"; - -/// @title Liquidity Book periphery library for Pending Fees -/// @author Trader Joe -/// @notice Periphery library to help compute pending fees from ids. -library PendingFees { - /// @notice Return the fees amounts that are cached for a given user - /// @param LBPair the address of the pair - /// @param user the address of the user - /// @return amountX the amount of tokenX that are cached - /// @return amountY the amount of tokenY that are cached - function getCachedFees(address LBPair, address user) internal view returns (uint256 amountX, uint256 amountY) { - (amountX, amountY) = ILBPair(LBPair).pendingFees(user, new uint256[](0)); - } - - /// @notice Return the ids and amounts that have fees for a given user in the given list of ids - /// @dev The returned arrays will be equal or smaller than the given arrays - /// @param LBPair the address of the pair - /// @param user the address of the user - /// @param ids the list of ids where the user want to know if there are pending fees - /// @return cachedX the amount of tokenX that are cached - /// @return cachedY the amount of tokenY that are cached - /// @return idsWithFees the list of ids that have pending fees - /// @return amountsX the list of amount of tokenX that are pending for each id - /// @return amountsY the list of amount of tokenY that are pending for each id - function getIdsWithFees(address LBPair, address user, uint256[] memory ids) - internal - view - returns ( - uint256 cachedX, - uint256 cachedY, - uint256[] memory idsWithFees, - uint256[] memory amountsX, - uint256[] memory amountsY - ) - { - idsWithFees = new uint256[](ids.length); - amountsX = new uint256[](ids.length); - amountsY = new uint256[](ids.length); - - uint256[] memory id = new uint256[](1); - - (cachedX, cachedY) = getCachedFees(LBPair, user); - - uint256 j; - for (uint256 i; i < ids.length;) { - id[0] = ids[i]; - - (uint256 amountX, uint256 amountY) = ILBPair(LBPair).pendingFees(user, id); - - unchecked { - if (amountX > cachedX || amountY > cachedY) { - idsWithFees[j] = ids[i]; - - if (amountX > cachedX) amountsX[j] = amountX - cachedX; - if (amountY > cachedY) amountsY[j] = amountY - cachedY; - - ++j; - } - - ++i; - } - } - - // resize the array, safe because we only decrease the size - assembly { - mstore(idsWithFees, j) - mstore(amountsX, j) - mstore(amountsY, j) - } - } -}