delegatecall在不同版本的Solidity中的使用语法

假如合约A通过delegatecall调用合约B的test()函数

(1) 对于0.4.26版本的Solidity,delegatecall的使用语法语法如下:

ContractBAddress.delegatecall(bytes4(keccak256("test()"))); // test为函数名

(2) 对于0.6.8版本的Solidity,delegatecall的使用语法语法如下:

contract A {

bool flag;

bytes memory result;

function callLogicContract(address logicContractAddr) public {

(flag, result) = ContractAddr.delegatecall(abi.encodeWithSignature("test(address)", this));

emit Log("delegatecall return ", r); // r为true或false

uint256 result = bytesToUint(s);

emit Log2("test() return ", result);

}

}

contract B{

uint256 public num1;

uint256 public num2;

uint256 public num3; //num1, num2, num3声明的顺序和位置必须和DataContract中的一样,否则无法修改DataContract中的num3

address public caller;

address public dataContract;

event Log(string str, uint256 num);

event Log2(string str, address addr);

function test(address addr) public returns(uint256) {

dataContract = addr; // 数据合约DataContract地址

caller = msg.sender; // 运行数据合约DataContract的以太坊账号

num3 = num3*10; // DataContract通过delegatecall调用LogicContract的test函数,test函数里修改的是DataContract中的num3而不是LogicContract中的num3

emit Log("after upgrading contract, num3=", num3);

emit Log2("dataContract=", dataContract);

emit Log2("caller=", caller);

return num3;

}

}

我的csdn:powervip-CSDN博客

我的学习笔记:www.0101.vip

我的github: https://github.com/powervip

如果你觉得这篇文章写得还可以,请帮忙点个赞,谢谢!

你的鼓励,我的动力!

相关推荐
磐链科技16 小时前
区块链开发的技术核心与步骤实施
区块链
区块链小八歌17 小时前
从 Berachain Dev 活跃于 Robinhood Chain,看 Berachain 的生态影响力
区块链
今天AI了吗18 小时前
去中心化 AI 反馈系统:数据不上链,凭证与激励分开管
人工智能·windows·python·数据分析·去中心化·区块链·embedding
磐链科技19 小时前
2026年9月Web3观察:机构入场加速,安全警钟长鸣
安全·web3·区块链
ZDGJ60992 天前
恒指期货的交易规则有哪些
区块链
Web3李李3 天前
短剧+Web3:破解行业获客留客难题,打造零门槛用户增长闭环
web3·去中心化·区块链·软件开发·dapp开发·web3短剧·web3项目开发
磐链科技3 天前
异构链的“翻译官”:区块链开发中跨链消息传递协议(IBC)的代码实现拆解
区块链
ZDGJ60994 天前
如何选择适合自己的恒指期货产品
区块链
水母量化4 天前
自动化开盘卖出如何适应价格笼子规则
运维·自动化·区块链·量化交易
IvorySQL4 天前
PostgreSQL 日报 |SUM-AVG 溢出检测不足(9 月 5 日)
数据库·postgresql·区块链