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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
pub use jsonrpsee::core::Error as JsonRpseeError;

use crate::{types::*, BTC_RELAY_MODULE, ISSUE_MODULE, SYSTEM_MODULE, VAULT_REGISTRY_MODULE};
use codec::Error as CodecError;
use jsonrpsee::{
    client_transport::ws::WsHandshakeError,
    types::error::{CallError, ErrorObjectOwned},
};
use prometheus::Error as PrometheusError;
use serde_json::Error as SerdeJsonError;
use sp_core::crypto::SecretStringError;
use std::{array::TryFromSliceError, fmt::Debug, io::Error as IoError, num::TryFromIntError, str::Utf8Error};
use subxt::error::{DispatchError, TransactionError};
pub use subxt::{error::RpcError, Error as SubxtError};
use thiserror::Error;
use tokio::time::error::Elapsed;
use url::ParseError as UrlParseError;

#[derive(Error, Debug)]
pub enum Error {
    #[error("Could not get exchange rate info")]
    ExchangeRateInfo,
    #[error("Could not get issue id")]
    RequestIssueIDNotFound,
    #[error("Could not get redeem id")]
    RequestRedeemIDNotFound,
    #[error("Could not get replace id")]
    RequestReplaceIDNotFound,
    #[error("Could not get block")]
    BlockNotFound,
    #[error("Could not get foreign asset")]
    AssetNotFound,
    #[error("Could not unlock local asset registry")]
    CannotOpenAssetRegistry,
    #[error("Cannot acquire lock for lending assets")]
    CannotAccessLendingAssets,
    #[error("Could not get vault")]
    VaultNotFound,
    #[error("Vault has been liquidated")]
    VaultLiquidated,
    #[error("Channel closed unexpectedly")]
    ChannelClosed,
    #[error("Cannot replace existing transaction")]
    PoolTooLowPriority,
    #[error("Transaction did not get included - block hash not found")]
    BlockHashNotFound,
    #[error("Transaction is invalid: {0}")]
    InvalidTransaction(String),
    #[error("Request has timed out")]
    Timeout,
    #[error("Block is not in the relay main chain")]
    BlockNotInRelayMainChain,
    #[error("Invalid currency")]
    InvalidCurrency,
    #[error("Invalid keyring arguments")]
    KeyringArgumentError,
    #[error("Failed to parse keyring account")]
    KeyringAccountParsingError,
    #[error("Storage item not found")]
    StorageItemNotFound,
    #[error("Insufficient funds")]
    InsufficientFunds,
    #[error("Currency not found")]
    CurrencyNotFound,
    #[error("Operation not supported on token variant")]
    TokenUnsupported,

