Skip to content

Commit 886cbce

Browse files
authored
fix: remove hub convert functions in favor of preview (#925)
1 parent b5c7d12 commit 886cbce

33 files changed

+213
-261
lines changed

snapshots/Hub.Operations.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
2-
"add": "113832",
3-
"draw": "122026",
2+
"add": "113898",
3+
"draw": "122004",
44
"eliminateDeficit: full": "57818",
55
"eliminateDeficit: partial": "67394",
6-
"payFee": "73744",
7-
"refreshPremium": "96823",
6+
"payFee": "73722",
7+
"refreshPremium": "96801",
88
"remove: full": "75059",
99
"remove: partial": "80306",
10-
"reportDeficit": "118722",
11-
"restore: full": "97460",
12-
"restore: partial": "110890",
13-
"transferShares": "79425"
10+
"reportDeficit": "118767",
11+
"restore: full": "97438",
12+
"restore: partial": "110868",
13+
"transferShares": "79403"
1414
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"borrowNative": "226536",
3-
"repayNative": "241556",
4-
"supplyAsCollateralNative": "155884",
5-
"supplyNative": "132139",
2+
"borrowNative": "226404",
3+
"repayNative": "241336",
4+
"supplyAsCollateralNative": "155937",
5+
"supplyNative": "132192",
66
"withdrawNative: full": "129987",
77
"withdrawNative: partial": "142283"
88
}

snapshots/SignatureGateway.Operations.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"borrowWithSig": "214872",
3-
"repayWithSig": "242881",
2+
"borrowWithSig": "214740",
3+
"repayWithSig": "242661",
44
"setSelfAsUserPositionManagerWithSig": "75492",
55
"setUsingAsCollateralWithSig": "85259",
6-
"supplyWithSig": "148960",
6+
"supplyWithSig": "149013",
77
"updateUserDynamicConfigWithSig": "62980",
88
"updateUserRiskPremiumWithSig": "61661",
99
"withdrawWithSig": "136147"

snapshots/Spoke.Getters.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"getUserAccountData: supplies: 0, borrows: 0": "12142",
33
"getUserAccountData: supplies: 1, borrows: 0": "47232",
44
"getUserAccountData: supplies: 2, borrows: 0": "77437",
5-
"getUserAccountData: supplies: 2, borrows: 1": "98178",
6-
"getUserAccountData: supplies: 2, borrows: 2": "117573"
5+
"getUserAccountData: supplies: 2, borrows: 1": "98134",
6+
"getUserAccountData: supplies: 2, borrows: 2": "117485"
77
}
Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
{
2-
"borrow: first": "199032",
3-
"borrow: second action, same reserve": "179553",
4-
"liquidationCall (receiveShares): full": "263815",
5-
"liquidationCall (receiveShares): partial": "305858",
6-
"liquidationCall: full": "291754",
7-
"liquidationCall: partial": "315848",
8-
"permitReserve + repay (multicall)": "235861",
9-
"permitReserve + supply (multicall)": "141007",
10-
"permitReserve + supply + enable collateral (multicall)": "176310",
11-
"repay: full": "151846",
12-
"repay: partial": "176309",
2+
"borrow: first": "198966",
3+
"borrow: second action, same reserve": "179487",
4+
"liquidationCall (receiveShares): full": "263639",
5+
"liquidationCall (receiveShares): partial": "305682",
6+
"liquidationCall: full": "291578",
7+
"liquidationCall: partial": "315672",
8+
"permitReserve + repay (multicall)": "235751",
9+
"permitReserve + supply (multicall)": "141073",
10+
"permitReserve + supply + enable collateral (multicall)": "176376",
11+
"repay: full": "151780",
12+
"repay: partial": "176199",
1313
"setUserPositionManagerWithSig: disable": "44896",
1414
"setUserPositionManagerWithSig: enable": "68925",
15-
"supply + enable collateral (multicall)": "154251",
16-
"supply: 0 borrows, collateral disabled": "115762",
17-
"supply: 0 borrows, collateral enabled": "120159",
18-
"supply: 1 borrow": "120151",
19-
"supply: second action, same reserve": "103059",
15+
"supply + enable collateral (multicall)": "154317",
16+
"supply: 0 borrows, collateral disabled": "115828",
17+
"supply: 0 borrows, collateral enabled": "120225",
18+
"supply: 1 borrow": "120217",
19+
"supply: second action, same reserve": "103125",
2020
"updateUserDynamicConfig: 1 collateral": "73827",
2121
"updateUserDynamicConfig: 2 collaterals": "88687",
22-
"updateUserRiskPremium: 1 borrow": "95254",
23-
"updateUserRiskPremium: 2 borrows": "111346",
22+
"updateUserRiskPremium: 1 borrow": "95210",
23+
"updateUserRiskPremium: 2 borrows": "111258",
2424
"usingAsCollateral: 0 borrows, enable": "59042",
25-
"usingAsCollateral: 1 borrow, disable": "105589",
25+
"usingAsCollateral: 1 borrow, disable": "105545",
2626
"usingAsCollateral: 1 borrow, enable": "32364",
27-
"usingAsCollateral: 2 borrows, disable": "128054",
27+
"usingAsCollateral: 2 borrows, disable": "127966",
2828
"usingAsCollateral: 2 borrows, enable": "41942",
2929
"withdraw: 0 borrows, full": "127742",
3030
"withdraw: 0 borrows, partial": "132783",
31-
"withdraw: 1 borrow, partial": "161765",
32-
"withdraw: 2 borrows, partial": "184218",
31+
"withdraw: 1 borrow, partial": "161721",
32+
"withdraw: 2 borrows, partial": "184130",
3333
"withdraw: non collateral": "126851"
3434
}

