current position:Home>How to use delegatecall and what is its specific function?

How to use delegatecall and what is its specific function?

2022-02-03 20:53:28 Q & A of Denglian community

Look at this file , I still don't quite understand . Can a simple piece of code explain .




Take the answer 1:

I was just there stackexchange Find a good example to explain Delegate invocation delegatecall:

contract D {  uint public n;  address public sender;  function delegatecallSetN(address _e, uint _n) {    _e.delegatecall(bytes4(keccak256("setN(uint256)")), _n); // D's storage is set, E is not modified   }}contract E {  uint public n;  address public sender;  function setN(uint _n) {    n = _n;    sender = msg.sender;  }}

When used C call delegatecallSetN, D Of sender Set to C (E Not modify ).

delegatecall It's equivalent to another contract ( Or library ) Function of “ PULL ” To the current contract (D) To execute , Just like the internal function of the current contract .

A classic use of delegate invocation is contract escalation , If contract data is separated from logic , Logical functions are implemented by delegate calls , It is easy to upgrade the logic .




copyright notice
author[Q & A of Denglian community],Please bring the original link to reprint, thank you.
https://en.netfreeman.com/2022/02/202202032053256605.html

Random recommended