Skip to content

Commit 5ce18dd

Browse files
committed
fix: add tests of userVaultOrPoolAssetAddresses view
1 parent 3fa94ff commit 5ce18dd

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

packages/libs/contracts-sdk/contracts/fees/facets/FeeViewsFacet.sol

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { EnumerableSet } from "@openzeppelin/contracts/utils/structs/EnumerableS
1111
* @notice A contract that contains the views for the Fee Diamond
1212
*/
1313
contract FeeViewsFacet {
14+
using EnumerableSet for EnumerableSet.AddressSet;
1415

1516
/* ========== VIEWS ========== */
1617

@@ -24,4 +25,38 @@ contract FeeViewsFacet {
2425
return LibFeeStorage.getStorage().deposits[user][vaultAddress];
2526
}
2627

28+
/**
29+
* @notice Gets the userVaultOrPoolAssetAddresses for a user
30+
* if this list gets too long and the view call is timing out,
31+
* you can use the "one at a time" functions below
32+
* @param user the user to get the userVaultOrPoolAssetAddresses for
33+
* @return the userVaultOrPoolAssetAddresses for the user
34+
*/
35+
function userVaultOrPoolAssetAddresses(address user) external view returns (address[] memory) {
36+
return LibFeeStorage.getStorage().userVaultOrPoolAssetAddresses[user].values();
37+
}
38+
39+
/**
40+
* @notice Gets the length of the userVaultOrPoolAssetAddresses for a user
41+
* @param user the user to get the length of the userVaultOrPoolAssetAddresses for
42+
* @return the length of the userVaultOrPoolAssetAddresses for the user
43+
*/
44+
function userVaultOrPoolAssetAddressesLength(address user) external view returns (uint256) {
45+
return LibFeeStorage.getStorage().userVaultOrPoolAssetAddresses[user].length();
46+
}
47+
48+
/**
49+
* @notice Gets the userVaultOrPoolAssetAddresses at the given index for a user
50+
* @param user the user to get the userVaultOrPoolAssetAddresses at the given index for
51+
* @param index the index to get the userVaultOrPoolAssetAddresses at
52+
* @return the userVaultOrPoolAssetAddresses at the given index for the user
53+
*/
54+
function userVaultOrPoolAssetAddressesAtIndex(address user, uint256 index) external view returns (address) {
55+
return LibFeeStorage.getStorage().userVaultOrPoolAssetAddresses[user].at(index);
56+
}
57+
58+
59+
60+
61+
2762
}

