-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathERC4626MultiToken.sol
295 lines (227 loc) · 9.54 KB
/
ERC4626MultiToken.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
import {FixedPointMathLib} from "solady/utils/FixedPointMathLib.sol";
import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";
import {ERC20} from "solmate/tokens/ERC20.sol";
import {ReentrancyGuard} from "solmate/utils/ReentrancyGuard.sol";
import {IERC4626MultiToken} from "./interfaces/IERC4626MultiToken.sol";
/// @title Minimal ERC4626 tokenized Vault multi asset implementation
/// @author Maia DAO (https://github.com/Maia-DAO)
abstract contract ERC4626MultiToken is ERC20, ReentrancyGuard, IERC4626MultiToken {
using SafeTransferLib for address;
using FixedPointMathLib for uint256;
/*//////////////////////////////////////////////////////////////
IMMUTABLES
//////////////////////////////////////////////////////////////*/
/// @inheritdoc IERC4626MultiToken
address[] public assets;
/// @inheritdoc IERC4626MultiToken
uint256[] public weights;
/// @inheritdoc IERC4626MultiToken
mapping(address => uint256) public assetId;
/// @inheritdoc IERC4626MultiToken
uint256 public totalWeights;
/// @inheritdoc IERC4626MultiToken
function getAssets() external view returns (address[] memory) {
return assets;
}
constructor(address[] memory _assets, uint256[] memory _weights, string memory _name, string memory _symbol)
ERC20(_name, _symbol, 18)
{
assets = _assets;
weights = _weights;
uint256 length = _weights.length;
uint256 _totalWeights;
if (length != _assets.length || length == 0) revert InvalidLength();
for (uint256 i = 0; i < length;) {
require(ERC20(_assets[i]).decimals() == 18);
require(_weights[i] > 0);
_totalWeights += _weights[i];
assetId[_assets[i]] = i + 1;
emit AssetAdded(_assets[i], _weights[i]);
unchecked {
i++;
}
}
totalWeights = _totalWeights;
}
function receiveAssets(uint256[] memory assetsAmounts) private {
uint256 length = assetsAmounts.length;
for (uint256 i = 0; i < length;) {
assets[i].safeTransferFrom(msg.sender, address(this), assetsAmounts[i]);
unchecked {
i++;
}
}
}
function sendAssets(uint256[] memory assetsAmounts, address receiver) private {
uint256 length = assetsAmounts.length;
for (uint256 i = 0; i < length;) {
assets[i].safeTransfer(receiver, assetsAmounts[i]);
unchecked {
i++;
}
}
}
/*//////////////////////////////////////////////////////////////
DEPOSIT/WITHDRAWAL LOGIC
//////////////////////////////////////////////////////////////*/
/// @inheritdoc IERC4626MultiToken
function deposit(uint256[] calldata assetsAmounts, address receiver)
public
virtual
nonReentrant
returns (uint256 shares)
{
// Check for rounding error since we round down in previewDeposit.
require((shares = previewDeposit(assetsAmounts)) != 0, "ZERO_SHARES");
// Need to transfer before minting or ERC777s could reenter.
receiveAssets(assetsAmounts);
_mint(receiver, shares);
emit Deposit(msg.sender, receiver, assetsAmounts, shares);
afterDeposit(assetsAmounts, shares);
}
/// @inheritdoc IERC4626MultiToken
function mint(uint256 shares, address receiver)
public
virtual
nonReentrant
returns (uint256[] memory assetsAmounts)
{
assetsAmounts = previewMint(shares); // No need to check for rounding error, previewMint rounds up.
// Need to transfer before minting or ERC777s could reenter.
receiveAssets(assetsAmounts);
_mint(receiver, shares);
emit Deposit(msg.sender, receiver, assetsAmounts, shares);
afterDeposit(assetsAmounts, shares);
}
/// @inheritdoc IERC4626MultiToken
function withdraw(uint256[] calldata assetsAmounts, address receiver, address owner)
public
virtual
nonReentrant
returns (uint256 shares)
{
shares = previewWithdraw(assetsAmounts); // No need to check for rounding error, previewWithdraw rounds up.
if (msg.sender != owner) {
uint256 allowed = allowance[owner][msg.sender]; // Saves gas for limited approvals.
if (allowed != type(uint256).max) allowance[owner][msg.sender] = allowed - shares;
}
beforeWithdraw(assetsAmounts, shares);
_burn(owner, shares);
emit Withdraw(msg.sender, receiver, owner, assetsAmounts, shares);
sendAssets(assetsAmounts, receiver);
}
/// @inheritdoc IERC4626MultiToken
function redeem(uint256 shares, address receiver, address owner)
public
virtual
nonReentrant
returns (uint256[] memory assetsAmounts)
{
if (msg.sender != owner) {
uint256 allowed = allowance[owner][msg.sender]; // Saves gas for limited approvals.
if (allowed != type(uint256).max) allowance[owner][msg.sender] = allowed - shares;
}
assetsAmounts = previewRedeem(shares);
uint256 length = assetsAmounts.length;
for (uint256 i = 0; i < length;) {
// Check for rounding error since we round down in previewRedeem.
if (assetsAmounts[i] == 0) revert ZeroAssets();
unchecked {
i++;
}
}
beforeWithdraw(assetsAmounts, shares);
_burn(owner, shares);
emit Withdraw(msg.sender, receiver, owner, assetsAmounts, shares);
sendAssets(assetsAmounts, receiver);
}
/*//////////////////////////////////////////////////////////////
ACCOUNTING LOGIC
//////////////////////////////////////////////////////////////*/
function totalAssets() public view virtual returns (uint256);
/// @inheritdoc IERC4626MultiToken
function convertToShares(uint256[] calldata assetsAmounts) public view virtual returns (uint256 shares) {
uint256 _totalWeights = totalWeights;
uint256 length = assetsAmounts.length;
if (length != assets.length) revert InvalidLength();
shares = type(uint256).max;
for (uint256 i = 0; i < length;) {
uint256 share = assetsAmounts[i].mulDiv(_totalWeights, weights[i]);
if (share < shares) shares = share;
unchecked {
i++;
}
}
}
/// @inheritdoc IERC4626MultiToken
function convertToAssets(uint256 shares) public view virtual returns (uint256[] memory assetsAmounts) {
uint256 _totalWeights = totalWeights;
uint256 length = assets.length;
assetsAmounts = new uint256[](length);
for (uint256 i = 0; i < length;) {
assetsAmounts[i] = shares.mulDiv(weights[i], _totalWeights);
unchecked {
i++;
}
}
}
/// @inheritdoc IERC4626MultiToken
function previewDeposit(uint256[] calldata assetsAmounts) public view virtual returns (uint256) {
return convertToShares(assetsAmounts);
}
/// @inheritdoc IERC4626MultiToken
function previewMint(uint256 shares) public view virtual returns (uint256[] memory assetsAmounts) {
uint256 _totalWeights = totalWeights;
uint256 length = assets.length;
assetsAmounts = new uint256[](length);
for (uint256 i = 0; i < length;) {
assetsAmounts[i] = shares.mulDivUp(weights[i], _totalWeights);
unchecked {
i++;
}
}
}
/// @inheritdoc IERC4626MultiToken
function previewWithdraw(uint256[] calldata assetsAmounts) public view virtual returns (uint256 shares) {
uint256 _totalWeights = totalWeights;
uint256 length = assetsAmounts.length;
if (length != assets.length) revert InvalidLength();
for (uint256 i = 0; i < length;) {
uint256 share = assetsAmounts[i].mulDivUp(_totalWeights, weights[i]);
if (share > shares) shares = share;
unchecked {
i++;
}
}
}
/// @inheritdoc IERC4626MultiToken
function previewRedeem(uint256 shares) public view virtual returns (uint256[] memory) {
return convertToAssets(shares);
}
/*//////////////////////////////////////////////////////////////
DEPOSIT/WITHDRAWAL LIMIT LOGIC
//////////////////////////////////////////////////////////////*/
/// @inheritdoc IERC4626MultiToken
function maxDeposit(address) public view virtual returns (uint256) {
return type(uint256).max;
}
/// @inheritdoc IERC4626MultiToken
function maxMint(address) public view virtual returns (uint256) {
return type(uint256).max;
}
/// @inheritdoc IERC4626MultiToken
function maxWithdraw(address owner) public view virtual returns (uint256[] memory) {
return convertToAssets(balanceOf[owner]);
}
/// @inheritdoc IERC4626MultiToken
function maxRedeem(address owner) public view virtual returns (uint256) {
return balanceOf[owner];
}
/*//////////////////////////////////////////////////////////////
INTERNAL HOOKS LOGIC
//////////////////////////////////////////////////////////////*/
function beforeWithdraw(uint256[] memory assetsAmounts, uint256 shares) internal virtual {}
function afterDeposit(uint256[] memory assetsAmounts, uint256 shares) internal virtual {}
}