Skip to content

Commit

Permalink
Add renames
Browse files Browse the repository at this point in the history
  • Loading branch information
toninorair committed Jun 25, 2024
1 parent 0d82736 commit e091fd4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 59 deletions.
95 changes: 48 additions & 47 deletions src/WrappedM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ contract WrappedM is IWrappedM, Migratable, ERC20Extended {

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

function wrap(address destination_, uint256 amount_) external {
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());
}
Expand All @@ -58,14 +74,6 @@ 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_));

IMTokenLike(mToken).transferFrom(msg.sender, address(this), amount_);
}

function startEarningFor(address account_) external {
if (!_isApprovedEarner(account_)) revert NotApprovedEarner();

Expand Down Expand Up @@ -107,19 +115,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_) {
Expand Down Expand Up @@ -159,7 +160,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_);
Expand All @@ -170,12 +171,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_);

Expand All @@ -189,6 +207,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_);

Expand Down Expand Up @@ -219,36 +250,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_);
Expand Down
8 changes: 4 additions & 4 deletions src/interfaces/IWrappedM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ interface IWrappedM is IMigratable, IERC20Extended {

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

function wrap(address destination, uint256 amount) external;

function unwrap(address destination, uint256 amount) external;

function claimFor(address account) external returns (uint240 yield);

function claimExcess() external returns (uint240 yield);

function deposit(address destination, uint256 amount) external;

function startEarningFor(address account) external;

function stopEarningFor(address account) external;

function withdraw(address destination, uint256 amount) external;

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

function accruedYieldOf(address account) external view returns (uint240 yield);
Expand Down
16 changes: 8 additions & 8 deletions test/Test.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ contract Tests is Test {
_wrappedM.startEarningFor(_bob);

vm.prank(_alice);
_wrappedM.deposit(_alice, 100_000000);
_wrappedM.wrap(_alice, 100_000000);

_mToken.setBalanceOf(address(_wrappedM), 100_000000);

Expand All @@ -140,7 +140,7 @@ contract Tests is Test {
assertEq(_wrappedM.excess(), 0);

vm.prank(_carol);
_wrappedM.deposit(_carol, 100_000000);
_wrappedM.wrap(_carol, 100_000000);

_mToken.setBalanceOf(address(_wrappedM), 200_000000);

Expand Down Expand Up @@ -174,7 +174,7 @@ contract Tests is Test {
assertEq(_wrappedM.excess(), 100_000000);

vm.prank(_bob);
_wrappedM.deposit(_bob, 100_000000);
_wrappedM.wrap(_bob, 100_000000);

_mToken.setBalanceOf(address(_wrappedM), 500_000000);

Expand All @@ -191,7 +191,7 @@ contract Tests is Test {
assertEq(_wrappedM.excess(), 100_000000);

vm.prank(_dave);
_wrappedM.deposit(_dave, 100_000000);
_wrappedM.wrap(_dave, 100_000000);

_mToken.setBalanceOf(address(_wrappedM), 600_000000);

Expand Down Expand Up @@ -368,7 +368,7 @@ contract Tests is Test {
assertEq(_wrappedM.excess(), 600_000001);

vm.prank(_alice);
_wrappedM.withdraw(_alice, 266_666664);
_wrappedM.unwrap(_alice, 266_666664);

_mToken.setBalanceOf(address(_wrappedM), 1_233_333336);

Expand All @@ -384,7 +384,7 @@ contract Tests is Test {
assertEq(_wrappedM.excess(), 600_000001);

vm.prank(_bob);
_wrappedM.withdraw(_bob, 333_333330);
_wrappedM.unwrap(_bob, 333_333330);

_mToken.setBalanceOf(address(_wrappedM), 900_000006);

Expand All @@ -400,7 +400,7 @@ contract Tests is Test {
assertEq(_wrappedM.excess(), 600_000001);

vm.prank(_carol);
_wrappedM.withdraw(_carol, 250_000000);
_wrappedM.unwrap(_carol, 250_000000);

_mToken.setBalanceOf(address(_wrappedM), 650_000006);

Expand All @@ -416,7 +416,7 @@ contract Tests is Test {
assertEq(_wrappedM.excess(), 600_000001);

vm.prank(_dave);
_wrappedM.withdraw(_dave, 50_000000);
_wrappedM.unwrap(_dave, 50_000000);

_mToken.setBalanceOf(address(_wrappedM), 600_000006);

Expand Down

0 comments on commit e091fd4

Please sign in to comment.