I have some functions that I'm trying to save gas on.
onlyOwner
modifier is self explanatory.
This is related to a previous question that I had, where I noticed that address(this)
is different from msg.sender
in a given contract. Upon further reading I get the impression that address(this)
is a hash of msg.sender
, but the two are not considered the same. Is this correct?
Since address(this)
is related to the owner address, is the owner address (msg.sender
, set within the contract's constructor) considered different enough from the contract address address(this)
so as to be considered external
visibility?
// can the owner (set in the constructor) call this function once it is deployed on the blockchain?
function setXYZ(address _address) external onlyOwner returns (bool success) {
XYZ[_address] = 4;
return true;
}
Seems to work when I test it within my IDE, but I don't know if there is a difference in implementations between my test environment and the blockchain environment.
Upon further reading I get the impression that
address(this)
is a hash ofmsg.sender
, but the two are not considered the same. Is this correct?
address(this)
returns address of the currently executed contract. When a contract is deployed, it's assigned an address which is determined by hash of "the deployer address combined with other data". Once the contract address is assigned, it never changes.
On the other hand, msg.sender
reflects address of whoever is currently executing the contract. So if Alice executes a function, msg.sender
is her address - and if Bob executes the same function, msg.sender
reflects his address.
As the OpenZeppelin Ownable
code shows, the deployer address (msg.sender
in constructor
) becomes the initial owner
.
And the onlyOwner
modifier validates whether the current msg.sender
is the owner. If they match - all good. If the currrent msg.sender
is different from the owner
, it reverts the transaction.
Seems to work when I test it within my IDE, but I don't know if there is a difference in implementations between my test environment and the blockchain environment.
This code will work the same on all EVM networks.
There are few very small differences between the EVM implementation in emulators and live networks (as well as between different live networks). But they mostly affect low-level features such as ordering of transactions in a block or gas usage optimizations.