0

Let's say I have any function in my smart contract and for a small period of time I want that function to not be PUBLIC but PRIVATE. Is it possible in Solidity? If not, could this be solved with a modifier?

3
  • I think it would help to clarify the scenario here. What are you trying to accomplish by having the function change modes? BTW, to my knowledge, it is not possible to try to do things specifically the way you mention. Commented Sep 14, 2018 at 22:05
  • Basically, I allow users to enter within an array. I want to stop this functionality for let's say 60 minutes. I have in mind on how I could accomplish that, however out of curiosity I wanted to know if I can just make it private or use a modifier so they can't call the function.
    – Iulian
    Commented Sep 14, 2018 at 22:09
  • 1
    I think you should consider replacing for a small period of time with under certain conditions. Then, whatever it is you were planning to do during that short period of time, just raise a flag (some internal state variable) when you're done. This flag can then be used in order to determine whether or not the function can be called publicly. Why? Because time-dependent logic can be extremely "non-deterministic", and therefore much harder to implement / verify / test. Commented Sep 15, 2018 at 7:29

2 Answers 2

2

It is not possible to change the visibility of a contract function. Visibility on the function actually affects whether the function will appear in the contract ABI and stuff, so definitely not right to go down this path anyway.

Based on your comments, it looks like you are trying to make a function only work for a certain amount of time. I believe is is best accomplished using a require statement which checks the time or blocks of the function call compared to contract creation, or some other fixed time.

Something like this:

pragma solidity ^0.4.24;

contract Test {
    address[] public signups;
    uint createTime;

    constructor() public {
        createTime = now;
    }

    function signupsStopAfter10Min() public {
        require(now < createTime + 10 minutes);
        signups.push(msg.sender);
    }
}

You could of course make this require statement it's own modifier if you think this will apply to multiple functions.

1

Definitively it is not possible to have visibility switch after the compilation phase. Surely not after deployment. Any public function/variable will remain public for ever. The same if private: private for ever.

It is not a state to be changed, but a immutable part of the code deposited on blockchain.

On the other hand, depending on your needs, you can easily filter out any access to your function, even for a certain time both absolute or measured starting from the last call, for selected categories of user using simple if and watching the block number in various way: it is an affidabile tick by 15 seconds (less or more...)

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.