Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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