-
Notifications
You must be signed in to change notification settings - Fork 3
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 thoughts for possibility of start earning #18
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -37,6 +37,15 @@ contract WrappedM is IWrappedM, Migratable, ERC20Extended { | |||||
|
||||||
mapping(address account => BalanceInfo balance) internal _balances; | ||||||
|
||||||
bool public isEarner; | ||||||
uint128 public lastIndexOfTotalEarningSupply; | ||||||
|
||||||
modifier onlyWhenEarner() { | ||||||
if (isEarner == true) revert NotInEarnerState(); | ||||||
|
||||||
_; | ||||||
} | ||||||
|
||||||
/* ============ Constructor ============ */ | ||||||
|
||||||
constructor(address mToken_) ERC20Extended("WrappedM by M^0", "wM", 6) { | ||||||
|
@@ -48,6 +57,22 @@ contract WrappedM is IWrappedM, Migratable, ERC20Extended { | |||||
|
||||||
/* ============ Interactive Functions ============ */ | ||||||
|
||||||
function wrap(address destination_, uint256 amount_) external onlyWhenEarner { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Users should be able to wrap even if the wrapper is not currently earning.
Suggested change
|
||||||
emit Transfer(address(0), destination_, amount_); | ||||||
|
||||||
_mint(destination_, UIntMath.safe240(amount_)); | ||||||
|
||||||
IMTokenLike(mToken).transferFrom(msg.sender, address(this), amount_); | ||||||
} | ||||||
|
||||||
function unwrap(address destination_, uint256 amount_) external { | ||||||
emit Transfer(msg.sender, address(0), amount_); | ||||||
|
||||||
_burn(msg.sender, UIntMath.safe240(amount_)); | ||||||
|
||||||
IMTokenLike(mToken).transfer(destination_, amount_); | ||||||
} | ||||||
|
||||||
function claimFor(address account_) external returns (uint240 yield_) { | ||||||
return _claim(account_, currentIndex()); | ||||||
} | ||||||
|
@@ -58,15 +83,18 @@ contract WrappedM is IWrappedM, Migratable, ERC20Extended { | |||||
IMTokenLike(mToken).transfer(vault, yield_); | ||||||
} | ||||||
|
||||||
function deposit(address destination_, uint256 amount_) external { | ||||||
emit Transfer(address(0), destination_, amount_); | ||||||
|
||||||
_addAmount(destination_, UIntMath.safe240(amount_)); | ||||||
function startEarningM() external { | ||||||
isEarner = true; | ||||||
IMTokenLike(mToken).startEarning(); | ||||||
} | ||||||
|
||||||
IMTokenLike(mToken).transferFrom(msg.sender, address(this), amount_); | ||||||
function stopEarningM() external { | ||||||
toninorair marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
isEarner = false; | ||||||
lastIndexOfTotalEarningSupply = currentIndex(); | ||||||
IMTokenLike(mToken).stopEarning(); | ||||||
} | ||||||
|
||||||
function startEarningFor(address account_) external { | ||||||
function startEarningFor(address account_) external onlyWhenEarner { | ||||||
if (!_isApprovedEarner(account_)) revert NotApprovedEarner(); | ||||||
|
||||||
(bool isEarning_, , uint240 rawBalance_) = _getBalanceInfo(account_); | ||||||
|
@@ -89,7 +117,7 @@ contract WrappedM is IWrappedM, Migratable, ERC20Extended { | |||||
_addTotalEarningSupply(rawBalance_, currentIndex_); | ||||||
} | ||||||
|
||||||
function stopEarningFor(address account_) external { | ||||||
function stopEarningFor(address account_) external onlyWhenEarner { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think
Suggested change
|
||||||
if (_isApprovedEarner(account_)) revert ApprovedEarner(); | ||||||
|
||||||
(bool isEarning_, , ) = _getBalanceInfo(account_); | ||||||
|
@@ -107,19 +135,12 @@ contract WrappedM is IWrappedM, Migratable, ERC20Extended { | |||||
uint240 amount_ = IndexingMath.getPresentAmountRoundedDown(uint112(rawBalance_), index_); | ||||||
|
||||||
_setBalanceInfo(account_, false, 0, amount_); | ||||||
|
||||||
totalNonEarningSupply += amount_; | ||||||
|
||||||
_subtractTotalEarningSupply(amount_, currentIndex_); | ||||||
} | ||||||
|
||||||
function withdraw(address destination_, uint256 amount_) external { | ||||||
emit Transfer(msg.sender, address(0), amount_); | ||||||
|
||||||
_subtractAmount(msg.sender, UIntMath.safe240(amount_)); | ||||||
|
||||||
IMTokenLike(mToken).transfer(destination_, amount_); | ||||||
} | ||||||
|
||||||
/* ============ View/Pure Functions ============ */ | ||||||
|
||||||
function accruedYieldOf(address account_) external view returns (uint240 yield_) { | ||||||
|
@@ -135,7 +156,7 @@ contract WrappedM is IWrappedM, Migratable, ERC20Extended { | |||||
} | ||||||
|
||||||
function currentIndex() public view returns (uint128 index_) { | ||||||
return IMTokenLike(mToken).currentIndex(); | ||||||
return isEarner ? IMTokenLike(mToken).currentIndex() : lastIndexOfTotalEarningSupply; | ||||||
} | ||||||
|
||||||
function excess() public view returns (uint240 yield_) { | ||||||
|
@@ -159,7 +180,7 @@ contract WrappedM is IWrappedM, Migratable, ERC20Extended { | |||||
|
||||||
/* ============ Internal Interactive Functions ============ */ | ||||||
|
||||||
function _addAmount(address recipient_, uint240 amount_) internal { | ||||||
function _mint(address recipient_, uint240 amount_) internal { | ||||||
(bool isEarning_, , ) = _getBalanceInfo(recipient_); | ||||||
|
||||||
if (!isEarning_) return _addNonEarningAmount(recipient_, amount_); | ||||||
|
@@ -170,12 +191,29 @@ contract WrappedM is IWrappedM, Migratable, ERC20Extended { | |||||
_addEarningAmount(recipient_, amount_, currentIndex_); | ||||||
} | ||||||
|
||||||
function _burn(address account_, uint240 amount_) internal { | ||||||
(bool isEarning_, , ) = _getBalanceInfo(account_); | ||||||
|
||||||
if (!isEarning_) return _subtractNonEarningAmount(account_, amount_); | ||||||
|
||||||
uint128 currentIndex_ = currentIndex(); | ||||||
|
||||||
_claim(account_, currentIndex_); | ||||||
_subtractEarningAmount(account_, amount_, currentIndex_); | ||||||
} | ||||||
|
||||||
function _addNonEarningAmount(address recipient_, uint240 amount_) internal { | ||||||
(, , uint240 rawBalance_) = _getBalanceInfo(recipient_); | ||||||
_setBalanceInfo(recipient_, false, 0, rawBalance_ + amount_); | ||||||
totalNonEarningSupply += amount_; | ||||||
} | ||||||
|
||||||
function _subtractNonEarningAmount(address account_, uint240 amount_) internal { | ||||||
(, , uint240 rawBalance_) = _getBalanceInfo(account_); | ||||||
_setBalanceInfo(account_, false, 0, rawBalance_ - amount_); | ||||||
totalNonEarningSupply -= amount_; | ||||||
} | ||||||
|
||||||
function _addEarningAmount(address recipient_, uint240 amount_, uint128 currentIndex_) internal { | ||||||
(, , uint240 rawBalance_) = _getBalanceInfo(recipient_); | ||||||
|
||||||
|
@@ -189,6 +227,19 @@ contract WrappedM is IWrappedM, Migratable, ERC20Extended { | |||||
_addTotalEarningSupply(amount_, currentIndex_); | ||||||
} | ||||||
|
||||||
function _subtractEarningAmount(address account_, uint240 amount_, uint128 currentIndex_) internal { | ||||||
(, , uint240 rawBalance_) = _getBalanceInfo(account_); | ||||||
|
||||||
_setBalanceInfo( | ||||||
account_, | ||||||
true, | ||||||
currentIndex_, | ||||||
rawBalance_ - IndexingMath.getPrincipalAmountRoundedUp(amount_, currentIndex_) | ||||||
); | ||||||
|
||||||
_subtractTotalEarningSupply(amount_, currentIndex_); | ||||||
} | ||||||
|
||||||
function _claim(address account_, uint128 currentIndex_) internal returns (uint240 yield_) { | ||||||
(bool isEarner_, uint128 index_, uint240 rawBalance_) = _getBalanceInfo(account_); | ||||||
|
||||||
|
@@ -219,36 +270,6 @@ contract WrappedM is IWrappedM, Migratable, ERC20Extended { | |||||
: BalanceInfo.wrap(uint256(amount_)); | ||||||
} | ||||||
|
||||||
function _subtractAmount(address account_, uint240 amount_) internal { | ||||||
(bool isEarning_, , ) = _getBalanceInfo(account_); | ||||||
|
||||||
if (!isEarning_) return _subtractNonEarningAmount(account_, amount_); | ||||||
|
||||||
uint128 currentIndex_ = currentIndex(); | ||||||
|
||||||
_claim(account_, currentIndex_); | ||||||
_subtractEarningAmount(account_, amount_, currentIndex_); | ||||||
} | ||||||
|
||||||
function _subtractNonEarningAmount(address account_, uint240 amount_) internal { | ||||||
(, , uint240 rawBalance_) = _getBalanceInfo(account_); | ||||||
_setBalanceInfo(account_, false, 0, rawBalance_ - amount_); | ||||||
totalNonEarningSupply -= amount_; | ||||||
} | ||||||
|
||||||
function _subtractEarningAmount(address account_, uint240 amount_, uint128 currentIndex_) internal { | ||||||
(, , uint240 rawBalance_) = _getBalanceInfo(account_); | ||||||
|
||||||
_setBalanceInfo( | ||||||
account_, | ||||||
true, | ||||||
currentIndex_, | ||||||
rawBalance_ - IndexingMath.getPrincipalAmountRoundedUp(amount_, currentIndex_) | ||||||
); | ||||||
|
||||||
_subtractTotalEarningSupply(amount_, currentIndex_); | ||||||
} | ||||||
|
||||||
function _transfer(address sender_, address recipient_, uint240 amount_, uint128 currentIndex_) internal { | ||||||
_claim(sender_, currentIndex_); | ||||||
_claim(recipient_, currentIndex_); | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.