Last active
June 18, 2021 10:24
-
-
Save TomiOhl/cf361e501849304c9cd3fa31bb3fc1ec to your computer and use it in GitHub Desktop.
Experimenting what would be the best way to store constants in Solidity.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity 0.8.4; | |
// A simple contract to test the differences of gas usage between accessing a storage variable and using a return value of a function. | |
// Result: more than 2000 less gass consumed by getFromInline() and getFromImmutable() than getFromStorage() (if called in a transaction). | |
// Conclusion: it's better not to use storage variables for constants. | |
contract StorageVsFunctionTest { | |
address internal uniAddress = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D; | |
address internal immutable uniAddressImmutable; | |
constructor() { | |
uniAddressImmutable = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D; | |
} | |
function getFromImmutable() public view returns (address) { | |
return uniAddressImmutable; | |
} | |
function getFromInline() public pure returns (address) { | |
return 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D; | |
} | |
function getFromStorage() public view returns (address) { | |
return uniAddress; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment