-
Notifications
You must be signed in to change notification settings - Fork 2
/
040-error-handling-in-solidity.sol
81 lines (65 loc) · 2.65 KB
/
040-error-handling-in-solidity.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* @title Error Handling in Solidity
* @notice Solidity has functions that help in error handling
* @dev A way of tackling this is that when an error happens, the state reverts back to its original state.
Below are some of the important methods for error handling:
assert(bool condition) − In case condition is not met, this method call causes an
invalid opcode and any changes done to state got reverted. This method is to be used for internal errors.
require(bool condition) − In case condition is not met, this method call reverts to original state.
- This method is to be used for errors in inputs or external components.
require(bool condition, string memory message) − In case condition is not met, this method call reverts to original state.
- This method is to be used for errors in inputs or external components. It provides an option to provide a custom message.
revert() − This method aborts the execution and revert any changes done to the state.
revert(string memory reason) − This method aborts the execution and revert any changes done to the state.
It provides an option to provide a custom message.
*/
contract LearnErrorHandling {
bool public sunny = true;
bool public umbrella = false;
uint256 finalCalc = 0;
// solar panel machine
function solarCalc() public {
// it should be sunny
// require(sunny);
require(sunny, "It is not sunny today!");
finalCalc += 3;
}
function internalTestUnits() public view {
assert(finalCalc != 6);
}
// machine that controls the weather
function weatherChange() public {
sunny = false;
}
// get finalCalc value
function getCalc() public view returns (uint256) {
return finalCalc;
}
// determine if user should bring an unbrella
// or not
function bringUmbrella() public {
if (!sunny) {
umbrella = true;
} else {
revert("No need to bring an umbrella today!");
}
}
}
contract Vendor {
address seller;
// modifier to let the msg.sender be the seller
modifier onlySeller() {
require(msg.sender == seller, "Only the seller can sell this!");
_;
}
// function to sell --only if you are the seller
function sell(uint256 amount) public payable onlySeller {
if (amount > msg.value) {
// revert the contract if the funds in msg.sender is smaller
// than the requested amount or price of the good.
revert("There is not enought Ether provided!");
}
}
}