Skip to content

Commit

Permalink
Merge pull request #2941 from subspace/domain_transfer_treasury
Browse files Browse the repository at this point in the history
Domains: add extrinsic to transfer amount to given account from treasury
  • Loading branch information
vedhavyas authored Jul 24, 2024
2 parents 13ec565 + 571f108 commit 5d3be70
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
19 changes: 18 additions & 1 deletion crates/pallet-domains/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use alloc::borrow::ToOwned;
use alloc::vec::Vec;
use frame_benchmarking::v2::*;
use frame_support::assert_ok;
use frame_support::traits::fungible::Mutate;
use frame_support::traits::fungible::{Inspect, Mutate};
use frame_support::traits::Hooks;
use frame_support::weights::Weight;
use frame_system::{Pallet as System, RawOrigin};
Expand Down Expand Up @@ -860,6 +860,23 @@ mod benchmarks {
assert_eq!(domain_obj.domain_config.operator_allow_list, new_allow_list);
}

#[benchmark]
fn transfer_treasury_funds() {
// Ensure the treasury account has balance
let treasury_amount = 5000u32.into();
let transfer_amount = 500u32.into();
let account = account("slashed_account", 1, SEED);
assert_eq!(T::Currency::balance(&account), 0u32.into());
T::Currency::set_balance(&T::TreasuryAccount::get(), treasury_amount);
#[extrinsic_call]
_(RawOrigin::Root, account.clone(), transfer_amount);
assert_eq!(T::Currency::balance(&account), transfer_amount);
assert_eq!(
T::Currency::balance(&T::TreasuryAccount::get()),
treasury_amount - transfer_amount
);
}

fn register_runtime<T: Config>() -> RuntimeId {
let genesis_storage = include_bytes!("../res/evm-domain-genesis-storage").to_vec();
let runtime_id = NextRuntimeId::<T>::get();
Expand Down
19 changes: 19 additions & 0 deletions crates/pallet-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ mod pallet {
use domain_runtime_primitives::EVMChainId;
use frame_support::pallet_prelude::*;
use frame_support::traits::fungible::{Inspect, InspectHold, Mutate, MutateHold};
use frame_support::traits::tokens::Preservation;
use frame_support::traits::Randomness as RandomnessT;
use frame_support::weights::Weight;
use frame_support::{Identity, PalletError};
Expand Down Expand Up @@ -1628,6 +1629,24 @@ mod pallet {

Ok(Some(actual_weight).into())
}

/// Transfer funds from treasury to given account
#[pallet::call_index(20)]
#[pallet::weight(T::WeightInfo::transfer_treasury_funds())]
pub fn transfer_treasury_funds(
origin: OriginFor<T>,
account_id: T::AccountId,
balance: BalanceOf<T>,
) -> DispatchResult {
ensure_root(origin)?;
T::Currency::transfer(
&T::TreasuryAccount::get(),
&account_id,
balance,
Preservation::Preserve,
)?;
Ok(())
}
}

#[pallet::genesis_config]
Expand Down
23 changes: 23 additions & 0 deletions crates/pallet-domains/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub trait WeightInfo {
fn unlock_funds() -> Weight;
fn unlock_nominator() -> Weight;
fn update_domain_operator_allow_list() -> Weight;
fn transfer_treasury_funds() -> Weight;
}

/// Weights for pallet_domains using the Substrate node and recommended hardware.
Expand Down Expand Up @@ -478,6 +479,17 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
.saturating_add(T::DbWeight::get().reads(1_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
/// Storage: `System::Account` (r:2 w:2)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
fn transfer_treasury_funds() -> Weight {
// Proof Size summary in bytes:
// Measured: `140`
// Estimated: `6196`
// Minimum execution time: 34_000_000 picoseconds.
Weight::from_parts(35_000_000, 6196)
.saturating_add(T::DbWeight::get().reads(2_u64))
.saturating_add(T::DbWeight::get().writes(2_u64))
}
}

// For backwards compatibility and tests
Expand Down Expand Up @@ -907,4 +919,15 @@ impl WeightInfo for () {
.saturating_add(ParityDbWeight::get().reads(1_u64))
.saturating_add(ParityDbWeight::get().writes(1_u64))
}
/// Storage: `System::Account` (r:2 w:2)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
fn transfer_treasury_funds() -> Weight {
// Proof Size summary in bytes:
// Measured: `140`
// Estimated: `6196`
// Minimum execution time: 34_000_000 picoseconds.
Weight::from_parts(35_000_000, 6196)
.saturating_add(ParityDbWeight::get().reads(2_u64))
.saturating_add(ParityDbWeight::get().writes(2_u64))
}
}

0 comments on commit 5d3be70

Please sign in to comment.