packages/libs/contracts-sdk/test/fees/AaveFeeForkTest.t.sol

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ contract FeeForkTest is Test {
103103
console.log("d.vaultShares", d.vaultShares);
104104
console.log("d.vaultProvider", d.vaultProvider);
105105

106+
// confirm that the asset is in the userVaultOrPoolAssetAddresses set
107+
address[] memory userVaultOrPoolAssetAddresses = feeViewsFacet.userVaultOrPoolAssetAddresses(APP_USER_ALICE);
108+
assertEq(userVaultOrPoolAssetAddresses.length, 1);
109+
assertEq(userVaultOrPoolAssetAddresses[0], REAL_USDC);
110+
106111
// confirm that the fee contract has the aTokens
107112
ERC20 aToken = ERC20(aavePool.getReserveAToken(REAL_USDC));
108113
uint256 feeContractAaveTokens = aToken.balanceOf(address(aavePerfFeeFacet));
@@ -131,6 +136,10 @@ contract FeeForkTest is Test {
131136
assertEq(d.vaultShares, 0);
132137
assertEq(d.vaultProvider, 0);
133138

139+
// confirm that the asset is no longer in the userVaultOrPoolAssetAddresses set
140+
userVaultOrPoolAssetAddresses = feeViewsFacet.userVaultOrPoolAssetAddresses(APP_USER_ALICE);
141+
assertEq(userVaultOrPoolAssetAddresses.length, 0);
142+
134143
// confirm the profit went to the fee contract, and some went to the user
135144
uint256 userBalance = underlyingERC20.balanceOf(APP_USER_ALICE);
136145
uint256 feeContractBalance = underlyingERC20.balanceOf(address(aavePerfFeeFacet));
@@ -187,6 +196,11 @@ contract FeeForkTest is Test {
187196
console.log("d.vaultShares", d.vaultShares);
188197
console.log("d.vaultProvider", d.vaultProvider);
189198

199+
// confirm that the asset is in the userVaultOrPoolAssetAddresses set
200+
address[] memory userVaultOrPoolAssetAddresses = feeViewsFacet.userVaultOrPoolAssetAddresses(APP_USER_ALICE);
201+
assertEq(userVaultOrPoolAssetAddresses.length, 1);
202+
assertEq(userVaultOrPoolAssetAddresses[0], REAL_USDC);
203+
190204
// confirm that the fee contract has the aTokens
191205
ERC20 aToken = ERC20(aavePool.getReserveAToken(REAL_USDC));
192206
uint256 feeContractAaveTokens = aToken.balanceOf(address(aavePerfFeeFacet));
@@ -206,6 +220,10 @@ contract FeeForkTest is Test {
206220
assertEq(d.vaultShares, 0);
207221
assertEq(d.vaultProvider, 0);
208222

223+
// confirm that the asset is no longer in the userVaultOrPoolAssetAddresses set
224+
userVaultOrPoolAssetAddresses = feeViewsFacet.userVaultOrPoolAssetAddresses(APP_USER_ALICE);
225+
assertEq(userVaultOrPoolAssetAddresses.length, 0);
226+
209227
// confirm the profit went to the fee contract, and some went to the user
210228
uint256 userBalance = underlyingERC20.balanceOf(APP_USER_ALICE);
211229
uint256 feeContractBalance = underlyingERC20.balanceOf(address(aavePerfFeeFacet));
@@ -256,6 +274,11 @@ contract FeeForkTest is Test {
256274
console.log("d.vaultShares", d.vaultShares);
257275
console.log("d.vaultProvider", d.vaultProvider);
258276

277+
// confirm that the asset is in the userVaultOrPoolAssetAddresses set
278+
address[] memory userVaultOrPoolAssetAddresses = feeViewsFacet.userVaultOrPoolAssetAddresses(APP_USER_ALICE);
279+
assertEq(userVaultOrPoolAssetAddresses.length, 1);
280+
assertEq(userVaultOrPoolAssetAddresses[0], REAL_USDC);
281+
259282
// confirm that the fee contract has the aTokens
260283
ERC20 aToken = ERC20(aavePool.getReserveAToken(REAL_USDC));
261284
uint256 feeContractAaveTokens = aToken.balanceOf(address(aavePerfFeeFacet));
@@ -276,6 +299,11 @@ contract FeeForkTest is Test {
276299
vm.stopPrank();
277300
console.log("deposited to aave");
278301

302+
// confirm that the asset is still in the userVaultOrPoolAssetAddresses set
303+
userVaultOrPoolAssetAddresses = feeViewsFacet.userVaultOrPoolAssetAddresses(APP_USER_ALICE);
304+
assertEq(userVaultOrPoolAssetAddresses.length, 1);
305+
assertEq(userVaultOrPoolAssetAddresses[0], REAL_USDC);
306+
279307
depositAmount = depositAmount * 2;
280308

281309
d = feeViewsFacet.deposits(APP_USER_ALICE, REAL_USDC);
@@ -311,6 +339,10 @@ contract FeeForkTest is Test {
311339
assertEq(d.vaultShares, 0);
312340
assertEq(d.vaultProvider, 0);
313341

342+
// confirm that the asset is no longer in the userVaultOrPoolAssetAddresses set
343+
userVaultOrPoolAssetAddresses = feeViewsFacet.userVaultOrPoolAssetAddresses(APP_USER_ALICE);
344+
assertEq(userVaultOrPoolAssetAddresses.length, 0);
345+
314346
// confirm the profit went to the fee contract, and some went to the user
315347
uint256 userBalance = underlyingERC20.balanceOf(APP_USER_ALICE);
316348
uint256 feeContractBalance = underlyingERC20.balanceOf(address(aavePerfFeeFacet));

packages/libs/contracts-sdk/test/fees/MorphoFee.t.sol

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ contract FeeTest is Test {
7979
console.log("feeContractVaultShares", feeContractVaultShares);
8080
assertEq(feeContractVaultShares, d.vaultShares);
8181

82+
// confirm that the asset is in the userVaultOrPoolAssetAddresses set
83+
address[] memory userVaultOrPoolAssetAddresses = feeViewsFacet.userVaultOrPoolAssetAddresses(APP_USER_ALICE);
84+
assertEq(userVaultOrPoolAssetAddresses.length, 1);
85+
assertEq(userVaultOrPoolAssetAddresses[0], address(mockERC4626));
86+
87+
8288
// send more assets to the vault to create profit
8389
mockERC20.mint(address(mockERC4626), 100);
8490

@@ -98,6 +104,10 @@ contract FeeTest is Test {
98104
assertEq(d.vaultShares, 0);
99105
assertEq(d.vaultProvider, 0);
100106

107+
// confirm that the asset is no longer in the userVaultOrPoolAssetAddresses set
108+
userVaultOrPoolAssetAddresses = feeViewsFacet.userVaultOrPoolAssetAddresses(APP_USER_ALICE);
109+
assertEq(userVaultOrPoolAssetAddresses.length, 0);
110+
101111
// confirm the profit went to the fee contract, and some went to the user
102112
uint256 userBalance = mockERC20.balanceOf(APP_USER_ALICE);
103113
uint256 feeContractBalance = mockERC20.balanceOf(address(morphoPerfFeeFacet));
@@ -161,13 +171,23 @@ contract FeeTest is Test {
161171
console.log("feeContractVaultShares", feeContractVaultShares);
162172
assertEq(feeContractVaultShares, d.vaultShares);
163173

174+
// confirm that the asset is in the userVaultOrPoolAssetAddresses set
175+
address[] memory userVaultOrPoolAssetAddresses = feeViewsFacet.userVaultOrPoolAssetAddresses(APP_USER_ALICE);
176+
assertEq(userVaultOrPoolAssetAddresses.length, 1);
177+
assertEq(userVaultOrPoolAssetAddresses[0], address(mockERC4626));
178+
164179
// deposit again
165180
mockERC20.mint(APP_USER_ALICE, depositAmount);
166181
vm.startPrank(APP_USER_ALICE);
167182
mockERC20.approve(address(morphoPerfFeeFacet), depositAmount);
168183
morphoPerfFeeFacet.depositToMorpho(address(mockERC4626), depositAmount);
169184
vm.stopPrank();
170185

186+
// confirm that the asset is still in the userVaultOrPoolAssetAddresses set
187+
userVaultOrPoolAssetAddresses = feeViewsFacet.userVaultOrPoolAssetAddresses(APP_USER_ALICE);
188+
assertEq(userVaultOrPoolAssetAddresses.length, 1);
189+
assertEq(userVaultOrPoolAssetAddresses[0], address(mockERC4626));
190+
171191
// deposited twice, so total deposit amount is times 2
172192
depositAmount = depositAmount * 2;
173193

@@ -200,6 +220,10 @@ contract FeeTest is Test {
200220
assertEq(d.vaultShares, 0);
201221
assertEq(d.vaultProvider, 0);
202222

223+
// confirm that the asset is no longer in the userVaultOrPoolAssetAddresses set
224+
userVaultOrPoolAssetAddresses = feeViewsFacet.userVaultOrPoolAssetAddresses(APP_USER_ALICE);
225+
assertEq(userVaultOrPoolAssetAddresses.length, 0);
226+
203227
// confirm the profit went to the fee contract, and some went to the user
204228
uint256 userBalance = mockERC20.balanceOf(APP_USER_ALICE);
205229
uint256 feeContractBalance = mockERC20.balanceOf(address(morphoPerfFeeFacet));

packages/libs/contracts-sdk/test/fees/MorphoFeeForkTest.t.sol

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ contract FeeForkTest is Test {
104104
console.log("feeContractVaultShares", feeContractVaultShares);
105105
assertEq(feeContractVaultShares, d.vaultShares);
106106

107+
// confirm that the asset is in the userVaultOrPoolAssetAddresses set
108+
address[] memory userVaultOrPoolAssetAddresses = feeViewsFacet.userVaultOrPoolAssetAddresses(APP_USER_ALICE);
109+
assertEq(userVaultOrPoolAssetAddresses.length, 1);
110+
assertEq(userVaultOrPoolAssetAddresses[0], address(morphoVault));
111+
107112
// find the underlying morpho market and advance timestamp to 1 week from now to accrue interest, to simulate profit
108113
uint256 withdrawalQueueLength = morphoVault.withdrawQueueLength();
109114
console.log("withdrawalQueueLength", withdrawalQueueLength);
@@ -134,6 +139,10 @@ contract FeeForkTest is Test {
134139
assertEq(d.vaultShares, 0);
135140
assertEq(d.vaultProvider, 0);
136141

142+
// confirm that the asset is no longer in the userVaultOrPoolAssetAddresses set
143+
userVaultOrPoolAssetAddresses = feeViewsFacet.userVaultOrPoolAssetAddresses(APP_USER_ALICE);
144+
assertEq(userVaultOrPoolAssetAddresses.length, 0);
145+
137146
// confirm the profit went to the fee contract, and some went to the user
138147
uint256 userBalance = underlyingERC20.balanceOf(APP_USER_ALICE);
139148
uint256 feeContractBalance = underlyingERC20.balanceOf(address(morphoPerfFeeFacet));
@@ -192,6 +201,11 @@ contract FeeForkTest is Test {
192201
console.log("feeContractVaultShares", feeContractVaultShares);
193202
assertEq(feeContractVaultShares, d.vaultShares);
194203

204+
// confirm that the asset is in the userVaultOrPoolAssetAddresses set
205+
address[] memory userVaultOrPoolAssetAddresses = feeViewsFacet.userVaultOrPoolAssetAddresses(APP_USER_ALICE);
206+
assertEq(userVaultOrPoolAssetAddresses.length, 1);
207+
assertEq(userVaultOrPoolAssetAddresses[0], address(morphoVault));
208+
195209
// check that asset balance will be slightly lower if we withdraw now, due to fees / rounding
196210
uint256 expectedTotalWithdrawal = morphoVault.convertToAssets(d.vaultShares);
197211
console.log("expectedTotalWithdrawal", expectedTotalWithdrawal);
@@ -208,6 +222,10 @@ contract FeeForkTest is Test {
208222
assertEq(d.vaultShares, 0);
209223
assertEq(d.vaultProvider, 0);
210224

225+
// confirm that the asset is no longer in the userVaultOrPoolAssetAddresses set
226+
userVaultOrPoolAssetAddresses = feeViewsFacet.userVaultOrPoolAssetAddresses(APP_USER_ALICE);
227+
assertEq(userVaultOrPoolAssetAddresses.length, 0);
228+
211229
// confirm there was no profit
212230
uint256 userBalance = underlyingERC20.balanceOf(APP_USER_ALICE);
213231
uint256 feeContractBalance = underlyingERC20.balanceOf(address(morphoPerfFeeFacet));
@@ -259,6 +277,11 @@ contract FeeForkTest is Test {
259277
console.log("feeContractVaultShares", feeContractVaultShares);
260278
assertEq(feeContractVaultShares, d.vaultShares);
261279

280+
// confirm that the asset is in the userVaultOrPoolAssetAddresses set
281+
address[] memory userVaultOrPoolAssetAddresses = feeViewsFacet.userVaultOrPoolAssetAddresses(APP_USER_ALICE);
282+
assertEq(userVaultOrPoolAssetAddresses.length, 1);
283+
assertEq(userVaultOrPoolAssetAddresses[0], address(morphoVault));
284+
262285
// deposit again
263286
vm.startPrank(USDC_MINTER);
264287
underlyingERC20.mint(APP_USER_ALICE, depositAmount);
@@ -268,6 +291,11 @@ contract FeeForkTest is Test {
268291
morphoPerfFeeFacet.depositToMorpho(address(morphoVault), depositAmount);
269292
vm.stopPrank();
270293

294+
// confirm that the asset is still in the userVaultOrPoolAssetAddresses set
295+
userVaultOrPoolAssetAddresses = feeViewsFacet.userVaultOrPoolAssetAddresses(APP_USER_ALICE);
296+
assertEq(userVaultOrPoolAssetAddresses.length, 1);
297+
assertEq(userVaultOrPoolAssetAddresses[0], address(morphoVault));
298+
271299
// deposited twice, so total deposit amount is times 2
272300
depositAmount = depositAmount * 2;
273301

@@ -311,6 +339,10 @@ contract FeeForkTest is Test {
311339
assertEq(d.vaultShares, 0);
312340
assertEq(d.vaultProvider, 0);
313341

342+
// confirm that the asset is no longer in the userVaultOrPoolAssetAddresses set
343+
userVaultOrPoolAssetAddresses = feeViewsFacet.userVaultOrPoolAssetAddresses(APP_USER_ALICE);
344+
assertEq(userVaultOrPoolAssetAddresses.length, 0);
345+
314346
// confirm the profit went to the fee contract, and some went to the user
315347
uint256 userBalance = underlyingERC20.balanceOf(APP_USER_ALICE);
316348
uint256 feeContractBalance = underlyingERC20.balanceOf(address(morphoPerfFeeFacet));

0 commit comments

Comments
 (0)