Skip to content
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

feat: Simple Wrapper #3

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
branch = v1
[submodule "lib/common"]
path = lib/common
url = git@github.com:MZero-Labs/common.git
7 changes: 4 additions & 3 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
{
"plugins": ["prettier-plugin-solidity"],
"plugins": [
"prettier-plugin-solidity"
],
"overrides": [
{
"files": "*.sol",
"options": {
"bracketSpacing": true,
"compiler": "0.8.25",
"compiler": "0.8.23",
"parser": "solidity-parse",
"printWidth": 120,
"tabWidth": 4,
}
}
]
}

28 changes: 22 additions & 6 deletions .solhint.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
{
"extends": ["solhint:recommended"],
"plugins": ["prettier"],
"extends": [
"solhint:recommended"
],
"plugins": [
"prettier"
],
"rules": {
"prettier/prettier": "error",
"code-complexity": ["warn", 10],
"compiler-version": ["error", "0.8.25"],
"code-complexity": [
"warn",
10
],
"compiler-version": [
"error",
"0.8.23"
],
"comprehensive-interface": "off",
"const-name-snakecase": "off",
"func-name-mixedcase": "off",
Expand All @@ -14,10 +24,16 @@
"ignoreConstructors": true
}
],
"function-max-lines": ["warn", 100],
"function-max-lines": [
"warn",
100
],
"immutable-vars-naming": "off",
"imports-on-top": "error",
"max-line-length": ["warn", 120],
"max-line-length": [
"warn",
120
],
"no-empty-blocks": "off",
"no-inline-assembly": "off",
"not-rely-on-time": "off",
Expand Down
2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ gas_reports = ["*"]
gas_reports_ignore = []
ignored_error_codes = []
optimizer = false
solc_version = "0.8.25"
solc_version = "0.8.23"
verbosity = 3

[profile.production]
Expand Down
1 change: 1 addition & 0 deletions lib/common
Submodule common added at e80940
17 changes: 0 additions & 17 deletions script/Foo.s.sol

This file was deleted.

9 changes: 0 additions & 9 deletions src/Foo.sol

This file was deleted.

97 changes: 97 additions & 0 deletions src/WrappedM.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// SPDX-License-Identifier: GPL-3.0

pragma solidity 0.8.23;

import { UIntMath } from "../lib/common/src/libs/UIntMath.sol";

import { IERC20 } from "../lib/common/src/interfaces/IERC20.sol";

import { ERC20Extended } from "../lib/common/src/ERC20Extended.sol";

import { IMTokenLike } from "./interfaces/IMTokenLike.sol";
import { IWrappedM } from "./interfaces/IWrappedM.sol";

contract WrappedM is IWrappedM, ERC20Extended {
/* ============ Variables ============ */

uint56 internal constant _EXP_SCALED_ONE = 1e12;

address public immutable mToken;

uint256 public totalSupply;

mapping(address account => uint256 balance) public balanceOf;

/* ============ Modifiers ============ */

modifier onlyEarner() {
if (!IMTokenLike(mToken).isEarning(msg.sender)) revert NotEarner();

_;
}

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

constructor(address mToken_) ERC20Extended("WrappedM by M^0", "wM", 6) {
mToken = mToken_;
}

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

function deposit(address account_, uint256 amount_) external onlyEarner returns (uint256 shares_) {
shares_ = _getPrincipalAmountRoundedDown(UIntMath.safe240(amount_), IMTokenLike(mToken).currentIndex());

emit Transfer(address(0), account_, shares_);

balanceOf[account_] += shares_;
totalSupply += shares_;

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

function withdraw(address account_, uint256 shares_) external {
emit Transfer(account_, address(0), shares_);

uint256 amount_ = _getPresentAmountRoundedDown(UIntMath.safe112(shares_), IMTokenLike(mToken).currentIndex());

balanceOf[account_] += shares_;
totalSupply += shares_;

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

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

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

function _transfer(address sender_, address recipient_, uint256 amount_) internal override {
emit Transfer(sender_, recipient_, amount_);

balanceOf[sender_] -= amount_;
balanceOf[recipient_] += amount_;
}

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

function _multiplyDown(uint112 x_, uint128 index_) internal pure returns (uint240) {
unchecked {
return uint240((uint256(x_) * index_) / _EXP_SCALED_ONE);
}
}

function _divideDown(uint240 x_, uint128 index_) internal pure returns (uint112 z) {
if (index_ == 0) revert DivisionByZero();

unchecked {
return UIntMath.safe112((uint256(x_) * _EXP_SCALED_ONE) / index_);
}
}

function _getPresentAmountRoundedDown(uint112 principalAmount_, uint128 index_) internal pure returns (uint240) {
return _multiplyDown(principalAmount_, index_);
}

function _getPrincipalAmountRoundedDown(uint240 presentAmount_, uint128 index_) internal pure returns (uint112) {
return _divideDown(presentAmount_, index_);
}
}
11 changes: 11 additions & 0 deletions src/interfaces/IMTokenLike.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: GPL-3.0

pragma solidity 0.8.23;

interface IMTokenLike {
/* ============ View/Pure Functions ============ */

function currentIndex() external view returns (uint128);

function isEarning(address account) external view returns (bool);
}
25 changes: 25 additions & 0 deletions src/interfaces/IWrappedM.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: GPL-3.0

pragma solidity 0.8.23;

import { IERC20Extended } from "../../lib/common/src/interfaces/IERC20Extended.sol";

interface IWrappedM is IERC20Extended {
/* ============ Events ============ */

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

error NotEarner();

error DivisionByZero();

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

function deposit(address account, uint256 amount) external returns (uint256 shares);

function withdraw(address account, uint256 shares) external;

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

function mToken() external view returns (address);
}
41 changes: 0 additions & 41 deletions test/Foo.t.sol

This file was deleted.

Loading