Skip to content

Commit 4a6e01e

Browse files
author
Aton
authored
issue init account (paritytech#236)
* issue init account * remove some comments
1 parent 3096192 commit 4a6e01e

File tree

6 files changed

+85
-20
lines changed

6 files changed

+85
-20
lines changed
Binary file not shown.

xrml/xassets/assets/src/lib.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,17 @@ decl_storage! {
295295
Module::<T>::register_asset(pcx, false, Zero::zero()).unwrap();
296296
// init for asset_list
297297
for (asset, is_psedu_intention, init_list) in config.asset_list.iter() {
298-
let t = asset.token();
298+
let token = asset.token();
299299
Module::<T>::register_asset(asset.clone(), *is_psedu_intention, Zero::zero()).unwrap();
300300

301301
for (accountid, value) in init_list {
302-
Module::<T>::issue(&t, &accountid, As::sa(*value)).unwrap();
302+
let value = As::sa(*value);
303+
Module::<T>::init_asset_for(&accountid, &token);
304+
let total_free_token = Module::<T>::total_asset_balance(&token, AssetType::Free);
305+
let free_token = Module::<T>::free_balance(&accountid, &token);
306+
Module::<T>::set_total_asset_balance(&token, AssetType::Free, total_free_token + value);
307+
// not create account
308+
Module::<T>::set_asset_balance(&accountid, &token, AssetType::Free, free_token + value);
303309
}
304310
}
305311

@@ -345,19 +351,20 @@ impl<T: Trait> Module<T> {
345351
Self::asset_balance(who, token, AssetType::Free)
346352
}
347353

348-
fn set_free_balance(who: &T::AccountId, token: &Token, value: T::Balance) {
349-
Self::set_asset_balance(who, token, AssetType::Free, value)
350-
}
351-
352354
fn set_free_balance_creating(who: &T::AccountId, token: &Token, value: T::Balance) {
355+
let is_existing = balances::FreeBalance::<T>::exists(who);
353356
if token.as_slice() == <Self as ChainT>::TOKEN {
354-
balances::Module::<T>::set_free_balance_creating(who, value);
357+
if is_existing {
358+
balances::Module::<T>::set_free_balance(who, value);
359+
} else {
360+
// set_free_balance_creating would access `existential_deposit` storage
361+
balances::Module::<T>::set_free_balance_creating(who, value);
362+
}
355363
} else {
356-
let need_create = balances::FreeBalance::<T>::exists(who);
357-
if !need_create {
364+
if is_existing == false {
358365
balances::Module::<T>::set_free_balance_creating(who, Zero::zero());
359366
}
360-
Self::set_free_balance(who, token, value)
367+
Self::set_asset_balance(who, token, AssetType::Free, value)
361368
}
362369
}
363370

@@ -594,7 +601,8 @@ impl<T: Trait> Module<T> {
594601
Self::init_asset_for(who, token);
595602

596603
Self::set_total_asset_balance(token, AssetType::Free, new_total_free_token);
597-
Self::set_asset_balance(who, token, AssetType::Free, new_free_token);
604+
// Self::set_asset_balance(who, token, AssetType::Free, new_free_token);
605+
Self::set_free_balance_creating(who, token, new_free_token);
598606

599607
AssetTriggerEventAfter::<T>::on_issue(token, who, value)?;
600608
Ok(())
@@ -771,10 +779,6 @@ impl<T: Trait> Module<T> {
771779
Self::all_type_balance_of(who, &<Self as ChainT>::TOKEN.to_vec())
772780
}
773781

774-
// fn pcx_set_free_balance(who: &T::AccountId, value: T::Balance) {
775-
// Self::set_free_balance(who, &<Self as ChainT>::TOKEN.to_vec(), value);
776-
// }
777-
778782
fn pcx_set_free_balance_creating(who: &T::AccountId, value: T::Balance) {
779783
Self::set_free_balance_creating(who, &<Self as ChainT>::TOKEN.to_vec(), value);
780784
}

xrml/xassets/assets/src/mock.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
7979
.unwrap()
8080
.0,
8181
);
82+
// indices
83+
r.extend(
84+
indices::GenesisConfig::<Test> { ids: vec![1, 2, 3] }
85+
.build_storage()
86+
.unwrap()
87+
.0,
88+
);
8289

8390
let btc_asset = Asset::new(
8491
b"BTC".to_vec(), // token

xrml/xassets/assets/src/tests.rs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ use runtime_io::with_externalities;
1010
fn test_genesis() {
1111
with_externalities(&mut new_test_ext(), || {
1212
// Check that GenesisBuilder works properly.
13+
assert_eq!(Indices::lookup_index(0), Some(1));
14+
assert_eq!(Indices::lookup_index(1), Some(2));
15+
assert_eq!(Indices::lookup_index(2), Some(3));
16+
17+
assert_eq!(XAssets::pcx_free_balance(&1), 1000);
18+
assert_eq!(XAssets::pcx_free_balance(&2), 510);
19+
assert_eq!(XAssets::pcx_free_balance(&3), 1000);
20+
1321
// check token_list
1422
let btc_token = b"BTC".to_vec();
1523

@@ -30,10 +38,21 @@ fn test_genesis_token_issue() {
3038
with_externalities(&mut new_test_ext(), || {
3139
let btc_token = b"BTC".to_vec();
3240
let chainx_token = XAssets::TOKEN.to_vec();
41+
assert_eq!(
42+
XAssets::asset_balance(&1, &chainx_token, AssetType::Free),
43+
1000
44+
);
45+
assert_eq!(Indices::lookup_index(0), Some(1));
46+
assert_eq!(
47+
XAssets::asset_balance(&2, &chainx_token, AssetType::Free),
48+
510
49+
);
50+
assert_eq!(Indices::lookup_index(1), Some(2));
3351
assert_eq!(
3452
XAssets::asset_balance(&3, &chainx_token, AssetType::Free),
3553
1000
3654
);
55+
assert_eq!(Indices::lookup_index(2), Some(3));
3756
assert_eq!(XAssets::asset_balance(&3, &btc_token, AssetType::Free), 100);
3857

3958
assert_eq!(XAssets::assets_of(&3), [chainx_token, btc_token]);
@@ -462,6 +481,41 @@ fn test_error_issue_and_destroy3() {
462481
})
463482
}
464483

484+
#[test]
485+
fn test_account_init() {
486+
with_externalities(&mut new_test_ext(), || {
487+
let a: u64 = 1; // accountid
488+
let id1: u64 = 1000;
489+
let id2: u64 = 1001;
490+
let id3: u64 = 1002;
491+
let btc_token = b"BTC".to_vec();
492+
let chainx_token = XAssets::TOKEN.to_vec();
493+
XAssets::issue(&btc_token, &a, 100).unwrap();
494+
495+
// issue init
496+
XAssets::issue(&btc_token, &id1, 100).unwrap();
497+
assert_eq!(Indices::lookup_index(3), Some(id1));
498+
// transfer pcx init
499+
assert_ok!(XAssets::transfer(
500+
Some(a).into(),
501+
id2.into(),
502+
chainx_token.clone(),
503+
25,
504+
b"".to_vec()
505+
));
506+
assert_eq!(Indices::lookup_index(4), Some(id2));
507+
// transfer token init
508+
assert_ok!(XAssets::transfer(
509+
Some(a).into(),
510+
id3.into(),
511+
btc_token.clone(),
512+
25,
513+
b"".to_vec()
514+
));
515+
assert_eq!(Indices::lookup_index(5), Some(id3));
516+
})
517+
}
518+
465519
#[test]
466520
fn test_transfer_not_init() {
467521
with_externalities(&mut new_test_ext(), || {
@@ -477,22 +531,23 @@ fn test_transfer_not_init() {
477531
25,
478532
b"".to_vec()
479533
));
480-
//assert_eq!(Indices::lookup_index(1), Some(new_id));
534+
assert_eq!(Indices::lookup_index(3), Some(new_id));
481535
assert_ok!(XAssets::transfer(
482536
Some(a).into(),
483537
new_id.into(),
484538
btc_token.clone(),
485539
25,
486540
b"".to_vec()
487541
));
542+
assert_eq!(Indices::lookup_index(4), None);
488543
assert_ok!(XAssets::transfer(
489544
Some(a).into(),
490545
new_id.into(),
491546
chainx_token.clone(),
492547
25,
493548
b"".to_vec()
494549
));
495-
550+
assert_eq!(Indices::lookup_index(4), None);
496551
assert_eq!(XAssets::free_balance(&a, &chainx_token), 1000 - 25);
497552
assert_eq!(XAssets::free_balance(&new_id, &chainx_token), 25);
498553
})

xrml/xbridge/bitcoin/src/b58.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ pub fn from(data: Vec<u8>) -> Result<Vec<u8>, &'static str> {
6161
Ok(ret)
6262
}
6363

64-
6564
pub fn to_base58(data: Vec<u8>) -> Vec<u8> {
6665
let zcount = data.iter().take_while(|x| **x == 0).count();
6766
let size: usize = (data.len() - zcount) * 138 / 100 + 1;
@@ -81,7 +80,7 @@ pub fn to_base58(data: Vec<u8>) -> Vec<u8> {
8180
buffer[j] = (carry % 58) as u8;
8281
carry /= 58;
8382

84-
if j > 0 {
83+
if j > 0 {
8584
j -= 1;
8685
}
8786
}

xrml/xbridge/bitcoin/src/tx/handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use self::extracter::Extracter;
22
use super::*;
3-
use keys::DisplayLayout;
43
use b58;
4+
use keys::DisplayLayout;
55

66
pub struct TxHandler<'a>(&'a H256);
77

0 commit comments

Comments
 (0)