Skip to content

Changes for Automate DDC Account Top-Up System #554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions pallets/ddc-customers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ pub mod pallet {
#[pallet::storage]
pub type Buckets<T: Config> = StorageMap<_, Twox64Concat, BucketId, Bucket<T>, OptionQuery>;

#[pallet::storage]
pub type Mandate<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, BalanceOf<T>>;

#[pallet::event]
#[pallet::generate_deposit(pub(crate) fn deposit_event)]
pub enum Event<T: Config> {
Expand Down Expand Up @@ -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<T> },
/// A mandate updated
MandateUpdated { owner_id: T::AccountId, amount: BalanceOf<T> },
}

#[pallet::error]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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<T>,#[pallet::compact] amount: BalanceOf<T>,) -> DispatchResult {
let owner = ensure_signed(origin)?;

<Mandate<T>>::insert(owner.clone(), amount);
Self::deposit_event(Event::<T>::MandateCreated { owner_id: owner, amount });
Ok(())
}

#[pallet::call_index(8)]
#[pallet::weight(T::WeightInfo::remove_bucket())]
pub fn update_mandate(origin: OriginFor<T>,#[pallet::compact] amount: BalanceOf<T>,) -> DispatchResult {
let owner = ensure_signed(origin)?;

Self::do_update_mandate(owner, amount)?;
Ok(())
}
}

impl<T: Config> Pallet<T> {
Expand Down Expand Up @@ -682,6 +713,19 @@ pub mod pallet {
stored_bytes,
}
}

fn do_update_mandate(
owner: T::AccountId,
amount: BalanceOf<T>,
) -> DispatchResult {
let mut mandate_balance = Mandate::<T>::get(owner.clone()).ok_or(Error::<T>::NoMandateWithId)?;
mandate_balance = mandate_balance.checked_sub(&amount)
.ok_or(Error::<T>::ArithmeticUnderflow)?;

<Mandate<T>>::insert(owner.clone(), mandate_balance);
Self::deposit_event(Event::<T>::MandateUpdated{ owner_id: owner, amount });
Ok(())
}
}

impl<T: Config> BucketManager<T> for Pallet<T> {
Expand Down Expand Up @@ -762,6 +806,12 @@ pub mod pallet {
let actually_charged: BalanceOf<T>;
let mut ledger = Ledger::<T>::get(&bucket_owner).ok_or(Error::<T>::NotOwner)?;
let amount_to_deduct = amount.saturated_into::<BalanceOf<T>>();
//TODO amount_to_deduct > ledger then call deposite extra and reduce amount from mandate

if amount_to_deduct > ledger.active {
<Self as CustomerDepositor<T>>::deposit_extra(bucket_owner.clone(), amount)?;
Self::do_update_mandate(bucket_owner.clone(), amount.saturated_into::<BalanceOf<T>>())?;
}

if ledger.active >= amount_to_deduct {
actually_charged = amount_to_deduct;
Expand Down
Loading