Skip to content

Commit 8f8a61a

Browse files
authored
feat: reorder and tighter packing (#953)
1 parent 80c5191 commit 8f8a61a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+329
-331
lines changed

snapshots/Hub.Operations.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
2-
"add": "115482",
3-
"draw": "124510",
4-
"eliminateDeficit: full": "59744",
5-
"eliminateDeficit: partial": "69320",
6-
"mintFeeShares": "82875",
7-
"payFee": "71140",
8-
"refreshPremium": "71913",
9-
"remove: full": "73767",
10-
"remove: partial": "78496",
11-
"reportDeficit": "109874",
12-
"restore: full": "91042",
13-
"restore: partial": "104456",
14-
"transferShares": "69941"
2+
"add": "118313",
3+
"draw": "120582",
4+
"eliminateDeficit: full": "59772",
5+
"eliminateDeficit: partial": "69348",
6+
"mintFeeShares": "82887",
7+
"payFee": "69103",
8+
"refreshPremium": "70157",
9+
"remove: full": "76677",
10+
"remove: partial": "81324",
11+
"reportDeficit": "112891",
12+
"restore: full": "91259",
13+
"restore: partial": "99873",
14+
"transferShares": "67904"
1515
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"borrowNative": "228552",
3-
"repayNative": "241812",
4-
"supplyAsCollateralNative": "181646",
5-
"supplyNative": "135326",
6-
"withdrawNative: full": "123310",
7-
"withdrawNative: partial": "133937"
2+
"borrowNative": "226950",
3+
"repayNative": "240607",
4+
"supplyAsCollateralNative": "184580",
5+
"supplyNative": "137591",
6+
"withdrawNative: full": "125655",
7+
"withdrawNative: partial": "136868"
88
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"borrowWithSig": "214732",
3-
"repayWithSig": "240846",
2+
"borrowWithSig": "215130",
3+
"repayWithSig": "241641",
44
"setSelfAsUserPositionManagerWithSig": "75492",
5-
"setUsingAsCollateralWithSig": "124340",
6-
"supplyWithSig": "151932",
5+
"setUsingAsCollateralWithSig": "124443",
6+
"supplyWithSig": "154197",
77
"updateUserDynamicConfigWithSig": "62659",
88
"updateUserRiskPremiumWithSig": "61404",
9-
"withdrawWithSig": "129335"
9+
"withdrawWithSig": "131680"
1010
}

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": "11681",
3-
"getUserAccountData: supplies: 1, borrows: 0": "48353",
4-
"getUserAccountData: supplies: 2, borrows: 0": "79753",
5-
"getUserAccountData: supplies: 2, borrows: 1": "100026",
6-
"getUserAccountData: supplies: 2, borrows: 2": "118250"
3+
"getUserAccountData: supplies: 1, borrows: 0": "48456",
4+
"getUserAccountData: supplies: 2, borrows: 0": "79959",
5+
"getUserAccountData: supplies: 2, borrows: 1": "100232",
6+
"getUserAccountData: supplies: 2, borrows: 2": "118456"
77
}
Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
{
2-
"borrow: first": "192467",
3-
"borrow: second action, same reserve": "172422",
4-
"liquidationCall (receiveShares): full": "263784",
5-
"liquidationCall (receiveShares): partial": "294057",
6-
"liquidationCall: full": "285806",
7-
"liquidationCall: partial": "298696",
8-
"permitReserve + repay (multicall)": "227753",
9-
"permitReserve + supply (multicall)": "169719",
10-
"permitReserve + supply + enable collateral (multicall)": "258030",
11-
"repay: full": "154640",
12-
"repay: partial": "178248",
2+
"borrow: first": "187733",
3+
"borrow: second action, same reserve": "167705",
4+
"liquidationCall (receiveShares): full": "262664",
5+
"liquidationCall (receiveShares): partial": "290260",
6+
"liquidationCall: full": "287411",
7+
"liquidationCall: partial": "297607",
8+
"permitReserve + repay (multicall)": "223433",
9+
"permitReserve + supply (multicall)": "172653",
10+
"permitReserve + supply + enable collateral (multicall)": "261166",
11+
"repay: full": "153130",
12+
"repay: partial": "176738",
1313
"setUserPositionManagerWithSig: disable": "44896",
1414
"setUserPositionManagerWithSig: enable": "68925",
15-
"supply + enable collateral (multicall)": "181488",
16-
"supply: 0 borrows, collateral disabled": "119885",
17-
"supply: 0 borrows, collateral enabled": "149375",
18-
"supply: 1 borrow": "124792",
19-
"supply: second action, same reserve": "132275",
15+
"supply + enable collateral (multicall)": "184504",
16+
"supply: 0 borrows, collateral disabled": "122716",
17+
"supply: 0 borrows, collateral enabled": "152391",
18+
"supply: 1 borrow": "127705",
19+
"supply: second action, same reserve": "135291",
2020
"updateUserDynamicConfig: 1 collateral": "73487",
2121
"updateUserDynamicConfig: 2 collaterals": "87983",
22-
"updateUserRiskPremium: 1 borrow": "94626",
23-
"updateUserRiskPremium: 2 borrows": "103263",
22+
"updateUserRiskPremium: 1 borrow": "94746",
23+
"updateUserRiskPremium: 2 borrows": "103274",
2424
"usingAsCollateral: 0 borrows, enable": "78231",
25-
"usingAsCollateral: 1 borrow, disable": "104897",
26-
"usingAsCollateral: 1 borrow, enable": "118295",
27-
"usingAsCollateral: 2 borrows, disable": "125662",
28-
"usingAsCollateral: 2 borrows, enable": "139060",
29-
"withdraw: 0 borrows, full": "125180",
30-
"withdraw: 0 borrows, partial": "129867",
31-
"withdraw: 1 borrow, partial": "157417",
32-
"withdraw: 2 borrows, partial": "178171",
33-
"withdraw: non collateral": "111386"
25+
"usingAsCollateral: 1 borrow, disable": "105017",
26+
"usingAsCollateral: 1 borrow, enable": "118415",
27+
"usingAsCollateral: 2 borrows, disable": "125782",
28+
"usingAsCollateral: 2 borrows, enable": "139180",
29+
"withdraw: 0 borrows, full": "128210",
30+
"withdraw: 0 borrows, partial": "132901",
31+
"withdraw: 1 borrow, partial": "160550",
32+
"withdraw: 2 borrows, partial": "181304",
33+
"withdraw: non collateral": "111398"
3434
}