snapshots/Spoke.Operations.json

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
{
2-
"borrow: first": "269201",
3-
"borrow: second action, same reserve": "212722",
4-
"liquidationCall (receiveShares): full": "297201",
5-
"liquidationCall (receiveShares): partial": "339244",
6-
"liquidationCall: full": "325140",
7-
"liquidationCall: partial": "349234",
8-
"permitReserve + repay (multicall)": "269240",
9-
"permitReserve + supply (multicall)": "141007",
10-
"permitReserve + supply + enable collateral (multicall)": "176310",
11-
"repay: full": "146487",
12-
"repay: partial": "209688",
2+
"borrow: first": "269069",
3+
"borrow: second action, same reserve": "212590",
4+
"liquidationCall (receiveShares): full": "296959",
5+
"liquidationCall (receiveShares): partial": "339002",
6+
"liquidationCall: full": "324898",
7+
"liquidationCall: partial": "348992",
8+
"permitReserve + repay (multicall)": "269064",
9+
"permitReserve + supply (multicall)": "141073",
10+
"permitReserve + supply + enable collateral (multicall)": "176376",
11+
"repay: full": "146421",
12+
"repay: partial": "209512",
1313
"setUserPositionManagerWithSig: disable": "44896",
1414
"setUserPositionManagerWithSig: enable": "68925",
15-
"supply + enable collateral (multicall)": "154251",
16-
"supply: 0 borrows, collateral disabled": "115762",
17-
"supply: 0 borrows, collateral enabled": "120159",
18-
"supply: 1 borrow": "120151",
19-
"supply: second action, same reserve": "103059",
15+
"supply + enable collateral (multicall)": "154317",
16+
"supply: 0 borrows, collateral disabled": "115828",
17+
"supply: 0 borrows, collateral enabled": "120225",
18+
"supply: 1 borrow": "120217",
19+
"supply: second action, same reserve": "103125",
2020
"updateUserDynamicConfig: 1 collateral": "73827",
2121
"updateUserDynamicConfig: 2 collaterals": "88687",
22-
"updateUserRiskPremium: 1 borrow": "177341",
23-
"updateUserRiskPremium: 2 borrows": "262706",
22+
"updateUserRiskPremium: 1 borrow": "177231",
23+
"updateUserRiskPremium: 2 borrows": "262486",
2424
"usingAsCollateral: 0 borrows, enable": "59042",
25-
"usingAsCollateral: 1 borrow, disable": "187676",
25+
"usingAsCollateral: 1 borrow, disable": "187566",
2626
"usingAsCollateral: 1 borrow, enable": "32364",
27-
"usingAsCollateral: 2 borrows, disable": "287413",
27+
"usingAsCollateral: 2 borrows, disable": "287193",
2828
"usingAsCollateral: 2 borrows, enable": "41942",
2929
"withdraw: 0 borrows, full": "127742",
3030
"withdraw: 0 borrows, partial": "132783",
31-
"withdraw: 1 borrow, partial": "241352",
32-
"withdraw: 2 borrows, partial": "341078",
31+
"withdraw: 1 borrow, partial": "241242",
32+
"withdraw: 2 borrows, partial": "340858",
3333
"withdraw: non collateral": "126851"
3434
}

