Skip to content

Commit 71ca20b

Browse files
rft: use 256 bit arithmetic for share math (#729)
1 parent 3818e6a commit 71ca20b

File tree

4 files changed

+36
-48
lines changed

4 files changed

+36
-48
lines changed

snapshots/Hub.Operations.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"add": "119571",
2+
"add": "119161",
33
"draw": "112344",
4-
"refreshPremium": "100892",
5-
"remove: full": "80336",
6-
"remove: partial": "86836",
7-
"restore: full": "113988",
8-
"restore: partial": "119840"
4+
"refreshPremium": "100482",
5+
"remove: full": "79500",
6+
"remove: partial": "86000",
7+
"restore: full": "113578",
8+
"restore: partial": "119430"
99
}

snapshots/Spoke.Getters.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"getUserAccountData: supplies: 0, borrows: 0": "9329",
3-
"getUserAccountData: supplies: 1, borrows: 0": "45002",
4-
"getUserAccountData: supplies: 2, borrows: 0": "75761",
5-
"getUserAccountData: supplies: 2, borrows: 1": "99125",
6-
"getUserAccountData: supplies: 2, borrows: 2": "121045"
3+
"getUserAccountData: supplies: 1, borrows: 0": "44592",
4+
"getUserAccountData: supplies: 2, borrows: 0": "74941",
5+
"getUserAccountData: supplies: 2, borrows: 1": "98305",
6+
"getUserAccountData: supplies: 2, borrows: 2": "120225"
77
}

snapshots/Spoke.Operations.json

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
{
2-
"borrow: first": "257886",
3-
"borrow: second action, same reserve": "221358",
4-
"liquidationCall: full": "328791",
5-
"liquidationCall: partial": "352462",
6-
"permitReserve + repay (multicall)": "280764",
7-
"permitReserve + supply (multicall)": "140631",
8-
"permitReserve + supply + enable collateral (multicall)": "174116",
9-
"repay: full": "175217",
10-
"repay: partial": "239525",
2+
"borrow: first": "257066",
3+
"borrow: second action, same reserve": "220538",
4+
"liquidationCall: full": "326299",
5+
"liquidationCall: partial": "349560",
6+
"permitReserve + repay (multicall)": "279944",
7+
"permitReserve + supply (multicall)": "140221",
8+
"permitReserve + supply + enable collateral (multicall)": "173706",
9+
"repay: full": "174397",
10+
"repay: partial": "238705",
1111
"setUserPositionManagerWithSig: disable": "38977",
1212
"setUserPositionManagerWithSig: enable": "63000",
13-
"supply + enable collateral (multicall)": "152550",
14-
"supply: 0 borrows, collateral disabled": "116444",
15-
"supply: 0 borrows, collateral enabled": "119591",
16-
"supply: 1 borrow": "119580",
17-
"supply: second action, same reserve": "102491",
13+
"supply + enable collateral (multicall)": "152140",
14+
"supply: 0 borrows, collateral disabled": "116034",
15+
"supply: 0 borrows, collateral enabled": "119181",
16+
"supply: 1 borrow": "119170",
17+
"supply: second action, same reserve": "102081",
1818
"updateUserDynamicConfig: 1 collateral": "53152",
1919
"updateUserDynamicConfig: 2 collaterals": "58433",
20-
"updateUserRiskPremium: 1 borrow": "143698",
21-
"updateUserRiskPremium: 2 borrows": "195417",
20+
"updateUserRiskPremium: 1 borrow": "143288",
21+
"updateUserRiskPremium: 2 borrows": "195007",
2222
"usingAsCollateral: 0 borrows, enable": "54026",
23-
"usingAsCollateral: 1 borrow, disable": "153924",
23+
"usingAsCollateral: 1 borrow, disable": "153514",
2424
"usingAsCollateral: 1 borrow, enable": "24786",
25-
"withdraw: 0 borrows, full": "135122",
26-
"withdraw: 0 borrows, partial": "137225",
27-
"withdraw: 1 borrow, partial": "254549"
25+
"withdraw: 0 borrows, full": "133466",
26+
"withdraw: 0 borrows, partial": "135569",
27+
"withdraw: 1 borrow, partial": "252483"
2828
}

src/libraries/math/SharesMath.sol

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
// Copyright (c) 2025 Aave Labs
33
pragma solidity ^0.8.0;
44

5-
import {Math} from 'src/dependencies/openzeppelin/Math.sol';
5+
import {MathUtils} from 'src/libraries/math/MathUtils.sol';
66

77
library SharesMath {
8-
using Math for uint256;
8+
using MathUtils for uint256;
99

1010
/// @dev Virtual assets and shares are used to mitigate share manipulation attacks
1111
uint256 internal constant VIRTUAL_ASSETS = 1e6;
@@ -16,42 +16,30 @@ library SharesMath {
1616
uint256 totalAssets,
1717
uint256 totalShares
1818
) internal pure returns (uint256) {
19-
return
20-
assets.mulDiv(
21-
totalShares + VIRTUAL_SHARES,
22-
totalAssets + VIRTUAL_ASSETS,
23-
Math.Rounding.Floor
24-
);
19+
return assets.mulDivDown(totalShares + VIRTUAL_SHARES, totalAssets + VIRTUAL_ASSETS);
2520
}
2621

2722
function toAssetsDown(
2823
uint256 shares,
2924
uint256 totalAssets,
3025
uint256 totalShares
3126
) internal pure returns (uint256) {
32-
return
33-
shares.mulDiv(
34-
totalAssets + VIRTUAL_ASSETS,
35-
totalShares + VIRTUAL_SHARES,
36-
Math.Rounding.Floor
37-
);
27+
return shares.mulDivDown(totalAssets + VIRTUAL_ASSETS, totalShares + VIRTUAL_SHARES);
3828
}
3929

4030
function toSharesUp(
4131
uint256 assets,
4232
uint256 totalAssets,
4333
uint256 totalShares
4434
) internal pure returns (uint256) {
45-
return
46-
assets.mulDiv(totalShares + VIRTUAL_SHARES, totalAssets + VIRTUAL_ASSETS, Math.Rounding.Ceil);
35+
return assets.mulDivUp(totalShares + VIRTUAL_SHARES, totalAssets + VIRTUAL_ASSETS);
4736
}
4837

4938
function toAssetsUp(
5039
uint256 shares,
5140
uint256 totalAssets,
5241
uint256 totalShares
5342
) internal pure returns (uint256) {
54-
return
55-
shares.mulDiv(totalAssets + VIRTUAL_ASSETS, totalShares + VIRTUAL_SHARES, Math.Rounding.Ceil);
43+
return shares.mulDivUp(totalAssets + VIRTUAL_ASSETS, totalShares + VIRTUAL_SHARES);
5644
}
5745
}

0 commit comments

Comments
 (0)