ReasonJun

Solidity : call() vs delegatecall() 본문

Blockchain/Solidity

Solidity : call() vs delegatecall()

ReasonJun 2023. 10. 18. 17:28
728x90

The main difference between call() and delegatecall() in Solidity is that call() executes code in a new context, while delegatecall() executes code in the context of the caller.

 

call()

When you call a function using call(), the called function is executed in a new context, with its own stack and storage. This means that the called function does not have access to the caller's state, and any changes made to the state inside the called function will not be reflected in the caller's state.

delegatecall()

When you call a function using delegatecall(), the called function is executed in the context of the caller. This means that the called function has access to the caller's state, and any changes made to the state inside the called function will be reflected in the caller's state.

Here is a table that summarizes the key differences between call() and delegatecall():

Feature call() delegatecall()
Execution context New Caller
Access to caller's state No Yes
Changes to caller's state No Yes

Here are some examples of when you might use call() and delegatecall():

  • You would use call() if you want to call a function on another contract without giving it access to your state. For example, you might use call() to call a function on a smart contract that exchanges cryptocurrency.
  • You would use delegatecall() if you want to call a function on another contract and give it access to your state. For example, you might use delegatecall() to call a function on a smart contract that implements a proxy contract.
728x90

'Blockchain > Solidity' 카테고리의 다른 글

Solidity : fallback() vs receive()  (0) 2023.10.18
Solidity : receive()  (0) 2023.10.18
Solidity : Delegatecall  (0) 2023.10.18
Solidity : call()  (0) 2023.10.18
Solidity : transfer()  (0) 2023.10.18
Comments