src/hub/Hub.sol

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -467,26 +467,6 @@ contract Hub is IHub, AccessManaged {
467467
return _assets[assetId].toDrawnAssetsUp(shares);
468468
}
469469

470-
/// @inheritdoc IHub
471-
function convertToAddedAssets(uint256 assetId, uint256 shares) external view returns (uint256) {
472-
return _assets[assetId].toAddedAssetsDown(shares);
473-
}
474-
475-
/// @inheritdoc IHub
476-
function convertToAddedShares(uint256 assetId, uint256 assets) external view returns (uint256) {
477-
return _assets[assetId].toAddedSharesDown(assets);
478-
}
479-
480-
/// @inheritdoc IHub
481-
function convertToDrawnAssets(uint256 assetId, uint256 shares) external view returns (uint256) {
482-
return _assets[assetId].toDrawnAssetsUp(shares);
483-
}
484-
485-
/// @inheritdoc IHub
486-
function convertToDrawnShares(uint256 assetId, uint256 assets) external view returns (uint256) {
487-
return _assets[assetId].toDrawnSharesUp(assets);
488-
}
489-
490470
/// @inheritdoc IHubBase
491471
function getAssetUnderlyingAndDecimals(uint256 assetId) external view returns (address, uint8) {
492472
Asset storage asset = _assets[assetId];

src/hub/interfaces/IHub.sol

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -280,34 +280,6 @@ interface IHub is IHubBase, IAccessManaged {
280280
/// @return The number of listed assets.
281281
function getAssetCount() external view returns (uint256);
282282

283-
/// @notice Converts the given amount of supplied shares to assets amount for the specified asset.
284-
/// @dev Rounds down to the nearest assets amount.
285-
/// @param assetId The identifier of the asset.
286-
/// @param shares The amount of supplied shares to convert to assets amount.
287-
/// @return The amount of supplied assets converted from shares amount.
288-
function convertToAddedAssets(uint256 assetId, uint256 shares) external view returns (uint256);
289-
290-
/// @notice Converts the given amount of supplied assets to shares amount for the specified asset.
291-
/// @dev Rounds down to the nearest shares amount.
292-
/// @param assetId The identifier of the asset.
293-
/// @param assets The amount of supplied assets to convert to shares amount.
294-
/// @return The amount of supplied shares converted from assets amount.
295-
function convertToAddedShares(uint256 assetId, uint256 assets) external view returns (uint256);
296-
297-
/// @notice Converts the given amount of drawn shares to assets amount for the specified asset.
298-
/// @dev Rounds up to the nearest assets amount.
299-
/// @param assetId The identifier of the asset.
300-
/// @param shares The amount of drawn shares to convert to assets amount.
301-
/// @return The amount of drawn assets converted from shares amount.
302-
function convertToDrawnAssets(uint256 assetId, uint256 shares) external view returns (uint256);
303-
304-
/// @notice Converts the specified amount of drawn assets to shares amount.
305-
/// @dev Rounds up to the nearest shares amount.
306-
/// @param assetId The identifier of the asset.
307-
/// @param assets The amount of drawn assets to convert to shares amount.
308-
/// @return The amount of drawn shares converted from assets amount.
309-
function convertToDrawnShares(uint256 assetId, uint256 assets) external view returns (uint256);
310-
311283
/// @notice Returns information regarding the specified asset.
312284
/// @dev `drawnIndex`, `drawnRate` and `lastUpdateTimestamp` can be outdated due to passage of time.
313285
/// @param assetId The identifier of the asset.

tests/Base.t.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,11 +1343,11 @@ abstract contract Base is Test {
13431343
}
13441344

13451345
function getAddExRate(uint256 assetId) internal view returns (uint256) {
1346-
return hub1.convertToAddedAssets(assetId, MAX_SUPPLY_AMOUNT);
1346+
return hub1.previewRemoveByShares(assetId, MAX_SUPPLY_AMOUNT);
13471347
}
13481348

13491349
function getDebtExRate(uint256 assetId) internal view returns (uint256) {
1350-
return hub1.convertToDrawnAssets(assetId, MAX_SUPPLY_AMOUNT);
1350+
return hub1.previewRestoreByShares(assetId, MAX_SUPPLY_AMOUNT);
13511351
}
13521352

13531353
function getDeficit(IHub hub, uint256 assetId) internal view returns (uint256) {
@@ -1487,7 +1487,7 @@ abstract contract Base is Test {
14871487
uint256 expectedSuppliedAmount,
14881488
string memory label
14891489
) internal view {
1490-
uint256 expectedSuppliedShares = hub1.convertToAddedShares(assetId, expectedSuppliedAmount);
1490+
uint256 expectedSuppliedShares = hub1.previewAddByAssets(assetId, expectedSuppliedAmount);
14911491
assertEq(
14921492
hub1.getAddedShares(assetId),
14931493
expectedSuppliedShares,

tests/unit/Hub/Hub.Add.t.sol

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ contract HubAddTest is HubBase {
167167
drawAmount: drawAmount,
168168
skipTime: skipTime
169169
});
170-
vm.assume(hub1.convertToAddedShares(daiAssetId, daiAmount) < daiAmount);
170+
vm.assume(hub1.previewAddByAssets(daiAssetId, daiAmount) < daiAmount);
171171

172172
uint256 addAmount = hub1.previewAddByShares(daiAssetId, 1);
173173
vm.expectRevert(abi.encodeWithSelector(IHub.AddCapExceeded.selector, newAddCap));
@@ -318,7 +318,7 @@ contract HubAddTest is HubBase {
318318
// asset1
319319
assertEq(
320320
hub1.getAddedShares(assetId),
321-
hub1.convertToAddedShares(assetId, amount),
321+
hub1.previewAddByAssets(assetId, amount),
322322
'asset addedShares after'
323323
);
324324
assertEq(hub1.getAddedAssets(assetId), amount, 'asset addedAmount after');
@@ -330,7 +330,7 @@ contract HubAddTest is HubBase {
330330
);
331331
assertEq(
332332
hub1.getSpokeAddedShares(assetId, address(spoke1)),
333-
hub1.convertToAddedShares(assetId, amount),
333+
hub1.previewAddByAssets(assetId, amount),
334334
'spoke1 addedShares after'
335335
);
336336
assertEq(
@@ -344,7 +344,7 @@ contract HubAddTest is HubBase {
344344
// asset2
345345
assertEq(
346346
hub1.getAddedShares(assetId2),
347-
hub1.convertToAddedShares(assetId2, amount2),
347+
hub1.previewAddByAssets(assetId2, amount2),
348348
'asset2 addedShares after'
349349
);
350350
assertEq(hub1.getAssetLiquidity(assetId2), amount2, 'asset2 liquidity after');
@@ -355,7 +355,7 @@ contract HubAddTest is HubBase {
355355
);
356356
assertEq(
357357
hub1.getSpokeAddedShares(assetId2, address(spoke2)),
358-
hub1.convertToAddedShares(assetId2, amount2),
358+
hub1.previewAddByAssets(assetId2, amount2),
359359
'spoke2 addedShares after'
360360
);
361361
assertEq(
@@ -397,11 +397,11 @@ contract HubAddTest is HubBase {
397397
drawAmount: drawAmount,
398398
skipTime: 365 days * 10
399399
});
400-
assertLt(hub1.convertToAddedShares(daiAssetId, daiAmount), daiAmount); // index increased
400+
assertLt(hub1.previewAddByAssets(daiAssetId, daiAmount), daiAmount); // index increased
401401

402402
// add < 1 share
403403
uint256 amount = 1;
404-
assertTrue(hub1.convertToAddedShares(daiAssetId, amount) == 0);
404+
assertTrue(hub1.previewAddByAssets(daiAssetId, amount) == 0);
405405

406406
vm.expectRevert(IHub.InvalidShares.selector);
407407
vm.prank(address(spoke1));
@@ -428,7 +428,7 @@ contract HubAddTest is HubBase {
428428
skipTime: skipTime
429429
});
430430

431-
uint256 minAllowedAddedAmount = hub1.convertToAddedAssets(daiAssetId, 1);
431+
uint256 minAllowedAddedAmount = hub1.previewRemoveByShares(daiAssetId, 1);
432432
// 1 share converts to > 1 amount
433433
vm.assume(minAllowedAddedAmount > 1);
434434

@@ -465,7 +465,7 @@ contract HubAddTest is HubBase {
465465
assertEq(premium, 0); // zero premium debt
466466

467467
uint256 addAmount = 10e18; // this can be 0
468-
uint256 shares = hub1.convertToAddedShares(daiAssetId, addAmount);
468+
uint256 shares = hub1.previewAddByAssets(daiAssetId, addAmount);
469469
assertLt(shares, addAmount); // index increased, exch rate > 1
470470

471471
uint256 spokeAddedSharesBefore = hub1.getSpokeAddedShares(daiAssetId, address(spoke2));
@@ -524,10 +524,10 @@ contract HubAddTest is HubBase {
524524
uint256 daiAmount = 100e18;
525525
_addLiquidity(daiAssetId, daiAmount);
526526
_drawLiquidity(daiAssetId, daiAmount, true);
527-
assertLt(hub1.convertToAddedShares(daiAssetId, daiAmount), daiAmount); // index increased, exch rate > 1
527+
assertLt(hub1.previewAddByAssets(daiAssetId, daiAmount), daiAmount); // index increased, exch rate > 1
528528

529529
uint256 addAmount = 10e18;
530-
uint256 expectedAddedShares = hub1.convertToAddedShares(daiAssetId, addAmount);
530+
uint256 expectedAddedShares = hub1.previewAddByAssets(daiAssetId, addAmount);
531531

532532
uint256 addedAssetsBefore = hub1.getSpokeAddedAssets(daiAssetId, address(spoke2));
533533
uint256 addedSharesBefore = hub1.getSpokeAddedShares(daiAssetId, address(spoke2));
@@ -691,13 +691,13 @@ contract HubAddTest is HubBase {
691691
drawAmount: amount,
692692
skipTime: skipTime
693693
});
694-
vm.assume(hub1.convertToAddedShares(assetId, amount) < amount);
694+
vm.assume(hub1.previewAddByAssets(assetId, amount) < amount);
695695

696696
params.drawnAmount = amount;
697-
params.assetAddedAmount = hub1.convertToAddedAssets(assetId, params.assetAddedShares);
697+
params.assetAddedAmount = hub1.previewRemoveByShares(assetId, params.assetAddedShares);
698698
params.availableLiq = amount - params.drawnAmount;
699699
params.spoke2AddedShares = hub1.getSpokeAddedShares(assetId, address(spoke2));
700-
params.spoke2AddedAmount = hub1.convertToAddedAssets(assetId, params.spoke2AddedShares);
700+
params.spoke2AddedAmount = hub1.previewRemoveByShares(assetId, params.spoke2AddedShares);
701701
params.aliceBalance = MAX_SUPPLY_AMOUNT + params.drawnAmount;
702702
params.bobBalance = MAX_SUPPLY_AMOUNT - amount;
703703

@@ -722,9 +722,9 @@ contract HubAddTest is HubBase {
722722

723723
params.availableLiq += addAmount;
724724
params.assetAddedShares += addShares;
725-
params.assetAddedAmount = hub1.convertToAddedAssets(assetId, params.assetAddedShares);
725+
params.assetAddedAmount = hub1.previewRemoveByShares(assetId, params.assetAddedShares);
726726
params.spoke1AddedShares += addShares;
727-
params.spoke1AddedAmount = hub1.convertToAddedAssets(assetId, params.spoke1AddedShares);
727+
params.spoke1AddedAmount = hub1.previewRemoveByShares(assetId, params.spoke1AddedShares);
728728
params.bobBalance -= addAmount;
729729

730730
// hub
@@ -739,7 +739,7 @@ contract HubAddTest is HubBase {
739739
// spoke1
740740
assertEq(
741741
hub1.getSpokeAddedAssets(assetId, address(spoke1)),
742-
hub1.convertToAddedAssets(assetId, params.spoke1AddedShares),
742+
hub1.previewRemoveByShares(assetId, params.spoke1AddedShares),
743743
'spoke1 addedAmount after'
744744
);
745745
assertEq(
@@ -750,7 +750,7 @@ contract HubAddTest is HubBase {
750750
// spoke2
751751
assertEq(
752752
hub1.getSpokeAddedAssets(assetId, address(spoke2)),
753-
hub1.convertToAddedAssets(assetId, params.spoke2AddedShares),
753+
hub1.previewRemoveByShares(assetId, params.spoke2AddedShares),
754754
'spoke2 addedAmount after'
755755
);
756756
assertEq(

0 commit comments

Comments
 (0)