assets_chain_extension_types/
lib.rs

1// This file is part of Astar.
2
3// Copyright (C) Stake Technologies Pte.Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later
5
6// Astar is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// Astar is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with Astar. If not, see <http://www.gnu.org/licenses/>.
18
19#![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
48    Success = 0,
49    /// Account balance must be greater than or equal to the transfer amount.
50    BalanceLow = 1,
51    /// The account to alter does not exist.
52    NoAccount = 2,
53    /// The signing account has no permission to do the operation.
54    NoPermission = 3,
55    /// The given asset ID is unknown.
56    Unknown = 4,
57    /// The origin account is frozen.
58    Frozen = 5,
59    /// The asset ID is already taken.
60    InUse = 6,
61    /// Invalid witness data given.
62    BadWitness = 7,
63    /// Minimum balance should be non-zero.
64    MinBalanceZero = 8,
65    /// Unable to increment the consumer reference counters on the account. Either no provider
66    /// reference exists to allow a non-zero balance of a non-self-sufficient asset, or one
67    /// fewer then the maximum number of consumers has been reached.
68    UnavailableConsumer = 9,
69    /// Invalid metadata given.
70    BadMetadata = 10,
71    /// No approval exists that would allow the transfer.
72    Unapproved = 11,
73    /// The source account would not survive the transfer and it needs to stay alive.
74    WouldDie = 12,
75    /// The asset-account already exists.
76    AlreadyExists = 13,
77    /// The asset-account doesn't have an associated deposit.
78    NoDeposit = 14,
79    /// The operation would result in funds being burned.
80    WouldBurn = 15,
81    /// The asset is a live asset and is actively being used. Usually emit for operations such
82    /// as `start_destroy` which require the asset to be in a destroying state.
83    LiveAsset = 16,
84    /// The asset is not live, and likely being destroyed.
85    AssetNotLive = 17,
86    /// The asset status is not the expected status.
87    IncorrectStatus = 18,
88    /// The asset should be frozen before the given operation.
89    NotFrozen = 19,
90    /// Callback action resulted in error
91    CallbackFailed = 20,
92    /// Unknown error
93    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}