Skip to content

Commit

Permalink
Feature/associations (paritytech#103)
Browse files Browse the repository at this point in the history
* calc fee for relationship accountid and blockproducer finish

* modify all test

* remove FinancialRecords genesisconfig
  • Loading branch information
Aton authored Nov 16, 2018
1 parent 296df2e commit a41f262
Show file tree
Hide file tree
Showing 35 changed files with 601 additions and 21 deletions.
36 changes: 36 additions & 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ members = [
"cxrml/tokenbalances",
"cxrml/staking",
"cxrml/multisig",
"cxrml/associations",
"cxrml/bridge/btc",
"cxrml/funds/financialrecords",
"cxrml/funds/withdrawal",
Expand Down
35 changes: 35 additions & 0 deletions cxrml/associations/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[package]
name = "cxrml-associations"
version = "0.1.0"
authors = ["Chainpool <http://chainx.org>"]


[dependencies]
hex-literal = "0.1.0"
serde = { version = "1.0", default_features = false }
serde_derive = { version = "1.0", optional = true }
parity-codec = { version = "2.0", default-features = false }
parity-codec-derive = { version = "2.0", default-features = false }
substrate-primitives = { git = "https://github.com/paritytech/substrate", default_features = false }
sr-std = { git = "https://github.com/paritytech/substrate", default_features = false }
sr-io = { git = "https://github.com/paritytech/substrate", default_features = false }
sr-primitives = { git = "https://github.com/paritytech/substrate", default_features = false }
srml-support = { git = "https://github.com/paritytech/substrate", default_features = false }
srml-system = { git = "https://github.com/paritytech/substrate", default_features = false }
srml-balances = { git = "https://github.com/paritytech/substrate", default_features = false }

[features]
default = ["std"]
std=[
"serde/std",
"serde_derive",
"parity-codec/std",
"parity-codec-derive/std",
"substrate-primitives/std",
"sr-std/std",
"sr-io/std",
"sr-primitives/std",
"srml-support/std",
"srml-system/std",
"srml-balances/std",
]
151 changes: 151 additions & 0 deletions cxrml/associations/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
//! this module is for bch-bridge
#![cfg_attr(not(feature = "std"), no_std)]
// for encode/decode
// Needed for deriving `Serialize` and `Deserialize` for various types.
// We only implement the serde traits for std builds - they're unneeded
// in the wasm runtime.
#[cfg(feature = "std")]
#[macro_use]
extern crate serde_derive;

// Needed for deriving `Encode` and `Decode` for `RawEvent`.
#[macro_use]
extern crate parity_codec_derive;
extern crate parity_codec as codec;

// for substrate
// Needed for the set of mock primitives used in our tests.
#[cfg(feature = "std")]
extern crate substrate_primitives;

// for substrate runtime
// map!, vec! marco.
extern crate sr_std as rstd;
// Needed for tests (`with_externalities`).
#[cfg(feature = "std")]
extern crate sr_io as runtime_io;
extern crate sr_primitives as runtime_primitives;
// for substrate runtime module lib
// Needed for type-safe access to storage DB.
#[macro_use]
extern crate srml_support as runtime_support;
extern crate srml_system as system;
extern crate srml_balances as balances;

#[cfg(test)]
mod tests;

use rstd::prelude::*;
use runtime_support::dispatch::Result;
use runtime_support::{StorageMap, StorageValue};
use runtime_primitives::traits::{OnFinalise, CheckedAdd, CheckedSub};

use system::ensure_signed;

pub trait OnCalcFee<AccountId, Balance> {
fn on_calc_fee(who: &AccountId, total_fee: Balance) -> Result;
}

impl<AccountId, Balance> OnCalcFee<AccountId, Balance> for () {
fn on_calc_fee(_: &AccountId, _: Balance) -> Result { Ok(()) }
}

pub trait Trait: system::Trait + balances::Trait {
type OnCalcFee: OnCalcFee<Self::AccountId, Self::Balance>;
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
}

decl_event!(
pub enum Event<T> where
<T as system::Trait>::AccountId,
<T as balances::Trait>::Balance
{
InitAccount(AccountId, AccountId, Balance),
InitExchangeAccount(AccountId, AccountId),
}
);

decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn init_account(origin, who: T::AccountId, value: T::Balance) -> Result;
fn init_exchange_relationship(origin, who: T::AccountId) -> Result;
}
}

impl<T: Trait> OnFinalise<T::BlockNumber> for Module<T> {
fn on_finalise(_: T::BlockNumber) {
// do nothing
}
}

