Skip to content

Commit 2e0de23

Browse files
committed
stash
1 parent 771f3c4 commit 2e0de23

File tree

8 files changed

+162
-43
lines changed

8 files changed

+162
-43
lines changed

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cSpell.words": [
3+
"Frunique"
4+
, "fruniques"
5+
]
6+
}

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pallets/fruniques/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ sp-runtime ={ default-features = false, version = "6.0.0", git = "https://github
2626
pallet-uniques = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }
2727
pallet-balances = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }
2828

29+
pallet-rbac = { path="../rbac/", default-features = false, version = "4.0.0-dev"}
30+
2931
[dev-dependencies]
3032
sp-core = { default-features = false, version = "6.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }
3133
sp-io = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }

pallets/fruniques/src/functions.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use crate::types::*;
66
use frame_support::traits::tokens::nonfungibles::Inspect;
77
use scale_info::prelude::string::String;
88

9+
use pallet_rbac::types::*;
10+
911
use frame_support::pallet_prelude::*;
1012
use sp_runtime::{sp_std::vec::Vec, Permill};
1113

@@ -74,6 +76,12 @@ impl<T: Config> Pallet<T> {
7476
false
7577
}
7678

79+
pub fn do_initial_setup() -> DispatchResult {
80+
let pallet_id = Self::pallet_id();
81+
82+
Ok(())
83+
}
84+
7785
pub fn set_attribute(
7886
origin: OriginFor<T>,
7987
class_id: &T::CollectionId,
@@ -214,4 +222,11 @@ impl<T: Config> Pallet<T> {
214222

215223
Ok(())
216224
}
225+
226+
pub fn pallet_id()->IdOrVec{
227+
IdOrVec::Vec(
228+
Self::module_name().as_bytes().to_vec()
229+
)
230+
}
231+
217232
}

pallets/fruniques/src/lib.rs

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,30 @@ mod mock;
88
#[cfg(test)]
99
mod tests;
1010

11-
#[cfg(feature = "runtime-benchmarks")]
12-
mod benchmarking;
11+
// #[cfg(feature = "runtime-benchmarks")]
12+
// mod benchmarking;
1313

14-
pub mod types;
1514
mod functions;
15+
pub mod types;
1616

