Skip to content

Commit

Permalink
Merge pull request #149 from meTokens/perf/optimise
Browse files Browse the repository at this point in the history
Perf/optimise
  • Loading branch information
zgorizzo69 authored Apr 14, 2022
2 parents eadac7d + a208c28 commit be8ab54
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 51 deletions.
27 changes: 16 additions & 11 deletions contracts/curves/BancorCurve.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand All @@ -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];

Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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
);
Expand All @@ -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
);
Expand Down
4 changes: 2 additions & 2 deletions contracts/facets/FoundryFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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];
Expand Down
12 changes: 6 additions & 6 deletions contracts/facets/HubFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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");

Expand All @@ -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;

Expand Down
10 changes: 4 additions & 6 deletions contracts/facets/MeTokenRegistryFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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;
Expand Down
52 changes: 26 additions & 26 deletions contracts/libs/LibHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,52 +26,52 @@ struct HubInfo {
library LibHub {
event FinishUpdate(uint256 id);

function finishUpdate(uint256 id) internal returns (HubInfo memory) {
AppStorage storage s = LibAppStorage.diamondStorage();
HubInfo storage hubInfo = s.hubs[id];
function finishUpdate(uint256 id) internal {
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);
return hubInfo;
}

function getHubInfo(uint256 id)
internal
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) {
Expand Down

0 comments on commit be8ab54

Please sign in to comment.