Skip to content

Commit

Permalink
fix(MToken): disable startEarning if index has not been initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
PierrickGT committed Nov 20, 2024
1 parent 8e59e33 commit ef7c695
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/MToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { IContinuousIndexing } from "./interfaces/IContinuousIndexing.sol";
import { IMToken } from "./interfaces/IMToken.sol";

import { ContinuousIndexing } from "./abstract/ContinuousIndexing.sol";
import { ContinuousIndexingMath } from "./libs/ContinuousIndexingMath.sol";

/**
* @title MToken
Expand Down Expand Up @@ -94,6 +95,7 @@ contract MToken is IMToken, ContinuousIndexing, ERC20Extended {
/// @inheritdoc IMToken
function startEarning() external {
if (!_isApprovedEarner(msg.sender)) revert NotApprovedEarner();
if (currentIndex() == ContinuousIndexingMath.EXP_SCALED_ONE) revert IndexNotInitialized();

_startEarning(msg.sender);
}
Expand Down
3 changes: 3 additions & 0 deletions src/interfaces/IMToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ interface IMToken is IContinuousIndexing, IERC20Extended {

/* ============ Custom Errors ============ */

/// @notice Emitted when the index from the Hub chain has not yet been propagated to the Spoke chain.
error IndexNotInitialized();

/**
* @notice Emitted when there is insufficient balance to decrement from `account`.
* @param account The account with insufficient balance.
Expand Down
14 changes: 12 additions & 2 deletions test/MToken.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,16 @@ contract MTokenTests is TestUtils {
_mToken.startEarning();
}

function test_startEarning_indexNotInitialized() external {
_mToken.setLatestIndex(ContinuousIndexingMath.EXP_SCALED_ONE);
_registrar.addToList(RegistrarReader.EARNERS_LIST, _alice);

vm.expectRevert(IMToken.IndexNotInitialized.selector);

vm.prank(_alice);
_mToken.startEarning();
}

function test_startEarning() external {
_mToken.setTotalNonEarningSupply(1_000);

Expand Down Expand Up @@ -739,9 +749,9 @@ contract MTokenTests is TestUtils {
}

function test_startEarning_overflow() external {
_mToken.setLatestIndex(ContinuousIndexingMath.EXP_SCALED_ONE);
_mToken.setLatestIndex(ContinuousIndexingMath.EXP_SCALED_ONE + 1);

uint256 aliceBalance_ = uint256(type(uint112).max) + 20;
uint256 aliceBalance_ = uint256(type(uint112).max) + 1e22;

_mToken.setTotalNonEarningSupply(aliceBalance_);
_mToken.setInternalBalanceOf(_alice, aliceBalance_);
Expand Down

0 comments on commit ef7c695

Please sign in to comment.