Skip to content

Commit 938bce0

Browse files
committed
Change rounding descriptions
1 parent 801ca61 commit 938bce0

File tree

4 files changed

+12
-11
lines changed

4 files changed

+12
-11
lines changed

contracts/utils/Arrays.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ library Arrays {
3333
uint256 mid = Math.average(low, high);
3434

3535
// Note that mid will always be strictly less than high (i.e. it will be a valid array index)
36-
// because Math.average rounds down (it does integer division with truncation).
36+
// because Math.average rounds towards zero (it does integer division with truncation).
3737
if (unsafeAccess(array, mid).value > element) {
3838
high = mid;
3939
} else {

contracts/utils/math/Math.sol

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ library Math {
101101
/**
102102
* @dev Returns the ceiling of the division of two numbers.
103103
*
104-
* This differs from standard division with `/` in that it rounds up instead
105-
* of rounding down.
104+
* This differs from standard division with `/` in that it rounds towards infinity instead
105+
* of rounding towards zero.
106106
*/
107107
function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
108108
if (b == 0) {
@@ -214,7 +214,8 @@ library Math {
214214
}
215215

216216
/**
217-
* @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded down.
217+
* @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded
218+
* towards zero.
218219
*
219220
* Inspired by Henry S. Warren, Jr.'s "Hacker's Delight" (Chapter 11).
220221
*/
@@ -262,7 +263,7 @@ library Math {
262263
}
263264

264265
/**
265-
* @dev Return the log in base 2, rounded down, of a positive value.
266+
* @dev Return the log in base 2 of a positive value rounded towards zero.
266267
* Returns 0 if given 0.
267268
*/
268269
function log2(uint256 value) internal pure returns (uint256) {
@@ -315,7 +316,7 @@ library Math {
315316
}
316317

317318
/**
318-
* @dev Return the log in base 10, rounded down, of a positive value.
319+
* @dev Return the log in base 10 of a positive value rounded towards zero.
319320
* Returns 0 if given 0.
320321
*/
321322
function log10(uint256 value) internal pure returns (uint256) {
@@ -364,7 +365,7 @@ library Math {
364365
}
365366

366367
/**
367-
* @dev Return the log in base 256, rounded down, of a positive value.
368+
* @dev Return the log in base 256 of a positive value rounded towards zero.
368369
* Returns 0 if given 0.
369370
*
370371
* Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.

docs/modules/ROOT/pages/erc4626.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ image::erc4626-rate-loglogext.png[More exchange rates in logarithmic scale]
2929

3030
=== The attack
3131

32-
When depositing tokens, the number of shares a user gets is rounded down. This rounding takes away value from the user in favor or the vault (i.e. in favor of all the current share holders). This rounding is often negligible because of the amount at stake. If you deposit 1e9 shares worth of tokens, the rounding will have you lose at most 0.0000001% of your deposit. However if you deposit 10 shares worth of tokens, you could lose 10% of your deposit. Even worse, if you deposit <1 share worth of tokens, then you get 0 shares, and you basically made a donation.
32+
When depositing tokens, the number of shares a user gets is rounded towards zero. This rounding takes away value from the user in favor or the vault (i.e. in favor of all the current share holders). This rounding is often negligible because of the amount at stake. If you deposit 1e9 shares worth of tokens, the rounding will have you lose at most 0.0000001% of your deposit. However if you deposit 10 shares worth of tokens, you could lose 10% of your deposit. Even worse, if you deposit <1 share worth of tokens, then you get 0 shares, and you basically made a donation.
3333

3434
For a given amount of assets, the more shares you receive the safer you are. If you want to limit your losses to at most 1%, you need to receive at least 100 shares.
3535

test/token/ERC20/extensions/ERC4626.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -965,8 +965,8 @@ contract('ERC4626', function (accounts) {
965965
}
966966

967967
// 5. Bob mints 2000 shares (costs 3001 assets)
968-
// NOTE: Bob's assets spent got rounded up
969-
// NOTE: Alices's vault assets got rounded up
968+
// NOTE: Bob's assets spent got rounded towards infinity
969+
// NOTE: Alices's vault assets got rounded towards infinity
970970
{
971971
const { tx } = await this.vault.mint(2000, user2, { from: user2 });
972972
await expectEvent.inTransaction(tx, this.token, 'Transfer', {
@@ -1056,7 +1056,7 @@ contract('ERC4626', function (accounts) {
10561056
}
10571057

10581058
// 9. Alice withdraws 3643 assets (2000 shares)
1059-
// NOTE: Bob's assets have been rounded back up
1059+
// NOTE: Bob's assets have been rounded back towards infinity
10601060
{
10611061
const { tx } = await this.vault.withdraw(3643, user1, user1, { from: user1 });
10621062
await expectEvent.inTransaction(tx, this.vault, 'Transfer', {

0 commit comments

Comments
 (0)