Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
brenzi committed Aug 27, 2024
1 parent 1e48af8 commit 80bd1f8
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 95 deletions.
92 changes: 46 additions & 46 deletions democracy/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
use crate as dut;
use encointer_primitives::{balances::BalanceType, scheduler::CeremonyPhaseType};
use sp_runtime::{
traits::{ConstU128, ConstU64},
BuildStorage,
traits::{ConstU128, ConstU64},
BuildStorage,
};
use test_utils::*;

Expand All @@ -45,14 +45,14 @@ frame_support::construct_runtime!(
// }

impl dut::Config for TestRuntime {
type RuntimeEvent = RuntimeEvent;
type MaxReputationCount = ConstU32<10>;
// 10 6s blocks
type ConfirmationPeriod = ConstU64<60000>;
// 40 6s blocks
type ProposalLifetime = ConstU64<240000>;
type MinTurnout = ConstU128<20>; // 2%
type WeightInfo = (); // 2%
type RuntimeEvent = RuntimeEvent;
type MaxReputationCount = ConstU32<10>;
// 10 6s blocks
type ConfirmationPeriod = ConstU64<60000>;
// 40 6s blocks
type ProposalLifetime = ConstU64<240000>;
type MinTurnout = ConstU128<20>; // 2%
type WeightInfo = (); // 2%
}

// boilerplate
Expand All @@ -68,45 +68,45 @@ impl_encointer_treasuries!(TestRuntime);

// genesis values
pub fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::<TestRuntime>::default().build_storage().unwrap();
let mut t = frame_system::GenesisConfig::<TestRuntime>::default().build_storage().unwrap();

dut::GenesisConfig::<TestRuntime> { proposal_count: 0, ..Default::default() }
.assimilate_storage(&mut t)
.unwrap();
dut::GenesisConfig::<TestRuntime> { proposal_count: 0, ..Default::default() }
.assimilate_storage(&mut t)
.unwrap();

pallet_encointer_scheduler::GenesisConfig::<TestRuntime> {
current_ceremony_index: 7,
phase_durations: vec![
(CeremonyPhaseType::Registering, ONE_DAY),
(CeremonyPhaseType::Assigning, ONE_DAY),
(CeremonyPhaseType::Attesting, ONE_DAY),
],
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();
pallet_encointer_scheduler::GenesisConfig::<TestRuntime> {
current_ceremony_index: 7,
phase_durations: vec![
(CeremonyPhaseType::Registering, ONE_DAY),
(CeremonyPhaseType::Assigning, ONE_DAY),
(CeremonyPhaseType::Attesting, ONE_DAY),
],
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();

pallet_encointer_ceremonies::GenesisConfig::<TestRuntime> {
ceremony_reward: BalanceType::from_num(1),
location_tolerance: LOCATION_TOLERANCE, // [m]
time_tolerance: TIME_TOLERANCE, // [ms]
inactivity_timeout: 12,
endorsement_tickets_per_bootstrapper: 50,
endorsement_tickets_per_reputable: 2,
reputation_lifetime: 5,
meetup_time_offset: 0,
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();
pallet_encointer_ceremonies::GenesisConfig::<TestRuntime> {
ceremony_reward: BalanceType::from_num(1),
location_tolerance: LOCATION_TOLERANCE, // [m]
time_tolerance: TIME_TOLERANCE, // [ms]
inactivity_timeout: 12,
endorsement_tickets_per_bootstrapper: 50,
endorsement_tickets_per_reputable: 2,
reputation_lifetime: 5,
meetup_time_offset: 0,
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();

pallet_encointer_communities::GenesisConfig::<TestRuntime> {
min_solar_trip_time_s: 1,
max_speed_mps: 83,
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();
pallet_encointer_communities::GenesisConfig::<TestRuntime> {
min_solar_trip_time_s: 1,
max_speed_mps: 83,
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();

t.into()
t.into()
}
98 changes: 49 additions & 49 deletions treasuries/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
use core::marker::PhantomData;
use encointer_primitives::communities::CommunityIdentifier;
use frame_support::{
traits::{Currency, ExistenceRequirement::KeepAlive, Get},
PalletId,
traits::{Currency, ExistenceRequirement::KeepAlive, Get},
PalletId,
};
use log::info;
use parity_scale_codec::Decode;
Expand All @@ -38,60 +38,60 @@ mod mock;
mod tests;

pub type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use super::*;
use frame_support::pallet_prelude::*;

#[pallet::pallet]
pub struct Pallet<T>(PhantomData<T>);
#[pallet::pallet]
pub struct Pallet<T>(PhantomData<T>);

#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type Currency: Currency<Self::AccountId>;
#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type Currency: Currency<Self::AccountId>;

/// The treasuries' pallet id, used for deriving sovereign account IDs per community.
#[pallet::constant]
type PalletId: Get<PalletId>;
}
/// The treasuries' pallet id, used for deriving sovereign account IDs per community.
#[pallet::constant]
type PalletId: Get<PalletId>;
}

impl<T: Config> Pallet<T>
where
sp_core::H256: From<<T as frame_system::Config>::Hash>,
T::AccountId: AsRef<[u8; 32]>,
{
pub fn get_community_treasury_account_unchecked(
maybecid: Option<CommunityIdentifier>,
) -> T::AccountId {
let treasury_identifier =
[<T as Config>::PalletId::get().0.as_slice(), maybecid.encode().as_slice()]
.concat();
let treasury_id_hash: H256 = T::Hashing::hash_of(&treasury_identifier).into();
T::AccountId::decode(&mut treasury_id_hash.as_bytes())
.expect("32 bytes can always construct an AccountId32")
}
impl<T: Config> Pallet<T>
where
sp_core::H256: From<<T as frame_system::Config>::Hash>,
T::AccountId: AsRef<[u8; 32]>,
{
pub fn get_community_treasury_account_unchecked(
maybecid: Option<CommunityIdentifier>,
) -> T::AccountId {
let treasury_identifier =
[<T as Config>::PalletId::get().0.as_slice(), maybecid.encode().as_slice()]
.concat();
let treasury_id_hash: H256 = T::Hashing::hash_of(&treasury_identifier).into();
T::AccountId::decode(&mut treasury_id_hash.as_bytes())
.expect("32 bytes can always construct an AccountId32")
}

/// returns the account id where remaining funds of closed faucets go
pub fn do_spend_native(
maybecid: Option<CommunityIdentifier>,
beneficiary: T::AccountId,
amount: BalanceOf<T>,
) -> DispatchResultWithPostInfo {
let treasury = Self::get_community_treasury_account_unchecked(maybecid);
T::Currency::transfer(&treasury, &beneficiary, amount, KeepAlive)?;
info!(target: LOG, "treasury spent native: {:?}, {:?} to {:?}", maybecid, amount, beneficiary);
Self::deposit_event(Event::SpentNative { treasury, beneficiary, amount });
Ok(().into())
}
}
/// returns the account id where remaining funds of closed faucets go
pub fn do_spend_native(
maybecid: Option<CommunityIdentifier>,
beneficiary: T::AccountId,
amount: BalanceOf<T>,
) -> DispatchResultWithPostInfo {
let treasury = Self::get_community_treasury_account_unchecked(maybecid);
T::Currency::transfer(&treasury, &beneficiary, amount, KeepAlive)?;
info!(target: LOG, "treasury spent native: {:?}, {:?} to {:?}", maybecid, amount, beneficiary);
Self::deposit_event(Event::SpentNative { treasury, beneficiary, amount });
Ok(().into())
}
}

#[pallet::event]
#[pallet::generate_deposit(pub (super) fn deposit_event)]
pub enum Event<T: Config> {
/// treasury spent native tokens from community `cid` to `beneficiary` amounting `amount`
SpentNative { treasury: T::AccountId, beneficiary: T::AccountId, amount: BalanceOf<T> },
}
#[pallet::event]
#[pallet::generate_deposit(pub (super) fn deposit_event)]
pub enum Event<T: Config> {
/// treasury spent native tokens from community `cid` to `beneficiary` amounting `amount`
SpentNative { treasury: T::AccountId, beneficiary: T::AccountId, amount: BalanceOf<T> },
}
}

0 comments on commit 80bd1f8

Please sign in to comment.