Skip to content

Commit

Permalink
Merge pull request #112 from Ion-Protocol/jun/OZ-H-01
Browse files Browse the repository at this point in the history
[OZ] H-01
  • Loading branch information
junkim012 authored Jul 29, 2024
2 parents eb01b48 + a6b1601 commit 9a67318
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
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"
);
}
}

0 comments on commit 9a67318

Please sign in to comment.