Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Some minor gas optimizations #93

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Governance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -468,16 +468,18 @@ contract Governance is Multicall, UserProxyFactory, ReentrancyGuard, Ownable, IG
InitiativeVoteSnapshot memory _votesForInitiativeSnapshot,
InitiativeState memory _initiativeState
) public view returns (InitiativeStatus status, uint16 lastEpochClaim, uint256 claimableAmount) {
uint16 initiativeRegistrationEpoch = registeredInitiatives[_initiative];

// == Non existent Condition == //
if (registeredInitiatives[_initiative] == 0) {
if (initiativeRegistrationEpoch == 0) {
return (InitiativeStatus.NONEXISTENT, 0, 0);
/// By definition it has zero rewards
}

uint16 currentEpoch = epoch();

// == Just Registered Condition == //
if (registeredInitiatives[_initiative] == currentEpoch) {
if (initiativeRegistrationEpoch == currentEpoch) {
return (InitiativeStatus.WARM_UP, 0, 0);
/// Was registered this week, cannot have rewards
}
Expand All @@ -486,7 +488,7 @@ contract Governance is Multicall, UserProxyFactory, ReentrancyGuard, Ownable, IG
lastEpochClaim = initiativeStates[_initiative].lastEpochClaim;

// == Disabled Condition == //
if (registeredInitiatives[_initiative] == UNREGISTERED_INITIATIVE) {
if (initiativeRegistrationEpoch == UNREGISTERED_INITIATIVE) {
return (InitiativeStatus.DISABLED, lastEpochClaim, 0);
/// By definition it has zero rewards
}
Expand Down Expand Up @@ -877,8 +879,6 @@ contract Governance is Multicall, UserProxyFactory, ReentrancyGuard, Ownable, IG

(InitiativeStatus status,,) =
getInitiativeState(_initiative, votesSnapshot_, votesForInitiativeSnapshot_, initiativeState);
require(status != InitiativeStatus.NONEXISTENT, "Governance: initiative-not-registered");
require(status != InitiativeStatus.WARM_UP, "Governance: initiative-in-warm-up");
require(status == InitiativeStatus.UNREGISTERABLE, "Governance: cannot-unregister-initiative");

// Remove weight from current state
Expand Down
4 changes: 2 additions & 2 deletions src/UserProxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ contract UserProxy is IUserProxy {
PermitParams calldata _permitParams,
bool _doSendRewards,
address _recipient
) public onlyStakingV2 returns (uint256 lusdAmount, uint256 ethAmount) {
) external onlyStakingV2 returns (uint256 lusdAmount, uint256 ethAmount) {
require(_lqtyFrom == _permitParams.owner, "UserProxy: owner-not-sender");

uint256 initialLUSDAmount = lusd.balanceOf(address(this));
Expand All @@ -80,7 +80,7 @@ contract UserProxy is IUserProxy {

/// @inheritdoc IUserProxy
function unstake(uint256 _amount, bool _doSendRewards, address _recipient)
public
external
onlyStakingV2
returns (uint256 lusdAmount, uint256 ethAmount)
{
Expand Down
2 changes: 1 addition & 1 deletion src/utils/Math.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function add(uint88 a, int88 b) pure returns (uint88) {
if (b < 0) {
return a - abs(b);
}
return a + abs(b);
return a + uint88(b);
}

function max(uint256 a, uint256 b) pure returns (uint256) {
Expand Down
4 changes: 2 additions & 2 deletions test/Governance.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -568,15 +568,15 @@ abstract contract GovernanceTest is Test {
vm.warp(block.timestamp + governance.EPOCH_DURATION());

// should revert if the initiative isn't registered
vm.expectRevert("Governance: initiative-not-registered");
vm.expectRevert("Governance: cannot-unregister-initiative");
governance.unregisterInitiative(baseInitiative3);

governance.registerInitiative(baseInitiative3);
uint16 atEpoch = governance.registeredInitiatives(baseInitiative3);
assertEq(atEpoch, governance.epoch());

// should revert if the initiative is still in the registration warm up period
vm.expectRevert("Governance: initiative-in-warm-up");
vm.expectRevert("Governance: cannot-unregister-initiative");
/// @audit should fail due to not waiting enough time
governance.unregisterInitiative(baseInitiative3);

Expand Down
Loading