Skip to content

Commit

Permalink
Feature/fee (paritytech#250)
Browse files Browse the repository at this point in the history
provide new fee proportion
  • Loading branch information
Aton authored Jan 29, 2019
1 parent 846a71c commit 29bd474
Show file tree
Hide file tree
Showing 16 changed files with 170 additions and 127 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cli/src/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ pub fn testnet_genesis(genesis_spec: GenesisSpec) -> GenesisConfig {
}),
fee_manager: Some(XFeeManagerConfig {
switch: false,
producer_fee_proportion: (1, 10),
_genesis_phantom_data: Default::default(),
}),
xassets: Some(XAssetsConfig {
Expand Down
1 change: 0 additions & 1 deletion runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,3 @@ pub mod xspot_api {
}
}
}

2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ impl xsystem::Trait for Runtime {

impl xaccounts::Trait for Runtime {
type Event = Event;
type DetermineIntentionJackpotAccountId = xaccounts::SimpleAccountIdDeterminator<Runtime>;
}
// fees
impl fee_manager::Trait for Runtime {
Expand All @@ -263,7 +264,6 @@ impl xstaking::Trait for Runtime {
type OnRewardCalculation = xtokens::Module<Runtime>;
type OnReward = xtokens::Module<Runtime>;
type Event = Event;
type DetermineJackpotAccountId = xstaking::SimpleAccountIdDeterminator<Runtime>;
}

impl xtokens::Trait for Runtime {
Expand Down
1 change: 1 addition & 0 deletions runtime/wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
22 changes: 22 additions & 0 deletions xrml/xaccounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ extern crate xr_primitives;
extern crate xrml_xassets_assets as xassets;

use rstd::prelude::*;
use runtime_primitives::traits::Hash;
use runtime_support::dispatch::Result;

use xassets::Chain;
Expand All @@ -51,6 +52,27 @@ pub type URL = XString;
pub trait Trait: system::Trait {
/// The overarching event type.
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;

/// Generate virtual AccountId for each (psedu) intention
type DetermineIntentionJackpotAccountId: IntentionJackpotAccountIdFor<Self::AccountId>;
}

pub trait IntentionJackpotAccountIdFor<AccountId: Sized> {
fn accountid_for(origin: &AccountId) -> AccountId;
}

pub struct SimpleAccountIdDeterminator<T: Trait>(::rstd::marker::PhantomData<T>);

impl<T: Trait> IntentionJackpotAccountIdFor<T::AccountId> for SimpleAccountIdDeterminator<T>
where
T::AccountId: From<T::Hash> + AsRef<[u8]>,
{
fn accountid_for(origin: &T::AccountId) -> T::AccountId {
let name = Module::<T>::intention_name_of(origin)
.expect("The original account must be an existing intention.");
// name
T::Hashing::hash(&name).into()
}
}

/// Intention mutable properties
Expand Down
7 changes: 6 additions & 1 deletion xrml/xassets/assets/src/assetdef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ impl Default for Chain {

impl Chain {
pub fn iterator() -> Iter<'static, Chain> {
static CHAINS: [Chain; 4] = [Chain::ChainX, Chain::Bitcoin, Chain::Ethereum, Chain::Polkadot];
static CHAINS: [Chain; 4] = [
Chain::ChainX,
Chain::Bitcoin,
Chain::Ethereum,
Chain::Polkadot,
];
CHAINS.iter()
}
}
Expand Down
3 changes: 1 addition & 2 deletions xrml/xassets/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,15 +648,14 @@ impl<T: Trait> Module<T> {
Self::move_balance_with_checkflag(token, from, from_type, to, to_type, value, true)
}


pub fn move_balance_with_checkflag(
token: &Token,
from: &T::AccountId,
from_type: AssetType,
to: &T::AccountId,
to_type: AssetType,
value: T::Balance,
check: bool
check: bool,
) -> StdResult<(), AssetErr> {
if from == to && from_type == to_type {
// same account, same type, return directly
Expand Down
15 changes: 11 additions & 4 deletions xrml/xdex/spot/src/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,25 @@ pub struct OrderPair {
pub id: OrderPairID,
pub first: Token,
pub second: Token,
pub precision: u32, //价格精度
pub unit_precision:u32,//最小单位精度
pub precision: u32, //价格精度
pub unit_precision: u32, //最小单位精度
pub used: bool,
}
impl OrderPair {
pub fn new(id: OrderPairID, first: Token, second: Token, precision: u32, unit: u32,status: bool) -> Self {
pub fn new(
id: OrderPairID,
first: Token,
second: Token,
precision: u32,
unit: u32,
status: bool,
) -> Self {
OrderPair {
id: id,
first: first,
second: second,
precision: precision,
unit_precision:unit,
unit_precision: unit,
used: status,
}
}
Expand Down
66 changes: 33 additions & 33 deletions xrml/xdex/spot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ use runtime_support::dispatch::Result;
use runtime_support::{Parameter, StorageMap, StorageValue};
use system::ensure_signed;

use xassets::assetdef::{ChainT,Token};
use xassets::assetdef::{ChainT, Token};

const PRICE_MAX_ORDER: usize = 1000;

Expand Down Expand Up @@ -252,26 +252,35 @@ impl<T: Trait> Module<T> {
None
}

pub fn aver_asset_price(token:&Token)->Option<T::Balance> {
pub fn aver_asset_price(token: &Token) -> Option<T::Balance> {
/*
如果交易对ID是XXX/PCX,则:
返回:交易对Map[交易对ID].平均价 / 10^报价精度
如果交易对ID是PCX/XXX,则:
返回:10^交易对Map[交易对ID].报价精度 / 平均价
返回:10^交易对Map[交易对ID].报价精度 / 平均价
*/
let pair_len = <OrderPairLen<T>>::get();
for i in 0..pair_len {
if let Some(pair) = <OrderPairOf<T>>::get(i) {
if pair.first.eq(token) && pair.second.eq(&<xassets::Module<T> as ChainT>::TOKEN.to_vec()) {
if let Some((_,aver,_))= <OrderPairPriceOf<T>>::get(i) {
let price:T::Balance=As::sa(aver.as_()/(10_u64.pow(pair.precision.as_() )));
return Some(price );
if pair.first.eq(token)
&& pair
.second
.eq(&<xassets::Module<T> as ChainT>::TOKEN.to_vec())
{
if let Some((_, aver, _)) = <OrderPairPriceOf<T>>::get(i) {
let price: T::Balance =
As::sa(aver.as_() / (10_u64.pow(pair.precision.as_())));
return Some(price);
}
}
else if pair.first.eq(&<xassets::Module<T> as ChainT>::TOKEN.to_vec()) && pair.second.eq(token) {
if let Some((_,aver,_))= <OrderPairPriceOf<T>>::get(i) {
let price:T::Balance=As::sa(10_u64.pow(pair.precision.as_() )/aver.as_());
return Some(price) ;
} else if pair
.first
.eq(&<xassets::Module<T> as ChainT>::TOKEN.to_vec())
&& pair.second.eq(token)
{
if let Some((_, aver, _)) = <OrderPairPriceOf<T>>::get(i) {
let price: T::Balance =
As::sa(10_u64.pow(pair.precision.as_()) / aver.as_());
return Some(price);
}
}
}
Expand Down Expand Up @@ -371,11 +380,11 @@ impl<T: Trait> Module<T> {
if amount == Zero::zero() {
return Err("amount cann't be zero");
}
let min_unit=10_u64.pow(pair.unit_precision);
let min_unit = 10_u64.pow(pair.unit_precision);
if price < As::sa(min_unit.as_()) {
return Err("price cann't be less min_unit");
}
if price%As::sa(min_unit) != Zero::zero() {
if price % As::sa(min_unit) != Zero::zero() {
return Err("price % min_unit must be 0");
}

Expand Down Expand Up @@ -404,16 +413,17 @@ impl<T: Trait> Module<T> {
OrderDirection::Buy => {
if handicap.sell > Zero::zero()
&& price
> ((handicap.sell
* As::sa(100_u32 + <PriceVolatility<T>>::get())) / As::sa(100_u32))
> ((handicap.sell * As::sa(100_u32 + <PriceVolatility<T>>::get()))
/ As::sa(100_u32))
{
return Err("price cann't > PriceVolatility");
}
}
OrderDirection::Sell => {
if handicap.buy > Zero::zero()
&& price
< (handicap.buy * As::sa(100_u32 - <PriceVolatility<T>>::get()) / As::sa(100_u32))
< (handicap.buy * As::sa(100_u32 - <PriceVolatility<T>>::get())
/ As::sa(100_u32))
{
return Err("price cann't > PriceVolatility");
}
Expand Down Expand Up @@ -486,7 +496,7 @@ impl<T: Trait> Module<T> {
OrderDirection::Buy => handicap.sell,
OrderDirection::Sell => handicap.buy,
};
let min_unit=10_u64.pow(pair.unit_precision);
let min_unit = 10_u64.pow(pair.unit_precision);

loop {
if opponent_price == Zero::zero() {
Expand Down Expand Up @@ -574,17 +584,13 @@ impl<T: Trait> Module<T> {
//移动对手价
match order.direction {
OrderDirection::Buy => {
opponent_price = match opponent_price
.checked_add(&As::sa(min_unit))
{
opponent_price = match opponent_price.checked_add(&As::sa(min_unit)) {
Some(v) => v,
None => Default::default(),
};
}
OrderDirection::Sell => {
opponent_price = match opponent_price
.checked_sub(&As::sa(min_unit))
{
opponent_price = match opponent_price.checked_sub(&As::sa(min_unit)) {
Some(v) => v,
None => Default::default(),
};
Expand Down Expand Up @@ -866,7 +872,7 @@ impl<T: Trait> Module<T> {
//更新盘口
fn update_handicap(pair: &OrderPair, price: T::Price, direction: OrderDirection) {
//这里方向是反的,注意
let min_unit=10_u64.pow(pair.unit_precision);
let min_unit = 10_u64.pow(pair.unit_precision);

match <Quotations<T>>::get((pair.id, price)) {
Some(_list) => {}
Expand All @@ -875,10 +881,7 @@ impl<T: Trait> Module<T> {
OrderDirection::Buy => {
//更新卖一
if let Some(mut handicap) = <HandicapMap<T>>::get(pair.id) {
handicap.sell = match handicap
.sell
.checked_add(&As::sa(min_unit))
{
handicap.sell = match handicap.sell.checked_add(&As::sa(min_unit)) {
Some(v) => v,
None => Default::default(),
};
Expand All @@ -889,10 +892,7 @@ impl<T: Trait> Module<T> {
OrderDirection::Sell => {
//更新买一
if let Some(mut handicap) = <HandicapMap<T>>::get(pair.id) {
handicap.buy = match handicap
.buy
.checked_sub(&As::sa(min_unit))
{
handicap.buy = match handicap.buy.checked_sub(&As::sa(min_unit)) {
Some(v) => v,
None => Default::default(),
};
Expand Down
2 changes: 1 addition & 1 deletion xrml/xdex/spot/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn test_pair() {
let a: u64 = 1; // accountid
let first: Token = b"EOS".to_vec();
let second: Token = b"ETH".to_vec();
Spot::add_pair(first.clone(), second.clone(), 2, 1,100, true).unwrap();
Spot::add_pair(first.clone(), second.clone(), 2, 1, 100, true).unwrap();
assert_eq!(Spot::pair_len(), 2);

let pair = Spot::get_pair_by(&first, &second).unwrap();
Expand Down
2 changes: 2 additions & 0 deletions xrml/xfee/manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ srml-support = { git = "https://github.com/chainpool/substrate", default-feature

# xrml
xrml-xsystem = { path = "../../xsystem", default-features = false }
xrml-xaccounts = { path = "../../xaccounts", default-features = false }
xrml-xassets-assets = { path = "../../xassets/assets", default-features = false }

[features]
Expand All @@ -33,5 +34,6 @@ std = [

# xrml
"xrml-xsystem/std",
"xrml-xaccounts/std",
"xrml-xassets-assets/std",
]
Loading

0 comments on commit 29bd474

Please sign in to comment.