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

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

你的鼓励,我的动力!

相关推荐
2601_962885722 天前
2026 Python 股票数据接口选型指南(横评 + 决策树)
开发语言·python·区块链
磐链科技2 天前
智能合约系统:去中心化世界的可信执行基础设施
去中心化·区块链·智能合约
Leo.yuan2 天前
集团司库系统之外还需要什么?2026年资金分析与司库决策平台建设指南
大数据·人工智能·区块链
YFJ_mily4 天前
第二届区块链技术与基础模型国际学术会议(BTFM2027)
算法·区块链·能源·智能系统·rdlink研发家·机器人自动化
ss2734 天前
PostgreSQL表级锁、行级锁、轻量级锁
数据库·postgresql·区块链
磐链科技4 天前
区块链NFT链游开发未来发展趋势:解析多链互操作、游戏资产流通与Web3生态融合的创新方向
区块链
IvorySQL5 天前
PostgreSQL 日报| WAL summarizer 断言失败(10 月 26 日)
数据库·postgresql·区块链
Gu_WenYun5 天前
市场波动应对策略:灵活与坚定的平衡
人工智能·金融·区块链
磐链科技5 天前
交易所钱包开发的核心技术:解析多链集成、私钥管理与冷热钱包安全架构
安全·区块链·安全架构
MuMuMu12236 天前
越华环保集团碳惠小屋:边缘感知 + 区块链存证,碳普惠终端工程化落地实践
区块链