forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
revert.sw
62 lines (59 loc) · 1.42 KB
/
revert.sw
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
//! Functions to panic or revert with a given error code.
library;
use ::logging::log;
use ::error_signals::FAILED_REQUIRE_SIGNAL;
/// Will either panic or revert with a given number depending on the context.
/// If used in a predicate, it will panic.
/// If used in a contract, it will revert.
///
/// ### Arguments
///
/// * `code` - The code with which to revert the program.
///
/// ### Reverts
///
/// Reverts when called in a contract.
///
/// ### Panics
///
/// Panics when called in a predicate.
///
/// ### Examples
///
/// ```sway
/// fn foo(should_revert: bool) {
/// match should_revert {
/// true => revert(0),
/// false => {},
/// }
/// }
/// ```
pub fn revert(code: u64) {
__revert(code)
}
/// Checks if the given `condition` is `true` and if not, logs `value` and reverts.
///
/// ### Arguments
///
/// * `condition` - The condition upon which to decide whether to revert or not.
/// * `value` - The value which will be logged in case `condition` is `false`.
///
/// ### Reverts
///
/// Reverts when `condition` is `false`.
///
/// ### Examples
///
/// ```sway
/// fn foo(a: u64, b: u64) {
/// require(a == b, "a was not equal to b");
/// // If the condition was true, code execution will continue
/// log("The require function did not revert");
/// }
/// ```
pub fn require<T>(condition: bool, value: T) {
if !condition {
log(value);
revert(FAILED_REQUIRE_SIGNAL)
}
}