Skip to content

Commit

Permalink
Logic for the bare min start/stop earning
Browse files Browse the repository at this point in the history
  • Loading branch information
toninorair committed Jul 2, 2024
1 parent a5b81bb commit cf69acc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 22 additions & 2 deletions src/WrappedMToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,16 @@ contract WrappedMToken is IWrappedMToken, Migratable, ERC20Extended {

uint240 public totalNonEarningSupply;

uint128 public mIndexWhenEarningStopped;

mapping(address account => BalanceInfo balance) internal _balances;

modifier onlyWhenEarning() {
if (IMTokenLike(mToken).isEarning(address(this))) revert NotInEarningState();

_;
}

/* ============ Constructor ============ */

constructor(address mToken_) ERC20Extended("WrappedM by M^0", "wM", 6) {
Expand All @@ -48,7 +56,7 @@ contract WrappedMToken is IWrappedMToken, Migratable, ERC20Extended {

/* ============ Interactive Functions ============ */

function wrap(address recipient_, uint256 amount_) external {
function wrap(address recipient_, uint256 amount_) external onlyWhenEarning {
_mint(recipient_, UIntMath.safe240(amount_));

IMTokenLike(mToken).transferFrom(msg.sender, address(this), amount_);
Expand Down Expand Up @@ -110,6 +118,18 @@ contract WrappedMToken is IWrappedMToken, Migratable, ERC20Extended {
}
}

function startEarningM() external {
if (mIndexWhenEarningStopped != 0) revert OnlyEarningOnce();

IMTokenLike(mToken).startEarning();
}

function stopEarningM() external {
mIndexWhenEarningStopped = currentIndex();

IMTokenLike(mToken).stopEarning();
}

/* ============ View/Pure Functions ============ */

function accruedYieldOf(address account_) external view returns (uint240 yield_) {
Expand All @@ -123,7 +143,7 @@ contract WrappedMToken is IWrappedMToken, Migratable, ERC20Extended {
}

function currentIndex() public view returns (uint128 index_) {
return IMTokenLike(mToken).currentIndex();
return mIndexWhenEarningStopped == 0 ? IMTokenLike(mToken).currentIndex() : mIndexWhenEarningStopped;
}

function isEarning(address account_) external view returns (bool isEarning_) {
Expand Down
6 changes: 6 additions & 0 deletions src/interfaces/IMTokenLike.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ interface IMTokenLike {

function transferFrom(address sender, address recipient, uint256 amount) external returns (bool success);

function startEarning() external;

function stopEarning() external;

/* ============ View/Pure Functions ============ */

function isEarning(address account) external view returns (bool earning);

function balanceOf(address account) external view returns (uint256 balance);

function currentIndex() external view returns (uint128 currentIndex);
Expand Down
10 changes: 10 additions & 0 deletions src/interfaces/IWrappedMToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ interface IWrappedMToken is IMigratable, IERC20Extended {
/// @notice Emitted when calling `startEarning` for an account not approved as earner by TTG.
error NotApprovedEarner();

/// @notice Emitted when calling `startEarningM` after wrapper was already in earning state once.
error OnlyEarningOnce();

/// @notice Emitted when method is called when Wrapped M is not in earning state.
error NotInEarningState();

/// @notice Emitted in constructor if M Token is 0x0.
error ZeroMToken();

Expand All @@ -54,6 +60,10 @@ interface IWrappedMToken is IMigratable, IERC20Extended {

function claimExcess() external returns (uint240 yield);

function startEarningM() external;

function stopEarningM() external;

function startEarningFor(address account) external;

function stopEarningFor(address account) external;
Expand Down

0 comments on commit cf69acc

Please sign in to comment.