2

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?

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

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.

Hope it helps.

2

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))
        throw;
    _;
}

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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