[英]How to call the function of another smart contract in Solidity? With an example of OpenZeppelin transferOwnership function
我在学习OpenZeppelin的时候,发现它的Ownable库有一个function transferOwnership,可以给当前合约的所有者一个地址。 我可以理解将所有者更改为某人的帐户地址,但是,它也可以将所有者更改为合同地址。 我的问题是:如果我将当前合约的所有者更改为另一个合约地址,我如何使用另一个合约来处理我原始合约的所有者? 我用 super 关键字尝试了 inheritance,它不起作用。
失败代码如下。
顺便说一句,将当前合约的所有者更改为另一个合约地址是否有用? 有没有使用这种情况的示例项目?
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "@openzeppelin/contracts/access/Ownable.sol";
contract MyContract is Ownable {
function getCurrentBalance() public view onlyOwner returns (uint) {
return address(this).balance;
}
receive() external payable {}
}
contract ManageOwner is MyContract {
function changeOwner(address newOwner) public {
super.transferOwnership(newOwner);
}
}
我使用界面并成功。 代码如下:
import "@openzeppelin/contracts/access/Ownable.sol";
contract A is Ownable {
receive() external payable {}
function getCurrentBalance() public view onlyOwner returns (uint) {
return address(this).balance;
}
}
interface I {
function getCurrentBalance() external view returns (uint) ;
function transferOwnership(address newOwner) external;
}
contract B is Ownable {
I public itf = I(contract_A_address_);
receive() external payable {}
function getBalanceOfA() public view onlyOwner returns (uint) {
return itf.getCurrentBalance();
}
function changeAOwner(address newOwner) public onlyOwner{
itf.transferOwnership(newOwner);
}
}
首先,部署合约A。第二,将合约A地址复制到合约B。第三,部署B。第四,以合约B的地址作为参数调用合约A的transferOwnership function。 5、调用B合约的changeAOwner function处理合约A的所有权。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.