1

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.

Nirmal
  • 11
  • 2
  • 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 05: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 https://ethereum.stackexchange.com/questions/3667/difference-between-call-callcode-and-delegatecall – 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 03:02
  • The main use case for delegatecall is a contract that is a library it only contains code and no data. For example in tokens there's a library SafeMath which has extra checking for arithmetic operations, but it doesn't store any data. – Ismael Sep 26 '17 at 03:05
  • Ahh... I got it. so in my case I am acting on different token contract. Thanks for putting so much effort to explain it. Is there some other way to get this functionality? – Nirmal Sep 26 '17 at 17:42

0 Answers0