Skip to content

Instantly share code, notes, and snippets.

@nakajo2011
Last active May 27, 2020 10:50
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 nakajo2011/c8ae568e28bf10ebf74ecf8dadffde25 to your computer and use it in GitHub Desktop.
Save nakajo2011/c8ae568e28bf10ebf74ecf8dadffde25 to your computer and use it in GitHub Desktop.
Using immutable keyword
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