assets_chain_extension_types/
lib.rs1#![cfg_attr(not(feature = "std"), no_std)]
20
21use num_enum::{IntoPrimitive, TryFromPrimitive};
22use parity_scale_codec::{Decode, Encode};
23use sp_runtime::{DispatchError, ModuleError};
24
25pub const LOG_TARGET: &str = "pallet-chain-extension-assets";
26
27#[repr(u16)]
28#[derive(TryFromPrimitive, IntoPrimitive, Decode, Encode)]
29pub enum Command {
30 Transfer = 0,
31 Mint = 1,
32 Burn = 2,
33 ApproveTransfer = 4,
34 TransferApproved = 5,
35 BalanceOf = 6,
36 TotalSupply = 7,
37 Allowance = 8,
38 MetadataName = 9,
39 MetadataSymbol = 10,
40 MetadataDecimals = 11,
41 MinimumBalance = 12,
42}
43
44#[derive(PartialEq, Eq, Copy, Clone, Encode, Decode, Debug)]
45#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
46pub enum Outcome {
47 Success = 0,
49 BalanceLow = 1,
51 NoAccount = 2,
53 NoPermission = 3,
55 Unknown = 4,
57 Frozen = 5,
59 InUse = 6,
61 BadWitness = 7,
63 MinBalanceZero = 8,
65 UnavailableConsumer = 9,
69 BadMetadata = 10,
71 Unapproved = 11,
73 WouldDie = 12,
75 AlreadyExists = 13,
77 NoDeposit = 14,
79 WouldBurn = 15,
81 LiveAsset = 16,
84 AssetNotLive = 17,
86 IncorrectStatus = 18,
88 NotFrozen = 19,
90 CallbackFailed = 20,
92 RuntimeError = 99,
94}
95
96impl From<DispatchError> for Outcome {
97 fn from(input: DispatchError) -> Self {
98 let error_text = match input {
99 DispatchError::Module(ModuleError { message, .. }) => message,
100 _ => Some("No module error Info"),
101 };
102 match error_text {
103 Some("BalanceLow") => Outcome::BalanceLow,
104 Some("NoAccount") => Outcome::NoAccount,
105 Some("NoPermission") => Outcome::NoPermission,
106 Some("Unknown") => Outcome::Unknown,
107 Some("Frozen") => Outcome::Frozen,
108 Some("InUse") => Outcome::InUse,
109 Some("BadWitness") => Outcome::BadWitness,
110 Some("MinBalanceZero") => Outcome::MinBalanceZero,
111 Some("UnavailableConsumer") => Outcome::UnavailableConsumer,
112 Some("BadMetadata") => Outcome::BadMetadata,
113 Some("Unapproved") => Outcome::Unapproved,
114 Some("WouldDie") => Outcome::WouldDie,
115 Some("AlreadyExists") => Outcome::AlreadyExists,
116 Some("NoDeposit") => Outcome::NoDeposit,
117 Some("WouldBurn") => Outcome::WouldBurn,
118 Some("LiveAsset") => Outcome::LiveAsset,
119 Some("AssetNotLive") => Outcome::AssetNotLive,
120 Some("IncorrectStatus") => Outcome::IncorrectStatus,
121 Some("NotFrozen") => Outcome::NotFrozen,
122 Some("CallbackFailed") => Outcome::CallbackFailed,
123 _ => Outcome::RuntimeError,
124 }
125 }
126}
127
128#[macro_export]
129macro_rules! handle_result {
130 ($call_result:expr) => {{
131 return match $call_result {
132 Err(e) => {
133 log::trace!(target: LOG_TARGET, "err: {:?}", e);
134 let mapped_error = Outcome::from(e);
135 Ok(RetVal::Converging(mapped_error as u32))
136 }
137 Ok(_) => Ok(RetVal::Converging(Outcome::Success as u32)),
138 };
139 }};
140}
141
142#[macro_export]
143macro_rules! selector_bytes {
144 ($s:expr) => {{
145 let hash = blake2_256($s.as_bytes());
146 [hash[0], hash[1], hash[2], hash[3]]
147 }};
148}