Skip to content

Commit

Permalink
Apply gas fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tsudmi committed Oct 1, 2023
1 parent 66fd138 commit dfa2be4
Show file tree
Hide file tree
Showing 20 changed files with 63 additions and 51 deletions.
8 changes: 6 additions & 2 deletions contracts/base/Multicall.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import '../interfaces/IMulticall.sol';
*/
abstract contract Multicall is IMulticall {
/// @inheritdoc IMulticall
function multicall(bytes[] calldata data) public override returns (bytes[] memory results) {
function multicall(bytes[] calldata data) external override returns (bytes[] memory results) {
results = new bytes[](data.length);
for (uint256 i = 0; i < data.length; i++) {
for (uint256 i = 0; i < data.length; ) {
(bool success, bytes memory result) = address(this).delegatecall(data[i]);

if (!success) {
Expand All @@ -27,6 +27,10 @@ abstract contract Multicall is IMulticall {
}

results[i] = result;
unchecked {
// cannot realistically overflow
++i;
}
}
}
}
2 changes: 1 addition & 1 deletion contracts/keeper/KeeperOracles.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ abstract contract KeeperOracles is Ownable2Step, EIP712, IKeeperOracles {
constructor() Ownable2Step() EIP712('KeeperOracles', '1') {}

/// @inheritdoc IKeeperOracles
function addOracle(address oracle) public override onlyOwner {
function addOracle(address oracle) external override onlyOwner {
if (isOracle[oracle]) revert Errors.AlreadyAdded();

// SLOAD to memory
Expand Down
14 changes: 8 additions & 6 deletions contracts/keeper/KeeperRewards.sol
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ abstract contract KeeperRewards is KeeperOracles, IKeeperRewards {
rewardsRoot = params.rewardsRoot;
// cannot overflow on human timescales
lastRewardsTimestamp = uint64(block.timestamp);
rewardsNonce = nonce + 1;
unchecked {
// cannot realistically overflow
rewardsNonce = nonce + 1;
}

_osToken.setAvgRewardPerSecond(params.avgRewardPerSecond);

Expand All @@ -128,11 +131,9 @@ abstract contract KeeperRewards is KeeperOracles, IKeeperRewards {

/// @inheritdoc IKeeperRewards
function canUpdateRewards() public view override returns (bool) {
// SLOAD to memory
uint256 _lastRewardsTimestamp = lastRewardsTimestamp;
unchecked {
// cannot overflow as lastRewardsTimestamp & rewardsDelay are uint64
return _lastRewardsTimestamp + rewardsDelay < block.timestamp;
return lastRewardsTimestamp + rewardsDelay < block.timestamp;
}
}

Expand Down Expand Up @@ -190,7 +191,7 @@ abstract contract KeeperRewards is KeeperOracles, IKeeperRewards {
}

// SLOAD to memory
Reward memory lastReward = rewards[msg.sender];
Reward storage lastReward = rewards[msg.sender];
// check whether Vault's nonce is smaller that the current, otherwise it's already harvested
if (lastReward.nonce >= currentNonce) return (0, 0, false);

Expand All @@ -199,7 +200,8 @@ abstract contract KeeperRewards is KeeperOracles, IKeeperRewards {
harvested = true;

// update state
rewards[msg.sender] = Reward({nonce: currentNonce, assets: params.reward});
lastReward.nonce = currentNonce;
lastReward.assets = params.reward;

// check whether Vault has unlocked execution reward
if (IVaultMev(msg.sender).mevEscrow() == _sharedMevEscrow) {
Expand Down
7 changes: 5 additions & 2 deletions contracts/keeper/KeeperValidators.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ abstract contract KeeperValidators is KeeperOracles, KeeperRewards, IKeeperValid
}

/// @inheritdoc IKeeperValidators
function setValidatorsMinOracles(uint256 _validatorsMinOracles) public override onlyOwner {
function setValidatorsMinOracles(uint256 _validatorsMinOracles) external override onlyOwner {
_setValidatorsMinOracles(_validatorsMinOracles);
}

Expand Down Expand Up @@ -105,7 +105,10 @@ abstract contract KeeperValidators is KeeperOracles, KeeperRewards, IKeeperValid
);

// update state
exitSignaturesNonces[vault] = nonce + 1;
unchecked {
// cannot realistically overflow
exitSignaturesNonces[vault] = nonce + 1;
}

// emit event
emit ExitSignaturesUpdated(msg.sender, vault, nonce, exitSignaturesIpfsHash);
Expand Down
9 changes: 7 additions & 2 deletions contracts/libraries/ExitQueue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ library ExitQueue {
if (_unsafeAccess(self.checkpoints, mid).totalTickets > positionTicket) {
high = mid;
} else {
low = mid + 1;
unchecked {
// cannot underflow as mid < high
low = mid + 1;
}
}
}
return high;
Expand Down Expand Up @@ -124,8 +127,10 @@ library ExitQueue {
// cannot overflow as it is capped with underlying asset total supply
burnedShares += sharesDelta;
exitedAssets += Math.mulDiv(sharesDelta, checkpointAssets, checkpointShares);

// cannot overflow as checkpoints are created max once per day
checkpointIdx++;
}
checkpointIdx++;

// stop when required shares collected or reached end of checkpoints list
if (positionShares <= burnedShares || checkpointIdx >= length) {
Expand Down
4 changes: 1 addition & 3 deletions contracts/osToken/OsToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,7 @@ contract OsToken is ERC20, Ownable2Step, IOsToken {

// check whether any profit accrued
if (profitAccrued == 0) {
// SLOAD to memory
uint256 lastUpdateTimestamp = _lastUpdateTimestamp;
if (lastUpdateTimestamp != block.timestamp) {
if (_lastUpdateTimestamp != block.timestamp) {
_lastUpdateTimestamp = uint64(block.timestamp);
}
return;
Expand Down
2 changes: 1 addition & 1 deletion contracts/osToken/PriceFeed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ contract PriceFeed is IBalancerRateProvider, IChainlinkAggregator, IChainlinkV3A
uint80 answeredInRound
)
{
return (uint80(0), latestAnswer(), block.timestamp, block.timestamp, uint80(0));
return (0, latestAnswer(), block.timestamp, block.timestamp, 0);
}
}
2 changes: 1 addition & 1 deletion contracts/vaults/ethereum/EthGenesisVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ contract EthGenesisVault is Initializable, EthVault, IEthGenesisVault {
*/
function _pullAssets() private {
uint256 escrowBalance = address(_poolEscrow).balance;
if (escrowBalance > 0) _poolEscrow.withdraw(payable(this), escrowBalance);
if (escrowBalance != 0) _poolEscrow.withdraw(payable(this), escrowBalance);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions contracts/vaults/modules/VaultOsToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ abstract contract VaultOsToken is VaultImmutables, VaultState, VaultEnterExit, I
/// @inheritdoc IVaultOsToken
function osTokenPositions(address user) external view override returns (uint128 shares) {
OsTokenPosition memory position = _positions[user];
if (position.shares > 0) _syncPositionFee(position);
if (position.shares != 0) _syncPositionFee(position);
return position.shares;
}

Expand All @@ -66,7 +66,7 @@ abstract contract VaultOsToken is VaultImmutables, VaultState, VaultEnterExit, I

// fetch user position
OsTokenPosition memory position = _positions[msg.sender];
if (position.shares > 0) {
if (position.shares != 0) {
_syncPositionFee(position);
} else {
position.cumulativeFeePerShare = SafeCast.toUint128(_osToken.cumulativeFeePerShare());
Expand Down
2 changes: 1 addition & 1 deletion contracts/vaults/modules/VaultToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ abstract contract VaultToken is Initializable, ERC20Upgradeable, VaultState, IVa
/// @inheritdoc VaultState
function _updateExitQueue() internal virtual override returns (uint256 burnedShares) {
burnedShares = super._updateExitQueue();
if (burnedShares > 0) emit Transfer(address(this), address(0), burnedShares);
if (burnedShares != 0) emit Transfer(address(this), address(0), burnedShares);
}

/// @inheritdoc ERC20Upgradeable
Expand Down
2 changes: 1 addition & 1 deletion test/__snapshots__/EthErc20Vault.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ Object {
exports[`EthErc20Vault update exit queue emits transfer event 1`] = `
Object {
"calldataByteLength": 196,
"gasUsed": 137518,
"gasUsed": 137509,
}
`;
8 changes: 4 additions & 4 deletions test/__snapshots__/EthGenesisVault.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Object {
exports[`EthGenesisVault pulls assets on claim exited assets 1`] = `
Object {
"calldataByteLength": 68,
"gasUsed": 64372,
"gasUsed": 64329,
}
`;

Expand All @@ -38,20 +38,20 @@ Object {
exports[`EthGenesisVault update state deducts rewards on first state update 1`] = `
Object {
"calldataByteLength": 196,
"gasUsed": 145239,
"gasUsed": 145230,
}
`;

exports[`EthGenesisVault update state splits penalty between rewardEthToken and vault 1`] = `
Object {
"calldataByteLength": 196,
"gasUsed": 100632,
"gasUsed": 100623,
}
`;

exports[`EthGenesisVault update state splits reward between rewardEthToken and vault 1`] = `
Object {
"calldataByteLength": 196,
"gasUsed": 162339,
"gasUsed": 162330,
}
`;
2 changes: 1 addition & 1 deletion test/__snapshots__/EthVault.deposit.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ Object {
exports[`EthVault - deposit full vault: assets & shares update state and deposit 1`] = `
Object {
"calldataByteLength": 260,
"gasUsed": 207931,
"gasUsed": 207922,
}
`;
4 changes: 2 additions & 2 deletions test/__snapshots__/EthVault.multicall.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
exports[`EthVault - multicall can update state, redeem and queue for exit 1`] = `
Object {
"calldataByteLength": 676,
"gasUsed": 185901,
"gasUsed": 185776,
}
`;

exports[`EthVault - multicall can update state, redeem and queue for exit 2`] = `
Object {
"calldataByteLength": 516,
"gasUsed": 139838,
"gasUsed": 139713,
}
`;
2 changes: 1 addition & 1 deletion test/__snapshots__/EthVault.register.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`EthVault - register multiple validators succeeds 1`] = `
Object {
"calldataByteLength": 3652,
"gasUsed": 722975,
"gasUsed": 723005,
}
`;

Expand Down
6 changes: 3 additions & 3 deletions test/__snapshots__/EthVault.state.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
exports[`EthVault - state allocates fee to recipient when delta is above zero 1`] = `
Object {
"calldataByteLength": 196,
"gasUsed": 117797,
"gasUsed": 117825,
}
`;

exports[`EthVault - state applies penalty when delta is below zero 1`] = `
Object {
"calldataByteLength": 196,
"gasUsed": 115420,
"gasUsed": 115411,
}
`;

exports[`EthVault - state updates exit queue 1`] = `
Object {
"calldataByteLength": 196,
"gasUsed": 144912,
"gasUsed": 144903,
}
`;
12 changes: 6 additions & 6 deletions test/__snapshots__/EthVault.withdraw.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@
exports[`EthVault - withdraw claim exited assets for single user in multiple checkpoints in multiple transactions 1`] = `
Object {
"calldataByteLength": 68,
"gasUsed": 76885,
"gasUsed": 76834,
}
`;

exports[`EthVault - withdraw claim exited assets for single user in multiple checkpoints in multiple transactions 2`] = `
Object {
"calldataByteLength": 68,
"gasUsed": 49562,
"gasUsed": 49521,
}
`;

exports[`EthVault - withdraw claim exited assets for single user in multiple checkpoints in single transaction 1`] = `
Object {
"calldataByteLength": 68,
"gasUsed": 52411,
"gasUsed": 52309,
}
`;

exports[`EthVault - withdraw claim exited assets for single user in single checkpoint 1`] = `
Object {
"calldataByteLength": 68,
"gasUsed": 49562,
"gasUsed": 49521,
}
`;

Expand All @@ -47,13 +47,13 @@ Object {
exports[`EthVault - withdraw update exit queue adds checkpoint 1`] = `
Object {
"calldataByteLength": 196,
"gasUsed": 103499,
"gasUsed": 103490,
}
`;

exports[`EthVault - withdraw update exit queue for not all the queued shares 1`] = `
Object {
"calldataByteLength": 196,
"gasUsed": 103495,
"gasUsed": 103486,
}
`;
18 changes: 9 additions & 9 deletions test/__snapshots__/KeeperRewards.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,42 @@
exports[`KeeperRewards harvest (own escrow) succeeds for latest rewards root 1`] = `
Object {
"calldataByteLength": 292,
"gasUsed": 110889,
"gasUsed": 110938,
}
`;

exports[`KeeperRewards harvest (own escrow) succeeds for previous rewards root 1`] = `
Object {
"calldataByteLength": 292,
"gasUsed": 113058,
"gasUsed": 113092,
}
`;

exports[`KeeperRewards harvest (own escrow) succeeds for previous rewards root 2`] = `
Object {
"calldataByteLength": 196,
"gasUsed": 74469,
"gasUsed": 74497,
}
`;

exports[`KeeperRewards harvest (shared escrow) succeeds for latest rewards root 1`] = `
Object {
"calldataByteLength": 292,
"gasUsed": 144270,
"gasUsed": 144282,
}
`;

exports[`KeeperRewards harvest (shared escrow) succeeds for previous rewards root 1`] = `
Object {
"calldataByteLength": 292,
"gasUsed": 146439,
"gasUsed": 146436,
}
`;

exports[`KeeperRewards harvest (shared escrow) succeeds for previous rewards root 2`] = `
Object {
"calldataByteLength": 196,
"gasUsed": 90750,
"gasUsed": 90741,
}
`;

Expand All @@ -52,20 +52,20 @@ Object {
exports[`KeeperRewards update rewards succeeds 1`] = `
Object {
"calldataByteLength": 772,
"gasUsed": 141099,
"gasUsed": 141074,
}
`;

exports[`KeeperRewards update rewards succeeds 2`] = `
Object {
"calldataByteLength": 772,
"gasUsed": 123987,
"gasUsed": 123962,
}
`;

exports[`KeeperRewards update rewards succeeds with all signatures 1`] = `
Object {
"calldataByteLength": 1156,
"gasUsed": 147303,
"gasUsed": 147278,
}
`;
Loading

0 comments on commit dfa2be4

Please sign in to comment.