Skip to content

Commit 8f8fd84

Browse files
Improve some NatSpec and revert reasons (OpenZeppelin#3809)
Co-authored-by: JulissaDantes <julissadcj@gmail.com>
1 parent 8c9a831 commit 8f8fd84

File tree

6 files changed

+43
-15
lines changed

6 files changed

+43
-15
lines changed

contracts/token/ERC20/extensions/ERC20Votes.sol

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ abstract contract ERC20Votes is IVotes, ERC20Permit {
271271
return a - b;
272272
}
273273

274+
/**
275+
* @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
276+
*/
274277
function _unsafeAccess(Checkpoint[] storage ckpts, uint256 pos) private pure returns (Checkpoint storage result) {
275278
assembly {
276279
mstore(0, ckpts.slot)

contracts/token/ERC20/extensions/ERC4626.sol

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ import "../../../utils/math/Math.sol";
1717
* the ERC20 standard. Any additional extensions included along it would affect the "shares" token represented by this
1818
* contract and not the "assets" token which is an independent contract.
1919
*
20-
* CAUTION: Deposits and withdrawals may incur unexpected slippage. Users should verify that the amount received of
21-
* shares or assets is as expected. EOAs should operate through a wrapper that performs these checks such as
20+
* CAUTION: When the vault is empty or nearly empty, deposits are at high risk of being stolen through frontrunning with
21+
* a "donation" to the vault that inflates the price of a share. This is variously known as a donation or inflation
22+
* attack and is essentially a problem of slippage. Vault deployers can protect against this attack by making an initial
23+
* deposit of a non-trivial amount of the asset, such that price manipulation becomes infeasible. Withdrawals may
24+
* similarly be affected by slippage. Users can protect against this attack as well unexpected slippage in general by
25+
* verifying the amount received is as expected, using a wrapper that performs these checks such as
2226
* https://github.com/fei-protocol/ERC4626#erc4626router-and-base[ERC4626Router].
2327
*
2428
* _Available since v4.7._
@@ -134,7 +138,11 @@ abstract contract ERC4626 is ERC20, IERC4626 {
134138
return shares;
135139
}
136140

137-
/** @dev See {IERC4626-mint}. */
141+
/** @dev See {IERC4626-mint}.
142+
*
143+
* As opposed to {deposit}, minting is allowed even if the vault is in a state where the price of a share is zero.
144+
* In this case, the shares will be minted without requiring any assets to be deposited.
145+
*/
138146
function mint(uint256 shares, address receiver) public virtual override returns (uint256) {
139147
require(shares <= maxMint(receiver), "ERC4626: mint more than max");
140148

@@ -267,6 +275,9 @@ abstract contract ERC4626 is ERC20, IERC4626 {
267275
emit Withdraw(caller, receiver, owner, assets, shares);
268276
}
269277

278+
/**
279+
* @dev Checks if vault is "healthy" in the sense of having assets backing the circulating shares.
280+
*/
270281
function _isVaultHealthy() private view returns (bool) {
271282
return totalAssets() > 0 || totalSupply() == 0;
272283
}

contracts/utils/Checkpoints.sol

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ library Checkpoints {
2828

2929
/**
3030
* @dev Returns the value at a given block number. If a checkpoint is not available at that block, the closest one
31-
* before it is returned, or zero otherwise.
31+
* before it is returned, or zero otherwise. Because the number returned corresponds to that at the end of the
32+
* block, the requested block number must be in the past, excluding the current block.
3233
*/
3334
function getAtBlock(History storage self, uint256 blockNumber) internal view returns (uint256) {
3435
require(blockNumber < block.number, "Checkpoints: block not yet mined");
@@ -143,8 +144,8 @@ library Checkpoints {
143144
// Copying to memory is important here.
144145
Checkpoint memory last = _unsafeAccess(self, pos - 1);
145146

146-
// Checkpoints keys must be increasing.
147-
require(last._blockNumber <= key, "Checkpoint: invalid key");
147+
// Checkpoint keys must be non-decreasing.
148+
require(last._blockNumber <= key, "Checkpoint: decreasing keys");
148149

149150
// Update or push new checkpoint
150151
if (last._blockNumber == key) {
@@ -205,6 +206,9 @@ library Checkpoints {
205206
return high;
206207
}
207208

209+
/**
210+
* @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
211+
*/
208212
function _unsafeAccess(Checkpoint[] storage self, uint256 pos) private pure returns (Checkpoint storage result) {
209213
assembly {
210214
mstore(0, self.slot)
@@ -304,8 +308,8 @@ library Checkpoints {
304308
// Copying to memory is important here.
305309
Checkpoint224 memory last = _unsafeAccess(self, pos - 1);
306310

307-
// Checkpoints keys must be increasing.
308-
require(last._key <= key, "Checkpoint: invalid key");
311+
// Checkpoint keys must be non-decreasing.
312+
require(last._key <= key, "Checkpoint: decreasing keys");
309313

310314
// Update or push new checkpoint
311315
if (last._key == key) {
@@ -366,6 +370,9 @@ library Checkpoints {
366370
return high;
367371
}
368372

373+
/**
374+
* @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
375+
*/
369376
function _unsafeAccess(Checkpoint224[] storage self, uint256 pos)
370377
private
371378
pure
@@ -469,8 +476,8 @@ library Checkpoints {
469476
// Copying to memory is important here.
470477
Checkpoint160 memory last = _unsafeAccess(self, pos - 1);
471478

472-
// Checkpoints keys must be increasing.
473-
require(last._key <= key, "Checkpoint: invalid key");
479+
// Checkpoint keys must be non-decreasing.
480+
require(last._key <= key, "Checkpoint: decreasing keys");
474481

475482
// Update or push new checkpoint
476483
if (last._key == key) {
@@ -531,6 +538,9 @@ library Checkpoints {
531538
return high;
532539
}
533540

541+
/**
542+
* @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
543+
*/
534544
function _unsafeAccess(Checkpoint160[] storage self, uint256 pos)
535545
private
536546
pure

contracts/utils/math/Math.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ library Math {
7575
}
7676

7777
// Make sure the result is less than 2^256. Also prevents denominator == 0.
78-
require(denominator > prod1);
78+
require(denominator > prod1, "Math: mulDiv overflow");
7979

8080
///////////////////////////////////////////////
8181
// 512 by 256 division.

scripts/generate/templates/Checkpoints.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ function upperLookup(${opts.historyTypeName} storage self, ${opts.keyTypeName} k
6767
const legacyOperations = opts => `\
6868
/**
6969
* @dev Returns the value at a given block number. If a checkpoint is not available at that block, the closest one
70-
* before it is returned, or zero otherwise.
70+
* before it is returned, or zero otherwise. Because the number returned corresponds to that at the end of the
71+
* block, the requested block number must be in the past, excluding the current block.
7172
*/
7273
function getAtBlock(${opts.historyTypeName} storage self, uint256 blockNumber) internal view returns (uint256) {
7374
require(blockNumber < block.number, "Checkpoints: block not yet mined");
@@ -184,8 +185,8 @@ function _insert(
184185
// Copying to memory is important here.
185186
${opts.checkpointTypeName} memory last = _unsafeAccess(self, pos - 1);
186187
187-
// Checkpoints keys must be increasing.
188-
require(last.${opts.keyFieldName} <= key, "Checkpoint: invalid key");
188+
// Checkpoint keys must be non-decreasing.
189+
require(last.${opts.keyFieldName} <= key, "Checkpoint: decreasing keys");
189190
190191
// Update or push new checkpoint
191192
if (last.${opts.keyFieldName} == key) {
@@ -246,6 +247,9 @@ function _lowerBinaryLookup(
246247
return high;
247248
}
248249
250+
/**
251+
* @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
252+
*/
249253
function _unsafeAccess(${opts.checkpointTypeName}[] storage self, uint256 pos)
250254
private
251255
pure

test/utils/Checkpoints.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ contract('Checkpoints', function (accounts) {
151151
});
152152

153153
it('cannot push values in the past', async function () {
154-
await expectRevert(this.contract.push(last(this.checkpoints).key - 1, '0'), 'Checkpoint: invalid key');
154+
await expectRevert(this.contract.push(last(this.checkpoints).key - 1, '0'), 'Checkpoint: decreasing keys');
155155
});
156156

157157
it('can update last value', async function () {

0 commit comments

Comments
 (0)