Skip to content

Commit c250244

Browse files
committed
feat: add solution for ex09
1 parent 40be4be commit c250244

File tree

2 files changed

+107
-4
lines changed

2 files changed

+107
-4
lines changed

exercises/ex09-mock/pallet-to-mock/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ use frame_support::{
77
traits::Currency,
88
};
99

10-
// TODO: Uncomment the following lines and fill the tests/mock.rs file
11-
// in order for the tests to compile and run successfully
12-
// #[cfg(test)]
13-
// mod tests;
10+
#[cfg(test)]
11+
mod tests;
1412

1513
pub type BalanceOf<T> =
1614
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,106 @@
1+
use crate::{self as pallet_to_mock, PriceOracle};
2+
use frame_support::{parameter_types, traits::ConstU64};
3+
use sp_core::H256;
4+
use sp_runtime::{
5+
testing::Header,
6+
traits::{BlakeTwo256, IdentityLookup},
7+
};
18

9+
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<TestRuntime>;
10+
type Block = frame_system::mocking::MockBlock<TestRuntime>;
11+
12+
// Configure a mock runtime to test the pallet.
13+
frame_support::construct_runtime!(
14+
pub enum TestRuntime where
15+
Block = Block,
16+
NodeBlock = Block,
17+
UncheckedExtrinsic = UncheckedExtrinsic,
18+
{
19+
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
20+
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
21+
PalletToMock: pallet_to_mock::{Pallet, Call, Storage, Event<T>}
22+
}
23+
);
24+
25+
parameter_types! {
26+
pub const BlockHashCount: u64 = 250;
27+
pub const SS58Prefix: u8 = 42;
28+
}
29+
30+
impl frame_system::Config for TestRuntime {
31+
type AccountData = pallet_balances::AccountData<u64>;
32+
type AccountId = u64;
33+
type BaseCallFilter = frame_support::traits::Everything;
34+
type BlockHashCount = BlockHashCount;
35+
type BlockLength = ();
36+
type BlockNumber = u64;
37+
type BlockWeights = ();
38+
type Call = Call;
39+
type DbWeight = ();
40+
type Event = Event;
41+
type Hash = H256;
42+
type Hashing = BlakeTwo256;
43+
type Header = Header;
44+
type Index = u64;
45+
type Lookup = IdentityLookup<Self::AccountId>;
46+
type MaxConsumers = frame_support::traits::ConstU32<16>;
47+
type OnKilledAccount = ();
48+
type OnNewAccount = ();
49+
type OnSetCode = ();
50+
type Origin = Origin;
51+
type PalletInfo = PalletInfo;
52+
type SS58Prefix = SS58Prefix;
53+
type SystemWeightInfo = ();
54+
type Version = ();
55+
}
56+
57+
impl pallet_balances::Config for TestRuntime {
58+
type AccountStore = System;
59+
type Balance = u64;
60+
type DustRemoval = ();
61+
type Event = Event;
62+
type ExistentialDeposit = ConstU64<1>;
63+
type MaxLocks = ();
64+
type MaxReserves = ();
65+
type ReserveIdentifier = [u8; 8];
66+
type WeightInfo = ();
67+
}
68+
69+
parameter_types! {
70+
pub const ValueToMint: u64 = 200;
71+
}
72+
73+
pub struct MyPriceOracle;
74+
75+
impl PriceOracle for MyPriceOracle {
76+
type Error = ();
77+
78+
fn get_price() -> Result<u64, Self::Error> {
79+
Ok(100)
80+
}
81+
}
82+
83+
impl pallet_to_mock::Config for TestRuntime {
84+
type Currency = Balances;
85+
type Event = Event;
86+
type SomePriceOracle = MyPriceOracle;
87+
type ValueToMint = ValueToMint;
88+
}
89+
90+
// Build genesis storage according to the mock runtime.
91+
pub fn new_test_ext() -> sp_io::TestExternalities {
92+
let mut t = frame_system::GenesisConfig::default().build_storage::<TestRuntime>().unwrap();
93+
pallet_balances::GenesisConfig::<TestRuntime> {
94+
balances: vec![(1, 100)],
95+
}
96+
.assimilate_storage(&mut t)
97+
.unwrap();
98+
99+
let mut ext = sp_io::TestExternalities::new(t);
100+
// In order to emit events the block number must be more than 0
101+
ext.execute_with(|| System::set_block_number(1));
102+
ext
103+
}
104+
105+
// Mock users AccountId
106+
pub const ALICE: u64 = 1;

0 commit comments

Comments
 (0)