11

Imagine that Alice calls a contract method C1 and that method calls another method in contract C2.

Regularly msg.sender equals the Alice Account in the C1 method. And msg.sender equals the C1 contract address in the C2 method.

I would like to know if it is possible to call C2 in such a way that msg.sender is still the Alice address instead of C1 address. And that C2 can access C2's state variables.

I have tried to use delegatecall and callcode, but those functions seems that C2 has only access to the C1 state but not the C2 state. I guess that those calls are more prepared for libraries.

It would be good to have an example of this call.

eth
  • 85,679
  • 53
  • 285
  • 406
jbaylina
  • 775
  • 5
  • 12

2 Answers2

9

I have tried to use delegatecall and callcode, but those functions seems that C2 has only access to the C1 state but not the C2 state.

Correct.

If you want C2's state, you can use tx.origin or make C2's method take an extra address _caller (example) that C1 passes Alice's address. C2 then uses _caller instead of msg.sender.

Using tx.origin means that Alice can't be a contract and How do I make my DAPP "Serenity-Proof?" suggests to avoid using tx.origin.

eth
  • 85,679
  • 53
  • 285
  • 406
4

If all you want to know is the originator of the original transaction (as opposed to the current call), you can simply refer to tx.origin instead of msg.caller.

Nick Johnson
  • 8,144
  • 1
  • 28
  • 35
  • It's a good answer! but I let it open because I'm finding some thing that can be done in the C1 as C2 is already deployed. – jbaylina May 19 '16 at 09:29