Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ndkazu committed Nov 3, 2024
1 parent 8323d0a commit ffc4288
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 65 deletions.
10 changes: 5 additions & 5 deletions substrate/frame/distribution/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl<T: Config> Pallet<T> {
// check if the pot has enough fund for the Spend
// Check that the Pot as enough funds for the transfer
let remaining_balance = balance.saturating_sub(project.amount);

// we check that holding the necessary amount cannot fail
if remaining_balance > minimum_balance && balance > project.amount {
// Create a new Spend
Expand All @@ -95,17 +95,17 @@ impl<T: Config> Pallet<T> {
&HoldReason::FundsReserved.into(),
&pot,
project.amount,
)
)
.expect("Funds Reserve Failed");

// Emmit an event
let now = T::BlockNumberProvider::current_block_number();
Self::deposit_event(Event::SpendCreated {
when: now,
amount: new_spend.amount,
project_id: project.project_id.clone(),
});
});

false
} else {
true
Expand Down
11 changes: 3 additions & 8 deletions substrate/frame/distribution/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,24 +226,19 @@ pub mod pallet {
let info = Spends::<T>::get(&project_id).ok_or(Error::<T>::InexistentSpend)?;
if now >= info.valid_from {
let call0: <T as Config>::RuntimeCall =
(Call::<T>::execute_claim { project_id: project_id.clone() }).into();
(Call::<T>::execute_claim { project_id: project_id.clone() }).into();
let call1: CallOf<T> = call0.clone().into();
let call = T::Preimages::bound(call1)?;
Self::schedule_enactment(project_id.clone(), call)?;
Self::deposit_event(Event::WillBeEnacted {
project_id,
});
Self::deposit_event(Event::WillBeEnacted { project_id });
Ok(())
} else{
} else {
Self::deposit_event(Event::NotClaimingPeriod {
project_id,
claiming_period: info.valid_from,
});
Ok(())
}



}

#[pallet::call_index(1)]
Expand Down
10 changes: 6 additions & 4 deletions substrate/frame/distribution/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@
use crate as pallet_distribution;
pub use frame_support::{
derive_impl, parameter_types,
traits::{ConstU128, ConstU16, ConstU32, EqualPrivilegeOnly, ConstU64, OnFinalize, OnInitialize},
weights::Weight,PalletId,
traits::{
ConstU128, ConstU16, ConstU32, ConstU64, EqualPrivilegeOnly, OnFinalize, OnInitialize,
},
weights::Weight,
PalletId,
};
pub use frame_system::EnsureRoot;
pub use sp_core::H256;
pub use sp_runtime::{
traits::{AccountIdConversion, BlakeTwo256, IdentityLookup},
BuildStorage,
};
pub use frame_system::EnsureRoot;
pub type Block = frame_system::mocking::MockBlock<Test>;
pub type Balance = u64;
pub type AccountId = u64;
Expand All @@ -44,7 +47,6 @@ frame_support::construct_runtime!(
}
);


