// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.8.2 <0.9.0; contract Callee { uint public x; function setX(uint _x)public{ x = _x; } } contract Caller{ address calleeAddress; constructor(address _calleeAddress){ calleeAddress = _calleeAddress; } function setCalleeX(uint _x)public{ Callee callee = Callee(calleeAddress); callee.setX(_x); } } //0xd9145CCE52D386f254917e481eB44e9943F39138 // Callee合约实例的地址 先部署Callee合约获取地址作为Caller的构造参数
Callee.sol
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.8.2 <0.9.0; contract Callee { uint public x; function setX(uint _x)public{ x = _x; } }
Caller.sol
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.8.2 <0.9.0; import "./Callee.sol"; contract Caller{ address calleeAddress; constructor(address _calleeAddress){ calleeAddress = _calleeAddress; } function setCalleeX(uint _x)public{ Callee callee = Callee(calleeAddress); callee.setX(_x); } } // 0xd9145CCE52D386f254917e481eB44e9943F39138
ICallee.sol
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.8.2 <0.9.0; interface ICallee { function setX(uint _x) external; }
Callee.sol
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.8.2 <0.9.0; // import "./ICallee.sol"; // 柔性,不强制一定要is Icallee 只要实现是一致就行 // contract Callee is Icallee { contract Callee { uint public x; uint public y; function setX(uint _x)public{ x = _x; } function setY(uint _y)public { y=_y; } //..... }
Caller.sol
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.8.2 <0.9.0; interface ICallee { function setX(uint _x) external; function setY(uint _x) external; } contract Caller{ address calleeAddress; constructor(address _calleeAddress){ calleeAddress = _calleeAddress; } function setCalleeX(uint _x)public{ ICallee callee = ICallee(calleeAddress); callee.setY(_x); } } // 0xd9145CCE52D386f254917e481eB44e9943F39138