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

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

你的鼓励,我的动力!

相关推荐
MuMuMu12231 天前
越华环保集团碳惠小屋:物联网 + 区块链,构建碳普惠终端数据可信与隐私保护体系
物联网·区块链
IvorySQL1 天前
PostgreSQL 日报| PgBouncer 修复三个 CVE 漏洞(10 月 15 日)
数据库·postgresql·区块链
十二同学啊2 天前
反洗钱体系详解:KYC、风险评级、交易监控与可疑交易报告
web3·区块链·智能合约
海宇大数据2 天前
零信任架构实战:基于海宇公安二要素认证即时版构建自动化证券账户实名鉴权网关
人工智能·架构·自动化·区块链
磐链科技2 天前
DApp技术架构全面解析:从区块链底层、前后端开发到链上数据交互体系设计
架构·区块链
ZDGJ60992 天前
国际期货交易时段流动性影响因素
区块链
IvorySQL3 天前
PostgreSQL 日报| pg_vectorsearch 开源向量搜索发布(10 月 14 日)
数据库·人工智能·postgresql·开源·区块链
IvorySQL4 天前
PostgreSQL 日报|堆表选择性索引更新(10 月 13 日)
数据库·人工智能·postgresql·开源·区块链
hughnz4 天前
代币化石油:用区块链实现能源资产数字化
区块链