Skip to content

Commit ae5e00d

Browse files
author
Aton
authored
Feature/transfer (paritytech#111)
* merge transfer from balances to tokenbalances for chainx * add test * remove public call for balances module
1 parent 19b1ec4 commit ae5e00d

File tree

12 files changed

+365
-116
lines changed

12 files changed

+365
-116
lines changed

cxrml/associations/src/lib.rs

+22-8
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,25 @@ impl<T: Trait> Module<T> {
9797
fn deposit_event(event: Event<T>) {
9898
<system::Module<T>>::deposit_event(<T as Trait>::Event::from(event).into());
9999
}
100+
101+
fn check_no_init(who: &T::AccountId) -> Result {
102+
if Self::relationship(who).is_some() {
103+
return Err("has register this account");
104+
} else {
105+
if balances::FreeBalance::<T>::exists(who) {
106+
return Err("this account is exist");
107+
}
108+
}
109+
Ok(())
110+
}
111+
112+
pub fn is_init(who: &T::AccountId) -> bool {
113+
if let Err(_) = Self::check_no_init(who) {
114+
true
115+
} else {
116+
false
117+
}
118+
}
100119
}
101120

102121
impl<T: Trait> Module<T> {
@@ -105,13 +124,8 @@ impl<T: Trait> Module<T> {
105124
// deduct fee first
106125
T::OnCalcFee::on_calc_fee(&from, Self::init_fee())?;
107126

108-
if Self::relationship(&who).is_some() {
109-
return Err("has register this account");
110-
} else {
111-
if balances::FreeBalance::<T>::exists(&who) {
112-
return Err("this account is exist");
113-
}
114-
}
127+
Self::check_no_init(&who)?;
128+
115129
Relationship::<T>::insert(&who, from.clone());
116130

117131
let from_balance = balances::Module::<T>::free_balance(&from);
@@ -127,7 +141,7 @@ impl<T: Trait> Module<T> {
127141
};
128142

129143
balances::Module::<T>::set_free_balance(&from, new_from_balance);
130-
balances::Module::<T>::set_free_balance(&who, new_to_balance);
144+
balances::Module::<T>::set_free_balance_creating(&who, new_to_balance);
131145

132146
Self::deposit_event(RawEvent::InitAccount(from, who, value));
133147
Ok(())

cxrml/bridge/btc/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ use blockchain::Chain;
8080
use keys::DisplayLayout;
8181
pub use keys::{Address, Error as AddressError};
8282
pub use tx::RelayTx;
83-
use tx::{handle_input, handle_output, handle_proposal, handle_cert, validate_transaction, UTXO};
83+
use tx::{handle_cert, handle_input, handle_output, handle_proposal, validate_transaction, UTXO};
8484

8585
pub trait Trait:
8686
system::Trait + balances::Trait + timestamp::Trait + financial_records::Trait
@@ -355,7 +355,6 @@ impl<T: Trait> Module<T> {
355355
}
356356
TxType::SendCert => {
357357
handle_cert::<T>(&tx.raw, &tx.block_hash, &who, &cert_address);
358-
359358
}
360359
_ => {
361360
let _utxos = handle_output::<T>(

cxrml/bridge/btc/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ extern crate srml_consensus as consensus;
33
use substrate_primitives::{Blake2Hasher, H256 as S_H256};
44

55
use self::base58::FromBase58;
6+
use self::keys::DisplayLayout;
67
use super::*;
78
use runtime_io;
89
use runtime_io::with_externalities;
9-
use self::keys::DisplayLayout;
1010
use runtime_primitives::testing::{Digest, DigestItem, Header};
1111
use runtime_primitives::traits::BlakeTwo256;
1212
use runtime_primitives::BuildStorage;

cxrml/bridge/btc/src/tx/mod.rs

+25-7
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use runtime_support::{StorageMap, StorageValue};
1010

1111
pub use self::proposal::{handle_proposal, Proposal};
1212
use super::{
13-
AccountMap, AddressMap, BlockHeaderFor, BlockTxids, CertCache, CandidateTx, DepositCache,
14-
NetworkId, NumberForHash, RegInfoMaxIndex, RegInfoSet, ReceiveAddress, RedeemScript, Trait,
13+
AccountMap, AddressMap, BlockHeaderFor, BlockTxids, CandidateTx, CertCache, DepositCache,
14+
NetworkId, NumberForHash, ReceiveAddress, RedeemScript, RegInfoMaxIndex, RegInfoSet, Trait,
1515
TxProposal, TxSet, TxType, UTXOMaxIndex, UTXOSet,
1616
};
1717
use b58::from;
@@ -149,7 +149,10 @@ impl<T: Trait> TxStorage<T> {
149149
// todo 检查block是否存在
150150
<BlockTxids<T>>::mutate(block_hash.clone(), |v| v.push(hash.clone()));
151151

152-
<TxSet<T>>::insert(hash, (who.clone(), address, tx_type, balance, block_hash, tx));
152+
<TxSet<T>>::insert(
153+
hash,
154+
(who.clone(), address, tx_type, balance, block_hash, tx),
155+
);
153156
}
154157

155158
fn find_tx(txid: &H256) -> Option<Transaction> {
@@ -220,7 +223,16 @@ impl<T: Trait> RollBack<T> for TxStorage<T> {
220223
struct RegInfoStorage<T: Trait>(PhantomData<T>);
221224

222225
impl<T: Trait> RegInfoStorage<T> {
223-
fn add(accounts: (H256, keys::Address, T::AccountId, T::BlockNumber, Vec<u8>, TxType)) {
226+
fn add(
227+
accounts: (
228+
H256,
229+
keys::Address,
230+
T::AccountId,
231+
T::BlockNumber,
232+
Vec<u8>,
233+
TxType,
234+
),
235+
) {
224236
let mut index = <RegInfoMaxIndex<T>>::get();
225237
<RegInfoSet<T>>::insert(index, accounts.clone());
226238
index += 1;
@@ -499,8 +511,14 @@ pub fn handle_output<T: Trait>(
499511
runtime_io::print("----new account-------");
500512
let time = <system::Module<T>>::block_number();
501513
let chainxaddr = <AddressMap<T>>::get(send_address.clone()).unwrap();
502-
let account = (tx.hash(), send_address.clone(), chainxaddr, time,
503-
channel[2..].to_vec(),tx_type);
514+
let account = (
515+
tx.hash(),
516+
send_address.clone(),
517+
chainxaddr,
518+
time,
519+
channel[2..].to_vec(),
520+
tx_type,
521+
);
504522
<RegInfoStorage<T>>::add(account);
505523
runtime_io::print("------insert new account in AccountsMap-----");
506524
}
@@ -529,7 +547,7 @@ pub fn handle_cert<T: Trait>(
529547
runtime_io::print(&account[..]);
530548
let id: T::AccountId = Decode::decode(&mut account.as_slice()).unwrap();
531549

532-
<CertCache<T>>::put((name[2..].to_vec(),id));
550+
<CertCache<T>>::put((name[2..].to_vec(), id));
533551
}
534552
}
535553
}

cxrml/exchange/matchorder/src/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,6 @@ impl<T: Trait> Module<T> {
371371
maker_fee,
372372
taker_fee,
373373
) {
374-
375374
Self::deposit_event(RawEvent::MatchFail(
376375
match_bid.id,
377376
in_bid_detail.pair.clone(),
@@ -494,7 +493,7 @@ impl<T: Trait> Module<T> {
494493

495494
if insert_head == true {
496495
let new_nodeid = Self::new_nodeid();
497-
let mut list_vec:Vec<BidId>=Vec::new();
496+
let mut list_vec: Vec<BidId> = Vec::new();
498497
list_vec.push(in_bid_detail.id);
499498

500499
let new_bid = Bid {
@@ -526,7 +525,7 @@ impl<T: Trait> Module<T> {
526525
if finish == false {
527526
//追加在最后
528527
let new_nodeid = Self::new_nodeid();
529-
let mut list_vec:Vec<BidId>=Vec::new();
528+
let mut list_vec: Vec<BidId> = Vec::new();
530529
list_vec.push(in_bid_detail.id);
531530

532531
let new_bid = Bid {
@@ -572,7 +571,7 @@ impl<T: Trait> Module<T> {
572571
}
573572
for mm in 0..node.data.list.len() {
574573
if in_bid_detail.id == node.data.list[mm] {
575-
let mut list_vec:Vec<BidId>=Vec::new();
574+
let mut list_vec: Vec<BidId> = Vec::new();
576575
list_vec.push(in_bid_detail.id);
577576

578577
Self::remove_from_bid_list(&mut node, &list_vec);

cxrml/exchange/matchorder/src/tests.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
8383
transfer_fee: 0,
8484
creation_fee: 0,
8585
reclaim_rebate: 0,
86-
}.build_storage()
86+
}
87+
.build_storage()
8788
.unwrap(),
8889
);
8990

@@ -92,7 +93,8 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
9293
token_list: vec![],
9394
transfer_token_fee: 10,
9495
chainx_precision: 8,
95-
}.build_storage()
96+
}
97+
.build_storage()
9698
.unwrap(),
9799
);
98100

@@ -102,7 +104,8 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
102104
pair_list: vec![],
103105
max_command_id: 0,
104106
average_price_len: 10000,
105-
}.build_storage()
107+
}
108+
.build_storage()
106109
.unwrap(),
107110
);
108111

@@ -112,7 +115,8 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
112115
fee_precision: 100000,
113116
maker_match_fee: 50,
114117
taker_match_fee: 100,
115-
}.build_storage()
118+
}
119+
.build_storage()
116120
.unwrap(),
117121
);
118122
r.into()
@@ -462,4 +466,4 @@ fn print_bid(pair: OrderPair, order_type: OrderType) {
462466
} else {
463467
println!("-------------------end -----------------");
464468
}
465-
}
469+
}

cxrml/exchange/pendingorders/src/lib.rs

-18
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,13 @@ impl<T: Trait> Module<T> {
244244
) -> Result {
245245
//判断交易对是否存在
246246
if let Err(_) = Self::is_valid_pair(&pair) {
247-
248247
return Err("not a existed pair in orderpair list");
249248
}
250249
//判定 数量和价格
251250
if amount == Zero::zero() {
252-
253251
return Err("amount cann't be zero");
254252
}
255253
if price == Zero::zero() {
256-
257254
return Err("price cann't be zero");
258255
}
259256
//手续费
@@ -269,7 +266,6 @@ impl<T: Trait> Module<T> {
269266
pair.second.clone(),
270267
)) < sum
271268
{
272-
273269
return Err("transactor's free token balance too low, can't put buy order");
274270
}
275271
// 锁定用户资产
@@ -279,15 +275,13 @@ impl<T: Trait> Module<T> {
279275
sum,
280276
ReservedType::Exchange,
281277
) {
282-
283278
return Err(msg);
284279
}
285280
}
286281
OrderType::Sell => {
287282
if <tokenbalances::Module<T>>::free_token(&(sender.clone(), pair.first.clone()))
288283
< As::sa(amount.as_())
289284
{
290-
291285
return Err("transactor's free token balance too low, can't put sell order");
292286
}
293287
// 锁定用户资产
@@ -297,7 +291,6 @@ impl<T: Trait> Module<T> {
297291
As::sa(amount.as_()),
298292
ReservedType::Exchange,
299293
) {
300-
301294
return Err(msg);
302295
}
303296
}
@@ -420,7 +413,6 @@ impl<T: Trait> Module<T> {
420413
back_amount,
421414
ReservedType::Exchange,
422415
) {
423-
424416
return Err(msg);
425417
}
426418

@@ -448,15 +440,13 @@ impl<T: Trait> Module<T> {
448440
));
449441
}
450442
_ => {
451-
452443
return Err(
453444
"order status error( FiillAll|FillPartAndCancel|Cancel) cann't be cancel",
454445
);
455446
}
456447
}
457448
Ok(())
458449
} else {
459-
460450
Err("cann't find this index of order")
461451
}
462452
}
@@ -486,14 +476,12 @@ impl<T: Trait> Module<T> {
486476
} else if maker_order.hasfill_amount < maker_order.amount {
487477
maker_order.status = OrderStatus::FillPart;
488478
} else {
489-
490479
return Err(" maker order has not enough amount");
491480
}
492481

