0

I am looking for a mechanism that allows me to restrict the creation of a contract A from anywhere but another contract B.

contract A {
    // ...
}

contract B {
    constructor() {
        A a = new A(); // legal
    }
}

contract C {
    constructor() {
        A a = new A() // illegal
    }
}

In C++ this is possible with the friendship relation between classes and private constructors:

class A {
    friend class B;

private:
    A() { }
}

class B {
    B() {
        A *a = new A(); // legal
    }
}

class C {
    C() {
        A *a = new A() // illegal
    }
}

Is there a mechanism in solidity that allows constraining in such ways at compile time or literally anyone can create a new instance of a contract from anywhere(at compile time)?

1 Answer 1

1

I don't think it's possible, because every contract is public by construction. Creating an instance of the contract just makes it easier for someone to interact with the contract, which is impossible to prohibit. What you can do is make some methods private. This will prevent such methods from being executed by other accounts (external or from other contracts).

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.