Skip to content

Commit

Permalink
refactor: Improve calculation of interest in updateInterest function
Browse files Browse the repository at this point in the history
  • Loading branch information
jpgonzalezra committed May 25, 2024
1 parent c391a26 commit 7f303a6
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/InterestBearingToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity 0.8.26;

import { ERC20 } from "solmate/tokens/ERC20.sol";
import { Owned } from "solmate/auth/Owned.sol";

// import { console } from "forge-std/console.sol";

contract InterestBearingToken is ERC20, Owned {
Expand Down Expand Up @@ -42,16 +43,16 @@ contract InterestBearingToken is ERC20, Owned {
function mint(address to_, uint256 amount_) external onlyOwner {
_revertIfInvalidRecipient(to_);
_revertIfInsufficientAmount(amount_);
_mint(to_, amount_);
_updateRewards(to_);
_mint(to_, amount_);
}

function burn(uint256 amount_) external {
_revertIfInsufficientAmount(amount_);
if (this.balanceOf(msg.sender) < amount_) revert InsufficientAmount(amount_);
address caller = msg.sender;
_burn(caller, amount_);
_updateRewards(caller);
_burn(caller, amount_);
}

function updateInterest(address account_) external {
Expand All @@ -71,11 +72,13 @@ contract InterestBearingToken is ERC20, Owned {
emit StartedEarning(account_);
return;
}
uint256 balance = this.balanceOf(account_);
// we are calculating always using the raw balance (simple interest)
uint256 rawBalance = this.balanceOf(account_);

// Safe to use unchecked here, since `block.timestamp` is always greater than `lastUpdateTimestamp[account_]`.
unchecked {
uint256 timeElapsed = timestamp - lastUpdateTimestamp[account_];
uint256 interest = (balance * timeElapsed * yearlyRate) / (10_000 * uint256(SECONDS_PER_YEAR));
uint256 interest = (rawBalance * timeElapsed * yearlyRate) / (10_000 * uint256(SECONDS_PER_YEAR));
accruedInterest[account_] += interest;
}
lastUpdateTimestamp[account_] = block.timestamp;
Expand Down

0 comments on commit 7f303a6

Please sign in to comment.