header-langage
简体中文
English
Tiếng Việt
Scan to Download the APP
Partnered With:

Simple Introduction to Solidity|Lecture 23: Delegatecall

Read this article in 10 Minutes
This article introduces another low-level function delegatecall in solidity.
Original title: " A minimalist introduction to Solidity: 23. Delegatecall "
Original author: 0xAA


I am re-learning solidity recently, to consolidate the details, and also write an "Introduction to Solidity Minimalist", For novices (programmers can find other tutorials), 1-3 lectures are updated every week.


All codes and tutorials are open source on github: github.com/AmazingAng/WTFSolidity


delegatecall


Delegatecall is similar to call and is a low-level member function of address type in solidity. delegate means delegate/representative, so what does delegatecall delegate?


When user A calls contract C through contract B, the function of contract C is executed. Context (Context can be understood as including variables and states environment) is also of contract C: msg.sender is the address of B, and if the function changes some state variables, the resulting effect will act on the variables of contract C.


  context of call

And when user A delegatecalls contract C through contract B, the function of contract C is executed, but the context is still that of contract B: msg.sender is the address of A, and if the function changes some state variables, the effect will be Will act on the variables of contract B.  


The context of delegatecall 

You can understand it this way: a wealthy businessman hands all his assets (state variables) Give it to a VC agent (the function of the target contract) to take care of it. What is executed is the function of VC, but what is changed is the state of the rich merchant.


Delegatecall syntax is similar to call, also:    



  The binary code is obtained by using the structured coding function abi.encodeWithSignature:



The function signature is "function name (parameter types separated by commas)". For example abi.encodeWithSignature("f(uint256,address)", _x, _addr).


Different from call, delegatecall can specify the gas sent by the transaction when calling the contract, but cannot specify the amount of ETH sent


Note: delegatecall has security risks. When using it, ensure that the state variable storage structure of the current contract and the target contract are the same, and the target contract is safe, otherwise it will cause asset loss.


Under what circumstances will delegatecall be used?


At present, there are two main application scenarios for delegatecall :


Proxy Contract (Proxy Contract): Separate the storage contract and logic contract of the smart contract: the proxy contract (Proxy Contract) stores all related variables and saves The address of the logic contract; all functions are stored in the logic contract (Logic Contract) and executed through delegatecall. When upgrading, just point the proxy contract to the new logic contract.


EIP-2535 Diamonds: Diamonds is a standard that supports building modular smart contract systems that scale in production. Diamond is an agency contract with multiple fulfillment contracts. For more information, see: About the Diamond Standard.


delegatecall example


Call structure: you (A) call the target contract C through contract B .


Called contract C


Let’s write a simple target contract C first: There are two public variables: num and sender, which are uint256 and address types respectively; there is a function that can set num as the incoming _num, and set sender as msg.sender.



The calling contract B


First, the variable storage layout of the contract B must be the same as that of the target contract C, the two variables, And the order is num and sender



Next, we use call and delegatecall to call the setVars function of contract C to better understand their differences.


The callSetVars function invokes setVars via call. It has two parameters _addr and _num, which correspond to the address of contract C and the parameters of setVars respectively.



and the delegatecallSetVars function calls setVars through delegatecall. Same as the callSetVars function above, there are two parameters _addr and _num, which correspond to the address of contract C and the parameters of setVars respectively.   



Run result


We put contract B and C All deployed, and then call callSetVars in contract C, the incoming parameters are contract B address and 10. After running, the state variables in contract C will be modified: num will be changed to 10, and sender will be the address of contract B.


Next, we call delegatecallSetVars in contract C, and the incoming parameters are contract B address and 100. Since it is a delegatecall, the context is contract B. After running, the state variables in contract B will be modified: num is changed to 100, and sender is changed to your wallet address of the contract.


Summary


In this lecture we introduced another low-level function delegatecall in solidity. Similar to call, it can be used to call other contracts; the difference lies in the running context, B call C, the context is C; and B delegatecall C, the context is B. At present, the largest applications of delegatecall are proxy contracts and EIP-2535 Diamonds (diamonds).


Original link


Welcome to join the official BlockBeats community:

Telegram Subscription Group: https://t.me/theblockbeats

Telegram Discussion Group: https://t.me/BlockBeats_App

Official Twitter Account: https://twitter.com/BlockBeatsAsia

举报 Correction/Report
Choose Library
Add Library
Cancel
Finish
Add Library
Visible to myself only
Public
Save
Correction/Report
Submit