Skip to content

Commit

Permalink
Feature/transfer (paritytech#111)
Browse files Browse the repository at this point in the history
* merge transfer from balances to tokenbalances for chainx 

* add test

* remove public call for balances module
  • Loading branch information
Aton authored Nov 19, 2018
1 parent 19b1ec4 commit ae5e00d
Show file tree
Hide file tree
Showing 12 changed files with 365 additions and 116 deletions.
30 changes: 22 additions & 8 deletions cxrml/associations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,25 @@ impl<T: Trait> Module<T> {
fn deposit_event(event: Event<T>) {
<system::Module<T>>::deposit_event(<T as Trait>::Event::from(event).into());
}

fn check_no_init(who: &T::AccountId) -> Result {
if Self::relationship(who).is_some() {
return Err("has register this account");
} else {
if balances::FreeBalance::<T>::exists(who) {
return Err("this account is exist");
}
}
Ok(())
}

pub fn is_init(who: &T::AccountId) -> bool {
if let Err(_) = Self::check_no_init(who) {
true
} else {
false
}
}
}

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

if Self::relationship(&who).is_some() {
return Err("has register this account");
} else {
if balances::FreeBalance::<T>::exists(&who) {
return Err("this account is exist");
}
}
Self::check_no_init(&who)?;

Relationship::<T>::insert(&who, from.clone());

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

balances::Module::<T>::set_free_balance(&from, new_from_balance);
balances::Module::<T>::set_free_balance(&who, new_to_balance);
balances::Module::<T>::set_free_balance_creating(&who, new_to_balance);

Self::deposit_event(RawEvent::InitAccount(from, who, value));
Ok(())
Expand Down
3 changes: 1 addition & 2 deletions cxrml/bridge/btc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ use blockchain::Chain;
use keys::DisplayLayout;
pub use keys::{Address, Error as AddressError};
pub use tx::RelayTx;
use tx::{handle_input, handle_output, handle_proposal, handle_cert, validate_transaction, UTXO};
use tx::{handle_cert, handle_input, handle_output, handle_proposal, validate_transaction, UTXO};

pub trait Trait:
system::Trait + balances::Trait + timestamp::Trait + financial_records::Trait
Expand Down Expand Up @@ -355,7 +355,6 @@ impl<T: Trait> Module<T> {
}
TxType::SendCert => {
handle_cert::<T>(&tx.raw, &tx.block_hash, &who, &cert_address);

}
_ => {
let _utxos = handle_output::<T>(
Expand Down
2 changes: 1 addition & 1 deletion cxrml/bridge/btc/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ extern crate srml_consensus as consensus;
use substrate_primitives::{Blake2Hasher, H256 as S_H256};

use self::base58::FromBase58;
use self::keys::DisplayLayout;
use super::*;
use runtime_io;
use runtime_io::with_externalities;
use self::keys::DisplayLayout;
use runtime_primitives::testing::{Digest, DigestItem, Header};
use runtime_primitives::traits::BlakeTwo256;
use runtime_primitives::BuildStorage;
Expand Down
32 changes: 25 additions & 7 deletions cxrml/bridge/btc/src/tx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use runtime_support::{StorageMap, StorageValue};

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

<TxSet<T>>::insert(hash, (who.clone(), address, tx_type, balance, block_hash, tx));
<TxSet<T>>::insert(
hash,
(who.clone(), address, tx_type, balance, block_hash, tx),
);
}

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

impl<T: Trait> RegInfoStorage<T> {
fn add(accounts: (H256, keys::Address, T::AccountId, T::BlockNumber, Vec<u8>, TxType)) {
fn add(
accounts: (
H256,
keys::Address,
T::AccountId,
T::BlockNumber,
Vec<u8>,
TxType,
),
) {
let mut index = <RegInfoMaxIndex<T>>::get();
<RegInfoSet<T>>::insert(index, accounts.clone());
index += 1;
Expand Down Expand Up @@ -499,8 +511,14 @@ pub fn handle_output<T: Trait>(
runtime_io::print("----new account-------");
let time = <system::Module<T>>::block_number();
let chainxaddr = <AddressMap<T>>::get(send_address.clone()).unwrap();
let account = (tx.hash(), send_address.clone(), chainxaddr, time,
channel[2..].to_vec(),tx_type);
let account = (
tx.hash(),
send_address.clone(),
chainxaddr,
time,
channel[2..].to_vec(),
tx_type,
);
<RegInfoStorage<T>>::add(account);
runtime_io::print("------insert new account in AccountsMap-----");
}
Expand Down Expand Up @@ -529,7 +547,7 @@ pub fn handle_cert<T: Trait>(
runtime_io::print(&account[..]);
let id: T::AccountId = Decode::decode(&mut account.as_slice()).unwrap();

<CertCache<T>>::put((name[2..].to_vec(),id));
<CertCache<T>>::put((name[2..].to_vec(), id));
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions cxrml/exchange/matchorder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,6 @@ impl<T: Trait> Module<T> {
maker_fee,
taker_fee,
) {

Self::deposit_event(RawEvent::MatchFail(
match_bid.id,
in_bid_detail.pair.clone(),
Expand Down Expand Up @@ -494,7 +493,7 @@ impl<T: Trait> Module<T> {

if insert_head == true {
let new_nodeid = Self::new_nodeid();
let mut list_vec:Vec<BidId>=Vec::new();
let mut list_vec: Vec<BidId> = Vec::new();
list_vec.push(in_bid_detail.id);

let new_bid = Bid {
Expand Down Expand Up @@ -526,7 +525,7 @@ impl<T: Trait> Module<T> {
if finish == false {
//追加在最后
let new_nodeid = Self::new_nodeid();
let mut list_vec:Vec<BidId>=Vec::new();
let mut list_vec: Vec<BidId> = Vec::new();
list_vec.push(in_bid_detail.id);

let new_bid = Bid {
Expand Down Expand Up @@ -572,7 +571,7 @@ impl<T: Trait> Module<T> {
}
for mm in 0..node.data.list.len() {
if in_bid_detail.id == node.data.list[mm] {
let mut list_vec:Vec<BidId>=Vec::new();
let mut list_vec: Vec<BidId> = Vec::new();
list_vec.push(in_bid_detail.id);

Self::remove_from_bid_list(&mut node, &list_vec);
Expand Down
14 changes: 9 additions & 5 deletions cxrml/exchange/matchorder/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
transfer_fee: 0,
creation_fee: 0,
reclaim_rebate: 0,
}.build_storage()
}
.build_storage()
.unwrap(),
);

Expand All @@ -92,7 +93,8 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
token_list: vec![],
transfer_token_fee: 10,
chainx_precision: 8,
}.build_storage()
}
.build_storage()
.unwrap(),
);

Expand All @@ -102,7 +104,8 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
pair_list: vec![],
max_command_id: 0,
average_price_len: 10000,
}.build_storage()
}
.build_storage()
.unwrap(),
);

Expand All @@ -112,7 +115,8 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
fee_precision: 100000,
maker_match_fee: 50,
taker_match_fee: 100,
}.build_storage()
}
.build_storage()
.unwrap(),
);
r.into()
Expand Down Expand Up @@ -462,4 +466,4 @@ fn print_bid(pair: OrderPair, order_type: OrderType) {
} else {
println!("-------------------end -----------------");
}
}
}
18 changes: 0 additions & 18 deletions cxrml/exchange/pendingorders/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,13 @@ impl<T: Trait> Module<T> {
) -> Result {
//判断交易对是否存在
if let Err(_) = Self::is_valid_pair(&pair) {

return Err("not a existed pair in orderpair list");
}
//判定 数量和价格
if amount == Zero::zero() {

return Err("amount cann't be zero");
}
if price == Zero::zero() {

return Err("price cann't be zero");
}
//手续费
Expand All @@ -269,7 +266,6 @@ impl<T: Trait> Module<T> {
pair.second.clone(),
)) < sum
{

return Err("transactor's free token balance too low, can't put buy order");
}
// 锁定用户资产
Expand All @@ -279,15 +275,13 @@ impl<T: Trait> Module<T> {
sum,
ReservedType::Exchange,
) {

return Err(msg);
}
}
OrderType::Sell => {
if <tokenbalances::Module<T>>::free_token(&(sender.clone(), pair.first.clone()))
< As::sa(amount.as_())
{

return Err("transactor's free token balance too low, can't put sell order");
}
// 锁定用户资产
Expand All @@ -297,7 +291,6 @@ impl<T: Trait> Module<T> {
As::sa(amount.as_()),
ReservedType::Exchange,
) {

return Err(msg);
}
}
Expand Down Expand Up @@ -420,7 +413,6 @@ impl<T: Trait> Module<T> {
back_amount,
ReservedType::Exchange,
) {

return Err(msg);
}

Expand Down Expand Up @@ -448,15 +440,13 @@ impl<T: Trait> Module<T> {
));
}
_ => {

return Err(
"order status error( FiillAll|FillPartAndCancel|Cancel) cann't be cancel",
);
}
}
Ok(())
} else {

Err("cann't find this index of order")
}
}
Expand Down Expand Up @@ -486,14 +476,12 @@ impl<T: Trait> Module<T> {
} else if maker_order.hasfill_amount < maker_order.amount {
maker_order.status = OrderStatus::FillPart;
} else {

return Err(" maker order has not enough amount");
}

