Skip to content

Commit b9709dc

Browse files
committed
more testing
1 parent 7fc3633 commit b9709dc

File tree

5 files changed

+618
-384
lines changed

5 files changed

+618
-384
lines changed

contracts/mocks/ERC4626Mock.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
pragma solidity ^0.8.0;
44

5-
import "../token/ERC20/extensions/draft-ERC4626.sol";
5+
import "../token/ERC20/extensions/ERC4626.sol";
66

77
// mock class using ERC20
88
contract ERC4626Mock is ERC4626 {

contracts/token/ERC20/extensions/draft-ERC4626.sol renamed to contracts/token/ERC20/extensions/ERC4626.sol

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pragma solidity ^0.8.0;
44

55
import "../ERC20.sol";
66
import "../utils/SafeERC20.sol";
7-
import "../../../interfaces/draft-IERC4626.sol";
7+
import "../../../interfaces/IERC4626.sol";
88

99
abstract contract ERC4626 is ERC20, IERC4626 {
1010
IERC20Metadata private immutable _asset;
@@ -23,20 +23,26 @@ abstract contract ERC4626 is ERC20, IERC4626 {
2323
return _asset.balanceOf(address(this));
2424
}
2525

26-
/** @dev See {IERC4262-convertToShares} */
26+
/**
27+
* @dev See {IERC4262-convertToShares}
28+
*
29+
* Will revert if asserts > 0, totalSupply > 0 and totalAssets = 0. That corresponds to a case where any asset
30+
* would represent an infinite amout of shares.
31+
*/
2732
function convertToShares(uint256 assets) public view virtual override returns (uint256 shares) {
33+
uint256 supply = totalSupply();
34+
2835
return
29-
totalSupply() == 0 ? (assets * (10**decimals())) / (10**_asset.decimals()) : totalAssets() == 0
30-
? type(uint256).max
31-
: (assets * totalSupply()) / totalAssets();
36+
(assets == 0 || supply == 0)
37+
? (assets * 10**decimals()) / 10**_asset.decimals()
38+
: (assets * supply) / totalAssets();
3239
}
3340

3441
/** @dev See {IERC4262-convertToAssets} */
3542
function convertToAssets(uint256 shares) public view virtual override returns (uint256 assets) {
36-
return
37-
totalSupply() == 0
38-
? (shares * (10**_asset.decimals())) / (10**decimals())
39-
: (shares * totalAssets()) / totalSupply();
43+
uint256 supply = totalSupply();
44+
45+
return (supply == 0) ? (shares * 10**_asset.decimals()) / 10**decimals() : (shares * totalAssets()) / supply;
4046
}
4147

4248
/** @dev See {IERC4262-maxDeposit} */
@@ -61,22 +67,24 @@ abstract contract ERC4626 is ERC20, IERC4626 {
6167

6268
/** @dev See {IERC4262-previewDeposit} */
6369
function previewDeposit(uint256 assets) public view virtual override returns (uint256) {
64-
return convertToShares(assets); // TODO: apply fees?
70+
return convertToShares(assets);
6571
}
6672

6773
/** @dev See {IERC4262-previewMint} */
6874
function previewMint(uint256 shares) public view virtual override returns (uint256) {
69-
return convertToAssets(shares); // TODO: apply fees?
75+
uint256 assets = convertToAssets(shares);
76+
return assets + (convertToShares(assets) < shares ? 1 : 0);
7077
}
7178

7279
/** @dev See {IERC4262-previewWithdraw} */
7380
function previewWithdraw(uint256 assets) public view virtual override returns (uint256) {
74-
return convertToShares(assets); // TODO: apply fees?
81+
uint256 shares = convertToShares(assets);
82+
return shares + (convertToAssets(shares) < assets ? 1 : 0);
7583
}
7684

7785
/** @dev See {IERC4262-previewRedeem} */
7886
function previewRedeem(uint256 shares) public view virtual override returns (uint256) {
79-
return convertToAssets(shares); // TODO: apply fees?
87+
return convertToAssets(shares);
8088
}
8189

8290
/** @dev See {IERC4262-deposit} */

0 commit comments

Comments
 (0)