Skip to content

Commit

Permalink
Feature/withdrawal (paritytech#98)
Browse files Browse the repository at this point in the history
* withdrawal  module init and rename some dir module

* withdrawal module finish

* withdrawal finish
  • Loading branch information
Aton authored and gguoss committed Nov 16, 2018
1 parent 4840b69 commit c808b6f
Show file tree
Hide file tree
Showing 20 changed files with 504 additions and 143 deletions.
48 changes: 37 additions & 11 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ chainx-network = { path = "./network" }
chainx-pool = { path = "./pool" }
chainx-rpc = { path = "./rpc" }
chainx-api = { path = "./api" }
cxrml-pendingorders = { path = "./cxrml/exchange/pendingorders" }
cxrml-exchange-pendingorders = { path = "./cxrml/exchange/pendingorders" }
cxrml-tokenbalances = { path = "./cxrml/tokenbalances" }

env_logger = "0.5.13"
Expand Down Expand Up @@ -67,9 +67,10 @@ members = [
"cxrml/support",
"cxrml/tokenbalances",
"cxrml/staking",
"cxrml/funds/financialrecords",
"cxrml/multisig",
"cxrml/bridge/btc",
"cxrml/funds/financialrecords",
"cxrml/funds/withdrawal",
"cxrml/exchange/pendingorders",
"cxrml/exchange/matchorder",
"cxrml/example",
Expand Down
4 changes: 2 additions & 2 deletions cxrml/bridge/btc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ srml-consensus = { git = "https://github.com/paritytech/substrate", default-feat
srml-timestamp = { git = "https://github.com/paritytech/substrate", default-features = false }
cxrml-support = { path = "../../support", default-features = false }
cxrml-tokenbalances = { path = "../../tokenbalances", default-features = false }
cxrml-financialrecords = { path = "../../funds/financialrecords", default-features = false }
cxrml-funds-financialrecords = { path = "../../funds/financialrecords", default-features = false }

# bitcoin-rust
chain = { git = "https://github.com/chainx-org/bitcoin-rust", default-features = false }
Expand Down Expand Up @@ -56,7 +56,7 @@ std=[
"srml-timestamp/std",
"cxrml-support/std",
"cxrml-tokenbalances/std",
"cxrml-financialrecords/std",
"cxrml-funds-financialrecords/std",

# bitcoin-rust
"chain/std",
Expand Down
15 changes: 11 additions & 4 deletions cxrml/bridge/btc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ extern crate srml_balances as balances;
extern crate srml_timestamp as timestamp;
#[cfg(test)]
extern crate cxrml_support as cxsupport;
#[cfg(test)]
extern crate cxrml_tokenbalances as tokenbalances;
extern crate cxrml_financialrecords as finacial_recordes;
extern crate cxrml_funds_financialrecords as finacial_recordes;

// bitcoin-rust
extern crate serialization as ser;
Expand Down Expand Up @@ -80,8 +79,8 @@ pub use blockchain::BestHeader;
pub use keys::{Address, Error as AddressError};

pub trait Trait
: system::Trait + balances::Trait + timestamp::Trait + finacial_recordes::Trait
{
: system::Trait + balances::Trait + timestamp::Trait + finacial_recordes::Trait
{
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
}

Expand All @@ -102,6 +101,14 @@ decl_module! {
}
}

impl<T: Trait> tokenbalances::TokenT for Module<T> {
const SYMBOL: &'static [u8] = b"btc";
fn check_addr(addr: &[u8], _: &[u8]) -> Result {
Self::verify_btc_address(addr).map_err(|_| "verify btc addr err")?;
Ok(())
}
}

impl<T: Trait> OnFinalise<T::BlockNumber> for Module<T> {
fn on_finalise(_: T::BlockNumber) {
// do nothing
Expand Down
6 changes: 3 additions & 3 deletions cxrml/exchange/matchorder/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "cxrml-matchorder"
name = "cxrml-exchange-matchorder"
version = "0.1.0"
authors = ["Chainpool <https://www.chainx.org>"]

Expand All @@ -21,7 +21,7 @@ log = "0.4"
# cxrml module
cxrml-support = { path = "../../support", default_features = false }
cxrml-tokenbalances = { path = "../../tokenbalances", default_features = false }
cxrml-pendingorders = { path = "../pendingorders", default_features = false }
cxrml-exchange-pendingorders = { path = "../pendingorders", default_features = false }

[features]
default = ["std"]
Expand All @@ -39,5 +39,5 @@ std=[
"srml-balances/std",
"cxrml-support/std",
"cxrml-tokenbalances/std",
"cxrml-pendingorders/std",
"cxrml-exchange-pendingorders/std",
]
2 changes: 1 addition & 1 deletion cxrml/exchange/matchorder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extern crate srml_balances as balances;
extern crate srml_system as system;

// for chainx runtime module lib
extern crate cxrml_pendingorders as pendingorders;
extern crate cxrml_exchange_pendingorders as pendingorders;
extern crate cxrml_support as cxsupport;
extern crate cxrml_tokenbalances as tokenbalances;

Expand Down
2 changes: 1 addition & 1 deletion cxrml/exchange/pendingorders/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "cxrml-pendingorders"
name = "cxrml-exchange-pendingorders"
version = "0.1.0"
authors = ["Chainpool <https://www.chainx.org>"]

Expand Down
2 changes: 1 addition & 1 deletion cxrml/exchange/pendingorders/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,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_matchorder as matchorder;
//extern crate cxrml_exchange_matchorder as matchorder;

#[cfg(test)]
mod tests;
Expand Down
2 changes: 1 addition & 1 deletion cxrml/funds/financialrecords/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "cxrml-financialrecords"
name = "cxrml-funds-financialrecords"
version = "0.1.0"
authors = ["Chainpool <https://www.chainx.org>"]

Expand Down
44 changes: 14 additions & 30 deletions cxrml/funds/financialrecords/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ use runtime_support::dispatch::Result;
use runtime_support::{StorageMap, StorageValue};
use runtime_primitives::traits::OnFinalise;

use system::ensure_signed;

pub use tokenbalances::{Symbol, ReservedType};
use cxsupport::storage::linked_node::{
Node, NodeT, LinkedNodeCollection, MultiNodeIndex,
Expand All @@ -64,9 +62,6 @@ pub trait Trait: tokenbalances::Trait {

decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn withdraw(origin, sym: Symbol, value: T::TokenBalance) -> Result;
/// set withdrawal fee, call by ROOT
fn set_withdrawal_fee(val: T::Balance) -> Result;
}
}

Expand Down Expand Up @@ -147,7 +142,7 @@ impl Default for Action {
}
}

#[derive(PartialEq, Eq, Clone, Copy, Encode, Decode, Default)]
#[derive(PartialEq, Eq, Clone, Encode, Decode, Default)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
pub struct Record<Symbol, TokenBalance, BlockNumber> where
Symbol: Clone, TokenBalance: Copy, BlockNumber: Copy,
Expand All @@ -156,6 +151,8 @@ pub struct Record<Symbol, TokenBalance, BlockNumber> where
symbol: Symbol,
balance: TokenBalance,
init_blocknum: BlockNumber,
addr: Vec<u8>,
ext: Vec<u8>,
}

type RecordT<T> = Record<Symbol, <T as tokenbalances::Trait>::TokenBalance, <T as system::Trait>::BlockNumber>;
Expand All @@ -167,6 +164,8 @@ impl<Symbol, TokenBalance, BlockNumber> Record<Symbol, TokenBalance, BlockNumber
pub fn mut_action(&mut self) -> &mut Action { &mut self.action }
pub fn symbol(&self) -> Symbol { self.symbol.clone() }
pub fn balance(&self) -> TokenBalance { self.balance }
pub fn addr(&self) -> Vec<u8> { self.addr.clone() }
pub fn ext(&self) -> Vec<u8> { self.ext.clone() }
/// block num for the record init time.
pub fn blocknum(&self) -> BlockNumber { self.init_blocknum }
}
Expand Down Expand Up @@ -266,16 +265,6 @@ impl<T: Trait> Module<T> {
fn deposit_event(event: Event<T>) {
<system::Module<T>>::deposit_event(<T as Trait>::Event::from(event).into());
}
// public call
fn set_withdrawal_fee(val: T::Balance) -> Result {
<WithdrawalFee<T>>::put(val);
Self::deposit_event(RawEvent::SetWithdrawalFee(val));
Ok(())
}
fn withdraw(origin: T::Origin, sym: Symbol, value: T::TokenBalance) -> Result {
let who = ensure_signed(origin)?;
Self::withdrawal(&who, &sym, value)
}
}

impl<T: Trait> Module<T> {
Expand Down Expand Up @@ -367,8 +356,8 @@ impl<T: Trait> Module<T> {
}

/// withdrawal, notice this func has include withdrawal_init and withdrawal_locking
fn withdrawal(who: &T::AccountId, sym: &Symbol, balance: T::TokenBalance) -> Result {
let index = Self::withdrawal_with_index(who, sym, balance)?;
pub fn withdrawal(who: &T::AccountId, sym: &Symbol, balance: T::TokenBalance, addr: Vec<u8>, ext: Vec<u8>) -> Result {
let index = Self::withdrawal_with_index(who, sym, balance, addr, ext)?;

// set to withdraw cache
let n = Node::new(WithdrawLog::<T::AccountId> { accountid: who.clone(), index });
Expand Down Expand Up @@ -461,7 +450,7 @@ impl<T: Trait> Module<T> {

<tokenbalances::Module<T>>::is_valid_token(sym)?;

let r = Record { action: Action::Deposit(Default::default()), symbol: sym.clone(), balance: balance, init_blocknum: <system::Module<T>>::block_number() };
let r = Record { action: Action::Deposit(Default::default()), symbol: sym.clone(), balance: balance, init_blocknum: <system::Module<T>>::block_number(), addr: Vec::new(), ext: Vec::new() };
let index = Self::new_record(who, &r)?;
Self::deposit_event(RawEvent::DepositInit(who.clone(), index, r.symbol(), r.balance(), r.blocknum()));

Expand Down Expand Up @@ -501,7 +490,7 @@ impl<T: Trait> Module<T> {
}
}
/// withdrawal init, notice this func return index to show the index of records for this account
pub fn withdrawal_with_index(who: &T::AccountId, sym: &Symbol, balance: T::TokenBalance) -> StdResult<u32, &'static str> {
fn withdrawal_with_index(who: &T::AccountId, sym: &Symbol, balance: T::TokenBalance, addr: Vec<u8>, ext: Vec<u8>) -> StdResult<u32, &'static str> {
Self::before(who, sym, true)?;

<tokenbalances::Module<T>>::is_valid_token_for(who, sym)?;
Expand All @@ -510,18 +499,13 @@ impl<T: Trait> Module<T> {
return Err("not enough free token to withdraw");
}

let mut index = 0_u32;
<cxsupport::Module<T>>::handle_fee_after(who, Self::withdrawal_fee(), true, || {
let r = Record { action: Action::Withdrawal(Default::default()), symbol: sym.clone(), balance: balance, init_blocknum: <system::Module<T>>::block_number() };
index = Self::new_record(who, &r)?;
Self::deposit_event(RawEvent::WithdrawalInit(who.clone(), index, r.symbol(), r.balance(), r.blocknum()));
Ok(())
})?;

let r = Record { action: Action::Withdrawal(Default::default()), symbol: sym.clone(), balance: balance, init_blocknum: <system::Module<T>>::block_number(), addr, ext };
let index = Self::new_record(who, &r)?;
Self::deposit_event(RawEvent::WithdrawalInit(who.clone(), index, r.symbol(), r.balance(), r.blocknum()));
Ok(index)
}
/// withdrawal lock, should use index to find out which record to change to locking state
pub fn withdrawal_locking_with_index(who: &T::AccountId, index: u32) -> StdResult<u32, &'static str> {
fn withdrawal_locking_with_index(who: &T::AccountId, index: u32) -> StdResult<u32, &'static str> {
let key = (who.clone(), index);
if let Some(ref mut r) = <RecordsOf<T>>::get(&key) {
if r.is_finish() {
Expand Down Expand Up @@ -554,7 +538,7 @@ impl<T: Trait> Module<T> {
}
}
/// withdrawal finish, should use index to find out which record to changed to final, success flag mark success, if false, release the token to free
pub fn withdrawal_finish_with_index(who: &T::AccountId, index: u32, success: bool) -> StdResult<u32, &'static str> {
fn withdrawal_finish_with_index(who: &T::AccountId, index: u32, success: bool) -> StdResult<u32, &'static str> {
let key = (who.clone(), index);
if let Some(ref mut r) = <RecordsOf<T>>::get(&key) {
if r.is_finish() {
Expand Down
Loading

0 comments on commit c808b6f

Please sign in to comment.