// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
// Function Modifiers are customizable modification for functions
contract Owner {
address owner;
// 当部署合约的时候,我们想把地址设置给owner(msg.sender)
constructor() public {
owner = msg.sender;
}
modifier onlyOwner() {
// 个性化的逻辑,修改function
require(msg.sender == owner);
// _代表这个function还在继续
_;
}
// 练习: 写一个叫costs的修饰符,用于检查sender的金额比price大,并且需要接受一个参数
modifier costs(uint256 price) {
// what msg.value: 表示发送给合约的信息中带有的金额
require(msg.value >= price);
_;
}
}
contract Register is Owner {
mapping(address => bool) registeredAddresses;
uint256 price;
constructor(uint256 initialPrice) public {
price = initialPrice;
}
function register() public payable costs(price) {
registeredAddresses[msg.sender] = true;
}
// onlyOwner 就是 函数修饰符
// 作用时表示只有owner可以改变价格
function changePrice(uint256 _price) public onlyOwner {
price = _price;
}
}