diff --git a/pallets/ddc-customers/src/lib.rs b/pallets/ddc-customers/src/lib.rs index 35002fb8b..4cd4160de 100644 --- a/pallets/ddc-customers/src/lib.rs +++ b/pallets/ddc-customers/src/lib.rs @@ -174,6 +174,9 @@ pub mod pallet { #[pallet::storage] pub type Buckets = StorageMap<_, Twox64Concat, BucketId, Bucket, OptionQuery>; + #[pallet::storage] + pub type Mandate = StorageMap<_, Blake2_128Concat, T::AccountId, BalanceOf>; + #[pallet::event] #[pallet::generate_deposit(pub(crate) fn deposit_event)] pub enum Event { @@ -214,6 +217,10 @@ pub mod pallet { }, /// Bucket with specific id marked as removed BucketRemoved { bucket_id: BucketId }, + /// A mandate created + MandateCreated { owner_id: T::AccountId, amount: BalanceOf }, + /// A mandate updated + MandateUpdated { owner_id: T::AccountId, amount: BalanceOf }, } #[pallet::error] @@ -248,6 +255,8 @@ pub mod pallet { AlreadyRemoved, /// Bucket belongs to another cluster ClusterMismatch, + /// Mandate with speicifed id doesn't exist. + NoMandateWithId, } #[pallet::genesis_config] @@ -543,6 +552,28 @@ pub mod pallet { Ok(()) } + + /// Mandate creation + /// + /// Only an owner can create a mandate + #[pallet::call_index(7)] + #[pallet::weight(T::WeightInfo::remove_bucket())] + pub fn create_mandate(origin: OriginFor,#[pallet::compact] amount: BalanceOf,) -> DispatchResult { + let owner = ensure_signed(origin)?; + + >::insert(owner.clone(), amount); + Self::deposit_event(Event::::MandateCreated { owner_id: owner, amount }); + Ok(()) + } + + #[pallet::call_index(8)] + #[pallet::weight(T::WeightInfo::remove_bucket())] + pub fn update_mandate(origin: OriginFor,#[pallet::compact] amount: BalanceOf,) -> DispatchResult { + let owner = ensure_signed(origin)?; + + Self::do_update_mandate(owner, amount)?; + Ok(()) + } } impl Pallet { @@ -682,6 +713,19 @@ pub mod pallet { stored_bytes, } } + + fn do_update_mandate( + owner: T::AccountId, + amount: BalanceOf, + ) -> DispatchResult { + let mut mandate_balance = Mandate::::get(owner.clone()).ok_or(Error::::NoMandateWithId)?; + mandate_balance = mandate_balance.checked_sub(&amount) + .ok_or(Error::::ArithmeticUnderflow)?; + + >::insert(owner.clone(), mandate_balance); + Self::deposit_event(Event::::MandateUpdated{ owner_id: owner, amount }); + Ok(()) + } } impl BucketManager for Pallet { @@ -762,6 +806,12 @@ pub mod pallet { let actually_charged: BalanceOf; let mut ledger = Ledger::::get(&bucket_owner).ok_or(Error::::NotOwner)?; let amount_to_deduct = amount.saturated_into::>(); + //TODO amount_to_deduct > ledger then call deposite extra and reduce amount from mandate + + if amount_to_deduct > ledger.active { + >::deposit_extra(bucket_owner.clone(), amount)?; + Self::do_update_mandate(bucket_owner.clone(), amount.saturated_into::>())?; + } if ledger.active >= amount_to_deduct { actually_charged = amount_to_deduct;