0

As far as I understand is, Delegate is called to pass ETH from another contract. In my perspective, it seems like a transaction goes through twice. Why we are using delegate call?

2 Answers 2

4

Delegatecall doesnt send any ETH or anything to the called contract. It just allows you to execute code from a contract in the context of another contract. Like so :

Contract Logic {
 uint a; 

 function updateA() external {
   a =1;
 }
}

Contract Proxy{
 uint a = 0;
 address logic;

 function delegate() {
  (bool s,) = logic.delegateCall(abi.encodeWithSignature("updateA()");
  require(s);
  //After that, Proxy's a will be 1, Logic's a will stay untouched 
 }
}

It's typically used for proxies (e.g: if you want to be able to update the logic later, or if you need to deploy multiple instances of the same contract and want to save on deployment costs by deploying the logic only once)

1

delegatecall is not exactly used for passing ETH or sending transactions between contracts. Instead, it is used for code reuse and interaction between smart contracts, particularly in the context of contract upgrades and proxy patterns.

  • Code Reuse and Libraries: delegatecall allows a contract to call functions from another contract or library and execute their code in the context of the calling contract. This feature enables code reuse, where a contract can access and execute functions from external libraries to perform specific tasks without duplicating the code.

  • Contract Upgrades and Proxy Patterns: By using delegatecall along with a proxy contract, you can separate the storage and logic of a contract. The proxy contract holds the state (storage), while the logic contract contains the implementation code. Upgrading the contract involves deploying a new logic contract with updated code and updating the proxy contract to use the new implementation. The proxy contract uses delegatecall to execute the logic from the new implementation while maintaining its own storage.

  • Context Preservation: When using delegatecall, the calling contract's context (storage, msg.sender, msg.value, etc.) is preserved. This means that even if the logic contract is upgraded, the proxy contract retains its state and external interfaces, making it transparent to the users.

1
  • ChatGPT answer?
    – Foxxxey
    Jul 20, 2023 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.