I think your problem appear because ceoAddress
is not initialized properly, and it will have the value of 0x0000000000000000000000000000000000000000
.
Now to change it the contract requires msg.sender == ceoAddress
, this implies that msg.sender
should be 0x0000000000000000000000000000000000000000
. This operation can't be done because you do not have the private key that generates such address.
One options is to initialize your variable in the constructor with the sender account
contract AccessControl {
address public ceoAddress;
modifier onlyCEO() {
require(msg.sender == ceoAddress);
_;
}
constructor() {
// ---- Initialize ceoAddress ----
ceoAddress = msg.sender;
}
function setCEO(address _newCEO) external onlyCEO {
require(_newCEO != address(0));
ceoAddress = _newCEO;
}
}
Truffle uses the first address returned by eth.accounts
to deploy contracts.
2_deploy_contracts.js
module.exports = function(deployer, network, accounts) {
// You can pass other parameters like gas or change the from
deployer.deploy(AccessControl, { from: accounts[2] });
}
Check Truffle documentation for more options to use.
It would be the best if it could be an existing address
What you need to understand is that all addresses (all possible combinations of a 40-character long hexadecimal string) exist. In other words, every possible address is a legal address which you can query for balance, send ether to, etc. However, not necessarily for every address does there exist a person who knows its private key.
So sending ether to an address whose private key is not known to anyone, would be equivalent to losing that ether forever, or at least until somebody obtains the private key of that address (and if somebody ever does, then they will be able to withdraw that ether). However, even without knowing the private key of a given address, anyone can still query for its balance and see that the "lost ether" is still there.
Now that we've clarified the "existing address" issue, you may try this:
address addr = address(keccak256(abi.encodePacked(now)));
This compiles well in Solc v0.4.25, and probably in earlier versions as well.
Best Answer
Though this works, I personally think it is a good idea to have both operand types equal on boolean operations , as you mentioned for future compatibility.
Instead of using > operator use equality == or != , as in case of address the greater operator may not make sense.
Instead of using 0x0 use address(0) or address(0x0), as it would make much more of type of address.
So your code would look like
Just my 2 cents