@@ -162,4 +162,63 @@ contract FeeForkTest is Test {
162162 tokensWithCollectedFees = feeAdminFacet.tokensWithCollectedFees ();
163163 assertEq (tokensWithCollectedFees.length , 0 );
164164 }
165+
166+ function testSingleDepositAndWithdrawFromAaveWithNoProfit () public {
167+ uint256 depositAmount = 50 * 10 ** erc20Decimals;
168+
169+ // mint the USDC to the user
170+ vm.startPrank (USDC_MINTER);
171+ underlyingERC20.mint (APP_USER_ALICE, depositAmount);
172+ vm.stopPrank ();
173+ console.log ("minted USDC to user " );
174+
175+ vm.startPrank (APP_USER_ALICE);
176+ underlyingERC20.approve (address (aavePerfFeeFacet), depositAmount);
177+ console.log ("approved USDC to aave " );
178+ aavePerfFeeFacet.depositToAave (REAL_USDC, depositAmount);
179+ vm.stopPrank ();
180+ console.log ("deposited to aave " );
181+
182+ LibFeeStorage.Deposit memory d = feeViewsFacet.deposits (APP_USER_ALICE, REAL_USDC);
183+
184+ assertEq (d.assetAmount, depositAmount);
185+ console.log ("d.vaultShares " , d.vaultShares);
186+ console.log ("d.vaultProvider " , d.vaultProvider);
187+
188+ // confirm that the fee contract has the aTokens
189+ ERC20 aToken = ERC20 (aavePool.getReserveAToken (REAL_USDC));
190+ uint256 feeContractAaveTokens = aToken.balanceOf (address (aavePerfFeeFacet));
191+ console.log ("feeContractAaveTokens " , feeContractAaveTokens);
192+ // due to aave fees / rounding math, we get back 1 less aToken than we deposited
193+ assertEq (feeContractAaveTokens / 10 ** aToken.decimals (), (depositAmount / 10 ** erc20Decimals) - 1 );
194+
195+ // now, do the withdrawal
196+ vm.startPrank (APP_USER_ALICE);
197+ aavePerfFeeFacet.withdrawFromAave (REAL_USDC);
198+ vm.stopPrank ();
199+
200+ // confirm the deposit is zeroed out
201+ d = feeViewsFacet.deposits (APP_USER_ALICE, REAL_USDC);
202+
203+ assertEq (d.assetAmount, 0 );
204+ assertEq (d.vaultShares, 0 );
205+ assertEq (d.vaultProvider, 0 );
206+
207+ // confirm the profit went to the fee contract, and some went to the user
208+ uint256 userBalance = underlyingERC20.balanceOf (APP_USER_ALICE);
209+ uint256 feeContractBalance = underlyingERC20.balanceOf (address (aavePerfFeeFacet));
210+
211+ console.log ("depositAmount " , depositAmount);
212+ console.log ("userBalance " , userBalance);
213+
214+ // The user's balance is exactly depositAmount - 1 due to aave aToken math and fee rounding:
215+ // When withdrawing, aave converts the aTokens back to assets, and due to integer division/rounding,
216+ // the user receives one less unit than deposited. This is expected for this test scenario.
217+ assertEq (userBalance, depositAmount - 1 );
218+ assertEq (feeContractBalance, 0 );
219+
220+ // test that the MockERC20 is not in the set of tokens that have collected fees
221+ address [] memory tokensWithCollectedFees = feeAdminFacet.tokensWithCollectedFees ();
222+ assertEq (tokensWithCollectedFees.length , 0 );
223+ }
165224}
0 commit comments