Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OZ] H-01 #112

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/token/RewardToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ abstract contract RewardToken is

RewardTokenStorage storage $ = _getRewardTokenStorage();

uint256 _supplyFactor = $.supplyFactor;
uint256 _supplyFactor = supplyFactor();
uint256 amountNormalized = amount.rayDivDown(_supplyFactor);

uint256 oldSenderBalance = $._normalizedBalances[from];
Expand Down
69 changes: 69 additions & 0 deletions test/unit/fuzz/RewardToken.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { WadRayMath } from "../../../src/libraries/math/WadRayMath.sol";
import { IERC20Errors } from "../../../src/token/IERC20Errors.sol";

import { RewardTokenSharedSetup } from "../../helpers/RewardTokenSharedSetup.sol";
import { IonPoolSharedSetup } from "../../helpers/IonPoolSharedSetup.sol";

import { ECDSA } from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import { MessageHashUtils } from "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol";
Expand Down Expand Up @@ -361,3 +362,71 @@ contract RewardToken_FuzzUnitTest is RewardTokenSharedSetup {
assertEq(rewardModule.allowance(sendingUser, spender), locals.amountOfRewardTokens);
}
}

contract RewardToken_FuzzUnitTest_WithIonPool is IonPoolSharedSetup {
/**
* The transfer function should take into account the up to date supply
* factor inclusive of the interest rate accrual.
* The decrease in sender's `balanceOf` should be equal to the increase in the recipient`s `balanceOf`.
* Same goes for `normalizedBalanceOf`.
*/
function testFuzz_TransferWithSupplyFactorIncrease(
uint256 supplyAmt,
uint256 transferAmt,
uint256 timeDelta
)
public
{
uint8 ilkIndex = 0;
address recipient = makeAddr("RECIPIENT");

ionPool.updateIlkDebtCeiling(ilkIndex, type(uint256).max);

supplyAmt = bound(supplyAmt, 1e18, 100e18);
timeDelta = bound(timeDelta, 1 days, 10 days);
transferAmt = bound(transferAmt, supplyAmt / 10, supplyAmt / 2);

uint256 collateralAmt = supplyAmt;
uint256 borrowAmt = bound(supplyAmt, supplyAmt / 2, supplyAmt);

deal(address(underlying), address(lender1), supplyAmt);

vm.startPrank(lender1);
ionPool.underlying().approve(address(ionPool), supplyAmt);
ionPool.supply(lender1, supplyAmt, new bytes32[](0));
vm.stopPrank();

deal(address(collaterals[ilkIndex]), address(borrower1), collateralAmt);

vm.startPrank(borrower1);
collaterals[ilkIndex].approve(address(gemJoins[ilkIndex]), collateralAmt);
gemJoins[ilkIndex].join(borrower1, collateralAmt);
ionPool.depositCollateral(ilkIndex, borrower1, borrower1, collateralAmt, new bytes32[](0));
ionPool.borrow(ilkIndex, borrower1, borrower1, borrowAmt, new bytes32[](0));
vm.stopPrank();

uint256 prevSupplyFactor = ionPool.supplyFactor();

vm.warp(block.timestamp + timeDelta);

uint256 newSupplyFactor = ionPool.supplyFactor();

assertGt(newSupplyFactor, prevSupplyFactor, "supply factor must go up");

uint256 prevBalanceOf = ionPool.balanceOf(lender1);
uint256 prevRecipientBalanceOf = ionPool.balanceOf(recipient);

vm.prank(lender1);
ionPool.transfer(recipient, transferAmt);

uint256 newBalanceOf = ionPool.balanceOf(lender1);
uint256 newRecipientBalanceOf = ionPool.balanceOf(recipient);

assertApproxEqAbs(
prevBalanceOf - newBalanceOf,
newRecipientBalanceOf - prevRecipientBalanceOf,
ionPool.supplyFactor() / 1e27,
"the balanceOf change must be equal within a rounding error bound"
);
}
}
Loading