The Solidity document says


The use of callcode is discouraged and will be removed in the future.

But in what reason is using <address>.callcode() discouraged?

Is using CALLCODE EVM opcode also discouraged?


delegatecall is in favor of callcode, whose bug is fixed by delegatecall.

  • What is meant by the bug? – Константин Ван Nov 15 '17 at 12:06
  • So both delegatecall and callcode are taking the called (another) contract's function code into current execution context, so the state variables of current context instead of the called contract are updated. But callcode won't preserve msg.sender and msg.value - meaning if A invokes B who delegatecall to C, the msg.sender in the delegatecalled function is A (but if callcode is used then msg.sender would be B). – nrek Nov 15 '17 at 13:46
  • Isn't that what CALLCODE's supposed to do ("intended purpose")? I'd appreciate it if you tell me the reason why it is considered as a bug. – Константин Ван Nov 15 '17 at 14:40
  • 1
    Here's another function 'call' - It will make message call into the called contract's context, and it'll modify the called contract's storage space. Also if A call B B call C, then in C's context msg.sender will be B - I guess this is what you meant "intended purpose". But delegatecall and callcode are like calling library code in current context, while the only difference between them is that delegatecall preserves msg.sender and msg.value. In the context of "calling library code in current context", I think the right way is to preserve msg.sender - i.e. the bug callcode has. – nrek Nov 16 '17 at 5:48
  • “There is no need to use CALLCODE because DELEGATECALL can also do it well and is more suitable for calling library code, which is main use of CALLCODE and DELEGATECALL, than CALLCODE. Using CALLCODE instead of DELEGATECALL has more cons (that you said as a bug) than pros. So they discourage to use CALLCODE.” Have I got that right? – Константин Ван Nov 16 '17 at 11:28

Delegatecall and Callcode are very low-level functions and should only be used as a last resort as they break the type-safety of Solidity.

All these functions are low-level functions and should be used with care. Specifically, any unknown contract might be malicious and if you call it, you hand over control to that contract which could in turn call back into your contract, so be prepared for changes to your state variables when the call returns.

source: solidity_docs

  • If that is the reason, why would delegatecall not be discouraged? – Константин Ван Nov 15 '17 at 15:41
  • The use of callcode is discouraged and will be removed in the future.( – cryptoKTM Nov 15 '17 at 16:01

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.