Skip to content

Commit 0ef3e19

Browse files
authored
Pending Fees (#8)
1 parent e79c7c3 commit 0ef3e19

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

src/PendingFeesContract.sol

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.8.0;
4+
5+
import "./periphery/PendingFees.sol";
6+
7+
/// @title Liquidity Book periphery contract for Pending Fees
8+
/// @author Trader Joe
9+
/// @notice Periphery contract to help compute pending fees from ids.
10+
contract PendingFeesContract {
11+
using PendingFees for address;
12+
13+
/// @notice Return the fees amounts that are cached for a given user
14+
/// @param LBPair the address of the pair
15+
/// @param user the address of the user
16+
/// @return amountX the amount of tokenX that are cached
17+
/// @return amountY the amount of tokenY that are cached
18+
function getCachedFees(address LBPair, address user) external view returns (uint256 amountX, uint256 amountY) {
19+
return LBPair.getCachedFees(user);
20+
}
21+
22+
/// @notice Return the ids and amounts that have fees for a given user in the given list of ids
23+
/// @dev The returned arrays will be equal or smaller than the given arrays
24+
/// @param LBPair the address of the pair
25+
/// @param user the address of the user
26+
/// @param ids the list of ids where the user want to know if there are pending fees
27+
/// @return cachedX the amount of tokenX that are cached
28+
/// @return cachedY the amount of tokenY that are cached
29+
/// @return idsWithFees the list of ids that have pending fees
30+
/// @return amountsX the list of amount of tokenX that are pending for each id
31+
/// @return amountsY the list of amount of tokenY that are pending for each id
32+
function getIdsWithFees(address LBPair, address user, uint256[] memory ids)
33+
external
34+
view
35+
returns (
36+
uint256 cachedX,
37+
uint256 cachedY,
38+
uint256[] memory idsWithFees,
39+
uint256[] memory amountsX,
40+
uint256[] memory amountsY
41+
)
42+
{
43+
return LBPair.getIdsWithFees(user, ids);
44+
}
45+
}

src/periphery/PendingFees.sol

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)