Skip to content

Commit

Permalink
fix: format code solved & tests passed
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumebogard committed Jul 20, 2022
1 parent f4b95bb commit 51dd2f3
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 118 deletions.
59 changes: 36 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::type_complexity)]

pub use pallet::*;

pub mod types;

pub use types::*;
use codec::EncodeLike;

mod mock;
#[cfg(test)]
mod subscribe_call;
mod mock;

pub use frame_support::{
ReversibleStorageHasher,
storage::IterableStorageMap,
traits::tokens::{
currency::{Currency, ReservableCurrency},
ExistenceRequirement,
},
ReversibleStorageHasher,
};


// #[cfg(test)]
// mod mock;
//
Expand All @@ -33,9 +32,8 @@ pub use frame_support::{
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_support::{pallet_prelude::*, sp_runtime::traits::Zero};
use frame_system::pallet_prelude::*;
use frame_support::sp_runtime::traits::Zero;

#[pallet::config]
pub trait Config: frame_system::Config {
Expand Down Expand Up @@ -73,9 +71,16 @@ pub mod pallet {
#[pallet::storage]
#[pallet::unbounded]
#[pallet::getter(fn subscriptions)]
pub type Subscriptions<T: Config> =
StorageMap<_, Blake2_256, u32, Vec<(Subscription<T::BlockNumber, BalanceOf<T>, T::AccountId>, T::AccountId)>, OptionQuery>;

pub type Subscriptions<T: Config> = StorageMap<
_,
Blake2_256,
u32,
Vec<(
Subscription<T::BlockNumber, BalanceOf<T>, T::AccountId>,
T::AccountId,
)>,
OptionQuery,
>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
Expand All @@ -87,12 +92,12 @@ pub mod pallet {
#[pallet::error]
pub enum Error<T> {
/// Invalid subscription
InvalidSubscription
InvalidSubscription,
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(n: T::BlockNumber) -> Weight {
fn on_initialize(_n: T::BlockNumber) -> Weight {
//// prop 1:
// - loop over all subscriptions (.iter_values())
// - check if the subscription shoud be taken
Expand All @@ -106,37 +111,45 @@ pub mod pallet {
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(1)]
pub fn subscribe(origin: OriginFor<T>, to: T::AccountId,
amount: BalanceOf<T>, frequency: T::BlockNumber, number_of_installment: Option<u32>) -> DispatchResult {

pub fn subscribe(
origin: OriginFor<T>,
to: T::AccountId,
amount: BalanceOf<T>,
frequency: T::BlockNumber,
number_of_installment: Option<u32>,
) -> DispatchResult {
let from = ensure_signed(origin)?;

// check if subscription is valid
ensure!(!frequency.is_zero() && !amount.is_zero(), Error::<T>::InvalidSubscription);
ensure!(
!frequency.is_zero() && !amount.is_zero(),
Error::<T>::InvalidSubscription
);

let sub = Subscription {
frequency: frequency.clone(),
amount: amount.clone(),
frequency,
amount,
remaining_payments: number_of_installment,
beneficiary: to.clone(),
};

let subscriptions: Vec<(Subscription<T::BlockNumber, BalanceOf<T>, T::AccountId>, T::AccountId)>
= vec![(sub, from.clone())];
let subscriptions: Vec<(
Subscription<T::BlockNumber, BalanceOf<T>, T::AccountId>,
T::AccountId,
)> = vec![(sub, from.clone())];

let key = TryInto::<u32>::try_into(<frame_system::Pallet<T>>::block_number()).ok();
let key_to_u32 = match key {
let key_to_u32 = match key {
Some(key) => key + 1,
_ => 0,
};

<Subscriptions<T>>::mutate(key_to_u32,|val| {
<Subscriptions<T>>::mutate(key_to_u32, |val| {
*val = Option::from(subscriptions);
});

Self::deposit_event(Event::SubscriptionCreated(to, from,amount, frequency));
Self::deposit_event(Event::SubscriptionCreated(to, from, amount, frequency));
Ok(())
}

}
}
82 changes: 40 additions & 42 deletions src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@

use crate::{self as pallet_subscription, Config};
use frame_system as system;
use frame_support::{assert_noop, assert_ok, construct_runtime, parameter_types,
traits::Everything, traits::Currency};
use frame_system::ensure_signed;
use frame_support::{
construct_runtime, parameter_types,
traits::Everything,
};
use sp_core::H256;
use sp_io::TestExternalities;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
};

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<TestRuntime>;
Expand All @@ -31,47 +30,46 @@ parameter_types! {
frame_system::limits::BlockWeights::simple_max(1024);
}
impl frame_system::Config for TestRuntime {
type BaseCallFilter = Everything;
type BlockWeights = ();
type BlockLength = ();
type Origin = Origin;
type Call = Call;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = Event;
type BlockHashCount = BlockHashCount;
type DbWeight = ();
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
type AccountData = ();
type AccountId = u64;
type BaseCallFilter = Everything;
type BlockHashCount = BlockHashCount;
type BlockLength = ();
type BlockNumber = u64;
type BlockWeights = ();
type Call = Call;
type DbWeight = ();
type Event = Event;
type Hash = H256;
type Hashing = BlakeTwo256;
type Header = Header;
type Index = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type MaxConsumers = frame_support::traits::ConstU32<16>;
type OnKilledAccount = ();
type OnNewAccount = ();
type OnSetCode = ();
type Origin = Origin;
type PalletInfo = PalletInfo;
type SS58Prefix = ();
type SystemWeightInfo = ();
type Version = ();
}

impl Config for TestRuntime {
type Event = Event;
type Currency = ();
type MaxMetadataLength = ();
type Currency = ();
type Event = Event;
type MaxMetadataLength = ();
}

pub struct ExternalityBuilder;

impl ExternalityBuilder {
pub fn build() -> TestExternalities {
let storage = frame_system::GenesisConfig::default()
.build_storage::<TestRuntime>()
.unwrap();
let mut ext = TestExternalities::from(storage);
ext.execute_with(|| System::set_block_number(1));
ext
}
pub fn build() -> TestExternalities {
let storage =
frame_system::GenesisConfig::default().build_storage::<TestRuntime>().unwrap();
let mut ext = TestExternalities::from(storage);
ext.execute_with(|| System::set_block_number(1));
ext
}
}
139 changes: 88 additions & 51 deletions src/subscribe_call.rs
Original file line number Diff line number Diff line change
@@ -1,68 +1,105 @@
use super::{mock::*};
use crate::{Error};
use super::mock::*;
use crate::Error;
use frame_support::{assert_noop, assert_ok};

#[test]
fn subscribe() {
ExternalityBuilder::build().execute_with(|| {
assert_ok!(PalletSubscription::subscribe(Origin::signed(1), 2, 4000, 5, Some(4)));

assert_noop!(PalletSubscription::subscribe(Origin::signed(1), 2, 0, 5, Some(4)), Error::<TestRuntime>::InvalidSubscription);

assert_noop!(PalletSubscription::subscribe(Origin::signed(1), 2, 4000, 0 ,Some(4)), Error::<TestRuntime>::InvalidSubscription);

assert_noop!(PalletSubscription::subscribe(Origin::signed(1), 2 ,0, 0, Some(4)), Error::<TestRuntime>::InvalidSubscription);

let expected_event = Event::PalletSubscription(crate::Event::SubscriptionCreated(2, 1,4000, 5));

assert_eq!(System::events()[0].event, expected_event);
})
ExternalityBuilder::build().execute_with(|| {
assert_ok!(PalletSubscription::subscribe(
Origin::signed(1),
2,
4000,
5,
Some(4)
));

assert_noop!(
PalletSubscription::subscribe(Origin::signed(1), 2, 0, 5, Some(4)),
Error::<TestRuntime>::InvalidSubscription
);

assert_noop!(
PalletSubscription::subscribe(Origin::signed(1), 2, 4000, 0, Some(4)),
Error::<TestRuntime>::InvalidSubscription
);

assert_noop!(
PalletSubscription::subscribe(Origin::signed(1), 2, 0, 0, Some(4)),
Error::<TestRuntime>::InvalidSubscription
);

let expected_event =
Event::PalletSubscription(crate::Event::SubscriptionCreated(2, 1, 4000, 5));

assert_eq!(System::events()[0].event, expected_event);
})
}

#[test]
fn subscribe_multiple_events(){
ExternalityBuilder::build().execute_with(|| {

assert_ok!(PalletSubscription::subscribe(Origin::signed(1),2,4000,5, Some(4)));

let expected_event = Event::PalletSubscription(crate::Event::SubscriptionCreated(2,1, 4000, 5));
assert_eq!(System::events()[0].event, expected_event);

assert_ok!(PalletSubscription::subscribe(Origin::signed(7),10,6000,7, Some(4)));
let expected_event = Event::PalletSubscription(crate::Event::SubscriptionCreated (10, 7,6000, 7));
assert_eq!(System::events()[1].event, expected_event);

assert_ok!(PalletSubscription::subscribe(Origin::signed(8),11,6001,8, Some(4)));
let expected_event = Event::PalletSubscription(crate::Event::SubscriptionCreated (11, 8,6001, 8));
assert_eq!(System::events()[2].event, expected_event);

})
fn subscribe_multiple_events() {
ExternalityBuilder::build().execute_with(|| {
assert_ok!(PalletSubscription::subscribe(
Origin::signed(1),
2,
4000,
5,
Some(4)
));

let expected_event =
Event::PalletSubscription(crate::Event::SubscriptionCreated(2, 1, 4000, 5));
assert_eq!(System::events()[0].event, expected_event);

assert_ok!(PalletSubscription::subscribe(
Origin::signed(7),
10,
6000,
7,
Some(4)
));
let expected_event =
Event::PalletSubscription(crate::Event::SubscriptionCreated(10, 7, 6000, 7));
assert_eq!(System::events()[1].event, expected_event);

assert_ok!(PalletSubscription::subscribe(
Origin::signed(8),
11,
6001,
8,
Some(4)
));
let expected_event =
Event::PalletSubscription(crate::Event::SubscriptionCreated(11, 8, 6001, 8));
assert_eq!(System::events()[2].event, expected_event);
})
}


#[test]
fn subscribe_frequency_zero(){
ExternalityBuilder::build().execute_with(|| {

assert_noop!(PalletSubscription::subscribe(Origin::signed(1),2,400,0,Some(4)), Error::<TestRuntime>::InvalidSubscription);

})
fn subscribe_frequency_zero() {
ExternalityBuilder::build().execute_with(|| {
assert_noop!(
PalletSubscription::subscribe(Origin::signed(1), 2, 400, 0, Some(4)),
Error::<TestRuntime>::InvalidSubscription
);
})
}

#[test]
fn subscribe_amount_zero(){
ExternalityBuilder::build().execute_with(|| {

assert_noop!(PalletSubscription::subscribe(Origin::signed(1),2,0,5,Some(4)), Error::<TestRuntime>::InvalidSubscription);

})
fn subscribe_amount_zero() {
ExternalityBuilder::build().execute_with(|| {
assert_noop!(
PalletSubscription::subscribe(Origin::signed(1), 2, 0, 5, Some(4)),
Error::<TestRuntime>::InvalidSubscription
);
})
}

#[test]
fn subscribe_amount_frequency_zero(){
ExternalityBuilder::build().execute_with(|| {

assert_noop!(PalletSubscription::subscribe(Origin::signed(1),2,0,0,Some(4)), Error::<TestRuntime>::InvalidSubscription);

})
fn subscribe_amount_frequency_zero() {
ExternalityBuilder::build().execute_with(|| {
assert_noop!(
PalletSubscription::subscribe(Origin::signed(1), 2, 0, 0, Some(4)),
Error::<TestRuntime>::InvalidSubscription
);
})
}
Loading

0 comments on commit 51dd2f3

Please sign in to comment.