Skip to content

Commit

Permalink
payout<->customers
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Genin committed Nov 13, 2023
1 parent e8233e4 commit 7ca5bf0
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 70 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 45 additions & 39 deletions pallets/ddc-customers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use codec::{Decode, Encode, HasCompact};

use ddc_primitives::{BucketId, ClusterId};
use ddc_traits::cluster::ClusterVisitor;
use ddc_traits::{cluster::ClusterVisitor, customer::CustomerCharger};
use frame_support::{
parameter_types,
traits::{Currency, DefensiveSaturating, ExistenceRequirement},
Expand Down Expand Up @@ -171,6 +171,11 @@ pub mod pallet {
pub fn DefaultBucketCount<T: Config>() -> BucketId {
0
}

#[pallet::storage]
#[pallet::getter(fn dac_account)]
pub type DACAccount<T: Config> = StorageValue<_, T::AccountId>;

#[pallet::storage]
#[pallet::getter(fn buckets_count)]
pub type BucketsCount<T: Config> =
Expand All @@ -191,12 +196,12 @@ pub mod pallet {
/// it will not be emitted for staking rewards when they are added to stake.
Deposited(T::AccountId, BalanceOf<T>),
/// An account has initiated unlock for amount. \[owner, amount\]
InitiatDepositUnlock(T::AccountId, BalanceOf<T>),
InitialDepositUnlock(T::AccountId, BalanceOf<T>),
/// An account has called `withdraw_unlocked_deposit` and removed unlocking chunks worth
/// `Balance` from the unlocking queue. \[owner, amount\]
Withdrawn(T::AccountId, BalanceOf<T>),
/// Total amount charged from all accounts to pay CDN nodes
Charged(BalanceOf<T>),
/// The acconut has been charged for the usage
Charged(T::AccountId, BalanceOf<T>),
/// Bucket with specific id created
BucketCreated(BucketId),
}
Expand All @@ -221,6 +226,10 @@ pub mod pallet {
BucketDoesNotExist,
/// DDC Cluster with provided id doesn't exist
ClusterDoesNotExist,
// unauthorised operation
Unauthorised,
// Arithmetic underflow
ArithmeticUnderflow,
}

#[pallet::genesis_config]
Expand Down Expand Up @@ -351,7 +360,7 @@ pub mod pallet {
/// can co-exists at the same time. In that case, [`Call::withdraw_unlocked_deposit`] need
/// to be called first to remove some of the chunks (if possible).
///
/// Emits `InitiatDepositUnlock`.
/// Emits `InitialDepositUnlock`.
///
/// See also [`Call::withdraw_unlocked_deposit`].
#[pallet::weight(10_000)]
Expand Down Expand Up @@ -380,7 +389,6 @@ pub mod pallet {
let current_block = <frame_system::Pallet<T>>::block_number();
// Note: locking for extra block to allow for accounting
let block = current_block + <T as pallet::Config>::UnlockingDelay::get();
log::debug!("Block for the unlock: {:?}", block);

if let Some(chunk) =
ledger.unlocking.last_mut().filter(|chunk| chunk.block == block)
Expand All @@ -398,7 +406,7 @@ pub mod pallet {

Self::update_ledger(&owner, &ledger);

Self::deposit_event(Event::<T>::InitiatDepositUnlock(ledger.owner, value));
Self::deposit_event(Event::<T>::InitialDepositUnlock(ledger.owner, value));
}
Ok(())
}
Expand Down Expand Up @@ -451,7 +459,7 @@ pub mod pallet {
<T as pallet::Config>::Currency::transfer(
&account_id,
&owner,
value,
value.clone(),
ExistenceRequirement::KeepAlive,
)?;
Self::deposit_event(Event::<T>::Withdrawn(owner, value));
Expand Down Expand Up @@ -506,40 +514,38 @@ pub mod pallet {

Ok(())
}
}

// Charge payments from content owners
pub fn charge_content_owners(
paying_accounts: Vec<BucketsDetails<BalanceOf<T>>>,
pricing: u128,
impl<T: Config> CustomerCharger<T> for Pallet<T> {
fn charge_content_owner(
content_owner: T::AccountId,
billing_vault: T::AccountId,
amount: u128,
) -> DispatchResult {
let mut total_charged = BalanceOf::<T>::zero();

for bucket_details in paying_accounts.iter() {
let bucket: Bucket<T::AccountId> = Self::buckets(bucket_details.bucket_id)
.ok_or(Error::<T>::BucketDoesNotExist)?;
let content_owner = bucket.owner_id;
let amount = bucket_details.amount * pricing.saturated_into::<BalanceOf<T>>();

let mut ledger = Self::ledger(&content_owner).ok_or(Error::<T>::NotOwner)?;
if ledger.active >= amount {
ledger.total -= amount;
ledger.active -= amount;
total_charged += amount;
Self::update_ledger(&content_owner, &ledger);
} else {
let diff = amount - ledger.active;
total_charged += ledger.active;
ledger.total -= ledger.active;
ledger.active = BalanceOf::<T>::zero();
let (ledger, charged) = ledger.charge_unlocking(diff);
Self::update_ledger(&content_owner, &ledger);
total_charged += charged;
}
}
log::debug!("Total charged: {:?}", &total_charged);
let mut ledger = Self::ledger(&content_owner).ok_or(Error::<T>::NotOwner)?;
let mut amount_to_deduct = amount.saturated_into::<BalanceOf<T>>();

ensure!(ledger.total >= ledger.active, Error::<T>::ArithmeticUnderflow);
if ledger.active >= amount_to_deduct {
ledger.active -= amount_to_deduct;
ledger.total -= amount_to_deduct;
Self::update_ledger(&content_owner, &ledger);
} else {
let diff = amount_to_deduct - ledger.active;
ledger.total -= ledger.active;
amount_to_deduct = ledger.active;
ledger.active = BalanceOf::<T>::zero();
let (ledger, _charged) = ledger.charge_unlocking(diff);
Self::update_ledger(&content_owner, &ledger);
};

Self::deposit_event(Event::<T>::Charged(total_charged));
log::debug!("Deposit event executed");
<T as pallet::Config>::Currency::transfer(
&Self::account_id(),
&billing_vault,
amount_to_deduct,
ExistenceRequirement::KeepAlive,
)?;
Self::deposit_event(Event::<T>::Charged(content_owner, amount_to_deduct));

Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions pallets/ddc-payouts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false, features = ["derive"] }
ddc-primitives = { version = "0.1.0", default-features = false, path = "../../primitives" }
ddc-traits = { version = "0.1.0", default-features = false, path = "../../traits" }
frame-benchmarking = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30", optional = true }
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" }
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" }
Expand Down
Loading

0 comments on commit 7ca5bf0

Please sign in to comment.