493482
maker_order.lastupdate_time = <system::Module<T>>::block_number();
494483
maker_order
495484
} else {
496-
497485
return Err("cann't find this maker order");
498486
};
499487

@@ -508,14 +496,12 @@ impl<T: Trait> Module<T> {
508496
} else if taker_order.hasfill_amount < taker_order.amount {
509497
taker_order.status = OrderStatus::FillPart;
510498
} else {
511-
512499
return Err(" taker order has not enough amount");
513500
}
514501

515502
taker_order.lastupdate_time = <system::Module<T>>::block_number();
516503
taker_order
517504
} else {
518-
519505
return Err("cann't find this taker order");
520506
};
521507

@@ -554,7 +540,6 @@ impl<T: Trait> Module<T> {
554540
&taker_user.clone(),
555541
&maker_order.channel().clone(),
556542
) {
557-
558543
return Err(msg);
559544
}
560545
//计算买家的数量,解锁second,并move 给卖家,和手续费账户
@@ -569,7 +554,6 @@ impl<T: Trait> Module<T> {
569554
&maker_user.clone(),
570555
&taker_order.channel().clone(),
571556
) {
572-
573557
return Err(msg);
574558
}
575559
}
@@ -586,7 +570,6 @@ impl<T: Trait> Module<T> {
586570
&taker_user.clone(),
587571
&maker_order.channel().clone(),
588572
) {
589-
590573
return Err(msg);
591574
}
592575
//计算卖家的数量,解锁second,并move 给买家,和手续费账户
@@ -601,7 +584,6 @@ impl<T: Trait> Module<T> {
601584
&maker_user.clone(),
602585
&taker_order.channel().clone(),
603586
) {
604-
605587
return Err(msg);
606588
}
607589
}

cxrml/exchange/pendingorders/src/tests.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
7474
transfer_fee: 0,
7575
creation_fee: 0,
7676
reclaim_rebate: 0,
77-
}.build_storage()
77+
}
78+
.build_storage()
7879
.unwrap(),
7980
);
8081

@@ -84,7 +85,8 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
8485
pair_list: vec![],
8586
max_command_id: 0,
8687
average_price_len: 10000,
87-
}.build_storage()
88+
}
89+
.build_storage()
8890
.unwrap(),
8991
);
9092
r.into()
@@ -593,4 +595,4 @@ fn print_fill(
593595
println!("maker_fee={:?}", fill.maker_fee());
594596
println!("taker_fee={:?}", fill.taker_fee());
595597
println!("time={:?}", fill.time());
596-
}
598+
}

0 commit comments

Comments
 (0)