0

I have two seperate contracts:

  1. SuperAdmin.sol
  2. User.sol

And they look like this:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.7;

import "./User.sol";

contract SuperAdmin {
  address[] public users;

  constructor() {}

  modifier restricted() {
    if (msg.sender == address(this)) _;
  }

  function addNewUser(address adminAddress) public restricted returns(address) {
    address newUser = User(adminAddress).getAddress();
    users.push(newUser);
    return newUser;
  }
}

And the other contract, looks like this:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.7;


contract User {
  address private partner;
  address private admin;

  constructor(address _admin) {
    partner = msg.sender;
    admin = _admin;
  }

  modifier restricted() {
    if (msg.sender == address(this)) _;
  }

  function getAddress() external view returns(address){
      return address(this);
  }

  function interact() restricted view{
    // do something
  }
}

Now, let me tell you about the purpose, SuperAdmin will be able to create Users, which is done by addNewUser in SuperAdmin.

But the issue is in my Ganache, I'm getting only Contract Call, but no Contract Creation, which is quite unexpected. At least I thought it would work a factory to create contracts.

enter image description here

And the address[] public users; is also not being modified. Pretty odd.

Even after adding new keyword, it's not creating a new contract / deploying a new contract as expected.

enter image description here

1 Answer 1

2

you need to use new keywork in your addNewUser function:

   User newUser;

   function addNewUser(address adminAddress) public restricted returns(address) {
    newUser= new User(adminAddress)
    users.push(address(newUser));
    return newUser;
  }
}

1

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.