decl_storage! {
trait Store for Module<T: Trait> as BridgeOfBCH {
pub Relationship get(relationship): map T::AccountId => Option<T::AccountId>;
pub TokenRelationship get(token_relationship): map (Vec<u8>, T::AccountId) => Vec<u8>;
pub ExchangeRelationship get(exchange_relationship): map T::AccountId => Option<T::AccountId>;
// fee
pub InitFee get(init_fee) config(): T::Balance;
}
}

impl<T: Trait> Module<T> {
// event
/// Deposit one of this module's events.
fn deposit_event(event: Event<T>) {
<system::Module<T>>::deposit_event(<T as Trait>::Event::from(event).into());
}
}

impl<T: Trait> Module<T> {
pub fn init_account(origin: T::Origin, who: T::AccountId, value: T::Balance) -> Result {
let from = ensure_signed(origin)?;
// 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");
}
}
Relationship::<T>::insert(&who, from.clone());

let from_balance = balances::Module::<T>::free_balance(&from);
let to_balance = balances::Module::<T>::free_balance(&who);

let new_from_balance = match from_balance.checked_sub(&value) {
Some(b) => b,
None => return Err("balance too low to send value"),
};
let new_to_balance = match to_balance.checked_add(&value) {
Some(b) => b,
None => return Err("destination balance too high to receive value"),
};

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

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

pub fn init_exchange_relationship(origin: T::Origin, who: T::AccountId) -> Result {
let from = ensure_signed(origin)?;
// deduct fee first
T::OnCalcFee::on_calc_fee(&from, Self::init_fee())?;

if Self::exchange_relationship(&who).is_some() {
return Err("has register this account");
} else {
if balances::FreeBalance::<T>::exists(&who) {
return Err("this account is exist");
}
}
ExchangeRelationship::<T>::insert(&who, from.clone());
Self::deposit_event(RawEvent::InitExchangeAccount(from, who));
Ok(())
}
}

4 changes: 4 additions & 0 deletions cxrml/associations/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
4 changes: 4 additions & 0 deletions cxrml/bridge/btc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ srml-system = { git = "https://github.com/paritytech/substrate", default-feature
srml-balances = { git = "https://github.com/paritytech/substrate", default-features = false }
srml-consensus = { git = "https://github.com/paritytech/substrate", default-features = false }
srml-timestamp = { git = "https://github.com/paritytech/substrate", default-features = false }
cxrml-system = { path = "../../system", default-features = false }
cxrml-associations = { path = "../../associations", default-features = false }
cxrml-support = { path = "../../support", default-features = false }
cxrml-tokenbalances = { path = "../../tokenbalances", default-features = false }
cxrml-funds-financialrecords = { path = "../../funds/financialrecords", default-features = false }
Expand Down Expand Up @@ -54,6 +56,8 @@ std=[
"srml-balances/std",
"srml-consensus/std",
"srml-timestamp/std",
"cxrml-system/std",
"cxrml-associations/std",
"cxrml-support/std",
"cxrml-tokenbalances/std",
"cxrml-funds-financialrecords/std",
Expand Down
4 changes: 4 additions & 0 deletions cxrml/bridge/btc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ extern crate srml_system as system;
extern crate srml_balances as balances;
extern crate srml_timestamp as timestamp;
#[cfg(test)]
extern crate cxrml_system as cxsystem;
#[cfg(test)]
extern crate cxrml_associations as associations;
#[cfg(test)]
extern crate cxrml_support as cxsupport;
extern crate cxrml_tokenbalances as tokenbalances;
extern crate cxrml_funds_financialrecords as finacial_recordes;
Expand Down
7 changes: 7 additions & 0 deletions cxrml/bridge/btc/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ impl timestamp::Trait for Test {
type Moment = u64;
}

impl cxsystem::Trait for Test {}

impl associations::Trait for Test {
type OnCalcFee = cxsupport::Module<Test>;
type Event = ();
}

impl cxsupport::Trait for Test {}

impl tokenbalances::Trait for Test {
Expand Down
4 changes: 4 additions & 0 deletions cxrml/exchange/matchorder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ srml-balances = { git = "https://github.com/paritytech/substrate", default_featu
log = "0.4"

# cxrml module
cxrml-system = { path = "../../system", default-features = false }
cxrml-associations = { path = "../../associations", default-features = false }
cxrml-support = { path = "../../support", default_features = false }
cxrml-tokenbalances = { path = "../../tokenbalances", default_features = false }
cxrml-exchange-pendingorders = { path = "../pendingorders", default_features = false }
Expand All @@ -37,6 +39,8 @@ std=[
"srml-support/std",
"srml-system/std",
"srml-balances/std",
"cxrml-system/std",
"cxrml-associations/std",
"cxrml-support/std",
"cxrml-tokenbalances/std",
"cxrml-exchange-pendingorders/std",
Expand Down
Loading

0 comments on commit a41f262

Please sign in to comment.