关于msg.sender

// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract owned { address owner; constructor() { owner = msg.sender; } //定义一个函数修改器,可被继承 //修饰时,函数体被插入到“;”处 //不符合条件时,将抛出异常 modifier onlyOwner { //当使用不是部署合约的账号调用时,就会出错 require(owner == msg.sender); ;

}

}

//函数修改器可被继承 contract testContract is owned {

constructor()  {
    owner = msg.sender;
}
function hello() public pure returns (string memory) {
    return "hello";
}
function innocence() public  {
    //只有合约创建者才能销毁
    this.hello();
    selfdestruct(payable(owner)); //销毁合约
}
//含有参数,只有大于18才能触发合约销毁
modifier over18(uint8 age)  {
    require(age >= 18);
    _;
}

function Kill(uint8 _age) public onlyOwner over18(_age){
    //只有合约创建者才能销毁
    selfdestruct(payable(owner)); 
}

}

contract testCall { address owner; function hello() public pure returns (string memory) { return "hello"; } function call(address a) public { //这个回将主调合约给销毁了 a.delegatecall(abi.encodeWithSignature("innocence()")); } } /所以在使用合约销毁要特别小心/

我想请问一下,就是我这三个合约是实现了只有部署合约的账号能调用自毁函数 selfdestruct,可是require(owner == msg.sender),不就是任何调用的合约都可以(真实运行是只有部署合约的地址可以),按照我的理解msg.sender是指向调用合约的地址。

请先 登录 后评论

1 个回答

PureBlack - 区块链开发
请先 登录 后评论
  • 1 关注
  • 0 收藏,1679 浏览
  • tianzai 提出于 2022-09-13 11:47

相似问题