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

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

Gas Optimizations #31

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

Comments

@code423n4
Copy link
Contributor

You can make the initialized variable not initialized bec its already 0 it wastes gas bec your sstore 25000 gas
https://github.com/code-423n4/2022-05-cudos/blob/4e08b7ba7c40252ac64fe4169f8a40622333ee63/solidity/contracts/Gravity.sol#L54
Make functions on admin function payable it saves gas bec of not checking for msg.value = zero
https://github.com/code-423n4/2022-05-cudos/blob/4e08b7ba7c40252ac64fe4169f8a40622333ee63/solidity/contracts/Gravity.sol#L124
Instead of using && in a require statement use multiple require statements to save gas
https://github.com/code-423n4/2022-05-cudos/blob/4e08b7ba7c40252ac64fe4169f8a40622333ee63/solidity/contracts/Gravity.sol#L301
Make require string less than 32 bytes save gas
https://github.com/code-423n4/2022-05-cudos/blob/4e08b7ba7c40252ac64fe4169f8a40622333ee63/solidity/contracts/Gravity.sol#L386
https://github.com/code-423n4/2022-05-cudos/blob/4e08b7ba7c40252ac64fe4169f8a40622333ee63/solidity/contracts/Gravity.sol#L390
https://github.com/code-423n4/2022-05-cudos/blob/4e08b7ba7c40252ac64fe4169f8a40622333ee63/solidity/contracts/Gravity.sol#L405
https://github.com/code-423n4/2022-05-cudos/blob/4e08b7ba7c40252ac64fe4169f8a40622333ee63/solidity/contracts/Gravity.sol#L416
https://github.com/code-423n4/2022-05-cudos/blob/4e08b7ba7c40252ac64fe4169f8a40622333ee63/solidity/contracts/Gravity.sol#L511
https://github.com/code-423n4/2022-05-cudos/blob/4e08b7ba7c40252ac64fe4169f8a40622333ee63/solidity/contracts/Gravity.sol#L527

Use custom errors instead of revert string to save gas
Custom errors from Solidity 0.8.4 are cheaper than revert strings (cheaper deployment cost and runtime cost when the revert condition is met) Source: Custom Errors in Solidity: Starting from Solidity v0.8.4, there is a convenient and gas-efficient way to explain to users why an operation failed through the use of custom errors. Until now, you could already use strings to give more information about failures (e.g., revert("Insufficient funds.");), but they are rather expensive, especially when it comes to deploy cost, and it is difficult to use dynamic information in them. Custom errors are defined using the error statement, which can be used inside and outside of contracts (including interfaces and libraries)
https://github.com/code-423n4/2022-05-cudos/blob/4e08b7ba7c40252ac64fe4169f8a40622333ee63/solidity/contracts/Gravity.sol#L386
https://github.com/code-423n4/2022-05-cudos/blob/4e08b7ba7c40252ac64fe4169f8a40622333ee63/solidity/contracts/Gravity.sol#L390
https://github.com/code-423n4/2022-05-cudos/blob/4e08b7ba7c40252ac64fe4169f8a40622333ee63/solidity/contracts/Gravity.sol#L396
https://github.com/code-423n4/2022-05-cudos/blob/4e08b7ba7c40252ac64fe4169f8a40622333ee63/solidity/contracts/Gravity.sol#L511
https://github.com/code-423n4/2022-05-cudos/blob/4e08b7ba7c40252ac64fe4169f8a40622333ee63/solidity/contracts/Gravity.sol#L527
++i costs less gas compared to i++ for unsigned integer, as pre-increment is cheaper (about 5 gas per iteration) i++ increments i and returns the initial value of i
https://github.com/code-423n4/2022-05-cudos/blob/4e08b7ba7c40252ac64fe4169f8a40622333ee63/solidity/contracts/Gravity.sol#L579
https://github.com/code-423n4/2022-05-cudos/blob/4e08b7ba7c40252ac64fe4169f8a40622333ee63/solidity/contracts/Gravity.sol#L660

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