fuel_abi_types/
error_codes.rs

1use thiserror::Error;
2
3/// Revert with this value for a failing call to `std::revert::require`.
4pub const FAILED_REQUIRE_SIGNAL: u64 = 0xffff_ffff_ffff_0000;
5
6/// Revert with this value for a failing call to `std::token::transfer_to_address`.
7pub const FAILED_TRANSFER_TO_ADDRESS_SIGNAL: u64 = 0xffff_ffff_ffff_0001;
8
9/// Revert with this value for a failing call to `std::message::send_message`.
10pub const FAILED_SEND_MESSAGE_SIGNAL: u64 = 0xffff_ffff_ffff_0002;
11
12/// Revert with this value for a failing call to `std::assert::assert_eq`.
13pub const FAILED_ASSERT_EQ_SIGNAL: u64 = 0xffff_ffff_ffff_0003;
14
15/// Revert with this value for a failing call to `std::assert::assert`.
16pub const FAILED_ASSERT_SIGNAL: u64 = 0xffff_ffff_ffff_0004;
17
18/// Revert with this value for a failing call to `std::assert::assert_ne`.
19pub const FAILED_ASSERT_NE_SIGNAL: u64 = 0xffff_ffff_ffff_0005;
20
21/// Revert with this value for a failing call to `std::revert::revert_with_log`.
22pub const REVERT_WITH_LOG_SIGNAL: u64 = 0xffff_ffff_ffff_0006;
23
24#[derive(Error, Debug)]
25pub enum ErrorSignal {
26    #[error("Failing call to `std::revert::require`.")]
27    Require,
28    #[error("Failing call to `std::token::transfer_to_address`.")]
29    TransferToAddress,
30    #[error("Failing call to `std::message::send_message`.")]
31    SendMessage,
32    #[error("Failing call to `std::assert::assert_eq`.")]
33    AssertEq,
34    #[error("Failing call to `std::assert::assert`.")]
35    Assert,
36    #[error("Failing call to `std::assert::assert_ne`.")]
37    AssertNe,
38    #[error("Failing call to `std::revert::revert_with_log`.")]
39    RevertWithLog,
40}
41
42#[derive(Error, Debug)]
43pub enum Error {
44    #[error("Unknown revert code: {0}")]
45    UnknownRevertCode(u64),
46}
47
48impl ErrorSignal {
49    /// Creates a new [ErrorSignal] from provided `revert_code`.
50    pub fn try_from_revert_code(revert_code: u64) -> Result<Self, Error> {
51        if revert_code == FAILED_REQUIRE_SIGNAL {
52            Ok(Self::Require)
53        } else if revert_code == FAILED_TRANSFER_TO_ADDRESS_SIGNAL {
54            Ok(Self::TransferToAddress)
55        } else if revert_code == FAILED_SEND_MESSAGE_SIGNAL {
56            Ok(Self::SendMessage)
57        } else if revert_code == FAILED_ASSERT_EQ_SIGNAL {
58            Ok(Self::AssertEq)
59        } else if revert_code == FAILED_ASSERT_SIGNAL {
60            Ok(Self::Assert)
61        } else if revert_code == FAILED_ASSERT_NE_SIGNAL {
62            Ok(Self::AssertNe)
63        } else if revert_code == REVERT_WITH_LOG_SIGNAL {
64            Ok(Self::RevertWithLog)
65        } else {
66            Err(Error::UnknownRevertCode(revert_code))
67        }
68    }
69
70    /// Converts this [ErrorSignal] to corresponding revert code. If the [ErrorSignal] is `Unknown`,
71    /// returns [u64::MAX].
72    pub fn to_revert_code(self) -> u64 {
73        match self {
74            ErrorSignal::Require => FAILED_REQUIRE_SIGNAL,
75            ErrorSignal::TransferToAddress => FAILED_TRANSFER_TO_ADDRESS_SIGNAL,
76            ErrorSignal::SendMessage => FAILED_SEND_MESSAGE_SIGNAL,
77            ErrorSignal::AssertEq => FAILED_ASSERT_EQ_SIGNAL,
78            ErrorSignal::Assert => FAILED_ASSERT_SIGNAL,
79            ErrorSignal::AssertNe => FAILED_ASSERT_NE_SIGNAL,
80            ErrorSignal::RevertWithLog => REVERT_WITH_LOG_SIGNAL,
81        }
82    }
83}