Skip to content

Commit

Permalink
support relationship (paritytech#116)
Browse files Browse the repository at this point in the history
* support relationship
* move destory account and feebuy account
  • Loading branch information
toxotguo authored Nov 22, 2018
1 parent 2ba822f commit 327447b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 25 deletions.
1 change: 0 additions & 1 deletion cxrml/exchange/matchorder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#[macro_use]
extern crate serde_derive;

#[macro_use]
extern crate log;

// Needed for deriving `Encode` and `Decode` for `RawEvent`.
Expand Down
40 changes: 20 additions & 20 deletions cxrml/exchange/pendingorders/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#[macro_use]
extern crate serde_derive;

#[macro_use]
extern crate log;

// Needed for deriving `Encode` and `Decode` for `RawEvent`.
Expand Down Expand Up @@ -43,10 +42,7 @@ extern crate srml_system as system;
// for chainx runtime module lib
extern crate cxrml_support as cxsupport;
extern crate cxrml_tokenbalances as tokenbalances;


extern crate cxrml_associations as associations;
#[cfg(test)]
extern crate cxrml_system as cxsystem;

#[cfg(test)]
Expand Down Expand Up @@ -88,8 +84,6 @@ pub trait Trait: tokenbalances::Trait {
+ Default;
/// The overarching event type.
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
const FEE_DESTROY_ACCOUNT: <Self as system::Trait>::AccountId;
const FEE_BUY_ACCOUNT: <Self as system::Trait>::AccountId;
}

decl_module! {
Expand Down Expand Up @@ -613,20 +607,26 @@ impl<T: Trait> Module<T> {
fn discount_fee(account: &T::AccountId, symbol: &Symbol, amount: T::Amount) -> T::Amount {
match <tokenbalances::Module<T>>::token_info(symbol) {
Some((token, _)) => {
//计算关联账户的额度
let free_token =
<tokenbalances::Module<T>>::free_token(&(account.clone(), symbol.clone()));
let father_free_token:<T as tokenbalances::Trait>::TokenBalance = match <associations::Module<T>>::exchange_relationship(account){
Some(father)=> <tokenbalances::Module<T>>::free_token(&(father.clone(), symbol.clone())),
None=>Zero::zero(),
};
let total_token=free_token+father_free_token;
//将精度考虑进去
let after_discount: T::Amount = if free_token > As::sa(0) {
if free_token <= As::sa(10_u128.pow(token.precision().as_()) * 10000) {
let after_discount: T::Amount = if total_token > As::sa(0) {
if total_token <= As::sa(10_u128.pow(token.precision().as_()) * 10000) {
As::sa((amount.as_() * 7_u128) / 10_u128)
} else if free_token <= As::sa(10_u128.pow(token.precision().as_()) * 100000) {
} else if total_token <= As::sa(10_u128.pow(token.precision().as_()) * 100000) {
As::sa((amount.as_() * 6_u128) / 10_u128)
} else if free_token <= As::sa(10_u128.pow(token.precision().as_()) * 1000000) {
} else if total_token <= As::sa(10_u128.pow(token.precision().as_()) * 1000000) {
As::sa((amount.as_() * 5_u128) / 10_u128)
} else if free_token <= As::sa(10_u128.pow(token.precision().as_()) * 10000000)
} else if total_token <= As::sa(10_u128.pow(token.precision().as_()) * 10000000)
{
As::sa((amount.as_() * 3_u128) / 10_u128)
} else if free_token <= As::sa(10_u128.pow(token.precision().as_()) * 100000000)
} else if total_token <= As::sa(10_u128.pow(token.precision().as_()) * 100000000)
{
As::sa((amount.as_() * 2_u128) / 10_u128)
} else {
Expand Down Expand Up @@ -711,20 +711,20 @@ impl<T: Trait> Module<T> {
return Err(msg);
}

if to == &T::FEE_BUY_ACCOUNT
if to == &<cxsystem::Module<T>>::fee_buy_account()
&& symbol.clone() == <T as tokenbalances::Trait>::CHAINX_SYMBOL.to_vec()
{
//前面自动生成的buy交易,不计算手续费 80%直接销毁 20% 给渠道
if let Err(msg) = Self::dispatch_fee(
symbol,
value,
from,
&T::FEE_DESTROY_ACCOUNT,
&<cxsystem::Module<T>>::death_account(),
&channel.clone(),
) {
return Err(msg);
};
} else if from == &T::FEE_BUY_ACCOUNT {
} else if from == &<cxsystem::Module<T>>::fee_buy_account() {
// 和手续费买盘的对手盘,不收手续费
if let Err(e) = <tokenbalances::Module<T>>::move_free_token(
&from.clone(),
Expand All @@ -751,7 +751,7 @@ impl<T: Trait> Module<T> {
symbol,
discount_fee,
from,
&T::FEE_DESTROY_ACCOUNT,
&<cxsystem::Module<T>>::death_account(),
&channel.clone(),
) {
return Err(msg);
Expand Down Expand Up @@ -785,7 +785,7 @@ impl<T: Trait> Module<T> {
&<T as tokenbalances::Trait>::CHAINX_SYMBOL.to_vec(),
discount_fee,
to,
&T::FEE_DESTROY_ACCOUNT,
&<cxsystem::Module<T>>::death_account(),
&channel.clone(),
) {
return Err(msg);
Expand All @@ -801,7 +801,7 @@ impl<T: Trait> Module<T> {
}
if let Err(e) = <tokenbalances::Module<T>>::move_free_token(
&from.clone(),
&T::FEE_BUY_ACCOUNT,
&<cxsystem::Module<T>>::fee_buy_account(),
&symbol.clone(),
fee,
) {
Expand All @@ -824,7 +824,7 @@ impl<T: Trait> Module<T> {
}
if let Err(e) = <tokenbalances::Module<T>>::move_free_token(
&from.clone(),
&T::FEE_BUY_ACCOUNT,
&<cxsystem::Module<T>>::fee_buy_account(),
&symbol.clone(),
fee,
) {
Expand Down Expand Up @@ -879,7 +879,7 @@ impl<T: Trait> Module<T> {
for id in 1..(fee_buy_order_max + 1) {
if let Some(buy) = <FeeBuyOrder<T>>::get(id) {
let _ = Self::do_put_order(
&T::FEE_BUY_ACCOUNT,
&<cxsystem::Module<T>>::fee_buy_account(),
&buy.0,
OrderType::Buy,
buy.1,
Expand Down
1 change: 1 addition & 0 deletions cxrml/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ decl_storage! {
trait Store for Module<T: Trait> as CXSystem {
pub BlockProdocer get(block_producer): Option<T::AccountId>;
pub DeathAccount get(death_account) config(): T::AccountId;
pub FeeBuyAccount get(fee_buy_account) config(): T::AccountId;
}
}

Expand Down
2 changes: 0 additions & 2 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,6 @@ impl pendingorders::Trait for Runtime {
type Amount = TokenBalance;
type Price = TokenBalance;
type Event = Event;
const FEE_BUY_ACCOUNT: AccountId = primitives::H256([1; 32]);
const FEE_DESTROY_ACCOUNT: AccountId = primitives::H256([0; 32]);
}

impl matchorder::Trait for Runtime {
Expand Down
5 changes: 3 additions & 2 deletions src/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extern crate cxrml_exchange_pendingorders;
extern crate cxrml_tokenbalances;
extern crate keys;
extern crate primitives as btc_primitives;

extern crate substrate_primitives ;

use self::base58::FromBase58;
use self::cxrml_exchange_pendingorders::OrderPair;
Expand Down Expand Up @@ -129,7 +129,8 @@ pub fn testnet_genesis(chainspec: ChainSpec) -> GenesisConfig {
block_gas_limit: 10_000_000,
}),
cxsystem: Some(CXSystemConfig {
death_account: Default::default(),
death_account: substrate_primitives::H256([0; 32]),
fee_buy_account: substrate_primitives::H256([1; 32]),
}),
tokenbalances: Some(TokenBalancesConfig {
chainx_precision: 8,
Expand Down

0 comments on commit 327447b

Please sign in to comment.