1

Suppose there is a modifier in a contract:

modifier onlySelf() {
        require(msg.sender == address(this), "Not Self");
        _;
    }

If i make a function in this contract like this:

function test() public onlySelf {
    <function_body>
}

How it will work? More explicitly i wanna know if i deploy the contract how this function will be called?

3 Answers 3

2

This modifier requires that the call to the test function is made from the contract instance itself. In a simple contract, it is the same as declaring the method internal/private but it costs more gas.

In all likelihood though it is being used to facilitate some level of contract upgradeability or possibly to allow contracts to have the same proxy address on different EVM chains. Without more context/code though it is hard to say why it is written.

To actually call it, you can use a method in the same contract instance such as:

function testTheTestFunction() public {
  return this.test()
};

You may need to add some specifics to testTheTestFunction depending on the return value of test() ect but the concept is basically it enforces access control so that the test function can only be called from the contract instance itself.

5
  • 1
    this will not work. because msg.sender will still be EoA. one needs to call using this.test() or cast contract address to contract type and issue call using dot operator
    – crypt
    Feb 8, 2023 at 17:55
  • Did not understand, could you elaborate it ????@crypt
    – Hacek
    Feb 8, 2023 at 17:57
  • i can send u the code.
    – Hacek
    Feb 8, 2023 at 17:58
  • see code here ethereum.stackexchange.com/questions/144552/…
    – crypt
    Feb 8, 2023 at 18:01
  • @crypt is right, I updated the code. Feb 8, 2023 at 21:56
1

Modifier is testing the sender against address of the contract, calling test function from other contracts or EoA will fail.

And if there are no other functions in this contract that make call to test function using call or delegatecall, it will not be possible to call test function from even within the contract.

Test can also be called from within the contract using this.test()

contract Storage {

    uint256 number;

    function store(uint256 num) public {
        number = num;
    }

    modifier onlyMe(){
        require(msg.sender == address(this));
        _;
    }

    function retrieve() public view returns (uint256){
        //return this.get();    this also works
        return ((Storage)(this)).get();
    }

    function get() public onlyMe view returns (uint256){
        return number;
    }
}
3
0

OnlySelf is a modifier. It is a special kind of function that is processed at the beginning of the callable function.

You can imagine it like this:

function test() public {
    require(msg.sender == address(this), "Not Self");
    <function_body>
}

The _; indicates where the <function_body> is inserted.

2
  • i know that, i wanna know how the function will be called after deploying? as no one can call this function except this contract.
    – Hacek
    Feb 8, 2023 at 17:28
  • 1
    Ah I see, contract has to call it using delegatecall
    – Nal Luksic
    Feb 8, 2023 at 20:01

Your Answer

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

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