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

Gas Optimizations #164

Open
code423n4 opened this issue May 14, 2022 · 2 comments
Open

Gas Optimizations #164

code423n4 opened this issue May 14, 2022 · 2 comments
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

Gas optimizations

  1. File:Cally.sol#223
uint256 premium = getPremium(vaultId);
require(msg.value >= premium, "Incorrect ETH amount sent");

Can become

require(msg.value >= getPremium(vaultId), "Incorrect ETH amount sent");

Variable premium is not needed as it is only used once. Saves 2600 gas in deployment and 12 gas in buyoption().
2. File:Cally.sol#227

uint32 auctionStartTimestamp = vault.currentExpiration;
require(block.timestamp >= auctionStartTimestamp, "Auction not started");

Can become

require(block.timestamp >= vault.currentExpiration, "Auction not started");

auctionStartTimestamp varable is not needed as vault.currentExpiration is only used once in the function. Saves 1800 gas in deployment and 4 gas in buyOption

  1. File: Cally.sol#395
Vault memory vault = _vaults[vaultId];
return premiumOptions[vault.premiumIndex];

Can become

return premiumOptions[_vaults[vaultId].premiumIndex];

This can save quite of bit of gas as the whole Vault struct doesnt have to be copied into memory. 42252 gas is saved on deployment and 733 gas in buyOption() when getPremium() is called.

4.File: Cally.sol#444

if (isVaultToken) {
    _vaultBeneficiaries[id] = address(0);
}

Can become

if (id % 2 != 0) {
    _vaultBeneficiaries[id] = address(0);
}

The calcuation can be done in the if statement to save 1400 gas on deployment and 13 from transferFrom().

  1. File:Cally.sol#224
require(msg.value >= premium, "Incorrect ETH amount sent");

Using == operator is 3 gas cheaper than >=. This also keeps people from acedently overpaying when buying an option.

Summary
Implementing each of these would reduce the deploymnt cost from 5,431,682 to 5,387,621 and reduces buyOption from 75,765 to 75,013.

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels May 14, 2022
code423n4 added a commit that referenced this issue May 14, 2022
@outdoteth
Copy link
Collaborator

This can be bumped to a medium severity issue:
Using == operator is 3 gas cheaper than >=. This also keeps people from acedently overpaying when buying an option: #84

@HardlyDifficult
Copy link
Collaborator

Moved 5 to #328

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization)
Projects
None yet
Development

No branches or pull requests

3 participants