5

You can specify the value in the call opcode but delegatecall only lets you specify the gas.

Dylan Kerler
  • 416
  • 1
  • 3
  • 16

1 Answers1

9

When you specify a value, you are transferring ETH from the calling contract to the called contract. But delegatecall doesn't actually switch which contract is running, instead it loads the other contract's code and runs it as if it was the calling contract.

For example, if you call Michael Jordan to dunk the basketball, it makes sense to pass him the basketball at the same time (msg.value) so he can dunk it.

However, if you are delegatecall loading Stephen Curry brain into yours so you yourself can make a three point shot from where you stand, then it's you're body that will be making the shot, and it makes no sense to first pass the ball to Stephen Curry.

  • That's a nice analogy but doesn't CALLCODE also do the same thing (loading in code in context of current address)? Yet with CALLCODE you can specify msg.value too – Dylan Kerler Jun 29 '21 at 20:06
  • @DylanKerler No, CALLCODE is different from DELEGATECALL https://ethereum.stackexchange.com/questions/3667/difference-between-call-callcode-and-delegatecall. – Ismael Jul 06 '21 at 03:23
  • @Ismael Of course it's different... It's a different opcode. I mean that CALLCODE does the same thing as DELEGATECALL in that it executes code in the context of the calling contract. So why does CALLCODE have the additional parameter? – Dylan Kerler Jul 08 '21 at 09:55
  • 3
    @DylanKerler An important difference is that CALLCODE changes msg.sender and DELEGATECALL doesn't. It makes sense to support transfers for CALLCODE since there's a sender and recipient. – Ismael Jul 08 '21 at 14:02
  • What a great real-life example! +1 – Mila A Mar 14 '24 at 19:12