snapshots/Spoke.Operations.json

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
{
2-
"borrow: first": "262245",
3-
"borrow: second action, same reserve": "205200",
4-
"liquidationCall (receiveShares): full": "263784",
5-
"liquidationCall (receiveShares): partial": "294057",
6-
"liquidationCall: full": "285806",
7-
"liquidationCall: partial": "298696",
8-
"permitReserve + repay (multicall)": "260757",
9-
"permitReserve + supply (multicall)": "169719",
10-
"permitReserve + supply + enable collateral (multicall)": "324363",
11-
"repay: full": "149297",
12-
"repay: partial": "211252",
2+
"borrow: first": "257816",
3+
"borrow: second action, same reserve": "200788",
4+
"liquidationCall (receiveShares): full": "262664",
5+
"liquidationCall (receiveShares): partial": "290260",
6+
"liquidationCall: full": "287411",
7+
"liquidationCall: partial": "297607",
8+
"permitReserve + repay (multicall)": "256742",
9+
"permitReserve + supply (multicall)": "172653",
10+
"permitReserve + supply + enable collateral (multicall)": "325777",
11+
"repay: full": "152587",
12+
"repay: partial": "210047",
1313
"setUserPositionManagerWithSig: disable": "44896",
1414
"setUserPositionManagerWithSig: enable": "68925",
15-
"supply + enable collateral (multicall)": "181488",
16-
"supply: 0 borrows, collateral disabled": "119885",
17-
"supply: 0 borrows, collateral enabled": "149375",
18-
"supply: 1 borrow": "124792",
19-
"supply: second action, same reserve": "132275",
15+
"supply + enable collateral (multicall)": "184504",
16+
"supply: 0 borrows, collateral disabled": "122716",
17+
"supply: 0 borrows, collateral enabled": "152391",
18+
"supply: 1 borrow": "127705",
19+
"supply: second action, same reserve": "135291",
2020
"updateUserDynamicConfig: 1 collateral": "73487",
2121
"updateUserDynamicConfig: 2 collaterals": "87983",
22-
"updateUserRiskPremium: 1 borrow": "166030",
23-
"updateUserRiskPremium: 2 borrows": "233254",
22+
"updateUserRiskPremium: 1 borrow": "161628",
23+
"updateUserRiskPremium: 2 borrows": "226221",
2424
"usingAsCollateral: 0 borrows, enable": "78231",
25-
"usingAsCollateral: 1 borrow, disable": "157649",
26-
"usingAsCollateral: 1 borrow, enable": "189699",
27-
"usingAsCollateral: 2 borrows, disable": "226349",
28-
"usingAsCollateral: 2 borrows, enable": "277051",
29-
"withdraw: 0 borrows, full": "125180",
30-
"withdraw: 0 borrows, partial": "129867",
31-
"withdraw: 1 borrow, partial": "226318",
32-
"withdraw: 2 borrows, partial": "313658",
33-
"withdraw: non collateral": "111386"
25+
"usingAsCollateral: 1 borrow, disable": "156074",
26+
"usingAsCollateral: 1 borrow, enable": "185297",
27+
"usingAsCollateral: 2 borrows, disable": "223079",
28+
"usingAsCollateral: 2 borrows, enable": "268127",
29+
"withdraw: 0 borrows, full": "128210",
30+
"withdraw: 0 borrows, partial": "132901",
31+
"withdraw: 1 borrow, partial": "224929",
32+
"withdraw: 2 borrows, partial": "307747",
33+
"withdraw: non collateral": "111398"
3434
}

