-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathError.sol
55 lines (45 loc) · 1.09 KB
/
Error.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
// There are three ways we can throw an Error
// 1. Require
// 2. Revert
// 3. Assert
// - gas refund, state updates are reverted
// - custom errors to save gas
contract Error {
function testRequire(uint _i) public pure {
require(_i <= 10, "i > 10");
// code
}
function testRevert(uint _i) public pure {
if(_i > 1) {
// code
if(_i > 2) {
// more code
if(_i > 10) {
revert("i > 10");
}
}
}
if(_i > 10) {
revert("i > 10");
}
// code
}
uint public num = 123;
function testAssert(uint _i) public view {
assert(num == 123);
}
function foo(uint _i) public {
// accidently updated num
num += 1;
require(_i < 10);
}
error MyError(address caller, uint i);
function testCustomError(uint _i) public view {
// code
if(_i > 10) {
revert MyError(msg.sender, _i);
}
}
}