Skip to content

Commit 8967349

Browse files
authored
Merge pull request #40 from rmrk-team/simplify-ensure-signed
Simplify ensure signed checks
2 parents 6e4c69f + 5bae966 commit 8967349

File tree

3 files changed

+40
-73
lines changed

3 files changed

+40
-73
lines changed

pallets/rmrk-core/src/functions.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,7 @@ impl<T: Config> Nft<T::AccountId, StringLimitOf<T>> for Pallet<T> {
103103
NFTs::<T>::remove(collection_id, nft_id);
104104
if let Some(kids) = Children::<T>::take(collection_id, nft_id) {
105105
for (child_collection_id, child_nft_id) in kids {
106-
Self::nft_burn(
107-
child_collection_id,
108-
child_nft_id,
109-
max_recursions - 1,
110-
)?;
106+
Self::nft_burn(child_collection_id, child_nft_id, max_recursions - 1)?;
111107
}
112108
}
113109
Ok((collection_id, nft_id))
@@ -262,7 +258,7 @@ impl<T: Config> Pallet<T> {
262258
Ok(match name {
263259
Some(n) => Some(Self::to_bounded_string(n)?),
264260
None => None,
265-
})
261+
})
266262
}
267263

268264
pub fn get_next_nft_id(collection_id: CollectionId) -> Result<NftId, Error<T>> {

pallets/rmrk-core/src/lib.rs

Lines changed: 31 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,15 @@
22
#![allow(clippy::unused_unit)]
33
#![allow(clippy::upper_case_acronyms)]
44

5-
use codec::HasCompact;
65
use frame_support::{
7-
dispatch::DispatchResult,
8-
ensure,
9-
traits::{
10-
tokens::nonfungibles::*, BalanceStatus, Currency, NamedReservableCurrency,
11-
ReservableCurrency,
12-
},
13-
transactional, BoundedVec,
6+
dispatch::DispatchResult, ensure, traits::tokens::nonfungibles::*, transactional, BoundedVec,
147
};
158
use frame_system::ensure_signed;
169

17-
use sp_runtime::{
18-
traits::{AtLeast32BitUnsigned, Bounded, CheckedAdd, StaticLookup, Zero},
19-
DispatchError, Permill,
20-
};
10+
use sp_runtime::{traits::StaticLookup, DispatchError, Permill};
2111
use sp_std::{convert::TryInto, vec, vec::Vec};
2212

23-
use types::{ClassInfo, ResourceInfo};
13+
use types::ResourceInfo;
2414

2515
use rmrk_traits::{
2616
primitives::*, AccountIdOrCollectionNftTuple, Collection, CollectionInfo, Nft, NftInfo,
@@ -260,10 +250,10 @@ pub mod pallet {
260250
royalty: Option<Permill>,
261251
metadata: BoundedVec<u8, T::StringLimit>,
262252
) -> DispatchResult {
263-
let sender = Self::ensure_protocol_or_signed(origin)?;
253+
let sender = ensure_signed(origin.clone())?;
264254

265255
let (collection_id, nft_id) = Self::nft_mint(
266-
sender.clone().unwrap_or_default(),
256+
sender.clone(),
267257
owner,
268258
collection_id,
269259
recipient,
@@ -274,12 +264,12 @@ pub mod pallet {
274264
pallet_uniques::Pallet::<T>::do_mint(
275265
collection_id,
276266
nft_id,
277-
sender.clone().unwrap_or_default(),
267+
sender.clone(),
278268
|_details| Ok(()),
279269
)?;
280270

281271
Self::deposit_event(Event::NftMinted {
282-
owner: sender.unwrap_or_default(),
272+
owner: sender,
283273
collection_id,
284274
nft_id,
285275
});
@@ -296,28 +286,28 @@ pub mod pallet {
296286
max: Option<u32>,
297287
symbol: BoundedVec<u8, T::StringLimit>,
298288
) -> DispatchResult {
299-
let sender = Self::ensure_protocol_or_signed(origin)?;
289+
let sender = ensure_signed(origin.clone())?;
300290

301291
let max = max.unwrap_or_default();
302292

303293
let collection_id =
304-
Self::collection_create(sender.clone().unwrap_or_default(), metadata, max, symbol)?;
294+
Self::collection_create(sender.clone(), metadata, max, symbol)?;
305295

306296
pallet_uniques::Pallet::<T>::do_create_class(
307297
collection_id,
308-
sender.clone().unwrap_or_default(),
309-
sender.clone().unwrap_or_default(),
298+
sender.clone(),
299+
sender.clone(),
310300
T::ClassDeposit::get(),
311301
false,
312302
pallet_uniques::Event::Created(
313303
collection_id,
314-
sender.clone().unwrap_or_default(),
315-
sender.clone().unwrap_or_default(),
304+
sender.clone(),
305+
sender.clone(),
316306
),
317307
)?;
318308

319309
Self::deposit_event(Event::CollectionCreated {
320-
issuer: sender.clone().unwrap_or_default(),
310+
issuer: sender.clone(),
321311
collection_id,
322312
});
323313
Ok(())
@@ -335,9 +325,7 @@ pub mod pallet {
335325
let max_recursions = T::MaxRecursions::get();
336326
let (_collection_id, nft_id) = Self::nft_burn(collection_id, nft_id, max_recursions)?;
337327

338-
pallet_uniques::Pallet::<T>::do_burn(collection_id, nft_id, |_, _| {
339-
Ok(())
340-
})?;
328+
pallet_uniques::Pallet::<T>::do_burn(collection_id, nft_id, |_, _| Ok(()))?;
341329

342330
Self::deposit_event(Event::NFTBurned { owner: sender, nft_id });
343331
Ok(())
@@ -350,22 +338,18 @@ pub mod pallet {
350338
origin: OriginFor<T>,
351339
collection_id: CollectionId,
352340
) -> DispatchResult {
353-
let sender = Self::ensure_protocol_or_signed(origin)?;
341+
let sender = ensure_signed(origin.clone())?;
354342

355-
Self::collection_burn(sender.clone().unwrap_or_default(), collection_id)?;
343+
Self::collection_burn(sender.clone(), collection_id)?;
356344

357345
let witness = pallet_uniques::Pallet::<T>::get_destroy_witness(&collection_id)
358346
.ok_or(Error::<T>::NoWitness)?;
359347
ensure!(witness.instances == 0u32, Error::<T>::CollectionNotEmpty);
360348

361-
pallet_uniques::Pallet::<T>::do_destroy_class(
362-
collection_id,
363-
witness,
364-
sender.clone(),
365-
)?;
349+
pallet_uniques::Pallet::<T>::do_destroy_class(collection_id, witness, sender.clone().into())?;
366350

367351
Self::deposit_event(Event::CollectionDestroyed {
368-
issuer: sender.unwrap_or_default(),
352+
issuer: sender,
369353
collection_id,
370354
});
371355
Ok(())
@@ -380,8 +364,7 @@ pub mod pallet {
380364
nft_id: NftId,
381365
new_owner: AccountIdOrCollectionNftTuple<T::AccountId>,
382366
) -> DispatchResult {
383-
let sender = Self::ensure_protocol_or_signed(origin)?
384-
.unwrap_or_default();
367+
let sender = ensure_signed(origin.clone())?;
385368

386369
let max_recursions = T::MaxRecursions::get();
387370
Self::nft_send(
@@ -409,7 +392,7 @@ pub mod pallet {
409392
collection_id: CollectionId,
410393
new_issuer: <T::Lookup as StaticLookup>::Source,
411394
) -> DispatchResult {
412-
let sender = Self::ensure_protocol_or_signed(origin)?;
395+
let sender = ensure_signed(origin.clone())?;
413396
let new_issuer = T::Lookup::lookup(new_issuer)?;
414397

415398
ensure!(
@@ -421,7 +404,7 @@ pub mod pallet {
421404
Self::collection_change_issuer(collection_id, new_issuer)?;
422405

423406
Self::deposit_event(Event::IssuerChanged {
424-
old_issuer: sender.unwrap_or_default(),
407+
old_issuer: sender,
425408
new_issuer,
426409
collection_id,
427410
});
@@ -438,11 +421,11 @@ pub mod pallet {
438421
key: BoundedVec<u8, T::KeyLimit>,
439422
value: BoundedVec<u8, T::ValueLimit>,
440423
) -> DispatchResult {
441-
let sender = Self::ensure_protocol_or_signed(origin)?;
424+
let sender = ensure_signed(origin.clone())?;
442425

443426
let collection =
444427
Collections::<T>::get(&collection_id).ok_or(Error::<T>::NoAvailableCollectionId)?;
445-
ensure!(collection.issuer == sender.unwrap_or_default(), Error::<T>::NoPermission);
428+
ensure!(collection.issuer == sender, Error::<T>::NoPermission);
446429

447430
if let Some(nft_id) = &maybe_nft_id {
448431
ensure!(
@@ -465,12 +448,12 @@ pub mod pallet {
465448
origin: OriginFor<T>,
466449
collection_id: CollectionId,
467450
) -> DispatchResult {
468-
let sender = Self::ensure_protocol_or_signed(origin)?;
451+
let sender = ensure_signed(origin.clone())?;
469452

470453
let collection_id = Self::collection_lock(collection_id)?;
471454

472455
Self::deposit_event(Event::CollectionLocked {
473-
issuer: sender.unwrap_or_default(),
456+
issuer: sender,
474457
collection_id,
475458
});
476459
Ok(())
@@ -490,13 +473,9 @@ pub mod pallet {
490473
license: Option<BoundedVec<u8, T::StringLimit>>,
491474
thumb: Option<BoundedVec<u8, T::StringLimit>>,
492475
) -> DispatchResult {
493-
let sender = Self::ensure_protocol_or_signed(origin)?;
476+
let sender = ensure_signed(origin.clone())?;
494477

495-
let mut pending = false;
496478
let nft = NFTs::<T>::get(collection_id, nft_id).ok_or(Error::<T>::NoAvailableNftId)?;
497-
if nft.rootowner != sender.unwrap_or_default() {
498-
pending = true;
499-
}
500479

501480
let resource_id = Self::get_next_resource_id()?;
502481
ensure!(
@@ -518,7 +497,7 @@ pub mod pallet {
518497
slot,
519498
license,
520499
thumb,
521-
pending,
500+
pending: nft.rootowner != sender,
522501
};
523502
Resources::<T>::insert((collection_id, nft_id, resource_id), res);
524503

@@ -534,10 +513,10 @@ pub mod pallet {
534513
nft_id: NftId,
535514
resource_id: ResourceId,
536515
) -> DispatchResult {
537-
let sender = Self::ensure_protocol_or_signed(origin)?;
516+
let sender = ensure_signed(origin.clone())?;
538517

539518
let nft = NFTs::<T>::get(collection_id, nft_id).ok_or(Error::<T>::NoAvailableNftId)?;
540-
ensure!(nft.rootowner == sender.unwrap_or_default(), Error::<T>::NoPermission);
519+
ensure!(nft.rootowner == sender, Error::<T>::NoPermission);
541520

542521
Resources::<T>::try_mutate_exists(
543522
(collection_id, nft_id, resource_id),
@@ -562,7 +541,7 @@ pub mod pallet {
562541
nft_id: NftId,
563542
priorities: Vec<Vec<u8>>,
564543
) -> DispatchResult {
565-
let _ = Self::ensure_protocol_or_signed(origin)?;
544+
let _sender = ensure_signed(origin.clone())?;
566545
let mut bounded_priorities = Vec::<BoundedVec<u8, T::StringLimit>>::new();
567546
for priority in priorities {
568547
let bounded_priority = Self::to_bounded_string(priority)?;
@@ -573,16 +552,4 @@ pub mod pallet {
573552
Ok(())
574553
}
575554
}
576-
577-
// Helpers
578-
impl<T: Config> Pallet<T> {
579-
pub(super) fn ensure_protocol_or_signed(origin: OriginFor<T>) -> Result<Option<T::AccountId>, sp_runtime::DispatchError> {
580-
let sender = match T::ProtocolOrigin::try_origin(origin) {
581-
Ok(_) => None,
582-
Err(origin) => Some(ensure_signed(origin)?),
583-
};
584-
Ok(sender)
585-
}
586-
587-
}
588555
}

pallets/rmrk-core/src/tests.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use sp_runtime::Permill;
44
// use crate::types::ClassType;
55

66
use super::*;
7-
use mock::Event as MockEvent;
8-
use mock::*;
7+
use mock::{Event as MockEvent, *};
98
use pallet_uniques as UNQ;
109
use sp_std::{convert::TryInto, vec::Vec};
1110

@@ -117,7 +116,12 @@ fn mint_nft_works() {
117116
#[test]
118117
fn mint_collection_max_logic_works() {
119118
ExtBuilder::default().build().execute_with(|| {
120-
assert_ok!(RMRKCore::create_collection(Origin::signed(ALICE), bvec![0u8; 20], Some(1), bvec![0u8; 15]));
119+
assert_ok!(RMRKCore::create_collection(
120+
Origin::signed(ALICE),
121+
bvec![0u8; 20],
122+
Some(1),
123+
bvec![0u8; 15]
124+
));
121125
assert_ok!(RMRKCore::mint_nft(
122126
Origin::signed(ALICE),
123127
ALICE,

0 commit comments

Comments
 (0)