maker_order.lastupdate_time = <system::Module<T>>::block_number();
maker_order
} else {

return Err("cann't find this maker order");
};

Expand All @@ -508,14 +496,12 @@ impl<T: Trait> Module<T> {
} else if taker_order.hasfill_amount < taker_order.amount {
taker_order.status = OrderStatus::FillPart;
} else {

return Err(" taker order has not enough amount");
}

taker_order.lastupdate_time = <system::Module<T>>::block_number();
taker_order
} else {

return Err("cann't find this taker order");
};

Expand Down Expand Up @@ -554,7 +540,6 @@ impl<T: Trait> Module<T> {
&taker_user.clone(),
&maker_order.channel().clone(),
) {

return Err(msg);
}
//计算买家的数量,解锁second,并move 给卖家,和手续费账户
Expand All @@ -569,7 +554,6 @@ impl<T: Trait> Module<T> {
&maker_user.clone(),
&taker_order.channel().clone(),
) {

return Err(msg);
}
}
Expand All @@ -586,7 +570,6 @@ impl<T: Trait> Module<T> {
&taker_user.clone(),
&maker_order.channel().clone(),
) {

return Err(msg);
}
//计算卖家的数量,解锁second,并move 给买家,和手续费账户
Expand All @@ -601,7 +584,6 @@ impl<T: Trait> Module<T> {
&maker_user.clone(),
&taker_order.channel().clone(),
) {

return Err(msg);
}
}
Expand Down
8 changes: 5 additions & 3 deletions cxrml/exchange/pendingorders/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
transfer_fee: 0,
creation_fee: 0,
reclaim_rebate: 0,
}.build_storage()
}
.build_storage()
.unwrap(),
);

Expand All @@ -84,7 +85,8 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
pair_list: vec![],
max_command_id: 0,
average_price_len: 10000,
}.build_storage()
}
.build_storage()
.unwrap(),
);
r.into()
Expand Down Expand Up @@ -593,4 +595,4 @@ fn print_fill(
println!("maker_fee={:?}", fill.maker_fee());
println!("taker_fee={:?}", fill.taker_fee());
println!("time={:?}", fill.time());
}
}
Loading

0 comments on commit ae5e00d

Please sign in to comment.