    #[error("Client does not support spec_version: expected {0}..{1}, got {2}")]
    InvalidSpecVersion(u32, u32, u32),
    #[error("Client metadata is different from parachain metadata: expected {0}, got {1}")]
    ParachainMetadataMismatch(String, String),
    #[error("Specified Bitcoin network differs from the one on the parachain: expected {0}, got {1}")]
    BitcoinNetworkMismatch(String, String),
    #[error("Failed to load credentials from file: {0}")]
    KeyLoadingFailure(#[from] KeyLoadingError),
    #[error("Error serializing: {0}")]
    Serialize(#[from] TryFromSliceError),
    #[error("Error converting: {0}")]
    Convert(#[from] TryFromIntError),
    #[error("Subxt runtime error: {0}")]
    SubxtRuntimeError(#[from] SubxtError),
    #[error("Error decoding: {0}")]
    CodecError(#[from] CodecError),
    #[error("Error encoding json data: {0}")]
    SerdeJsonError(#[from] SerdeJsonError),
    #[error("Error getting json-rpsee data: {0}")]
    JsonRpseeError(#[from] JsonRpseeError),
    #[error("Timeout: {0}")]
    TimeElapsed(#[from] Elapsed),
    #[error("UrlParseError: {0}")]
    UrlParseError(#[from] UrlParseError),
    #[error("PrometheusError: {0}")]
    PrometheusError(#[from] PrometheusError),
    #[error("Utf8Error: {0}")]
    Utf8Error(#[from] Utf8Error),
    // TODO: implement Display
    #[error("BitcoinError: {0}")]
    BitcoinError(String),
}

impl From<module_bitcoin::Error> for Error {
    fn from(value: module_bitcoin::Error) -> Self {
        Self::BitcoinError(format!("{value:?}"))
    }
}

impl Error {
    pub fn to_human(self) -> String {
        match self {
            Error::SubxtRuntimeError(SubxtError::Runtime(DispatchError::Module(module_error))) => {
                match module_error.as_root_error::<crate::metadata::Error>() {
                    Ok(root_error) => format!("{:?}", root_error),
                    Err(_) => format!("Unknown error: {:?}", module_error.raw()),
                }
            }
            err => err.to_string(),
        }
    }

    pub fn is_any_module_err(&self) -> bool {
        matches!(
            self,
            Error::SubxtRuntimeError(SubxtError::Runtime(DispatchError::Module(_))),
        )
    }

    fn is_module_err(&self, pallet_name: &str, error_name: &str) -> bool {
        if let Error::SubxtRuntimeError(SubxtError::Runtime(DispatchError::Module(module_error))) = self {
            if let Ok(details) = module_error.details() {
                return details.pallet.name() == pallet_name && details.variant.name == error_name;
            }
        }
        false
    }

    pub fn is_duplicate_block(&self) -> bool {
        self.is_module_err(BTC_RELAY_MODULE, &format!("{:?}", BtcRelayPalletError::DuplicateBlock))
    }

    pub fn is_invalid_chain_id(&self) -> bool {
        self.is_module_err(BTC_RELAY_MODULE, &format!("{:?}", BtcRelayPalletError::InvalidChainID))
    }

    pub fn is_issue_completed(&self) -> bool {
        self.is_module_err(ISSUE_MODULE, &format!("{:?}", IssuePalletError::IssueCompleted))
    }

    pub fn is_threshold_not_set(&self) -> bool {
        self.is_module_err(
            VAULT_REGISTRY_MODULE,
            &format!("{:?}", VaultRegistryPalletError::ThresholdNotSet),
        )
    }

    fn map_custom_error<T>(&self, call: impl Fn(&ErrorObjectOwned) -> Option<T>) -> Option<T> {
        if let Error::SubxtRuntimeError(SubxtError::Rpc(RpcError::ClientError(e))) = self {
            match e.downcast_ref::<JsonRpseeError>() {
                Some(e) => match e {
                    JsonRpseeError::Call(CallError::Custom(err)) => call(err),
                    _ => None,
                },
                None => {
                    log::error!("Failed to downcast RPC error; this is a bug please file an issue");
                    None
                }
            }
        } else {
            None
        }
    }

    pub fn is_invalid_transaction(&self) -> Option<String> {
        self.map_custom_error(|custom_error| {
            if custom_error.code() == POOL_INVALID_TX {
                Some(custom_error.data().map(ToString::to_string).unwrap_or_default())
            } else {
                None
            }
        })
    }

    pub fn is_pool_too_low_priority(&self) -> Option<()> {
        self.map_custom_error(|custom_error| {
            if custom_error.code() == POOL_TOO_LOW_PRIORITY {
                Some(())
            } else {
                None
            }
        })
    }

    pub fn is_rpc_disconnect_error(&self) -> bool {
        match self {
            Error::SubxtRuntimeError(SubxtError::Rpc(RpcError::ClientError(e))) => {
                match e.downcast_ref::<JsonRpseeError>() {
                    Some(e) => matches!(e, JsonRpseeError::RestartNeeded(_)),
                    None => {
                        log::error!("Failed to downcast RPC error; this is a bug please file an issue");
                        false
                    }
                }
            }
            Error::SubxtRuntimeError(SubxtError::Rpc(RpcError::SubscriptionDropped)) => true,
            _ => false,
        }
    }

    pub fn is_rpc_error(&self) -> bool {
        matches!(self, Error::SubxtRuntimeError(SubxtError::Rpc(_)))
    }

    pub fn is_block_hash_not_found_error(&self) -> bool {
        matches!(
            self,
            Error::SubxtRuntimeError(SubxtError::Transaction(TransactionError::BlockNotFound))
        )
    }

    pub fn is_ws_invalid_url_error(&self) -> bool {
        if let Error::SubxtRuntimeError(SubxtError::Rpc(RpcError::ClientError(e))) = self {
            match e.downcast_ref::<JsonRpseeError>() {
                Some(e) => {
                    return matches!(
                        e,
                    JsonRpseeError::Transport(err)
                        if matches!(err.downcast_ref::<WsHandshakeError>(), Some(WsHandshakeError::Url(_)))
                    )
                }
                None => {
                    log::error!("Failed to downcast RPC error; this is a bug please file an issue");
                }
            }
        }

        false
    }

    pub fn is_parachain_shutdown_error(&self) -> bool {
        self.is_module_err(SYSTEM_MODULE, &format!("{:?}", SystemPalletError::CallFiltered))
    }
}

#[derive(Error, Debug)]
pub enum KeyLoadingError {
    #[error("Key not found in file")]
    KeyNotFound,
    #[error("Json parsing error: {0}")]
    JsonError(#[from] SerdeJsonError),
    #[error("Io error: {0}")]
    IoError(#[from] IoError),
    #[error("Invalid secret string: {0:?}")]
    SecretStringError(SecretStringError),
}

// https://github.com/paritytech/substrate/blob/e60597dff0aa7ffad623be2cc6edd94c7dc51edd/client/rpc-api/src/author/error.rs#L80
const BASE_ERROR: i32 = 1000;
const POOL_INVALID_TX: i32 = BASE_ERROR + 10;
const POOL_TOO_LOW_PRIORITY: i32 = POOL_INVALID_TX + 4;