Skip to content

Commit 18ffc4b

Browse files
authored
allow specify balances for each users and each tokens (#66)
1 parent 47d36eb commit 18ffc4b

File tree

3 files changed

+42
-39
lines changed

3 files changed

+42
-39
lines changed

currencies/src/mock.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,33 +97,33 @@ pub const BOB: AccountId = 2;
9797
pub const EVA: AccountId = 5;
9898

9999
pub struct ExtBuilder {
100-
currency_ids: Vec<CurrencyId>,
101-
endowed_accounts: Vec<AccountId>,
102-
initial_balance: Balance,
100+
endowed_accounts: Vec<(AccountId, CurrencyId, Balance)>,
103101
// whether the configs are for `pallet_balances` or not
104102
is_for_pallet_balances: bool,
105103
}
106104

107105
impl Default for ExtBuilder {
108106
fn default() -> Self {
109107
Self {
110-
currency_ids: vec![NATIVE_CURRENCY_ID, X_TOKEN_ID],
111-
endowed_accounts: vec![0],
112-
initial_balance: 0,
108+
endowed_accounts: vec![],
113109
is_for_pallet_balances: false,
114110
}
115111
}
116112
}
117113

118114
impl ExtBuilder {
119-
pub fn balances(mut self, account_ids: Vec<AccountId>, initial_balance: Balance) -> Self {
120-
self.endowed_accounts = account_ids;
121-
self.initial_balance = initial_balance;
115+
pub fn balances(mut self, endowed_accounts: Vec<(AccountId, CurrencyId, Balance)>) -> Self {
116+
self.endowed_accounts = endowed_accounts;
122117
self
123118
}
124119

125120
pub fn one_hundred_for_alice_n_bob(self) -> Self {
126-
self.balances(vec![ALICE, BOB], 100)
121+
self.balances(vec![
122+
(ALICE, NATIVE_CURRENCY_ID, 100),
123+
(BOB, NATIVE_CURRENCY_ID, 100),
124+
(ALICE, X_TOKEN_ID, 100),
125+
(BOB, X_TOKEN_ID, 100),
126+
])
127127
}
128128

129129
pub fn make_for_pallet_balances(mut self) -> Self {
@@ -140,17 +140,16 @@ impl ExtBuilder {
140140
pallet_balances::GenesisConfig::<Runtime> {
141141
balances: self
142142
.endowed_accounts
143-
.iter()
144-
.map(|acc| (acc.clone(), self.initial_balance))
145-
.collect(),
143+
.into_iter()
144+
.filter(|(_, currency_id, _)| *currency_id == X_TOKEN_ID)
145+
.map(|(account_id, _, initial_balance)| (account_id, initial_balance))
146+
.collect::<Vec<_>>(),
146147
vesting: vec![],
147148
}
148149
.assimilate_storage(&mut t)
149150
.unwrap();
150151
} else {
151152
tokens::GenesisConfig::<Runtime> {
152-
tokens: self.currency_ids,
153-
initial_balance: self.initial_balance,
154153
endowed_accounts: self.endowed_accounts,
155154
}
156155
.assimilate_storage(&mut t)

tokens/src/lib.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#![cfg_attr(not(feature = "std"), no_std)]
22

33
use frame_support::{decl_error, decl_event, decl_module, decl_storage, ensure, Parameter};
4-
use rstd::convert::{TryFrom, TryInto};
4+
use rstd::{
5+
collections::btree_map::BTreeMap,
6+
convert::{TryFrom, TryInto},
7+
};
58
use sp_runtime::{
69
traits::{CheckedAdd, CheckedSub, MaybeSerializeDeserialize, Member, SimpleArithmetic, StaticLookup},
710
DispatchResult,
@@ -31,30 +34,38 @@ pub trait Trait: frame_system::Trait {
3134
+ Default
3235
+ Copy
3336
+ MaybeSerializeDeserialize;
34-
type CurrencyId: Parameter + Member + Copy + MaybeSerializeDeserialize;
37+
type CurrencyId: Parameter + Member + Copy + MaybeSerializeDeserialize + Ord;
3538
}
3639

3740
decl_storage! {
3841
trait Store for Module<T: Trait> as Tokens {
3942
/// The total issuance of a token type.
4043
pub TotalIssuance get(fn total_issuance) build(|config: &GenesisConfig<T>| {
41-
let issuance = config.initial_balance * (config.endowed_accounts.len() as u32).into();
42-
config.tokens.iter().map(|id| (id.clone(), issuance)).collect::<Vec<_>>()
44+
config
45+
.endowed_accounts
46+
.iter()
47+
.map(|(_, currency_id, initial_balance)| (currency_id, initial_balance))
48+
.fold(BTreeMap::<T::CurrencyId, T::Balance>::new(), |mut acc, (currency_id, initial_balance)| {
49+
if let Some(issuance) = acc.get_mut(currency_id) {
50+
*issuance = issuance.checked_add(initial_balance).expect("total issuance cannot overflow when building genesis");
51+
} else {
52+
acc.insert(*currency_id, *initial_balance);
53+
}
54+
acc
55+
})
56+
.into_iter()
57+
.collect::<Vec<_>>()
4358
}): map T::CurrencyId => T::Balance;
4459

4560
/// The balance of a token type under an account.
4661
pub Balance get(fn balance): double_map T::CurrencyId, blake2_256(T::AccountId) => T::Balance;
4762
}
4863
add_extra_genesis {
49-
config(tokens): Vec<T::CurrencyId>;
50-
config(initial_balance): T::Balance;
51-
config(endowed_accounts): Vec<T::AccountId>;
64+
config(endowed_accounts): Vec<(T::AccountId, T::CurrencyId, T::Balance)>;
5265

5366
build(|config: &GenesisConfig<T>| {
54-
config.tokens.iter().for_each(|currency_id| {
55-
config.endowed_accounts.iter().for_each(|account_id| {
56-
<Balance<T>>::insert(currency_id, account_id, &config.initial_balance);
57-
})
67+
config.endowed_accounts.iter().for_each(|(account_id, currency_id, initial_balance)| {
68+
<Balance<T>>::insert(currency_id, account_id, initial_balance);
5869
})
5970
})
6071
}

tokens/src/mock.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ parameter_types! {
3636
type AccountId = u64;
3737
impl frame_system::Trait for Runtime {
3838
type Origin = Origin;
39+
type Call = ();
3940
type Index = u64;
4041
type BlockNumber = u64;
41-
type Call = ();
4242
type Hash = H256;
4343
type Hashing = ::sp_runtime::traits::BlakeTwo256;
4444
type AccountId = AccountId;
@@ -70,30 +70,25 @@ pub const ALICE: AccountId = 1;
7070
pub const BOB: AccountId = 2;
7171

7272
pub struct ExtBuilder {
73-
currency_id: CurrencyId,
74-
endowed_accounts: Vec<AccountId>,
75-
initial_balance: Balance,
73+
endowed_accounts: Vec<(AccountId, CurrencyId, Balance)>,
7674
}
7775

7876
impl Default for ExtBuilder {
7977
fn default() -> Self {
8078
Self {
81-
currency_id: TEST_TOKEN_ID,
82-
endowed_accounts: vec![0],
83-
initial_balance: 0,
79+
endowed_accounts: vec![],
8480
}
8581
}
8682
}
8783

8884
impl ExtBuilder {
89-
pub fn balances(mut self, account_ids: Vec<AccountId>, initial_balance: Balance) -> Self {
90-
self.endowed_accounts = account_ids;
91-
self.initial_balance = initial_balance;
85+
pub fn balances(mut self, endowed_accounts: Vec<(AccountId, CurrencyId, Balance)>) -> Self {
86+
self.endowed_accounts = endowed_accounts;
9287
self
9388
}
9489

9590
pub fn one_hundred_for_alice_n_bob(self) -> Self {
96-
self.balances(vec![ALICE, BOB], 100)
91+
self.balances(vec![(ALICE, TEST_TOKEN_ID, 100), (BOB, TEST_TOKEN_ID, 100)])
9792
}
9893

9994
pub fn build(self) -> runtime_io::TestExternalities {
@@ -102,8 +97,6 @@ impl ExtBuilder {
10297
.unwrap();
10398

10499
GenesisConfig::<Runtime> {
105-
tokens: vec![self.currency_id],
106-
initial_balance: self.initial_balance,
107100
endowed_accounts: self.endowed_accounts,
108101
}
109102
.assimilate_storage(&mut t)

0 commit comments

Comments
 (0)