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

Add totalAssets(uint256 timestamp) to MockERC4626 #1049

Merged
merged 1 commit into from
Jun 13, 2024
Merged
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
20 changes: 13 additions & 7 deletions contracts/test/MockERC4626.sol
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,13 @@ contract MockERC4626 is ERC4626, MultiRolesAuthority {
}

function totalAssets() public view override returns (uint256) {
return asset.balanceOf(address(this)) + _getAccruedInterest();
return
asset.balanceOf(address(this)) +
_getAccruedInterest(block.timestamp);
}

function totalAssets(uint256 timestamp) public view returns (uint256) {
return asset.balanceOf(address(this)) + _getAccruedInterest(timestamp);
}

/// Mock ///
Expand Down Expand Up @@ -178,14 +184,16 @@ contract MockERC4626 is ERC4626, MultiRolesAuthority {
}

function _accrue() internal {
uint256 interest = _getAccruedInterest();
uint256 interest = _getAccruedInterest(block.timestamp);
if (interest > 0) {
ERC20Mintable(address(asset)).mint(interest);
}
_lastUpdated = block.timestamp;
}

function _getAccruedInterest() internal view returns (uint256) {
function _getAccruedInterest(
uint256 timestamp
) internal view returns (uint256) {
// If the rate is zero, no interest has accrued.
if (_rate == 0) {
return 0;
Expand All @@ -194,14 +202,12 @@ contract MockERC4626 is ERC4626, MultiRolesAuthority {
// If the block timestamp is less than last updated, the accrual
// calculation will underflow. This can occur when using anvil's state
// snapshots.
if (block.timestamp < _lastUpdated) {
if (timestamp < _lastUpdated) {
return 0;
}

// base_balance = base_balance * (1 + r * t)
uint256 timeElapsed = (block.timestamp - _lastUpdated).divDown(
365 days
);
uint256 timeElapsed = (timestamp - _lastUpdated).divDown(365 days);
uint256 accrued = asset.balanceOf(address(this)).mulDown(
_rate.mulDown(timeElapsed)
);
Expand Down
Loading