-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctionModifiers
59 lines (40 loc) · 1.36 KB
/
functionModifiers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
pragma solidity ^0.8.0;
/* Excercise:
- Create a function modifier called costs for the register function that checks to
see the sender's (hint look up msg.value) requires to be greater than or equal
to the price. Second hint, the function modifier should take an argument. */
contract Owner {
//function modifiers
address owner;
// upon deployment, set address to the owner (msg.sender)
constructor() {
owner = msg.sender;
}
// to write a modifier, statically declare the modifier keyword and then name it
// and inside, write the logic and modifications
modifier onlyOwner {
require(msg.sender == owner);
_;
// the underscore continues with the function
}
modifier costs (uint price) {
require(msg.value >= price);
_;
}
}
contract Register is Owner {
mapping (address => bool) registeredAddresses;
uint price;
constructor(uint initialPrice) {
price = initialPrice;
}
// function register will set msg.sender (current caller) to true
function register() public payable costs(price) {
registeredAddresses[msg.sender] = true;
}
// onlyOwner is the function modifier that requires
// only the owner to be able to change the price
function changePrice(uint _price) public onlyOwner {
price = _price;
}
}