parameter_types! {
pub MaxWeight: Weight = Weight::from_parts(2_000_000_000_000, u64::MAX);
}
Expand Down
79 changes: 31 additions & 48 deletions substrate/frame/distribution/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ pub fn run_to_block(n: BlockNumberFor<Test>) {

pub fn create_project(project_id: AccountId, amount: u128) {
let submission_block = <Test as Config>::BlockNumberProvider::current_block_number();
let project: types::ProjectInfo<Test> = ProjectInfo { project_id, submission_block, amount: amount.try_into().unwrap() };
let project: types::ProjectInfo<Test> =
ProjectInfo { project_id, submission_block, amount: amount.try_into().unwrap() };
Projects::<Test>::mutate(|value| {
let mut val = value.clone();
let _ = val.try_push(project);
Expand Down Expand Up @@ -71,30 +72,18 @@ fn spends_creation_works_but_not_executed_before_claim_period() {
let valid_from = now.saturating_add(<Test as Config>::BufferPeriod::get().into());

// The 3 Spends are known
let alice_spend: types::SpendInfo<Test> = SpendInfo {
amount: amount1,
valid_from,
whitelisted_project: ALICE,
claimed: false,
};

let bob_spend: types::SpendInfo<Test> = SpendInfo {
amount: amount2,
valid_from,
whitelisted_project: BOB,
claimed: false,
};

let dave_spend: types::SpendInfo<Test> = SpendInfo {
amount: amount3,
valid_from,
whitelisted_project: DAVE,
claimed: false,
};

let _=Distribution::claim_reward_for(RawOrigin::Signed(EVE).into(), ALICE);
let _=Distribution::claim_reward_for(RawOrigin::Signed(EVE).into(), BOB);
let _=Distribution::claim_reward_for(RawOrigin::Signed(EVE).into(), DAVE);
let alice_spend: types::SpendInfo<Test> =
SpendInfo { amount: amount1, valid_from, whitelisted_project: ALICE, claimed: false };

let bob_spend: types::SpendInfo<Test> =
SpendInfo { amount: amount2, valid_from, whitelisted_project: BOB, claimed: false };

let dave_spend: types::SpendInfo<Test> =
SpendInfo { amount: amount3, valid_from, whitelisted_project: DAVE, claimed: false };

let _ = Distribution::claim_reward_for(RawOrigin::Signed(EVE).into(), ALICE);
let _ = Distribution::claim_reward_for(RawOrigin::Signed(EVE).into(), BOB);
let _ = Distribution::claim_reward_for(RawOrigin::Signed(EVE).into(), DAVE);

// List of Spends actually created & stored
let list0: Vec<_> = Spends::<Test>::iter_keys().collect();
Expand All @@ -115,10 +104,10 @@ fn spends_creation_works_but_not_executed_before_claim_period() {
}),
]);

assert_eq!(Spends::<Test>::contains_key(ALICE),true);
assert_eq!(Spends::<Test>::get(ALICE),Some(alice_spend));
assert_eq!(Spends::<Test>::get(BOB),Some(bob_spend));
assert_eq!(Spends::<Test>::get(DAVE),Some(dave_spend));
assert_eq!(Spends::<Test>::contains_key(ALICE), true);
assert_eq!(Spends::<Test>::get(ALICE), Some(alice_spend));
assert_eq!(Spends::<Test>::get(BOB), Some(bob_spend));
assert_eq!(Spends::<Test>::get(DAVE), Some(dave_spend));
})
}

Expand All @@ -144,9 +133,9 @@ fn funds_are_locked() {

let total_on_hold = amount1.saturating_add(amount2).saturating_add(amount3);
let pot_account = Distribution::pot_account();
let _=Distribution::claim_reward_for(RawOrigin::Signed(EVE).into(), ALICE);
let _=Distribution::claim_reward_for(RawOrigin::Signed(EVE).into(), BOB);
let _=Distribution::claim_reward_for(RawOrigin::Signed(EVE).into(), DAVE);
let _ = Distribution::claim_reward_for(RawOrigin::Signed(EVE).into(), ALICE);
let _ = Distribution::claim_reward_for(RawOrigin::Signed(EVE).into(), BOB);
let _ = Distribution::claim_reward_for(RawOrigin::Signed(EVE).into(), DAVE);
let hold =
<<Test as Config>::NativeBalance as fungible::hold::Inspect<u64>>::balance_on_hold(
&HoldReason::FundsReserved.into(),
Expand Down Expand Up @@ -194,38 +183,32 @@ fn funds_claim_works() {
.saturating_add(<Test as Config>::EpochDurationBlocks::get().into());
run_to_block(now);

let _=Distribution::claim_reward_for(RawOrigin::Signed(EVE).into(), ALICE);
let _ = Distribution::claim_reward_for(RawOrigin::Signed(EVE).into(), ALICE);
let project = Spends::<Test>::get(ALICE).unwrap();
let project_id = project.whitelisted_project;
let balance_0 =
<<Test as Config>::NativeBalance as fungible::Inspect<u64>>::balance(&project_id);

// Spend is in storage
//assert!(Spends::<Test>::get(ALICE).is_some());

now = project.valid_from;
run_to_block(now);

assert_ok!(Distribution::claim_reward_for(RawOrigin::Signed(EVE).into(), project_id,));
expect_events(vec![
RuntimeEvent::Distribution(Event::WillBeEnacted {
project_id: ALICE,
}),
]);
expect_events(vec![RuntimeEvent::Distribution(Event::WillBeEnacted { project_id: ALICE })]);

now = now.saturating_add(<Test as Config>::BufferPeriod::get().into());
run_to_block(now);
let balance_1 =
<<Test as Config>::NativeBalance as fungible::Inspect<u64>>::balance(&project_id);

expect_events(vec![
RuntimeEvent::Distribution(Event::RewardClaimed {
when: now,
amount: project.amount,
project_id,
}),
]);
assert!(balance_1 > balance_0);

expect_events(vec![RuntimeEvent::Distribution(Event::RewardClaimed {
when: now,
amount: project.amount,
project_id,
})]);
assert!(balance_1 > balance_0);
assert_eq!(Projects::<Test>::get().len(), 0);
// Spend has been removed from storage
assert!(!Spends::<Test>::get(0).is_some());
Expand Down

0 comments on commit ffc4288

Please sign in to comment.