# Solidity - 理解call和delegatecall

By [Lujet](https://paragraph.com/@lujet) · 2023-11-06

---

**理解call和delegatecall**
=======================

使用ABI编码的方式调用合约函数， 为函数调用提供了抽象能力。

使用合约地址进行底层调用：

1.  底层调用失败不是发生异常(revert), 而是用返回值表示
    
2.  call()调用**切换**上下文
    
3.  delegatecall()调用**保持**上下文\*\*(只是使用了目标合约的代码逻辑，数据还是调用方的数据)\*\*
    

![call和delegatecall的区别](https://storage.googleapis.com/papyrus_images/7625cb1befb05de7653663714c38d9e8ced32e0f66046843aea66960868be766.png)

call和delegatecall的区别

### delegatecall 的应用场景：

1.  代理合约（`Proxy Contract`）：将智能合约的存储合约和逻辑合约分开; 代理合约存储所有相关的变量，并且保存逻辑合约的地址；逻辑合约存储所有函数，通过`delegatecall`执行。当升级时，只需要将代理合约指向新的逻辑合约即可。
    
2.  [EIP-2535 Diamonds（钻石）](https://eip2535diamonds.substack.com/p/introduction-to-the-diamond-standard)：钻石是一个支持构建可在生产中扩展的模块化智能合约系统的标准。钻石是具有多个实施合同的代理合同。

---

*Originally published on [Lujet](https://paragraph.com/@lujet/solidity-call-delegatecall)*
