Skip to content
Snippets Groups Projects

add pallet treasury

Merged Éloïs requested to merge elois-treasury into master
Files
2
@@ -129,12 +129,16 @@ pub mod pallet {
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// Force the destruction of an account because its free balance is insufficient to pay
/// the account creation price.
/// [who, balance]
ForceDestroy {
who: T::AccountId,
balance: T::Balance,
},
/// Random id assigned
/// [account_id, random_id]
RandomIdAssigned {
account_id: T::AccountId,
random_id: H256,
},
RandomIdAssigned { who: T::AccountId, random_id: H256 },
}
// HOOKS //
@@ -156,55 +160,58 @@ pub mod pallet {
total_weight += 100_000;
} else {
// If the account is not self-sufficient, it must pay the account creation fees
frame_system::Pallet::<T>::inc_providers(&account_id);
let res = <pallet_balances::Pallet<T> as Currency<T::AccountId>>::withdraw(
&account_id,
T::NewAccountPrice::get(),
frame_support::traits::WithdrawReasons::FEE,
ExistenceRequirement::KeepAlive,
);
if let Ok(imbalance) = res {
// The fees have been succesfully collected, we should:
// 1. Increment consumers to prevent the destruction of the account before
let account_data = frame_system::Pallet::<T>::get(&account_id);
let price = T::NewAccountPrice::get();
if account_data.free > price {
// The account can pay the new account price, we should:
// 1. Increment providers to create the account for frame_system point of view
// 2. Withdraw the "new account price" amount
// 3. Increment consumers to prevent the destruction of the account before
// the random id is assigned
// 2. Manage the funds collected
// 3. Submit random id generation request
// 4. Save the id of the random generation request.
let res =
frame_system::Pallet::<T>::inc_consumers_without_limit(&account_id);
debug_assert!(
res.is_ok(),
"Cannot fail because providers are incremented just before"
);
T::OnUnbalanced::on_unbalanced(imbalance);
let request_id = pallet_provide_randomness::Pallet::<T>::force_request(
pallet_provide_randomness::RandomnessType::RandomnessFromTwoEpochsAgo,
H256(T::AccountIdToSalt::convert(account_id.clone())),
);
PendingRandomIdAssignments::<T>::insert(request_id, account_id);
total_weight += 200_000;
} else {
// The charges could not be deducted, we slash the account
let res = frame_system::Pallet::<T>::dec_providers(&account_id);
debug_assert!(
res.is_ok(),
"Cannot fail because providers are incremented just before"
);
let account_data = frame_system::Pallet::<T>::get(&account_id);
// 4. Manage the funds collected
// 5. Submit random id generation request
// 6. Save the id of the random generation request.
frame_system::Pallet::<T>::inc_providers(&account_id);
let res = <pallet_balances::Pallet<T> as Currency<T::AccountId>>::withdraw(
&account_id,
account_data.free.saturating_add(account_data.reserved),
price,
frame_support::traits::WithdrawReasons::FEE,
ExistenceRequirement::AllowDeath,
ExistenceRequirement::KeepAlive,
);
debug_assert!(
res.is_ok(),
"Cannot fail because we checked that the free balance was sufficient"
);
if let Ok(imbalance) = res {
T::OnUnbalanced::on_unbalanced(imbalance);
} else {
log::warn!(
"Unexpected withdraw fail to destroy account {:?}",
&account_id
let res =
frame_system::Pallet::<T>::inc_consumers_without_limit(&account_id);
debug_assert!(
res.is_ok(),
"Cannot fail because providers are incremented just before"
);
T::OnUnbalanced::on_unbalanced(imbalance);
let request_id = pallet_provide_randomness::Pallet::<T>::force_request(
pallet_provide_randomness::RandomnessType::RandomnessFromTwoEpochsAgo,
H256(T::AccountIdToSalt::convert(account_id.clone())),
);
PendingRandomIdAssignments::<T>::insert(request_id, account_id);
total_weight += 200_000;
}
} else {
// The charges could not be deducted, we slash the account
let balance_to_suppr =
account_data.free.saturating_add(account_data.reserved);
// Force account data supression
frame_system::Account::<T>::mutate(&account_id, |a| {
a.data.set_balances(Default::default())
});
Self::deposit_event(Event::ForceDestroy {
who: account_id,
balance: balance_to_suppr,
});
T::OnUnbalanced::on_unbalanced(pallet_balances::NegativeImbalance::new(
balance_to_suppr,
));
total_weight += 300_000;
}
}
@@ -225,7 +232,7 @@ where
account.data.random_id = Some(randomness);
});
Self::deposit_event(Event::RandomIdAssigned {
account_id,
who: account_id,
random_id: randomness,
});
200_000
Loading