Skip to content

Commit 643658e

Browse files
authored
Merge pull request #24 from wuminzhe/poc1-final
Add kton tests
2 parents 555b935 + 1e2f55a commit 643658e

5 files changed

Lines changed: 404 additions & 0 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

srml/token/kton/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ system = { package = "srml-system", git = 'https://github.com/paritytech/substra
1616
timestamp = { package = "srml-timestamp", git = 'https://github.com/paritytech/substrate.git', default-features = false }
1717
substrate-primitives = { git = 'https://github.com/paritytech/substrate.git', default-features = false }
1818
dsupport = { package = "evo-support", path = "../../support", default-features = false }
19+
balances = { package = "srml-balances", git = 'https://github.com/paritytech/substrate.git', default-features = false }
1920

2021
[dev-dependencies]
2122
runtime_io = { package = "sr-io", git = 'https://github.com/paritytech/substrate.git' }
@@ -35,4 +36,5 @@ std = [
3536
"timestamp/std",
3637
"substrate-primitives/std",
3738
"dsupport/std",
39+
"balances/std",
3840
]

srml/token/kton/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ use dsupport::traits::SystemCurrency;
2323
mod imbalance;
2424
use imbalance::{NegativeImbalance, PositiveImbalance};
2525

26+
mod mock;
27+
mod tests;
28+
2629
const DEPOSIT_ID: LockIdentifier = *b"lockkton";
30+
const DECIMALS: u64 = 1000000000;
2731

2832
/// Struct to encode the vesting schedule of an individual account.
2933
#[derive(Encode, Decode, Copy, Clone, PartialEq, Eq)]

srml/token/kton/src/mock.rs

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#![cfg(test)]
2+
extern crate runtime_io;
3+
use primitives::BuildStorage;
4+
use primitives::{traits::{IdentityLookup}, testing::{Header}};
5+
use substrate_primitives::{H256, Blake2Hasher};
6+
use srml_support::impl_outer_origin;
7+
use crate::{GenesisConfig, Module, Trait};
8+
use super::*;
9+
10+
impl_outer_origin!{
11+
pub enum Origin for Test {}
12+
}
13+
14+
#[derive(Clone, PartialEq, Eq, Debug)]
15+
// Runtime
16+
pub struct Test;
17+
18+
impl system::Trait for Test {
19+
type Origin = Origin;
20+
type Index = u64;
21+
type BlockNumber = u64;
22+
type Hash = H256;
23+
type Hashing = ::primitives::traits::BlakeTwo256;
24+
type AccountId = u64;
25+
type Lookup = IdentityLookup<Self::AccountId>;
26+
type Header = Header;
27+
type Event = ();
28+
}
29+
30+
impl timestamp::Trait for Test {
31+
type Moment = u64;
32+
type OnTimestampSet = ();
33+
}
34+
35+
36+
impl balances::Trait for Test {
37+
type Balance = u64;
38+
type OnFreeBalanceZero = ();
39+
type OnNewAccount = ();
40+
type Event = ();
41+
type TransactionPayment = ();
42+
type TransferPayment = ();
43+
type DustRemoval = ();
44+
}
45+
46+
impl Trait for Test {
47+
type Balance = u64;
48+
type Currency = balances::Module<Self>;
49+
type Event = ();
50+
type OnMinted = ();
51+
type OnRemoval = ();
52+
type SystemRefund = ();
53+
}
54+
55+
pub struct ExtBuilder {
56+
transaction_base_fee: u64,
57+
transaction_byte_fee: u64,
58+
existential_deposit: u64,
59+
transfer_fee: u64,
60+
creation_fee: u64,
61+
sys_acc: u64,
62+
}
63+
64+
impl Default for ExtBuilder {
65+
fn default() -> Self {
66+
Self {
67+
transaction_base_fee: 0,
68+
transaction_byte_fee: 0,
69+
existential_deposit: 0,
70+
transfer_fee: 0,
71+
creation_fee: 0,
72+
sys_acc: 0
73+
}
74+
}
75+
}
76+
77+
impl ExtBuilder {
78+
pub fn existential_deposit(mut self, existential_deposit: u64) -> Self {
79+
self.existential_deposit = existential_deposit;
80+
self
81+
}
82+
83+
#[allow(dead_code)]
84+
pub fn transfer_fee(mut self, transfer_fee: u64) -> Self {
85+
self.transfer_fee = transfer_fee;
86+
self
87+
}
88+
pub fn creation_fee(mut self, creation_fee: u64) -> Self {
89+
self.creation_fee = creation_fee;
90+
self
91+
}
92+
pub fn transaction_fees(mut self, base_fee: u64, byte_fee: u64) -> Self {
93+
self.transaction_base_fee = base_fee;
94+
self.transaction_byte_fee = byte_fee;
95+
self
96+
}
97+
98+
99+
pub fn build(self) -> runtime_io::TestExternalities<Blake2Hasher> {
100+
let (mut t, mut c) = system::GenesisConfig::<Test>::default().build_storage().unwrap();
101+
let balance_factor = if self.existential_deposit > 0 {
102+
1000 * DECIMALS
103+
} else {
104+
1 * DECIMALS
105+
};
106+
107+
let _ = timestamp::GenesisConfig::<Test> {
108+
minimum_period: 5,
109+
}.assimilate_storage(&mut t, &mut c);
110+
111+
let _ = balances::GenesisConfig::<Test> {
112+
balances: vec![
113+
(1, 10 * balance_factor),
114+
(2, 20 * balance_factor),
115+
(3, 300 * balance_factor),
116+
(4, 400 * balance_factor),
117+
(10, balance_factor),
118+
(11, balance_factor * 1000), // 1 m
119+
(20, balance_factor),
120+
(21, balance_factor * 2000), // 2 m
121+
(30, balance_factor),
122+
(31, balance_factor * 2000), // 2 m
123+
(40, balance_factor),
124+
(41, balance_factor * 2000), // 2 m
125+
(100, 200000 * balance_factor),
126+
(101, 200000 * balance_factor),
127+
],
128+
transaction_base_fee: self.transaction_base_fee,
129+
transaction_byte_fee: self.transaction_byte_fee,
130+
existential_deposit: self.existential_deposit,
131+
transfer_fee: self.transfer_fee,
132+
creation_fee: self.creation_fee,
133+
vesting: vec![],
134+
}.assimilate_storage(&mut t, &mut c);
135+
136+
let _ = GenesisConfig::<Test> {
137+
sys_acc: 42,
138+
balances: vec![
139+
(1, 10 * balance_factor),
140+
],
141+
vesting: vec![],
142+
}.assimilate_storage(&mut t, &mut c);
143+
144+
t.into()
145+
146+
}
147+
}
148+
149+
pub type System = system::Module<Test>;
150+
pub type Ring = balances::Module<Test>;
151+
pub type Timestamp = timestamp::Module<Test>;
152+
pub type Kton = Module<Test>;

0 commit comments

Comments
 (0)