I am trying to make use of delegatecall function in solidity.

function delegateTokens(uint256 _value) returns (bool result) {
    tokens[msg.sender] = _value;
    return token.delegatecall(bytes4(sha3("transfer(address,uint256)")), address(this), _value);

There is no error thrown but values are not changed. Code is available at https://github.com/nirmalgupta/delegatecall steps to reproduce is also available there. Any help with this is appreciated.

  • Are you aware that if inside contract A you make a delegate call to B, you will modify A state but not B? In your code that delegatecall is modifyng the state of Custody contract and not the state of HumanStandardToken. – Ismael Sep 25 '17 at 5:14
  • whole idea of delegatecall is to make sure msg.sender not changed when call is made from A to B. So it should change state of B as if it was called by user who called contract A. By the way, neither contract A nor contract B is getting modified. – Nirmal Sep 25 '17 at 18:49
  • I'm afraid neither of the opcodes allow something like that ethereum.stackexchange.com/questions/3667/… – Ismael Sep 25 '17 at 19:05
  • Quote from the link says."// msg.sender is C if invoked by C.foo(). None of E's storage is updated". If you see my test code, I am making call as user1. This means msg.sender must be user1 in token.transfer call (which is correct). Then why mapping(address=>uint256) balances does not change for address user1? And if "None of E's storage is updated", then what is the use of delegatecall? This is something I am not able to understand. – Nirmal Sep 25 '17 at 23:58
  • In the test you call custody.delegateTokens(100, {from: user1});. Then we have in Custody.delegateTokens: this = custody.address and msg.sender = user1, and it delegatecalls to HumanStandardToken transfer method. In HumanStandardToken.transfer we have this = custody.address and msg.sender = user1. Delegatecall do not change this nor msg.sender. – Ismael Sep 26 '17 at 3:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.