ethereum - 坚固性修饰符

标签 ethereum solidity smartcontracts modifiers

因此,作为 Solidity 位学位类(class)的一部分,我希望创建一个名为 onlyOwner 的修饰符并将其分配给 changePrice 函数。我必须确保修饰符仅在发送者的地址与所有者的地址匹配时才允许执行函数。可以使用 msg.sender 获取发件人地址。

我尝试输入此内容来创建修改器,但它对我不起作用,我不确定为什么。任何帮助/推荐的代码将不胜感激!

pragma solidity ^0.4.17;

contract ModifiersTutorial {

address public owner;
uint256 public price = 0;
address public sender=msg.sender;

//
modifier onlyOwner(sender){
if (owner==sender);
}
//

// Use your modifier on the function below
function changePrice(uint256 _price) public onlyOwner {
    price = _price;
}

function ModifiersTutorial () {
    owner = msg.sender; // msg.sender in constructor equals to the address that created the contract
}

}

最佳答案

您的修饰符代码不正确。您需要一个下划线才能继续。

modifier onlyOwner(sender){
  if (owner==sender) _; // Note the underscore
}

此外,出于安全原因,您确实应该只使用 msg.sender 而不是将其传入。

modifier onlyOwner() {
  if (owner == msg.sender) _;
}

关于ethereum - 坚固性修饰符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48409082/

相关文章:

node.js - Web3.js 查看方法 call() 错误,因为返回值无效,是否耗尽了气体

solidity - 为什么松露返回 <indexed> 而不是事件的实际值?

ethereum - 与 Uni V2 交互时如何修复 'TransferHelper: ETH_TRANSFER_FAILED'

solidity - 为什么在这个智能合约上使用断言?

blockchain - Metamask注入(inject)的window.ethereum和web3.js之间有什么关系吗?我们可以两者都使用吗?

solidity - 如何使用松露控制台

ethereum - `window.ethereum` 文档在哪里?

javascript - 有没有办法一次多次调用 JavaScript 中的函数?使用 puppeteer 师

node.js - 如何从 node.js 应用程序永久监听智能合约中的事件?

ethereum - 以太坊 Solidity 的划分