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
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::all)]
use assets::AssetIdOf;
use common::prelude::EnsureDEXManager;
use common::{hash, DexInfoProvider, ManagementMode};
use frame_support::dispatch::DispatchResult;
use frame_support::ensure;
use frame_support::sp_runtime::DispatchError;
use frame_system::RawOrigin;
use permissions::{Scope, MANAGE_DEX};
use sp_std::vec::Vec;
pub mod migrations;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
type DEXInfo<T> = common::prelude::DEXInfo<AssetIdOf<T>>;
impl<T: Config> EnsureDEXManager<T::DEXId, T::AccountId, DispatchError> for Pallet<T> {
fn ensure_can_manage<OuterOrigin>(
dex_id: &T::DEXId,
origin: OuterOrigin,
mode: ManagementMode,
) -> Result<Option<T::AccountId>, DispatchError>
where
OuterOrigin: Into<Result<RawOrigin<T::AccountId>, OuterOrigin>>,
{
match origin.into() {
Ok(RawOrigin::Signed(who)) => {
let dex_info = Self::get_dex_info(&dex_id)?;
if !dex_info.is_public || mode != ManagementMode::Public {
Self::ensure_direct_manager(&dex_id, &who)?;
}
Ok(Some(who))
}
_ => Err(Error::<T>::InvalidAccountId.into()),
}
}
}
impl<T: Config> DexInfoProvider<T::DEXId, DEXInfo<T>> for Pallet<T> {
fn get_dex_info(dex_id: &T::DEXId) -> Result<DEXInfo<T>, DispatchError> {
Ok(DEXInfos::<T>::get(&dex_id).ok_or(Error::<T>::DEXDoesNotExist)?)
}
fn ensure_dex_exists(dex_id: &T::DEXId) -> DispatchResult {
ensure!(
DEXInfos::<T>::contains_key(&dex_id),
Error::<T>::DEXDoesNotExist
);
Ok(())
}
fn list_dex_ids() -> Vec<T::DEXId> {
DEXInfos::<T>::iter().map(|(k, _)| k).collect()
}
}
impl<T: Config> Pallet<T> {
fn ensure_direct_manager(dex_id: &T::DEXId, who: &T::AccountId) -> DispatchResult {
permissions::Pallet::<T>::check_permission_with_scope(
who.clone(),
MANAGE_DEX,
&Scope::Limited(hash(&dex_id)),
)
.map_err(|e| e.into())
}
}
pub use pallet::*;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_support::traits::StorageVersion;
#[pallet::config]
pub trait Config: frame_system::Config + common::Config + assets::Config {}
const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::storage_version(STORAGE_VERSION)]
#[pallet::without_storage_info]
pub struct Pallet<T>(PhantomData<T>);
#[pallet::call]
impl<T: Config> Pallet<T> {}
#[pallet::error]
pub enum Error<T> {
DEXIdAlreadyExists,
DEXDoesNotExist,
InvalidFeeValue,
InvalidAccountId,
}
#[pallet::storage]
#[pallet::getter(fn dex_id)]
pub type DEXInfos<T: Config> = StorageMap<_, Twox64Concat, T::DEXId, DEXInfo<T>>;
#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub dex_list: Vec<(T::DEXId, DEXInfo<T>)>,
}
#[cfg(feature = "std")]
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
Self {
dex_list: Default::default(),
}
}
}
#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
self.dex_list.iter().for_each(|(dex_id, dex_info)| {
DEXInfos::<T>::insert(dex_id.clone(), dex_info);
})
}
}
}