Last active
May 27, 2020 10:50
-
-
Save nakajo2011/c8ae568e28bf10ebf74ecf8dadffde25 to your computer and use it in GitHub Desktop.
Using immutable keyword
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
pragma solidity >=0.6.5; | |
contract SimpleDonationManager { | |
uint256 immutable minDonation = 42; | |
uint256 immutable maxDonation = calcMaxDonation(); | |
address immutable owner; | |
constructor() public { | |
owner = msg.sender; | |
// Error: Immutables cannot be read in constructor. | |
// assert(minDonation <= maxDonation); | |
// Error: Immutables can only be assigned once. | |
// owner = address(0); | |
} | |
function withdraw(uint256 amount) public { | |
require(msg.sender == owner); // Requires no sload! | |
msg.sender.transfer(amount); | |
} | |
receive() external payable { | |
require(msg.value >= minDonation && msg.value <= maxDonation); | |
} | |
function calcMaxDonation() internal pure returns (uint256 max) { | |
max = 10000 * 42; | |
// Error: Cannot read from Immutables in construction context. | |
// max = 10000 * minDonation; | |
} | |
// Error: cannot write to Immutables from runtime context. | |
// function changeOwner(address newOwner) external { | |
// owner = newOwner; | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment