7

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.

  • 1
    Possible duplicate of Invoke contract B from A and preserve msg.sender – user2284570 Jun 18 '18 at 20:45
  • Imagine you ask an exchange to send ether to a smart contract you created, you would be able to transfer tokens stored on behalf of the address sending ethers. Most exchange stores ether and and ERC20 tokens at the same address. – user2284570 Jun 18 '18 at 20:52
  • @user2284570 You have mark the new question as duplicate of the old. Also the answers here are better. – Ismael Jun 19 '18 at 3:37
  • @jbaylina what happens when you try to delegatecall C1 from C2 ? Why it doesn’t works ? – user2284570 Jun 20 '18 at 8:49
7

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.

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.

  • 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 9:29

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.