Limiting access to a sub-contract to the address that initialized it

I have two ERC721 tokens in my app, which cannot be inherited together into one, so I figured I’ll initialize the contracts in my main contract’s constructor.

I’m storing special data on the token-contract level that needs to be read and written back from the main contract. (the two token types interact with eachother’s data)
Due to protection levels, I cannot call a setter function on the token contracts without exposing it to the world via public, so I figured I’ll have a modifier that restricts access as a “public-but-practically-internal function” with the main contract’s address being passed.

Here’s what I mean:

Main contract:

FirstToken tokens1;
SecondToken tokens2;
constructor() public {
    tokens1 = new FirstToken(address(this));
    tokens2 = new SecondToken(address(this));

function doStuff() public {
  //same for tokens2 etc

Token contract(s):

address main;
uint exampleValue;

constructor (address source) public ERC721("FirstToken", "FTK") {
    main = source;

modifier restricted() {
    require(main == msg.sender);

function exampleFunction() public restricted {
  exampleValue = exampleValue + 1;

Would this modifier work without compromise, is there a better way to do this?

1 Like

Hi @poshdan,

Welcome to the community :wave:

OpenZeppelin Contracts used to have a Secondary contract but you can just use Ownable for this purpose:

This should give me some direction for now. Thanks!

1 Like