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 #92

Open
code423n4 opened this issue May 4, 2022 · 0 comments
Open

Gas Optimizations #92

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

Comments

@code423n4
Copy link
Contributor

GAS

G-01: for loop gas optimization

ForgottenRunesWarriorsMinter.sol#L162
ForgottenRunesWarriorsMinter.sol#L220
ForgottenRunesWarriorsMinter.sol#L259
ForgottenRunesWarriorsMinter.sol#L355

Unnecessary to update uint to default value of zero.
Prefix instead of postfix increments and unchecked increments can save gas.

Taking all changes into account I recommend change a loop like this:

for (uint256 i = 0; i < numWarriors; i++) {
    _mint(msg.sender);
}

To this:

for (uint256 i; i < numWarriors;) {
    _mint(msg.sender);
    unchecked { ++i; }
}

G-02: Parameters order could save gas on mint fail

ForgottenRunesWarriorsMinter.sol#L163

Parameters daMinters, daAmountPaid, daNumMinted and numSold are not necessary to _mint. Therefore gas could be saved in case _mint reverts if _mint was set before such parameters. Check example below.

for (uint256 i = 0; i < numWarriors; i++) {
    _mint(msg.sender);
}

daMinters.push(msg.sender);
daAmountPaid[msg.sender] += msg.value;
daNumMinted[msg.sender] += numWarriors;
numSold += numWarriors;

if (numSold == maxDaSupply) {
    // optimistic: save gas by not setting on every mint, but will
    // require manual `setFinalPrice` before refunds if da falls short
    finalPrice = currentPrice;
}

G-03: No need to use _msgSender

ForgottenRunesWarriorsGuild.sol#L101

The use of _msgSender when there is no implementation of a meta transaction mechanism that uses it, such as EIP-2771, very slightly increases gas consumption.

I suggest replacing _msgSender with msg.sender if there is no mechanism to support meta-transactions like EIP-2771 implemented.

G-04: Gas can be saved by shortening syntax

ForgottenRunesWarriorsMinter.sol#L182

Gas could be saved by changing:

require(mintlistMinted[msg.sender] == false, 'Already minted');

To:

require(!mintlistMinted[msg.sender], 'Already minted');

G-05: Lore R contains very large string

ForgottenRunesWarriorsGuild.sol#L33

Having in mind that this might be unnegotiable I suggest shortening Lore R to less than 32 bytes to save gas.

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels May 4, 2022
code423n4 added a commit that referenced this issue May 4, 2022
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

1 participant