1// This file is part of Substrate.
23// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
56// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
1718//! Runtime types that existed prior to BlockBuilder API version 6.
1920use crate::{ArithmeticError, TokenError};
21use codec::{Decode, Encode};
22use scale_info::TypeInfo;
23#[cfg(feature = "serde")]
24use serde::{Deserialize, Serialize};
2526/// [`ModuleError`] type definition before BlockBuilder API version 6.
27#[derive(Eq, Clone, Copy, Encode, Decode, Debug, TypeInfo)]
28#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
29pub struct ModuleError {
30/// Module index, matching the metadata module index.
31pub index: u8,
32/// Module specific error value.
33pub error: u8,
34/// Optional error message.
35#[codec(skip)]
36 #[cfg_attr(feature = "serde", serde(skip_deserializing))]
37pub message: Option<&'static str>,
38}
3940impl PartialEq for ModuleError {
41fn eq(&self, other: &Self) -> bool {
42 (self.index == other.index) && (self.error == other.error)
43 }
44}
4546/// [`DispatchError`] type definition before BlockBuilder API version 6.
47#[derive(Eq, Clone, Copy, Encode, Decode, Debug, TypeInfo, PartialEq)]
48#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
49pub enum DispatchError {
50/// Some error occurred.
51Other(
52#[codec(skip)]
53 #[cfg_attr(feature = "serde", serde(skip_deserializing))]
54&'static str,
55 ),
56/// Failed to lookup some data.
57CannotLookup,
58/// A bad origin.
59BadOrigin,
60/// A custom error in a module.
61Module(ModuleError),
62/// At least one consumer is remaining so the account cannot be destroyed.
63ConsumerRemaining,
64/// There are no providers so the account cannot be created.
65NoProviders,
66/// There are too many consumers so the account cannot be created.
67TooManyConsumers,
68/// An error to do with tokens.
69Token(TokenError),
70/// An arithmetic error.
71Arithmetic(ArithmeticError),
72}
7374/// [`DispatchOutcome`] type definition before BlockBuilder API version 6.
75pub type DispatchOutcome = Result<(), DispatchError>;
7677/// [`ApplyExtrinsicResult`] type definition before BlockBuilder API version 6.
78pub type ApplyExtrinsicResult =
79Result<DispatchOutcome, crate::transaction_validity::TransactionValidityError>;
8081/// Convert the legacy `ApplyExtrinsicResult` type to the latest version.
82pub fn convert_to_latest(old: ApplyExtrinsicResult) -> crate::ApplyExtrinsicResult {
83 old.map(|outcome| {
84 outcome.map_err(|e| match e {
85 DispatchError::Other(s) => crate::DispatchError::Other(s),
86 DispatchError::CannotLookup => crate::DispatchError::CannotLookup,
87 DispatchError::BadOrigin => crate::DispatchError::BadOrigin,
88 DispatchError::Module(err) => crate::DispatchError::Module(crate::ModuleError {
89 index: err.index,
90 error: [err.error, 0, 0, 0],
91 message: err.message,
92 }),
93 DispatchError::ConsumerRemaining => crate::DispatchError::ConsumerRemaining,
94 DispatchError::NoProviders => crate::DispatchError::NoProviders,
95 DispatchError::TooManyConsumers => crate::DispatchError::TooManyConsumers,
96 DispatchError::Token(err) => crate::DispatchError::Token(err),
97 DispatchError::Arithmetic(err) => crate::DispatchError::Arithmetic(err),
98 })
99 })
100}