@@ -4,7 +4,7 @@ pragma solidity ^0.8.0;
4
4
5
5
import "../ERC20.sol " ;
6
6
import "../utils/SafeERC20.sol " ;
7
- import "../../../interfaces/draft- IERC4626.sol " ;
7
+ import "../../../interfaces/IERC4626.sol " ;
8
8
9
9
abstract contract ERC4626 is ERC20 , IERC4626 {
10
10
IERC20Metadata private immutable _asset;
@@ -23,20 +23,26 @@ abstract contract ERC4626 is ERC20, IERC4626 {
23
23
return _asset.balanceOf (address (this ));
24
24
}
25
25
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
+ */
27
32
function convertToShares (uint256 assets ) public view virtual override returns (uint256 shares ) {
33
+ uint256 supply = totalSupply ();
34
+
28
35
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 ();
32
39
}
33
40
34
41
/** @dev See {IERC4262-convertToAssets} */
35
42
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;
40
46
}
41
47
42
48
/** @dev See {IERC4262-maxDeposit} */
@@ -61,22 +67,24 @@ abstract contract ERC4626 is ERC20, IERC4626 {
61
67
62
68
/** @dev See {IERC4262-previewDeposit} */
63
69
function previewDeposit (uint256 assets ) public view virtual override returns (uint256 ) {
64
- return convertToShares (assets); // TODO: apply fees?
70
+ return convertToShares (assets);
65
71
}
66
72
67
73
/** @dev See {IERC4262-previewMint} */
68
74
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 );
70
77
}
71
78
72
79
/** @dev See {IERC4262-previewWithdraw} */
73
80
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 );
75
83
}
76
84
77
85
/** @dev See {IERC4262-previewRedeem} */
78
86
function previewRedeem (uint256 shares ) public view virtual override returns (uint256 ) {
79
- return convertToAssets (shares); // TODO: apply fees?
87
+ return convertToAssets (shares);
80
88
}
81
89
82
90
/** @dev See {IERC4262-deposit} */
0 commit comments