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.

8
  • If you're the one writing A, just have it take the owner as a parameter. But there's (thankfully) no way to fake the value of msg.sender.
    – user19510
    Commented Jun 16, 2019 at 6:16
  • I don't want to fake it. Just want to know, if there is any way possible, any way, in which I can call functions of contract A, using its instance, as if it has come from the sender of the request. Commented Jun 16, 2019 at 6:20
  • ....and not from the contract B Commented Jun 16, 2019 at 6:20
  • You say you don't want to fake msg.sender, but then you ask how. :-) It's hard to suggest other solutions to your problem because I don't know what you're actually trying to do.
    – user19510
    Commented Jun 16, 2019 at 6:24
  • oh. I took it in another sense. :D Commented Jun 16, 2019 at 6:26

2 Answers 2

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.

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

1
  • 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. Commented Jun 17, 2019 at 8:45

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.