1717
#[frame_support::pallet]
1818
pub mod pallet {
1919
use super::*;
20-
use frame_support::{pallet_prelude::*, BoundedVec, transactional};
20+
use crate::types::*;
21+
use frame_support::{pallet_prelude::*, transactional, BoundedVec};
2122
use frame_system::pallet_prelude::*;
2223
use scale_info::prelude::vec::Vec;
23-
use sp_runtime::{Permill};
24-
use crate::types::*;
24+
use sp_runtime::Permill;
2525
/// Configure the pallet by specifying the parameters and types on which it depends.
2626
#[pallet::config]
2727
pub trait Config: frame_system::Config + pallet_uniques::Config {
28-
2928
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
3029

3130
type RemoveOrigin: EnsureOrigin<Self::Origin>;
3231

32+
/// Maximum number of children a Frunique can have
33+
#[pallet::constant]
34+
type ChildMaxLen: Get<u32>;
3335
}
3436

3537
#[pallet::pallet]
@@ -66,26 +68,22 @@ pub mod pallet {
6668
ValueTooLong,
6769
// Calling set on a non-existing attributes
6870
AttributesEmpty,
69-
// The collection doenst exist
71+
// The collection doesn't exist
7072
CollectionNotFound,
7173
}
7274

7375
#[pallet::storage]
7476
#[pallet::getter(fn frunique_cnt)]
7577
/// Keeps track of the number of Kitties in existence.
76-
pub(super) type FruniqueCnt<T: Config> = StorageValue<
77-
_,
78-
ItemId,
79-
ValueQuery
80-
>;
78+
pub(super) type FruniqueCnt<T: Config> = StorageValue<_, ItemId, ValueQuery>;
8179

8280
#[pallet::storage]
8381
#[pallet::getter(fn next_collection)]
8482
/// Keeps track of the number of collections in existence.
8583
pub(super) type NextCollection<T: Config> = StorageValue<
8684
_,
8785
CollectionId, // Next collection id.
88-
ValueQuery
86+
ValueQuery,
8987
>;
9088

9189
#[pallet::storage]
@@ -95,8 +93,8 @@ pub mod pallet {
9593
_,
9694
Blake2_128Concat,
9795
CollectionId,
98-
ItemId, // The next frunique id for a collection.
99-
ValueQuery
96+
ItemId, // The next frunique id for a collection.
97+
ValueQuery,
10098
>;
10199

102100
#[pallet::storage]
@@ -107,9 +105,9 @@ pub mod pallet {
107105
Blake2_128Concat,
108106
CollectionId,
109107
Blake2_128Concat,
110-
ItemId, // FruniqueId
108+
ItemId, // FruniqueId
111109
Option<HierarchicalInfo>, // ParentId and flag if it inherit attributes
112-
ValueQuery
110+
ValueQuery,
113111
>;
114112

115113
#[pallet::storage]
@@ -120,22 +118,19 @@ pub mod pallet {
120118
Blake2_128Concat,
121119
CollectionId,
122120
Blake2_128Concat,
123-
ItemId, // FruniqueId
121+
ItemId, // FruniqueId
124122
Option<HierarchicalInfo>, // ParentId and flag if it inherit attributes
125-
ValueQuery
123+
ValueQuery,
126124
>;
127125

128-
129126
#[pallet::call]
130127
impl<T: Config> Pallet<T>
131128
where
132129
T: pallet_uniques::Config<CollectionId = CollectionId, ItemId = ItemId>,
133130
{
134131
#[transactional]
135132
#[pallet::weight(10_000 + T::DbWeight::get().writes(10))]
136-
pub fn initial_setup(
137-
origin: OriginFor<T>,
138-
) -> DispatchResult {
133+
pub fn initial_setup(origin: OriginFor<T>) -> DispatchResult {
139134
T::RemoveOrigin::ensure_origin(origin.clone())?;
140135
// Self::do_initial_setup()?;
141136
Ok(())
@@ -146,7 +141,6 @@ pub mod pallet {
146141
origin: OriginFor<T>,
147142
metadata: Option<StringLimit<T>>,
148143
) -> DispatchResult {
149-
150144
let admin: T::AccountId = ensure_signed(origin.clone())?;
151145

152146
let new_collection_id: u32 = Self::next_collection().try_into().unwrap();
@@ -158,12 +152,9 @@ pub mod pallet {
158152
Self::account_id_to_lookup_source(&admin),
159153
)?;
160154

161-
Self::deposit_event(Event::FruniqueCollectionCreated(
162-
admin,
163-
new_collection_id,
164-
));
155+
Self::deposit_event(Event::FruniqueCollectionCreated(admin, new_collection_id));
165156

166-
<NextCollection<T>>::put(Self::next_collection() + 1 );
157+
<NextCollection<T>>::put(Self::next_collection() + 1);
167158

168159
Ok(())
169160
}
@@ -244,7 +235,6 @@ pub mod pallet {
244235
parent_info: Option<HierarchicalInfo>,
245236
attributes: Option<Vec<(BoundedVec<u8, T::KeyLimit>, BoundedVec<u8, T::ValueLimit>)>>,
246237
) -> DispatchResult {
247-
248238
let owner: T::AccountId = ensure_signed(origin.clone())?;
249239
let account_id = Self::account_id_to_lookup_source(&owner);
250240

@@ -257,14 +247,22 @@ pub mod pallet {
257247
instance_id,
258248
account_id,
259249
numeric_value,
260-
attributes
250+
attributes,
261251
)?;
262252

263-
Self::deposit_event(Event::FruniqueCreated(owner.clone(), owner, class_id, instance_id));
253+
Self::deposit_event(Event::FruniqueCreated(
254+
owner.clone(),
255+
owner,
256+
class_id,
257+
instance_id,
258+
));
264259

265260
if let Some(parent_info) = parent_info {
266261
// ! check if parent exists
267-
ensure!(Self::item_exists(&class_id, &parent_info.0), <Error<T>>::CollectionNotFound);
262+
ensure!(
263+
Self::item_exists(&class_id, &parent_info.0),
264+
<Error<T>>::CollectionNotFound
265+
);
268266
<FruniqueParent<T>>::insert(class_id, instance_id, Some(parent_info));
269267
// Self::do_hierarchical_division(origin.clone(), class_id, instance_id, parent_info)?;
270268
}
@@ -284,15 +282,12 @@ pub mod pallet {
284282
/// - This function is only available to the `admin` with sudo access.
285283
#[transactional]
286284
#[pallet::weight(10_000 + T::DbWeight::get().writes(1))]
287-
pub fn kill_storage(
288-
origin: OriginFor<T>,
289-
) -> DispatchResult{
285+
pub fn kill_storage(origin: OriginFor<T>) -> DispatchResult {
290286
T::RemoveOrigin::ensure_origin(origin.clone())?;
291287
let _ = <FruniqueCnt<T>>::put(0);
292288
let _ = <NextCollection<T>>::put(0);
293289
let _ = <NextFrunique<T>>::clear(1000, None);
294290
Ok(())
295291
}
296-
297292
}
298293
}

pallets/fruniques/src/mock.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ construct_runtime!(
2626
);
2727
parameter_types! {
2828
pub const BlockHashCount: u64 = 250;
29+
pub const ChildMaxLen: u32 = 10;
2930
}
3031
impl frame_system::Config for Test {
3132
type BaseCallFilter = frame_support::traits::Everything;
@@ -57,6 +58,7 @@ impl frame_system::Config for Test {
5758
impl pallet_fruniques::Config for Test {
5859
type Event = Event;
5960
type RemoveOrigin = EnsureRoot<Self::AccountId>;
61+
type ChildMaxLen = ChildMaxLen;
6062
}
6163

6264
parameter_types! {

pallets/fruniques/src/types.rs

Lines changed: 100 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
//! Defines the types required by the fruniques pallet
2-
use sp_runtime::{sp_std::vec::Vec};
2+
use super::*;
33
use frame_support::pallet_prelude::*;
44

5+
use frame_support::sp_io::hashing::blake2_256;
6+
use sp_runtime::sp_std::vec::Vec;
7+
58
pub type AttributeKey<T> = BoundedVec<u8, <T as pallet_uniques::Config>::KeyLimit>;
69
pub type AttributeValue<T> = BoundedVec<u8, <T as pallet_uniques::Config>::ValueLimit>;
710
pub type Attributes<T> = Vec<(AttributeKey<T>, AttributeValue<T>)>;
811

12+
pub type CollectionDescription = [u8;32];
913
pub type StringLimit<T> = BoundedVec<u8, <T as pallet_uniques::Config>::StringLimit>;
1014

1115
pub type CollectionId = u32;
@@ -21,10 +25,101 @@ pub struct FruniqueChild {
2125
pub weight: u32,
2226
}
2327

24-
#[derive(CloneNoBound, Encode, Decode, RuntimeDebugNoBound, TypeInfo, MaxEncodedLen,)]
28+
pub type ChildrenInfo = BoundedVec<FruniqueChild, T::ChildMaxLen>;
29+
30+
#[derive(CloneNoBound, Encode, Decode, RuntimeDebugNoBound, TypeInfo, MaxEncodedLen)]
2531
#[scale_info(skip_type_params(T))]
2632
#[codec(mel_bound())]
27-
pub struct FruniqueInheritance<T: pallet_uniques::Config> {
28-
pub parent: Option<(CollectionId, ItemId)>,
29-
pub children: Vec<(CollectionId, ItemId)>,
33+
pub struct FruniqueInheritance {
34+
pub parent: (CollectionId, ItemId),
35+
pub children: ChildrenInfo,
36+
}
37+
38+
#[derive(
39+
Encode, Decode, Clone, Eq, PartialEq, RuntimeDebugNoBound, MaxEncodedLen, TypeInfo, Copy,
40+
)]
41+
pub enum FruniqueRole {
42+
Owner,
43+
Admin,
44+
Collaborator,
45+
Collector,
46+
}
47+
48+
impl Default for FruniqueRole {
49+
fn default() -> Self {
50+
FruniqueRole::Collector
51+
}
52+
}
53+
54+
impl FruniqueRole {
55+
pub fn to_vec(self) -> Vec<u8> {
56+
match self {
57+
Self::Owner => "Owner".as_bytes().to_vec(),
58+
Self::Admin => "Admin".as_bytes().to_vec(),
59+
Self::Collaborator => "Collaborator".as_bytes().to_vec(),
60+
Self::Collector => "Collector".as_bytes().to_vec(),
61+
}
62+
}
63+
64+
pub fn id(&self) -> [u8; 32] {
65+
self.to_vec().using_encoded(blake2_256)
66+
}
67+
68+
pub fn enum_to_vec() -> Vec<Vec<u8>> {
69+
use crate::types::FruniqueRole::*;
70+
[Owner.to_vec(), Admin.to_vec(), Collaborator.to_vec(), Collector.to_vec()].to_vec()
71+
}
72+
}
73+
74+
/// Extrinsics which require previous authorization to call them
75+
#[derive(
76+
Encode, Decode, Clone, Eq, PartialEq, RuntimeDebugNoBound, MaxEncodedLen, TypeInfo, Copy,
77+
)]
78+
pub enum Permission {
79+
/// No authorization required
80+
None,
81+
/// Authorization required
82+
Required,
83+
/// Authorization required and must be approved by the owner
84+
Mint,
85+
/// Authorization required and must be approved by a holder / collector
86+
Transfer,
87+
}
88+
89+
impl Permission {
90+
pub fn to_vec(self) -> Vec<u8> {
91+
match self {
92+
Self::None => "None".as_bytes().to_vec(),
93+
Self::Required => "Required".as_bytes().to_vec(),
94+
Self::Mint => "Mint".as_bytes().to_vec(),
95+
Self::Transfer => "Transfer".as_bytes().to_vec(),
96+
}
97+
}
98+
99+
pub fn id(&self) -> [u8; 32] {
100+
self.to_vec().using_encoded(blake2_256)
101+
}
102+
103+
pub fn admin_permissions() -> Vec<Vec<u8>> {
104+
use crate::types::Permission::*;
105+
let mut admin_permissions = [
106+
None.to_vec(),
107+
Required.to_vec(),
108+
Mint.to_vec(),
109+
Transfer.to_vec(),
110+
]
111+
.to_vec();
112+
admin_permissions.append(&mut Permission::participant_permissions());
113+
admin_permissions
114+
}
115+
116+
pub fn participant_permissions() -> Vec<Vec<u8>> {
117+
use crate::types::Permission::*;
118+
[
119+
None.to_vec(),
120+
Required.to_vec(),
121+
Transfer.to_vec(),
122+
]
123+
.to_vec()
124+
}
30125
}

0 commit comments

Comments
 (0)