1

I want to initialise an instance of a contract inside another contract on behalf of some address.

e.g. I have contracts A and B

contract A{
    address owner;

    constructor() public{
        owner = msg.sender;
    }
}

and I want to instantiate this contract inside B as follows

contract B{
    A instance = new A();   // I'm talking about this instance
}

so, I want here is that the instance is created on behalf of the caller(either deployer of contract B or through some function).

i.e. The owner setup in the instance of A is the caller.

How can I do so?


Clearer example

I have two contracts A & B, and I create an instance of contract A inside B.

Contract A has a function, say anyFunction().

contract B{
    A instance;

    function anotherFunction() public{
        A.anyFunction();
    }

}

If I call anotherFunction(), how can I make it sure that the call made to function anyFunction() is on behalf of the caller of anotherFunction(), and not on behalf of contract B.

Aman Pandey
  • 137
  • 7

2 Answers2

2

The only direct options you have are msg.sender and tx.origin (which shouldn't be used basically ever). There is no way to get addresses which are "between" these addresses.

Otherwise, as smarx stated, you should pass the desired owner address as a parameter and use that.

Lauri Peltonen
  • 29,391
  • 3
  • 20
  • 57
0

use tx.origin (you MUST read my last statement before use it). It will understand that the one who call the anotherContract is the sender. You can read more at this question answer: What's the difference between 'msg.sender' and 'tx.origin'?. But use it with caution because IT IS VERY DANGEROUS: https://link.medium.com/x1bLy7S9yX

haxerl
  • 1,114
  • 7
  • 14
  • hey @haxeri , I'm not sure, but is there something like tx.sender? Do you mean to say msg.sender.

    or, maybe you want to say tx.origin.

    Quite unsure right now.

    – Aman Pandey Jun 17 '19 at 08:45