I know that msg.sender will be the address that connects to the contract.

Address(this) is the current contract address

Then == means it connects to itself. When using this condition in require, the function only accept the contract call itself internally? Is this the right understanding and does it make sense?

  • Add all answers..because i want to know
    – Uray
    May 27, 2021 at 7:35

3 Answers 3


The contract has to call one of its own functions and the message has to come from the outside. In that case, the msg.sender will indeed be itself.

pragma solidity 0.7.6;

interface IRecursive {
    function isMe() external view returns(bool);

contract Recursive is IRecursive {
    function isMe() external view override returns(bool) {
        if(msg.sender == address(this)) return true;
        return false;
    function tryThis() external view returns(bool) {
        return IRecursive(address(this)).isMe(); // we (this) are msg.sender to the receiver

When tryThis() uses the interface to make a call to the instance at its own address the call arrives from the outside and msg.sender is the address that called isMe() which just happens to be the same contract.

In some use-cases, it can be handy to elevate the contract itself to an equal footing with other users. However, keep in mind the gas trade-off in using external calls to self from the contract, since it adds close to 1000 gas units (980 in this case) to the call cost.

Hope it helps.


Yes, exactly. If a function contains

require(msg.sender == address(this));

It means only the contract can call it.

For example the Gnosis Multisig wallet has the modifier (syntax is for solc v0.4)

modifier onlyWallet() {
    if (msg.sender != address(this))

This implies that any function with that modifier is only callable by the contract. Some functions with that requirement are addOwner, removeOwner, changeRequirement, etc. It was done that way so important changes has to be accepted by an owners majority.


An other way that your contract to be secured by good practice, is coding like bottom contract.

You should define owner state on your contract and set modifier to your custom function that you want to be restricted by owner:

contract YourContract {
  address private owner;
  constructor() {
    owner = msg.sender;

  modifier onlyOwner {
    require(msg.sender == owner, "Only the owner can access it");

  function customFunction() public onlyOwner {
    // your statement
  • This was not asked I guess Nov 6, 2022 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.