0

Suppose, I have 2 contracts A and B:

contract A{
    function someMethod() public{
         // Something happens here
    }
}

And contract B containing its instance and its wrapper function

contract B{
     A instance = new A();

     function wrapper_someMethod() public{
          instance.someMethod();
     }
}

Now, what I want here is, whenever I call function wrapper_someMethod() using some address 0xABCDEF, the call to the someMethod() function goes from this address only (0xABCDEF), and not from the address(contract B).

How can I do so? Tell if there some other strategy to do it.

Thanks.

2 Answers 2

2

When a contract B is calling another contract A, msg.sender in contract A will always be B. There is no way around that.

You haven't given a lot of information. Depending on the rest of your code, here are some things to consider:

  • Move someMethod() to contract B
  • Call someMethod() directly, instead of through wrapper_someMethod()
  • Use tx.origin in contract someMethod() instead of msg.sender (take care of your security, in most cases tx.origin cannot be relied on for security-critical purposes)
  • Add a parameter address _caller to the someMethod() function. In wrapper_someMethod(), pass msg.sender as argument to someMethod()
3
  • Exactly, there are always security issues with tx.origin. I was just wondering if that is possible. I can't change contract A Looks like it isn't possible to do the way I want. Let me wait for a few more answers, then I'll mark your answer as accepted. Thanks. Commented Jun 14, 2019 at 15:43
  • Thanks for clearing all that Jesse. Appreciate that. Commented Jun 15, 2019 at 3:08
  • Could you please look at this question too ethereum.stackexchange.com/questions/71872/… Commented Jun 15, 2019 at 3:10
2

I voted for Jesse's answer. For the benefit of others who come across this, a little example of the last option mentioned:

contract A {
    function someMethod(address sender) public{
         // Something happens here
         // proceed using "sender" the msg.sender says the transaction is for.
    }
}

contract B {

     A instance = new A();

     function wrapper_someMethod() public{
          instance.someMethod(msg.sender);
     }
}

This is reliable. Think in terms of msg.sender says "Do this, on behalf of sender."

Hope it helps.

2
  • okay, that makes sense. But, maybe in my case, it won't work. I have a few doubts, let me make them clear. I'll get back if any other related doubt. Thanks. Commented Jun 14, 2019 at 17:19
  • Hey Rob, could you please look at this question too. ethereum.stackexchange.com/questions/71872/… Commented Jun 15, 2019 at 3:07

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.