-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path11_FunctionModifiers.sol
66 lines (53 loc) · 1.63 KB
/
11_FunctionModifiers.sol
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
57
58
59
60
61
62
63
64
65
66
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract FunctionModifiers{
bool public locked;
uint public count;
function enableLocked(bool _locked) external{
locked = _locked;
}
// Function Modifiers are used to modify the behaviour of a function.
// For example to add a prerequisite to a function.
modifier LockEnabled() {
require(!locked,"Locked");
// The function body is inserted where the special symbol "_;"
// appears in the definition of a modifier.
// So if condition of modifier is satisfied while calling this function, the function is executed and otherwise, an exception is thrown.
_;
}
function increment() external{
require(!locked,"Locked");
count+=1;
}
function decrement() external{
require(!locked,"Locked"); // The Same require statement is used
// which is redundant , it can be condensed into a modifier.
count-=1;
}
function incrementWithModifier() external LockEnabled{
count+=1;
}
function decrementWithModifier() external LockEnabled{
count-=1;
}
//modifier with input parameter
modifier checkThreshold(uint _num){
// The number cannot be greater than 1000 as an increment value.
require(_num<=1000,"Greater than 1000");
_;
}
function incrementInThreshold(uint _num) external LockEnabled checkThreshold(_num){
count+=_num;
}
modifier runFurtherCode(){
// First it will increment the count by 10
count+=10;
// Return to the function caller
_;
// Increment the overall value by 20
count+=20;
}
function runningFurtherCode() external runFurtherCode{
count+=100;
}
}