|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity ^0.8.0; |
| 4 | + |
| 5 | +import "joe-v2/interfaces/ILBPair.sol"; |
| 6 | + |
| 7 | +/// @title Liquidity Book periphery library for Pending Fees |
| 8 | +/// @author Trader Joe |
| 9 | +/// @notice Periphery library to help compute pending fees from ids. |
| 10 | +library PendingFees { |
| 11 | + /// @notice Return the fees amounts that are cached for a given user |
| 12 | + /// @param LBPair the address of the pair |
| 13 | + /// @param user the address of the user |
| 14 | + /// @return amountX the amount of tokenX that are cached |
| 15 | + /// @return amountY the amount of tokenY that are cached |
| 16 | + function getCachedFees(address LBPair, address user) internal view returns (uint256 amountX, uint256 amountY) { |
| 17 | + (amountX, amountY) = ILBPair(LBPair).pendingFees(user, new uint256[](0)); |
| 18 | + } |
| 19 | + |
| 20 | + /// @notice Return the ids and amounts that have fees for a given user in the given list of ids |
| 21 | + /// @dev The returned arrays will be equal or smaller than the given arrays |
| 22 | + /// @param LBPair the address of the pair |
| 23 | + /// @param user the address of the user |
| 24 | + /// @param ids the list of ids where the user want to know if there are pending fees |
| 25 | + /// @return cachedX the amount of tokenX that are cached |
| 26 | + /// @return cachedY the amount of tokenY that are cached |
| 27 | + /// @return idsWithFees the list of ids that have pending fees |
| 28 | + /// @return amountsX the list of amount of tokenX that are pending for each id |
| 29 | + /// @return amountsY the list of amount of tokenY that are pending for each id |
| 30 | + function getIdsWithFees(address LBPair, address user, uint256[] memory ids) |
| 31 | + internal |
| 32 | + view |
| 33 | + returns ( |
| 34 | + uint256 cachedX, |
| 35 | + uint256 cachedY, |
| 36 | + uint256[] memory idsWithFees, |
| 37 | + uint256[] memory amountsX, |
| 38 | + uint256[] memory amountsY |
| 39 | + ) |
| 40 | + { |
| 41 | + idsWithFees = new uint256[](ids.length); |
| 42 | + amountsX = new uint256[](ids.length); |
| 43 | + amountsY = new uint256[](ids.length); |
| 44 | + |
| 45 | + uint256[] memory id = new uint256[](1); |
| 46 | + |
| 47 | + (cachedX, cachedY) = getCachedFees(LBPair, user); |
| 48 | + |
| 49 | + uint256 j; |
| 50 | + for (uint256 i; i < ids.length;) { |
| 51 | + id[0] = ids[i]; |
| 52 | + |
| 53 | + (uint256 amountX, uint256 amountY) = ILBPair(LBPair).pendingFees(user, id); |
| 54 | + |
| 55 | + unchecked { |
| 56 | + if (amountX > cachedX || amountY > cachedY) { |
| 57 | + idsWithFees[j] = ids[i]; |
| 58 | + |
| 59 | + if (amountX > cachedX) amountsX[j] = amountX - cachedX; |
| 60 | + if (amountY > cachedY) amountsY[j] = amountY - cachedY; |
| 61 | + |
| 62 | + ++j; |
| 63 | + } |
| 64 | + |
| 65 | + ++i; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // resize the array, safe because we only decrease the size |
| 70 | + assembly { |
| 71 | + mstore(idsWithFees, j) |
| 72 | + mstore(amountsX, j) |
| 73 | + mstore(amountsY, j) |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments