Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 42789a2

Browse files
committed
use fungibles traits for treasury, tips and bounties pallets
1 parent 354aa13 commit 42789a2

File tree

11 files changed

+400
-238
lines changed

11 files changed

+400
-238
lines changed

bin/node/runtime/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ parameter_types! {
445445
pub enum HoldReason {
446446
/// The NIS Pallet has reserved it for a non-fungible receipt.
447447
Nis,
448+
// The Treasury Pallet has reserved it for a treasury spend proposal.
449+
Treasury,
448450
}
449451

450452
impl pallet_balances::Config for Runtime {
@@ -1105,6 +1107,7 @@ parameter_types! {
11051107
pub const MaximumReasonLength: u32 = 300;
11061108
pub const MaxApprovals: u32 = 100;
11071109
pub const MaxBalance: Balance = Balance::max_value();
1110+
pub const TreasuryHoldReason: HoldReason = HoldReason::Treasury;
11081111
}
11091112

11101113
impl pallet_treasury::Config for Runtime {
@@ -1118,6 +1121,7 @@ impl pallet_treasury::Config for Runtime {
11181121
EnsureRoot<AccountId>,
11191122
pallet_collective::EnsureProportionMoreThan<AccountId, CouncilCollective, 1, 2>,
11201123
>;
1124+
type HoldReason = TreasuryHoldReason;
11211125
type RuntimeEvent = RuntimeEvent;
11221126
type OnSlash = ();
11231127
type ProposalBond = ProposalBond;

frame/bounties/src/lib.rs

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ pub mod weights;
9090
use sp_std::prelude::*;
9191

9292
use frame_support::traits::{
93-
Currency, ExistenceRequirement::AllowDeath, Get, Imbalance, OnUnbalanced, ReservableCurrency,
93+
fungible::*,
94+
tokens::{Precision, Preservation},
95+
Get, Imbalance,
9496
};
9597

9698
use sp_runtime::{
@@ -108,8 +110,7 @@ pub use weights::WeightInfo;
108110
pub use pallet::*;
109111

110112
type BalanceOf<T, I = ()> = pallet_treasury::BalanceOf<T, I>;
111-
112-
type PositiveImbalanceOf<T, I = ()> = pallet_treasury::PositiveImbalanceOf<T, I>;
113+
type DebtOf<T, I = ()> = pallet_treasury::DebtOf<T, I>;
113114

114115
/// An index of a bounty. Just a `u32`.
115116
pub type BountyIndex = u32;
@@ -446,8 +447,9 @@ pub mod pallet {
446447

447448
let slash_curator = |curator: &T::AccountId,
448449
curator_deposit: &mut BalanceOf<T, I>| {
449-
let imbalance = T::Currency::slash_reserved(curator, *curator_deposit).0;
450-
T::OnSlash::on_unbalanced(imbalance);
450+
let imbalance =
451+
T::Currency::slash(&T::HoldReason::get(), curator, *curator_deposit).0;
452+
T::OnSlash::handle(imbalance);
451453
*curator_deposit = Zero::zero();
452454
};
453455

@@ -484,9 +486,13 @@ pub mod pallet {
484486
} else {
485487
// Else this is the curator, willingly giving up their role.
486488
// Give back their deposit.
487-
let err_amount =
488-
T::Currency::unreserve(curator, bounty.curator_deposit);
489-
debug_assert!(err_amount.is_zero());
489+
let err_amount = T::Currency::release(
490+
&T::HoldReason::get(),
491+
curator,
492+
bounty.curator_deposit,
493+
Precision::Exact,
494+
);
495+
debug_assert!(err_amount.is_ok());
490496
bounty.curator_deposit = Zero::zero();
491497
// Continue to change bounty status below...
492498
}
@@ -532,7 +538,7 @@ pub mod pallet {
532538
ensure!(signer == *curator, Error::<T, I>::RequireCurator);
533539

534540
let deposit = Self::calculate_curator_deposit(&bounty.fee);
535-
T::Currency::reserve(curator, deposit)?;
541+
T::Currency::hold(&T::HoldReason::get(), curator, deposit)?;
536542
bounty.curator_deposit = deposit;
537543

538544
let update_due = frame_system::Pallet::<T>::block_number() +
@@ -623,23 +629,36 @@ pub mod pallet {
623629
Error::<T, I>::Premature
624630
);
625631
let bounty_account = Self::bounty_account_id(bounty_id);
626-
let balance = T::Currency::free_balance(&bounty_account);
632+
let balance = T::Currency::balance(&bounty_account);
627633
let fee = bounty.fee.min(balance); // just to be safe
628634
let payout = balance.saturating_sub(fee);
629-
let err_amount = T::Currency::unreserve(&curator, bounty.curator_deposit);
630-
debug_assert!(err_amount.is_zero());
635+
let err_amount = T::Currency::release(
636+
&T::HoldReason::get(),
637+
&curator,
638+
bounty.curator_deposit,
639+
Precision::Exact,
640+
);
641+
debug_assert!(err_amount.is_ok());
631642

632643
// Get total child bounties curator fees, and subtract it from the parent
633644
// curator fee (the fee in present referenced bounty, `self`).
634645
let children_fee = T::ChildBountyManager::children_curator_fees(bounty_id);
635646
debug_assert!(children_fee <= fee);
636647

637648
let final_fee = fee.saturating_sub(children_fee);
638-
let res =
639-
T::Currency::transfer(&bounty_account, &curator, final_fee, AllowDeath); // should not fail
649+
let res = T::Currency::transfer(
650+
&bounty_account,
651+
&curator,
652+
final_fee,
653+
Preservation::Expendable,
654+
); // should not fail
640655
debug_assert!(res.is_ok());
641-
let res =
642-
T::Currency::transfer(&bounty_account, &beneficiary, payout, AllowDeath); // should not fail
656+
let res = T::Currency::transfer(
657+
&bounty_account,
658+
&beneficiary,
659+
payout,
660+
Preservation::Expendable,
661+
); // should not fail
643662
debug_assert!(res.is_ok());
644663

645664
*maybe_bounty = None;
@@ -693,8 +712,10 @@ pub mod pallet {
693712
// The reject origin would like to cancel a proposed bounty.
694713
BountyDescriptions::<T, I>::remove(bounty_id);
695714
let value = bounty.bond;
696-
let imbalance = T::Currency::slash_reserved(&bounty.proposer, value).0;
697-
T::OnSlash::on_unbalanced(imbalance);
715+
let credit =
716+
T::Currency::slash(&T::HoldReason::get(), &bounty.proposer, value)
717+
.0;
718+
T::OnSlash::handle(credit);
698719
*maybe_bounty = None;
699720

700721
Self::deposit_event(Event::<T, I>::BountyRejected {
@@ -716,9 +737,13 @@ pub mod pallet {
716737
},
717738
BountyStatus::Active { curator, .. } => {
718739
// Cancelled by council, refund deposit of the working curator.
719-
let err_amount =
720-
T::Currency::unreserve(curator, bounty.curator_deposit);
721-
debug_assert!(err_amount.is_zero());
740+
let err_amount = T::Currency::release(
741+
&T::HoldReason::get(),
742+
curator,
743+
bounty.curator_deposit,
744+
Precision::Exact,
745+
);
746+
debug_assert!(err_amount.is_ok());
722747
// Then execute removal of the bounty below.
723748
},
724749
BountyStatus::PendingPayout { .. } => {
@@ -734,12 +759,12 @@ pub mod pallet {
734759

735760
BountyDescriptions::<T, I>::remove(bounty_id);
736761

737-
let balance = T::Currency::free_balance(&bounty_account);
762+
let balance = T::Currency::balance(&bounty_account);
738763
let res = T::Currency::transfer(
739764
&bounty_account,
740765
&Self::account_id(),
741766
balance,
742-
AllowDeath,
767+
Preservation::Expendable,
743768
); // should not fail
744769
debug_assert!(res.is_ok());
745770
*maybe_bounty = None;
@@ -834,7 +859,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
834859
// reserve deposit for new bounty
835860
let bond = T::BountyDepositBase::get() +
836861
T::DataDepositPerByte::get() * (bounded_description.len() as u32).into();
837-
T::Currency::reserve(&proposer, bond)
862+
T::Currency::hold(&T::HoldReason::get(), &proposer, bond)
838863
.map_err(|_| Error::<T, I>::InsufficientProposersBalance)?;
839864

840865
BountyCount::<T, I>::put(index + 1);
@@ -860,7 +885,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
860885
impl<T: Config<I>, I: 'static> pallet_treasury::SpendFunds<T, I> for Pallet<T, I> {
861886
fn spend_funds(
862887
budget_remaining: &mut BalanceOf<T, I>,
863-
imbalance: &mut PositiveImbalanceOf<T, I>,
888+
imbalance: &mut DebtOf<T, I>,
864889
total_weight: &mut Weight,
865890
missed_any: &mut bool,
866891
) {
@@ -876,14 +901,24 @@ impl<T: Config<I>, I: 'static> pallet_treasury::SpendFunds<T, I> for Pallet<T, I
876901
bounty.status = BountyStatus::Funded;
877902

878903
// return their deposit.
879-
let err_amount = T::Currency::unreserve(&bounty.proposer, bounty.bond);
880-
debug_assert!(err_amount.is_zero());
904+
let released_amount = T::Currency::release(
905+
&T::HoldReason::get(),
906+
&bounty.proposer,
907+
bounty.bond,
908+
Precision::Exact,
909+
);
910+
debug_assert!(released_amount.is_ok());
881911

882912
// fund the bounty account
883-
imbalance.subsume(T::Currency::deposit_creating(
913+
let _ = T::Currency::deposit(
884914
&Self::bounty_account_id(index),
885915
bounty.value,
886-
));
916+
Precision::Exact,
917+
)
918+
.and_then(|debt| {
919+
imbalance.subsume(debt);
920+
Ok(())
921+
});
887922

888923
Self::deposit_event(Event::<T, I>::BountyBecameActive { index });
889924
false

0 commit comments

Comments
 (0)