pop_primitives/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std, no_main)]
2
3use codec::{Decode, Encode};
4#[cfg(feature = "std")]
5use scale_info::TypeInfo;
6pub use v0::error;
7
8/// Identifier for the class of asset.
9pub type AssetId = u32;
10
11pub mod v0 {
12	use super::*;
13	pub mod error {
14		use super::*;
15
16		/// Reason why a Pop API call failed.
17		#[derive(Encode, Decode, Debug, Eq, PartialEq)]
18		#[cfg_attr(feature = "std", derive(TypeInfo))]
19		#[repr(u8)]
20		#[allow(clippy::unnecessary_cast)]
21		pub enum Error {
22			/// An unknown error occurred. This variant captures any unexpected errors that the
23			/// contract cannot specifically handle. It is useful for cases where there are breaking
24			/// changes in the runtime or when an error falls outside the predefined categories. The
25			/// variant includes:
26			///
27			/// - `dispatch_error_index`: The index within the `DispatchError`.
28			/// - `error_index`: The index within the `DispatchError` variant (e.g. a `TokenError`).
29			/// - `error`: The specific error code or sub-index, providing additional context (e.g.
30			///   `error` in `ModuleError`).
31			Other { dispatch_error_index: u8, error_index: u8, error: u8 } = 0,
32			/// Failed to lookup some data.
33			CannotLookup = 1,
34			/// A bad origin.
35			BadOrigin = 2,
36			/// A custom error in a module.
37			///
38			/// - `index`: The pallet index.
39			/// - `error`: The error within the pallet.
40			Module { index: u8, error: u8 } = 3,
41			/// At least one consumer is remaining so the account cannot be destroyed.
42			ConsumerRemaining = 4,
43			/// There are no providers so the account cannot be created.
44			NoProviders = 5,
45			/// There are too many consumers so the account cannot be created.
46			TooManyConsumers = 6,
47			/// An error to do with tokens.
48			Token(TokenError) = 7,
49			/// An arithmetic error.
50			Arithmetic(ArithmeticError) = 8,
51			/// The number of transactional layers has been reached, or we are not in a transactional
52			/// layer.
53			Transactional(TransactionalError) = 9,
54			/// Resources exhausted, e.g. attempt to read/write data which is too large to manipulate.
55			Exhausted = 10,
56			/// The state is corrupt; this is generally not going to fix itself.
57			Corruption = 11,
58			/// Some resource (e.g. a preimage) is unavailable right now. This might fix itself later.
59			Unavailable = 12,
60			/// Root origin is not allowed.
61			RootNotAllowed = 13,
62			/// Unknown call.
63			UnknownCall = 254,
64			/// Decoding failed.
65			DecodingFailed = 255,
66		}
67
68		impl From<u32> for Error {
69			/// Converts a `u32` status code into an `Error`.
70			///
71			/// This conversion maps a raw status code returned by the runtime into the more
72			/// descriptive `Error` enum variant, providing better context and understanding of the
73			/// error.
74			fn from(value: u32) -> Self {
75				let encoded = value.to_le_bytes();
76				Error::decode(&mut &encoded[..]).unwrap_or(Error::DecodingFailed)
77			}
78		}
79
80		/// Description of what went wrong when trying to complete an operation on a token.
81		#[derive(Encode, Decode, Debug, Eq, PartialEq)]
82		#[cfg_attr(feature = "std", derive(TypeInfo))]
83		pub enum TokenError {
84			/// Funds are unavailable.
85			FundsUnavailable,
86			/// Some part of the balance gives the only provider reference to the account and thus cannot
87			/// be (re)moved.
88			OnlyProvider,
89			/// Account cannot exist with the funds that would be given.
90			BelowMinimum,
91			/// Account cannot be created.
92			CannotCreate,
93			/// The asset in question is unknown.
94			UnknownAsset,
95			/// Funds exist but are frozen.
96			Frozen,
97			/// Operation is not supported by the asset.
98			Unsupported,
99			/// Account cannot be created for a held balance.
100			CannotCreateHold,
101			/// Withdrawal would cause unwanted loss of account.
102			NotExpendable,
103			/// Account cannot receive the assets.
104			Blocked,
105		}
106
107		/// Arithmetic errors.
108		#[derive(Encode, Decode, Debug, Eq, PartialEq)]
109		#[cfg_attr(feature = "std", derive(TypeInfo))]
110		pub enum ArithmeticError {
111			/// Underflow.
112			Underflow,
113			/// Overflow.
114			Overflow,
115			/// Division by zero.
116			DivisionByZero,
117		}
118
119		/// Errors related to transactional storage layers.
120		#[derive(Encode, Decode, Debug, Eq, PartialEq)]
121		#[cfg_attr(feature = "std", derive(TypeInfo))]
122		pub enum TransactionalError {
123			/// Too many transactional layers have been spawned.
124			LimitReached,
125			/// A transactional layer was expected, but does not exist.
126			NoLayer,
127		}
128	}
129}