src/hub/Hub.sol

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ contract Hub is IHub, AccessManaged {
2323
using SafeCast for uint256;
2424
using WadRayMath for uint256;
2525
using SharesMath for uint256;
26-
using PercentageMath for uint128;
26+
using PercentageMath for *;
2727
using AssetLogic for Asset;
2828
using MathUtils for *;
2929

@@ -88,10 +88,10 @@ contract Hub is IHub, AccessManaged {
8888
drawnShares: 0,
8989
premiumShares: 0,
9090
premiumOffset: 0,
91-
drawnIndex: drawnIndex.toUint128(),
91+
drawnIndex: drawnIndex.toUint120(),
9292
realizedPremium: 0,
9393
underlying: underlying,
94-
lastUpdateTimestamp: lastUpdateTimestamp.toUint32(),
94+
lastUpdateTimestamp: lastUpdateTimestamp.toUint40(),
9595
decimals: decimals,
9696
drawnRate: drawnRate.toUint96(),
9797
irStrategy: irStrategy,
@@ -209,11 +209,11 @@ contract Hub is IHub, AccessManaged {
209209
asset.accrue();
210210
_validateAdd(asset, spoke, amount, from);
211211

212-
uint128 shares = asset.toAddedSharesDown(amount).toUint128();
212+
uint120 shares = asset.toAddedSharesDown(amount).toUint120();
213213
require(shares > 0, InvalidShares());
214214
asset.addedShares += shares;
215215
spoke.addedShares += shares;
216-
asset.liquidity += amount.toUint128();
216+
asset.liquidity += amount.toUint120();
217217

218218
asset.updateDrawnRate(assetId);
219219

@@ -235,10 +235,10 @@ contract Hub is IHub, AccessManaged {
235235
uint256 liquidity = asset.liquidity;
236236
require(amount <= liquidity, InsufficientLiquidity(liquidity));
237237

238-
uint128 shares = asset.toAddedSharesUp(amount).toUint128();
238+
uint120 shares = asset.toAddedSharesUp(amount).toUint120();
239239
asset.addedShares -= shares;
240240
spoke.addedShares -= shares;
241-
asset.liquidity = liquidity.uncheckedSub(amount).toUint128();
241+
asset.liquidity = liquidity.uncheckedSub(amount).toUint120();
242242

243243
asset.updateDrawnRate(assetId);
244244

@@ -260,10 +260,10 @@ contract Hub is IHub, AccessManaged {
260260
uint256 liquidity = asset.liquidity;
261261
require(amount <= liquidity, InsufficientLiquidity(liquidity));
262262

263-
uint128 drawnShares = asset.toDrawnSharesUp(amount).toUint128();
263+
uint120 drawnShares = asset.toDrawnSharesUp(amount).toUint120();
264264
asset.drawnShares += drawnShares;
265265
spoke.drawnShares += drawnShares;
266-
asset.liquidity = liquidity.uncheckedSub(amount).toUint128();
266+
asset.liquidity = liquidity.uncheckedSub(amount).toUint120();
267267

268268
asset.updateDrawnRate(assetId);
269269

@@ -288,12 +288,12 @@ contract Hub is IHub, AccessManaged {
288288
asset.accrue();
289289
_validateRestore(asset, spoke, drawnAmount, premiumAmount, from);
290290

291-
uint128 drawnShares = asset.toDrawnSharesDown(drawnAmount).toUint128();
291+
uint120 drawnShares = asset.toDrawnSharesDown(drawnAmount).toUint120();
292292
asset.drawnShares -= drawnShares;
293293
spoke.drawnShares -= drawnShares;
294294
_applyPremiumDelta(asset, spoke, premiumDelta, premiumAmount);
295295
uint256 totalAmount = drawnAmount + premiumAmount;
296-
asset.liquidity += totalAmount.toUint128();
296+
asset.liquidity += totalAmount.toUint120();
297297

298298
asset.updateDrawnRate(assetId);
299299

@@ -317,11 +317,11 @@ contract Hub is IHub, AccessManaged {
317317
asset.accrue();
318318
_validateReportDeficit(asset, spoke, drawnAmount, premiumAmount);
319319

320-
uint128 drawnShares = asset.toDrawnSharesDown(drawnAmount).toUint128();
320+
uint120 drawnShares = asset.toDrawnSharesDown(drawnAmount).toUint120();
321321
asset.drawnShares -= drawnShares;
322322
spoke.drawnShares -= drawnShares;
323323
_applyPremiumDelta(asset, spoke, premiumDelta, premiumAmount);
324-
uint128 deficitAmount = (drawnAmount + premiumAmount).toUint128();
324+
uint120 deficitAmount = (drawnAmount + premiumAmount).toUint120();
325325
asset.deficit += deficitAmount;
326326
spoke.deficit += deficitAmount;
327327

@@ -348,11 +348,11 @@ contract Hub is IHub, AccessManaged {
348348
uint256 deficit = coveredSpoke.deficit;
349349
require(amount <= deficit, InvalidAmount());
350350

351-
uint128 shares = asset.toAddedSharesUp(amount).toUint128();
351+
uint120 shares = asset.toAddedSharesUp(amount).toUint120();
352352
asset.addedShares -= shares;
353353
callerSpoke.addedShares -= shares;
354-
asset.deficit -= amount.toUint128();
355-
coveredSpoke.deficit = deficit.uncheckedSub(amount).toUint128();
354+
asset.deficit -= amount.toUint120();
355+
coveredSpoke.deficit = deficit.uncheckedSub(amount).toUint120();
356356

357357
asset.updateDrawnRate(assetId);
358358

@@ -415,8 +415,8 @@ contract Hub is IHub, AccessManaged {
415415
uint256 liquidity = asset.liquidity;
416416
require(amount <= liquidity, InsufficientLiquidity(liquidity));
417417

418-
asset.liquidity = liquidity.uncheckedSub(amount).toUint128();
419-
asset.swept += amount.toUint128();
418+
asset.liquidity = liquidity.uncheckedSub(amount).toUint120();
419+
asset.swept += amount.toUint120();
420420
asset.updateDrawnRate(assetId);
421421

422422
asset.underlying.safeTransfer(msg.sender, amount);
@@ -432,8 +432,8 @@ contract Hub is IHub, AccessManaged {
432432
asset.accrue();
433433
_validateReclaim(asset, msg.sender, amount);
434434

435-
asset.liquidity += amount.toUint128();
436-
asset.swept -= amount.toUint128();
435+
asset.liquidity += amount.toUint120();
436+
asset.swept -= amount.toUint120();
437437
asset.updateDrawnRate(assetId);
438438

439439
asset.underlying.safeTransferFrom(msg.sender, address(this), amount);
@@ -692,8 +692,8 @@ contract Hub is IHub, AccessManaged {
692692
SpokeData storage receiver,
693693
uint256 shares
694694
) internal {
695-
sender.addedShares -= shares.toUint128();
696-
receiver.addedShares += shares.toUint128();
695+
sender.addedShares -= shares.toUint120();
696+
receiver.addedShares += shares.toUint120();
697697
}
698698

699699
/// @dev Applies premium deltas on asset & spoke premium owed.
@@ -737,7 +737,7 @@ contract Hub is IHub, AccessManaged {
737737

738738
function _mintFeeShares(Asset storage asset, uint256 assetId) internal returns (uint256) {
739739
uint256 fees = asset.realizedFees;
740-
uint128 shares = asset.toAddedSharesDown(fees).toUint128();
740+
uint120 shares = asset.toAddedSharesDown(fees).toUint120();
741741
if (shares == 0) {
742742
return 0;
743743
}
@@ -899,7 +899,7 @@ contract Hub is IHub, AccessManaged {
899899
uint256 realizedPremium,
900900
PremiumDelta calldata premium,
901901
uint256 premiumAmount
902-
) internal pure returns (uint128, uint128, uint128) {
902+
) internal pure returns (uint120, uint120, uint120) {
903903
uint256 premiumBefore = premiumShares.rayMulUp(drawnIndex) - premiumOffset;
904904
premiumBefore += realizedPremium;
905905

@@ -911,6 +911,6 @@ contract Hub is IHub, AccessManaged {
911911
premiumAfter += realizedPremium;
912912
// can increase due to precision loss on premium (drawn unchanged)
913913
require(premiumAfter + premiumAmount - premiumBefore <= 2, InvalidPremiumChange());
914-
return (premiumShares.toUint128(), premiumOffset.toUint128(), realizedPremium.toUint128());
914+
return (premiumShares.toUint120(), premiumOffset.toUint120(), realizedPremium.toUint120());
915915
}
916916
}

0 commit comments

Comments
 (0)