Assume I have two contracts as follows

contract FirstContract{

function anyFunction(){}


contract SecondContract{

  FirstContract x = new FirstContract();

function anotherFunction(){
  x.anyFunction(); ///////


How can I restrict anyFunction() so that only SecondContract can call it and nothing else?


Use modifiers to restrict access to a function by other contracts/users.


contract FirstContract {

    address creator; // This will be the address of SecondContract.

    function FirstContract() {
        creator = msg.sender; // Which we set here.

    modifier onlyCreator() {
        require(msg.sender == creator); // If it is incorrect here, it reverts.
        _;                              // Otherwise, it continues.

    function anyFunction() onlyCreator {} // This can now only be accessed by SecondContract


If this is what you are looking for, you may also be interested in researching the more common owned pattern, which works similarly.

  • Cool, this answers my question. Is there any way I could limit which contracts can instantiate another contract? I tried putting the modifier on the constructor but it didn't work. – ninesalt Aug 12 '17 at 12:16
  • In theory, you could put that same code in the constructor without a modifier. In practice, there's no way to stop a sufficiently determined individual from creating an instance of a contract. If nothing else, he can just modify the code to allow it. – Matthew Schmidt Aug 12 '17 at 21:35

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.