1.结合法
结合前面两篇文档,将上篇的 contractC 加入接口定义,并通过接口获取函数签名;
contractD.sol
pragma solidity^0.5.0;
interface ContractInterface {
function setRandomN(uint _limit) external returns(bool);
function getRandomN() view external returns(uint);
}
contract contractD {
uint public randomN;
function callSetRandomN(ContractInterface _contract, uint _limit) public returns(bool) {
bytes memory _calldata = abi.encodeWithSelector(_contract.setRandomN.selector, _limit);
(bool success, bytes memory returnData) = address(_contract).call(_calldata);
require(success == true, "call failure");
return abi.decode(returnData, (bool));
}
function callGetRandomN(ContractInterface _contract) public returns(uint) {
bytes
本文介绍了在区块链智能合约中进行合约间调用的两种方法:结合法和继承法。结合法通过接口实现contractC的功能,实测成功;继承法则通过合约继承直接调用函数,以结构体为例展示了其实现过程,并进行了编译部署及测试,验证了合约间的有效交互。
订阅专栏 解锁全文
1411

被折叠的 条评论
为什么被折叠?



