I have user A which calls contract B which calls contract C which calls contract D.
How to call external library D from contract C while preserving msg.sender==B ? Of course, I cannot use tx.origin since tx.origin is A.

User A created address of C and address of D, but not B.

DELEGATECALL isn’t the solution, isn’t it ?

  • 1
    Each times, the address (this) is different. Please note the answer is not tx.origin in anyway. Ever ! – user2284570 Jun 17 '18 at 16:57
  • why not to pass B address as a parameter to C and have C to pass this as a parameter to D. – Jaime Jun 18 '18 at 9:55
  • @Jaime because ERC20 interface of C is source code verifiable on etherescan whereas I would keep D closed source. The less parameters I’m passing to D, the less peoples would have an idea of D. – user2284570 Jun 18 '18 at 16:33

From contract C you can make a delegatecall to D, this will preserve the sender (B)

But it will not change the storage, it will continue to access storage of contract C.

This only works if contract D doesn't need storage, or it was written to access storage from C (for example if D is a library contract).

| improve this answer | |

It all depends if you're the author of all these contracts or not. You can achieve this with combinations of DELEGATECALL, see this answer. But always beware, if you are not the only one in charge of critical aspects of the contracts (e.g. when they have proxy features, so somebody could redirect calls to unexpected code) , I would not recommend relying on delegatecall. Another solution would be msg.sender as a function parameter to another contract, and passing that address to the next one, and so on.

| improve this answer | |
  • But If I understand correctly, the answer you’re referencing seems to tell delagatecall can only be used for calling the same address (this of B would equals this of C) with other data. But in my case, I want to have contract C solidity code verified on etherscan.io and library D closed source (which is why I’d like to avoid knowing D uses msg.sender through C code) which means all contracts are stored into different addresses account. By depends if you're the author of all these contracts or not do you mean all stored at the same Ethereum address ? – user2284570 Jun 18 '18 at 0:37

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.