Skip to content

Instantly share code, notes, and snippets.

@TomiOhl
Last active June 18, 2021 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TomiOhl/cf361e501849304c9cd3fa31bb3fc1ec to your computer and use it in GitHub Desktop.
Save TomiOhl/cf361e501849304c9cd3fa31bb3fc1ec to your computer and use it in GitHub Desktop.
Experimenting what would be the best way to store constants in Solidity.
// 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