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

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

你的鼓励,我的动力!

相关推荐
manyunkeji011 小时前
DApp系统开发包含哪些核心功能?
区块链
Richown1 小时前
ERC-725 与 ERC-735 去中心化身份实现:声明发布、验证请求与链上凭证管理
区块链·react·erc-725
醉颜凉1 小时前
区块链不可篡改机制全解析:从哈希锁链到51%攻击
算法·区块链·哈希算法
菜鸟~noob2333 小时前
【AI+区块链】第二篇:EVMbench 技术解剖——一个 LLM Agent 智能合约漏洞检测 Harness 的工程架构与代码实现
人工智能·区块链·智能合约
Shota Kishi2 天前
Solana Stream SDK 的 UDP Shred 接收:data-only 与 recover 两种模式的取舍与实测
rust·区块链·solana
阿菜ACai3 天前
20261203-APE Chain 2.68 亿代币增发事件:深藏于链层面的回滚缺陷
区块链·智能合约·漏洞分析
OxYGC5 天前
[Web3] 钱包业务第十六篇: Solana 钱包链调研,钱包 SDK 开发,Staking 开发
web3·区块链·以太坊·比特币
大今野6 天前
人工智能与区块链核心考点全解析
人工智能·区块链·uml·专转本
莫叫石榴姐6 天前
现货黄金技术分析:3427破而复收之后,关键价位与下周情景推演
区块链·可信计算技术
OxYGC6 天前
[Web3] 钱包业务第十五篇: Bitcoin 链调研与钱包开发
web3·区块链·以太坊·比特币