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

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

你的鼓励,我的动力!

相关推荐
新新学长搞科研15 小时前
【会议征稿】第三届数字经济与计算机科学国际学术会议(DECS 2026)
区块链·计算机科学·数字经济
梦帮科技20 小时前
【3.0】上线与运维:节点监控、水龙头、区块浏览器与安全清单
运维·安全·web安全·金融·区块链·密码学·安全架构
kjmkq21 小时前
区块链项目出海宣发,依托全球Web3垂直媒体快速建立行业背书
web3·区块链·媒体
梦帮科技2 天前
【3.0修订版】跨链中继与事件编排:Relayer 监听、多签验证与故障自愈
web安全·金融·区块链·密码学·智能合约·安全架构·信任链
ZDGJ60992 天前
如何判断国际期货交易品种的流动性
区块链
梦帮科技2 天前
【3.0修订版】钱包与安全:Web4 智能钱包、硬件隔离与后量子密码实践
安全·去中心化·区块链·智能合约·同态加密·共识算法·信任链
梦帮科技2 天前
【3.0修订版】音乐权利 NFT:MusicRightsNFT 的确权思想与授权关联
网络协议·算法·web3·区块链·密码学·智能合约·零知识证明
脉动数据行情12 天前
Python asyncio 异步实现比特币 BTC 实时行情监听 高并发版
开发语言·python·区块链