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

Update Magic Numbers with constants and Update public variables with private for gas-efficiency #20

Open
allwin199 opened this issue Mar 17, 2024 · 2 comments

Comments

@allwin199
Copy link

This codebase currently uses magic numbers in certain areas. Replacing these magic numbers with descriptive constants will significantly improve code readability and maintainability.

uint256 public constant PRICE_DENOMINATOR = 1e6;

1e6 magic number can be updated with PRICE_DENOMINATOR in the below line

if (_priceMarkup < 1e6) {
revert PriceMarkupTooLow();
}

if (_tokenOracle.decimals() != 8 || _nativeAssetOracle.decimals() != 8) {
revert OracleDecimalsInvalid();
}

The above lines can be updated with the below lines of code.

uint256 private constant DECIMAL_PRECISION = 8;

.
.
.

if (_tokenOracle.decimals() != DECIMAL_PRECISION || _nativeAssetOracle.decimals() != DECIMAL_PRECISION) { 
     revert OracleDecimalsInvalid(); 
 } 

some more magic numbers are used. It can be updated with constants

Also, Instead of declaring all variables as public. All variables can be declared as private and we can write external getter functions to get the values. Which can prove to be gas-efficient.

-  uint256 public constant PRICE_DENOMINATOR = 1e6; 
+ uint256 private constant PRICE_DENOMINATOR = 1e6; 

External Getter function

function getPriceDenominator() external view returns(uint256){
     return PRICE_DENOMINATOR;
}
@allwin199 allwin199 changed the title Update Magic Number with constants and Update public variables with private for gas-efficiency Update Magic Numbers with constants and Update public variables with private for gas-efficiency Mar 17, 2024
@allwin199 allwin199 changed the title Update Magic Numbers with constants and Update public variables with private for gas-efficiency Update Magic Numbers with constants and Update public variables with private for gas-efficiency Mar 17, 2024
@allwin199
Copy link
Author

@kristofgazso Making these changes will help in code readability and gas cost.

Making these changes will break the test cases. I am willing to work on it and make sure everything works as intended.

@Narendra-Reddy1
Copy link

Narendra-Reddy1 commented Aug 16, 2024

Hey @allwin199 are you still working on this issue??
If not I'll take up this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants