0

I am using a factory pattern to deploy its children contracts here.

The child contract has a constructor as followed:

  constructor (
    string memory _potName,
    uint _duration,
    uint _minStake,
    PotType _potType
  )
  public payable { 
    require(msg.value >= _minStake); 
    ... 
  }

The factory contract create the child contract as:

  function createLotteryPot(
    string memory potName, uint duration, uint minStake,
    LotteryPot.PotType potType
  ) public payable returns(LotteryPot) {
    LotteryPot newContract = new LotteryPot({ _potName: potName,
      _duration: duration, _minStake: minStake, _potType: potType });
    ...
  }

My questions are:

  1. In the factory contract, how can I pass the msg.value to the children contract? This is a requirement that during child contract creation, the owner also need to commit some ethereum.

  2. How can I pass the msg.sender to the children contract to specify it is the owner instead of the contract? I understand I can pass in an additional parameter msg.sender. Just wonder is there a cleaner way to do it?

Thanks a lot for input.

1

As per the first question you can send value like this:

LotteryPot newContract = (new LotteryPot).value(amount)({ _potName: potName,
      _duration: duration, _minStake: minStake, _potType: potType });

Secondly, for sending the msg.sender you can change the signature as:

constructor (
    string memory _potName,
    uint _duration,
    uint _minStake,
    PotType _potType,
    address sender
  )
  public payable { 
    require(msg.value >= _minStake); 
    ... 
  }

and later on when calling the constructor from factory you can pass the sender as parameter like so:

LotteryPot newContract = (new LotteryPot).value(amount)({ _potName: potName,
      _duration: duration, _minStake: minStake, _potType: potType, sender: msg.sender });

Hope it will help.

  • Thanks for the answer. It works. Also, I am using openzeppelin Ownable library. Eventually, in child contract, I do if (tx.origin != msg.sender) { transferOwnership(tx.origin) }. In case someone else come across this same situation. – Jimmy Chu Mar 19 at 6:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.