From fca97a3c5ab4874a26a7600becb7ecd8def317f9 Mon Sep 17 00:00:00 2001 From: Victor Genin Date: Mon, 13 Nov 2023 20:16:44 +0200 Subject: [PATCH] customer debtors --- pallets/ddc-payouts/src/lib.rs | 48 ++++++++++++++++------------------ 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/pallets/ddc-payouts/src/lib.rs b/pallets/ddc-payouts/src/lib.rs index 77a66aa45..bf1a286dc 100644 --- a/pallets/ddc-payouts/src/lib.rs +++ b/pallets/ddc-payouts/src/lib.rs @@ -183,11 +183,13 @@ pub mod pallet { #[pallet::storage] #[pallet::getter(fn debtor_customers)] - pub type DebtorCustomers = StorageMap< + pub type DebtorCustomers = StorageDoubleMap< _, Blake2_128Concat, + ClusterId, + Blake2_128Concat, T::AccountId, - BoundedBTreeMap, + u128, ValueQuery, >; @@ -361,7 +363,7 @@ pub mod pallet { .checked_add(customer_charge.gets) .ok_or(Error::::ArithmeticOverflow)?; - let customer_id = payer.0; + let customer_id = payer.0.clone(); match T::CustomerCharger::charge_content_owner( customer_id.clone(), updated_billing_report.vault.clone(), @@ -385,28 +387,24 @@ pub mod pallet { }); }, Err(e) => { - // todo: save problematic charge - // todo: add logs - updated_billing_report - .charging_processed_batches - .try_insert(batch_index) - .map_err(|_| Error::::BoundedVecOverflow)?; - - /* --- - DebtorCustomers = StorageMap< - _, - Blake2_128Concat, - T::AccountId, - BoundedBTreeMap, - let mut debtor_customers = DebtorCustomers::::try_get(cluster_id.clone(), era) - .map_err(|_| Error::::BillingReportDoesNotExist)?; - - ensure!(billing_report.state == State::Initialized, Error::::NotExpectedState); - - billing_report.charging_max_batch_index = max_batch_index; - billing_report.state = State::ChargingCustomers; - ActiveBillingReports::::insert(cluster_id.clone(), era, billing_report); - --- */ + let customer_debt = BillingReportDebt { + cluster_id, + era, + batch_index, + amount: total_customer_charge, + }; + let mut customer_dept = + DebtorCustomers::::try_get(cluster_id, customer_id.clone()) + .unwrap_or(Zero::zero()); + + customer_dept = customer_dept + .checked_add(total_customer_charge) + .ok_or(Error::::ArithmeticOverflow)?; + DebtorCustomers::::insert( + cluster_id, + customer_id.clone(), + customer_dept, + ); Self::deposit_event(Event::::ChargeFailed { cluster_id,