1

I have a contract that instantiates a different contract - called Project contract. I made it so that I can deposit ETH into the Project contract and also withdraw its balance through functions of the first contract. The thing is: they are obviously separate. So, I could withdraw the funds from the Project contract directly on it. I do not want the users of first contract to be able to go into the Project contract directly and withdraw the balance from there rather than doing it through the first.

How do I limit their ability to depend only on the first contract?

2
  • 1
    You could include a authorized addess and check that the call comes from the you and not any other user, also check the posibility of using Ownable
    – Julissa DC
    Jan 27, 2022 at 17:34
  • How are both contracts related? Can you show the contract code?
    – Ismael
    Jan 31, 2022 at 6:59

1 Answer 1

0

When instantiating the Project Contract you need to make yourself an owner of that contract in its constructor. It'll be something like this:

contract FirstContract() {
   ProjectContract projectContract;
   constructor() {
     projectContract = new ProjectContract(address(this));
   }
}
contract ProjectContract() {
   address owner;

   constructor(address _owner) {
     owner = _owner;
   }

   function restricted() public {
     require(msg.sender == owner, "You cannot call this function");
     ...
   }
}

There is also a bunch of libraries to do this, with some additional functions, but the idea is the same.

https://docs.openzeppelin.com/contracts/2.x/access-control

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.