From f52428d26b7321344e9724eccc0c9bceba193322 Mon Sep 17 00:00:00 2001 From: Parv Date: Tue, 12 Apr 2022 16:27:58 +0530 Subject: [PATCH 1/4] perf(BancorCurve): optimisation - BancorCurve deployment cost reduced by 0.8% - avg burn cost reduced by 3.2% - max mint cost INCREASED by 0.00159075% --- contracts/curves/BancorCurve.sol | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/contracts/curves/BancorCurve.sol b/contracts/curves/BancorCurve.sol index 30c5cedf..513a20ce 100644 --- a/contracts/curves/BancorCurve.sol +++ b/contracts/curves/BancorCurve.sol @@ -26,6 +26,11 @@ contract BancorCurve is ICurve { // NOTE: keys are their respective hubId mapping(uint256 => CurveInfo) private _curves; + modifier onlyHub() { + require(msg.sender == hub, "!hub"); + _; + } + constructor(address _hub) { require(_hub != address(0), "!hub"); hub = _hub; @@ -35,8 +40,8 @@ contract BancorCurve is ICurve { function register(uint256 hubId, bytes calldata encodedCurveInfo) external override + onlyHub { - require(msg.sender == hub, "!hub"); require(encodedCurveInfo.length > 0, "!encodedCurveInfo"); (uint256 baseY, uint32 reserveWeight) = abi.decode( @@ -58,9 +63,8 @@ contract BancorCurve is ICurve { function initReconfigure(uint256 hubId, bytes calldata encodedCurveInfo) external override + onlyHub { - require(msg.sender == hub, "!hub"); - uint32 targetReserveWeight = abi.decode(encodedCurveInfo, (uint32)); CurveInfo storage curveInfo = _curves[hubId]; @@ -71,15 +75,17 @@ contract BancorCurve is ICurve { ); // targetBaseX = (old baseY * oldR) / newR - uint256 targetBaseY = (curveInfo.baseY * curveInfo.reserveWeight) / + // uint256 targetBaseY = (curveInfo.baseY * curveInfo.reserveWeight) / + // targetReserveWeight; + + curveInfo.targetBaseY = + (curveInfo.baseY * curveInfo.reserveWeight) / targetReserveWeight; - curveInfo.targetBaseY = targetBaseY; curveInfo.targetReserveWeight = targetReserveWeight; } /// @inheritdoc ICurve - function finishReconfigure(uint256 hubId) external override { - require(msg.sender == hub, "!hub"); + function finishReconfigure(uint256 hubId) external override onlyHub { CurveInfo storage curveInfo = _curves[hubId]; curveInfo.reserveWeight = curveInfo.targetReserveWeight; curveInfo.baseY = curveInfo.targetBaseY; @@ -143,6 +149,7 @@ contract BancorCurve is ICurve { uint256 balancePooled ) external view override returns (uint256 meTokensMinted) { CurveInfo memory curveInfo = _curves[hubId]; + if (supply > 0) { meTokensMinted = _viewMeTokensMinted( assetsDeposited, @@ -166,10 +173,9 @@ contract BancorCurve is ICurve { uint256 supply, uint256 balancePooled ) external view override returns (uint256 assetsReturned) { - CurveInfo memory curveInfo = _curves[hubId]; assetsReturned = _viewAssetsReturned( meTokensBurned, - curveInfo.reserveWeight, + _curves[hubId].reserveWeight, supply, balancePooled ); @@ -182,10 +188,9 @@ contract BancorCurve is ICurve { uint256 supply, uint256 balancePooled ) external view override returns (uint256 assetsReturned) { - CurveInfo memory curveInfo = _curves[hubId]; assetsReturned = _viewAssetsReturned( meTokensBurned, - curveInfo.targetReserveWeight, + _curves[hubId].targetReserveWeight, supply, balancePooled ); From 7c8471454dad5acb6bf4501bd61a5dc87603fcff Mon Sep 17 00:00:00 2001 From: Parv Date: Tue, 12 Apr 2022 16:28:35 +0530 Subject: [PATCH 2/4] perf: minor optimisation by removing single time sender variable --- contracts/facets/HubFacet.sol | 12 ++++++------ contracts/facets/MeTokenRegistryFacet.sol | 10 ++++------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/contracts/facets/HubFacet.sol b/contracts/facets/HubFacet.sol index 8ef92670..1337440e 100644 --- a/contracts/facets/HubFacet.sol +++ b/contracts/facets/HubFacet.sol @@ -77,8 +77,8 @@ contract HubFacet is IHubFacet, Modifiers { bytes memory encodedCurveInfo ) external override { HubInfo storage hubInfo = s.hubs[id]; - address sender = LibMeta.msgSender(); - require(sender == hubInfo.owner, "!owner"); + + require(LibMeta.msgSender() == hubInfo.owner, "!owner"); if (hubInfo.updating && block.timestamp > hubInfo.endTime) { LibHub.finishUpdate(id); } @@ -149,8 +149,8 @@ contract HubFacet is IHubFacet, Modifiers { /// @inheritdoc IHubFacet function cancelUpdate(uint256 id) external override { HubInfo storage hubInfo = s.hubs[id]; - address sender = LibMeta.msgSender(); - require(sender == hubInfo.owner, "!owner"); + + require(LibMeta.msgSender() == hubInfo.owner, "!owner"); require(hubInfo.updating, "!updating"); require(block.timestamp < hubInfo.startTime, "Update has started"); @@ -171,8 +171,8 @@ contract HubFacet is IHubFacet, Modifiers { override { HubInfo storage hubInfo = s.hubs[id]; - address sender = LibMeta.msgSender(); - require(sender == hubInfo.owner, "!owner"); + + require(LibMeta.msgSender() == hubInfo.owner, "!owner"); require(newOwner != hubInfo.owner, "Same owner"); hubInfo.owner = newOwner; diff --git a/contracts/facets/MeTokenRegistryFacet.sol b/contracts/facets/MeTokenRegistryFacet.sol index 78f55978..befb8c76 100644 --- a/contracts/facets/MeTokenRegistryFacet.sol +++ b/contracts/facets/MeTokenRegistryFacet.sol @@ -95,12 +95,11 @@ contract MeTokenRegistryFacet is address migration, bytes memory encodedMigrationArgs ) external override { - address sender = LibMeta.msgSender(); MeTokenInfo storage meTokenInfo = s.meTokens[meToken]; HubInfo memory hubInfo = s.hubs[meTokenInfo.hubId]; HubInfo memory targetHubInfo = s.hubs[targetHubId]; - require(sender == meTokenInfo.owner, "!owner"); + require(LibMeta.msgSender() == meTokenInfo.owner, "!owner"); require( block.timestamp >= meTokenInfo.endCooldown, "Cooldown not complete" @@ -150,9 +149,8 @@ contract MeTokenRegistryFacet is /// @inheritdoc IMeTokenRegistryFacet function cancelResubscribe(address meToken) external override { - address sender = LibMeta.msgSender(); MeTokenInfo storage meTokenInfo = s.meTokens[meToken]; - require(sender == meTokenInfo.owner, "!owner"); + require(LibMeta.msgSender() == meTokenInfo.owner, "!owner"); require(meTokenInfo.targetHubId != 0, "!resubscribing"); require( !IMigration(meTokenInfo.migration).migrationStarted(meToken), @@ -182,8 +180,8 @@ contract MeTokenRegistryFacet is override { MeTokenInfo storage meTokenInfo = s.meTokens[meToken]; - address sender = LibMeta.msgSender(); - require(sender == meTokenInfo.migration, "!migration"); + + require(LibMeta.msgSender() == meTokenInfo.migration, "!migration"); uint256 balancePooled = meTokenInfo.balancePooled; uint256 balanceLocked = meTokenInfo.balanceLocked; uint256 oldBalance = balancePooled + balanceLocked; From 27ddd8b9984bc2a208f392c3ec225b592729cddd Mon Sep 17 00:00:00 2001 From: Parv Date: Tue, 12 Apr 2022 18:04:38 +0530 Subject: [PATCH 3/4] perf: remove `finishUpdate` return - HubFacet deployment gas: 3.27373% decrease - FoundryFacet deployment gas: 2.84026% decrease - finishUpdate() call cost: 23.5681% decrease - call cost decreased for other functions as well --- contracts/facets/FoundryFacet.sol | 4 ++-- contracts/libs/LibHub.sol | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/contracts/facets/FoundryFacet.sol b/contracts/facets/FoundryFacet.sol index e3d18563..ead6412e 100644 --- a/contracts/facets/FoundryFacet.sol +++ b/contracts/facets/FoundryFacet.sol @@ -148,7 +148,7 @@ contract FoundryFacet is IFoundryFacet, Modifiers { // Handling changes if (hubInfo.updating && block.timestamp > hubInfo.endTime) { - hubInfo = LibHub.finishUpdate(meTokenInfo.hubId); + LibHub.finishUpdate(meTokenInfo.hubId); } else if (meTokenInfo.targetHubId != 0) { if (block.timestamp > meTokenInfo.endTime) { hubInfo = s.hubs[meTokenInfo.targetHubId]; @@ -263,7 +263,7 @@ contract FoundryFacet is IFoundryFacet, Modifiers { // Handling changes if (hubInfo.updating && block.timestamp > hubInfo.endTime) { - hubInfo = LibHub.finishUpdate(meTokenInfo.hubId); + LibHub.finishUpdate(meTokenInfo.hubId); } else if (meTokenInfo.targetHubId != 0) { if (block.timestamp > meTokenInfo.endTime) { hubInfo = s.hubs[meTokenInfo.targetHubId]; diff --git a/contracts/libs/LibHub.sol b/contracts/libs/LibHub.sol index 19f90bb4..f4f4222f 100644 --- a/contracts/libs/LibHub.sol +++ b/contracts/libs/LibHub.sol @@ -26,7 +26,7 @@ struct HubInfo { library LibHub { event FinishUpdate(uint256 id); - function finishUpdate(uint256 id) internal returns (HubInfo memory) { + function finishUpdate(uint256 id) internal { AppStorage storage s = LibAppStorage.diamondStorage(); HubInfo storage hubInfo = s.hubs[id]; require(block.timestamp > hubInfo.endTime, "Still updating"); @@ -50,7 +50,6 @@ library LibHub { s.hubs[id].endTime = 0; emit FinishUpdate(id); - return hubInfo; } function getHubInfo(uint256 id) From a208c2827df38cb6e117efb976d4ca16b8848834 Mon Sep 17 00:00:00 2001 From: Parv Date: Tue, 12 Apr 2022 21:50:05 +0530 Subject: [PATCH 4/4] perf: slight gas improvements --- contracts/libs/LibHub.sol | 49 ++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/contracts/libs/LibHub.sol b/contracts/libs/LibHub.sol index f4f4222f..7383d815 100644 --- a/contracts/libs/LibHub.sol +++ b/contracts/libs/LibHub.sol @@ -27,27 +27,27 @@ library LibHub { event FinishUpdate(uint256 id); function finishUpdate(uint256 id) internal { - AppStorage storage s = LibAppStorage.diamondStorage(); - HubInfo storage hubInfo = s.hubs[id]; + HubInfo storage hubInfo = LibAppStorage.diamondStorage().hubs[id]; + require(block.timestamp > hubInfo.endTime, "Still updating"); if (hubInfo.targetRefundRatio != 0) { - s.hubs[id].refundRatio = hubInfo.targetRefundRatio; - s.hubs[id].targetRefundRatio = 0; + hubInfo.refundRatio = hubInfo.targetRefundRatio; + hubInfo.targetRefundRatio = 0; } if (hubInfo.reconfigure) { ICurve(hubInfo.curve).finishReconfigure(id); - s.hubs[id].reconfigure = false; + hubInfo.reconfigure = false; } if (hubInfo.targetCurve != address(0)) { - s.hubs[id].curve = hubInfo.targetCurve; - s.hubs[id].targetCurve = address(0); + hubInfo.curve = hubInfo.targetCurve; + hubInfo.targetCurve = address(0); } - s.hubs[id].updating = false; - s.hubs[id].startTime = 0; - s.hubs[id].endTime = 0; + hubInfo.updating = false; + hubInfo.startTime = 0; + hubInfo.endTime = 0; emit FinishUpdate(id); } @@ -57,20 +57,21 @@ library LibHub { view returns (HubInfo memory hubInfo) { - AppStorage storage s = LibAppStorage.diamondStorage(); - hubInfo.active = s.hubs[id].active; - hubInfo.owner = s.hubs[id].owner; - hubInfo.vault = s.hubs[id].vault; - hubInfo.asset = s.hubs[id].asset; - hubInfo.curve = s.hubs[id].curve; - hubInfo.refundRatio = s.hubs[id].refundRatio; - hubInfo.updating = s.hubs[id].updating; - hubInfo.startTime = s.hubs[id].startTime; - hubInfo.endTime = s.hubs[id].endTime; - hubInfo.endCooldown = s.hubs[id].endCooldown; - hubInfo.reconfigure = s.hubs[id].reconfigure; - hubInfo.targetCurve = s.hubs[id].targetCurve; - hubInfo.targetRefundRatio = s.hubs[id].targetRefundRatio; + HubInfo storage sHubInfo = LibAppStorage.diamondStorage().hubs[id]; + + hubInfo.active = sHubInfo.active; + hubInfo.owner = sHubInfo.owner; + hubInfo.vault = sHubInfo.vault; + hubInfo.asset = sHubInfo.asset; + hubInfo.curve = sHubInfo.curve; + hubInfo.refundRatio = sHubInfo.refundRatio; + hubInfo.updating = sHubInfo.updating; + hubInfo.startTime = sHubInfo.startTime; + hubInfo.endTime = sHubInfo.endTime; + hubInfo.endCooldown = sHubInfo.endCooldown; + hubInfo.reconfigure = sHubInfo.reconfigure; + hubInfo.targetCurve = sHubInfo.targetCurve; + hubInfo.targetRefundRatio = sHubInfo.targetRefundRatio; } function count() internal view returns (uint256) {