2

How can you program a contract so certain functions can only be called by other functions within the contract? Would you use a modifier? In the example below I'd want a user to have to call metaAwesomeness() to access awesomeness(), which would otherwise be inaccessible via superDuperOnly():

contract superDuper {
    modifier superDuperOnly() {if (msg.sender != address(this)) throw; _};    
    function awesomeness() superDuperOnly {};
    function metaAwesomeness() {awesomeness()};
}

Taking it one step further, I'd also be interested to use this modifier on another contract, so only superDuper functions can call kindaCool's nifty():

import "./superDuper.sol";
contract kindaCool {
    function nifty() superDuperOnly {};
}

EDIT: for solution (Pt 1):

The first contract, superDuper, would look like:

contract superDuper {   
    function awesomeness() internal {};
    function metaAwesomeness() {awesomeness()};
}

1 Answer 1

2

Unless you're doing something really weird, you can limit functions to their own contract by simply marking them private.

If you want to limit functions in your contract to another contract, you're on the right track, but you need to manage the address of the particular instance of that contract that you're interested in and check that against msg.sender in your modifier.

2
  • 1
    To limit the function to your contract and its children, use internal. Sep 5, 2016 at 10:33
  • Ah, yes. internal seems to be the best way to limit contract functions as intended above. Reference. Sep 6, 2016 at 16:53

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.