-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18_Error.sol
More file actions
57 lines (43 loc) · 1.74 KB
/
Copy path18_Error.sol
File metadata and controls
57 lines (43 loc) · 1.74 KB
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
// An error will undo all changes made to the state during a transaction.
// You can throw an error by calling require, revert or assert.
// require is used to validate inputs and conditions before execution.
// revert is similar to require. See the code below for details.
// assert is used to check for code that should never be false. Failing assertion probably means that there is a bug.
// Use custom error to save gas.
contract Error {
// Require should be used to validate conditions such as:
// - inputs
// - conditions before execution
// - return values from calls to other functions
function testRequire(uint _i) public pure {
require(_i > 10, "Input must be greater than 10");
}
// Revert is useful when the condition to check is complex.
// This code does the exact same thing as the example above
function testRevert(uint _i) public pure {
if (_i <= 10) {
revert("Input must be greater than 10");
}
}
// Assert should only be used to test for internal errors,
// and to check invariants.
// Here we assert that num is always equal to 0
// since it is impossible to update the value of num
uint public num;
function testAssert() public view {
assert(num == 0);
}
// custom error
error InsufficientBalance(uint balance, uint withdrawAmount);
function testCustomError(uint _withdrawAmount) public view {
uint bal = address(this).balance;
if (bal < _withdrawAmount) {
revert InsufficientBalance({
balance: bal,
withdrawAmount: _withdrawAmount
});
}
}
}