Skip to content
This repository was archived by the owner on Oct 2, 2023. It is now read-only.
Closed
Prev Previous commit
Next Next commit
counter works
  • Loading branch information
asiniscalchi committed Jul 28, 2023
commit 5f67a28d21e90c484c9e452cf574cecb65aecd66
16 changes: 15 additions & 1 deletion pallets/living-assets-ownership/src/functions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
//! Contains helper and utility functions of the pallet
use super::*;
use frame_support::{ensure, sp_runtime::DispatchResult};
use frame_support::{
ensure,
sp_runtime::{
traits::{CheckedAdd, One},
DispatchResult,
},
};

impl<T: Config> Pallet<T> {
/// See [Self::create_collection]
Expand All @@ -13,8 +19,16 @@ impl<T: Config> Pallet<T> {
Error::<T>::CollectionAlreadyExists
);

// Retrieve the current collection count
let collection_id = Self::collection_counter();

OwnerOfCollection::<T>::insert(collection_id, &who);

// Increment collection counter by 1
let counter =
collection_id.checked_add(&One::one()).ok_or(Error::<T>::CollectionIdOverflow)?;
CollectionCounter::<T>::put(counter);

Self::deposit_event(Event::CollectionCreated { collection_id, who });

Ok(())
Expand Down
9 changes: 7 additions & 2 deletions pallets/living-assets-ownership/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ mod tests;

#[frame_support::pallet]
pub mod pallet {
use frame_support::pallet_prelude::{OptionQuery, ValueQuery, *};
use frame_support::{
pallet_prelude::{OptionQuery, ValueQuery, *},
sp_runtime::traits::{CheckedAdd, One},
};
use frame_system::pallet_prelude::*;

#[pallet::pallet]
Expand All @@ -27,7 +30,7 @@ pub mod pallet {
/// Because this pallet emits events, it depends on the runtime's definition of an event.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// Collection id type
type CollectionId: Member + Parameter + MaxEncodedLen + Copy + Default;
type CollectionId: Member + Parameter + MaxEncodedLen + Copy + Default + CheckedAdd + One;
}

/// Mapping from collection id to owner
Expand Down Expand Up @@ -55,6 +58,8 @@ pub mod pallet {
pub enum Error<T> {
/// Collection already exists
CollectionAlreadyExists,
/// Collection id overflow
CollectionIdOverflow,
}

// Dispatchable functions allows users to interact with the pallet and invoke state changes.
Expand Down
2 changes: 1 addition & 1 deletion pallets/living-assets-ownership/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ mod test {
#[test]
fn create_collection_and_check_counter() {
new_test_ext().execute_with(|| {
assert_ok!(LivingAssetsModule::create_collection(RuntimeOrigin::signed(1), 0));
assert_ok!(LivingAssetsModule::create_collection(RuntimeOrigin::signed(1), 0));
assert_eq!(LivingAssetsModule::collection_counter(), 1);
});
}
Expand Down