I have 2 Smart Contracts: A
& B
. B
has a function onlyA()
which I want that only A
can call. Below is my approach:
- Make B Ownable, then add the
onlyOwner()
modifier toonlyA()
. - Add the interface of
B
inA
so thatA
can call the functiononlyA()
from saycallMe()
. - Transfer ownership of
B
toA
after deploying it. - Now, user can call
callMe()
.
My usecase:
I'm building a Protocol Smart Contract (A
) that has an NFT Contract (B
) linked to it. I don't want anyone to directly mint an NFT from B
, but only by calling a function mintNFT()
in the Protocol Smart Contract (A
) which in turn calls the function onlyA()
in B
which internally mints the NFT.
Protocol.mintNFT() -> NFT.onlyA() -> ERC721._mint()
So I was thinking of using the above approach I listed. But I have to add the interface of my NFT Smart Contract to my Protocol Contract which I don't want to do. It just seems to me like an anti-pattern. It is considered bad practice to add interfaces like this?
I would highly appreciate any suggestions for an approach different from the one listed above. Also, any suggestions on the overall re-design of my Protocol Smart Contracts are also welcome.
Update:
Since both contracts are part of the same project, I was also wondering about simply importing the NFT contract in the Protocol Contract rather than defining the NFT interface. Which one is a better way, gas-wise & Solidity best practices wise?