Skip to content

Commit

Permalink
Fix/bitcoin 1.0.0 (paritytech#619)
Browse files Browse the repository at this point in the history
* split minimal_withdrawal and required fee 

fix https://github.com/chainpool/ChainX/issues/610
support a rpc for this feature

* fix bug for withdrawal list 

fix bug https://github.com/chainpool/ChainX/issues/609
when no txid, it's Broadcasting

* support application state for withdrawal_list rpc

support for https://github.com/chainpool/ChainX/issues/614
  • Loading branch information
atenjin authored and toxotguo committed May 20, 2019
1 parent eedcc53 commit 206af6c
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 14 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

Binary file modified cli/src/chainx_runtime.compact.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ xr-primitives = { path = "../xr-primitives" }
# chainx runtime module
xassets = { package = "xrml-xassets-assets", path = "../xrml/xassets/assets" }
xrecords = { package = "xrml-xassets-records", path = "../xrml/xassets/records" }
xprocess = { package = "xrml-xassets-process", path = "../xrml/xassets/process" }
xaccounts = { package = "xrml-xaccounts", path = "../xrml/xaccounts" }
xsession = { package = "xrml-xsession", path = "../xrml/xsession" }
xstaking = { package ="xrml-xmining-staking", path = "../xrml/xmining/staking" }
Expand Down
5 changes: 3 additions & 2 deletions rpc/src/chainx/impl_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use xr_primitives::{AddrStr, Name};

use xaccounts::IntentionProps;
use xassets::{Asset, AssetType, Chain, ChainT, Token};
use xprocess::WithdrawalLimit;
use xspot::{HandicapInfo, OrderIndex, OrderInfo, TradingPair, TradingPairIndex};
use xstaking::IntentionProfs;
use xtokens::{DepositVoteWeight, PseduIntentionVoteWeight};
Expand Down Expand Up @@ -176,7 +177,7 @@ where
}
}

fn minimal_withdrawal_value(&self, token: String) -> Result<Option<Balance>> {
fn withdrawal_limit(&self, token: String) -> Result<Option<WithdrawalLimit<Balance>>> {
let token: xassets::Token = token.as_bytes().to_vec();

// test valid before call runtime api
Expand All @@ -186,7 +187,7 @@ where
let b = self.best_number()?;
self.client
.runtime_api()
.minimal_withdrawal_value(&b, token)
.withdrawal_limit(&b, token)
.map_err(Into::into)
}

Expand Down
5 changes: 3 additions & 2 deletions rpc/src/chainx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use runtime_primitives::traits::Block as BlockT;
use state_machine::Backend;

use xassets::Chain;
use xprocess::WithdrawalLimit;
use xspot::TradingPairIndex;

mod error;
Expand Down Expand Up @@ -45,8 +46,8 @@ pub trait ChainXApi<Number, AccountId, Balance, BlockNumber, SignedBlock> {
#[rpc(name = "chainx_verifyAddressValidity")]
fn verify_addr(&self, token: String, addr: String, memo: String) -> Result<Option<bool>>;

#[rpc(name = "chainx_getMinimalWithdrawalValueByToken")]
fn minimal_withdrawal_value(&self, token: String) -> Result<Option<Balance>>;
#[rpc(name = "chainx_getWithdrawalLimitByToken")]
fn withdrawal_limit(&self, token: String) -> Result<Option<WithdrawalLimit<Balance>>>;

#[rpc(name = "chainx_getDepositList")]
fn deposit_list(
Expand Down
10 changes: 9 additions & 1 deletion rpc/src/chainx/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,10 @@ pub struct WithdrawInfo {
pub accountid: AccountIdForRpc,
/// btc-address
pub address: String,
/// withdraw status
/// withdraw tx status
pub status: Value,
/// application status
pub application_status: String,
/// OP_RETURN
pub memo: String,
}
Expand Down Expand Up @@ -427,6 +429,12 @@ impl From<RecordInfo<AccountId, Balance, BlockNumber, Timestamp>> for WithdrawIn
accountid: record.who.into(),
address: String::from_utf8_lossy(&record.addr).into_owned(),
status,
application_status: format!(
"{:?}",
record
.application_state
.expect("application state must exist for withdrawal records, not None; qed")
),
memo: String::from_utf8_lossy(&record.ext).into_owned(),
}
}
Expand Down
2 changes: 2 additions & 0 deletions runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ xr-primitives = { path = "../xr-primitives", default-features = false }
xsupport = { package = "xrml-xsupport", path = "../xrml/xsupport", default-features = false }
xassets = { package = "xrml-xassets-assets", path = "../xrml/xassets/assets", default-features = false }
xrecords = { package = "xrml-xassets-records", path = "../xrml/xassets/records", default-features = false }
xprocess = { package = "xrml-xassets-process", path = "../xrml/xassets/process", default-features = false }
xspot= { package = "xrml-xdex-spot", path = "../xrml/xdex/spot", default-features = false }
xbridge-common = { package = "xrml-xbridge-common", path = "../xrml/xbridge/common", default-features = false }
xbitcoin = { package = "xrml-xbridge-bitcoin", path = "../xrml/xbridge/bitcoin", default-features = false }
Expand All @@ -34,6 +35,7 @@ std = [
"xsupport/std",
"xassets/std",
"xrecords/std",
"xprocess/std",
"xspot/std",
"xbridge-common/std",
"xbitcoin/std",
Expand Down
3 changes: 2 additions & 1 deletion runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod xassets_api {
use super::*;
use rstd::collections::btree_map::BTreeMap;
use xassets::{Asset, AssetType, Memo, Token};
use xprocess::WithdrawalLimit;
use xr_primitives::AddrStr;

decl_runtime_apis! {
Expand All @@ -23,7 +24,7 @@ pub mod xassets_api {
fn withdrawal_list_of(chain: xassets::Chain) -> Vec<xrecords::RecordInfo<AccountIdForApi, Balance, BlockNumber, Timestamp>>;
fn deposit_list_of(chain: xassets::Chain) -> Vec<xrecords::RecordInfo<AccountIdForApi, Balance, BlockNumber, Timestamp>>;
fn verify_address(token: Token, addr: AddrStr, ext: Memo) -> Result<(), Vec<u8>>;
fn minimal_withdrawal_value(token: Token) -> Option<Balance>;
fn withdrawal_limit(token: Token) -> Option<WithdrawalLimit<Balance>>;
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub use xassets;
pub use xbitcoin;
pub use xbridge_common;
pub use xbridge_features;
pub use xprocess;

use xbridge_common::types::{GenericAllSessionInfo, GenericTrusteeIntentionProps};

Expand Down Expand Up @@ -403,8 +404,8 @@ impl_runtime_apis! {
XAssetsProcess::verify_address(token, addr, ext).map_err(|e| e.as_bytes().to_vec())
}

fn minimal_withdrawal_value(token: xassets::Token) -> Option<Balance> {
XAssetsProcess::minimal_withdrawal_value(&token)
fn withdrawal_limit(token: xassets::Token) -> Option<xprocess::WithdrawalLimit<Balance>> {
XAssetsProcess::withdrawal_limit(&token)
}
}

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.
26 changes: 22 additions & 4 deletions xrml/xassets/process/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
mod mock;
mod tests;

use parity_codec::{Decode, Encode};
#[cfg(feature = "std")]
use serde_derive::{Deserialize, Serialize};

// Substrate
use primitives::traits::As;
use rstd::prelude::Vec;
Expand All @@ -20,6 +24,14 @@ use xr_primitives::AddrStr;
use xsupport::token;
use xsupport::{debug, warn};

#[derive(PartialEq, Eq, Clone, Encode, Decode, Default)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
pub struct WithdrawalLimit<Balance> {
pub minimal_withdrawal: Balance,
pub fee: Balance,
}

pub trait Trait: xassets::Trait + xrecords::Trait + xbitcoin::Trait {}

decl_module! {
Expand All @@ -37,8 +49,9 @@ decl_module! {

Self::verify_addr(&token, &addr, &ext)?;

let min = Self::minimal_withdrawal_value(&token).expect("all token should has minimal withdrawal value");
if value <= min {
let limit = Self::withdrawal_limit(&token).ok_or("token should has withdrawal limit")?;
// withdrawal value should larger than minimal_withdrawal, allow equal
if value < limit.minimal_withdrawal {
return Err("withdrawal value should larger than requirement")
}

Expand Down Expand Up @@ -86,10 +99,15 @@ impl<T: Trait> Module<T> {
Self::verify_addr(&token, &addr, &ext)
}

pub fn minimal_withdrawal_value(token: &Token) -> Option<T::Balance> {
pub fn withdrawal_limit(token: &Token) -> Option<WithdrawalLimit<T::Balance>> {
match token.as_slice() {
<xbitcoin::Module<T> as ChainT>::TOKEN => {
Some(As::sa(xbitcoin::Module::<T>::btc_withdrawal_fee()))
let fee = As::sa(xbitcoin::Module::<T>::btc_withdrawal_fee());
let limit = WithdrawalLimit::<T::Balance> {
minimal_withdrawal: fee * As::sa(3) / As::sa(2),
fee,
};
Some(limit)
}
_ => None,
}
Expand Down
2 changes: 2 additions & 0 deletions xrml/xassets/records/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,6 @@ pub struct RecordInfo<AccountId, Balance, BlockNumber: Default, Timestamp> {
pub withdrawal_id: u32, // only for withdrawal
/// tx state
pub state: TxState,
/// application state
pub application_state: Option<ApplicationState>,
}
10 changes: 8 additions & 2 deletions xrml/xbridge/bitcoin/src/assets_records.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl<T: Trait> Module<T> {
height_or_time: HeightOrTime::<T::BlockNumber, T::Moment>::Height(appl.height()),
withdrawal_id: appl.id(), // only for withdrawal
state: TxState::Applying,
application_state: Some(appl.state()),
})
.collect::<Vec<_>>();

Expand Down Expand Up @@ -90,8 +91,12 @@ impl<T: Trait> Module<T> {
record.state = match proposal.sig_state {
VoteResult::Unfinish => TxState::Signing,
VoteResult::Finish => {
record.txid = tx_hash.clone();
TxState::Confirming(tx_confirmed, confirmations)
if tx_hash.len() != 0 {
record.txid = tx_hash.clone();
TxState::Confirming(tx_confirmed, confirmations)
} else {
TxState::Broadcasting
}
}
};
}
Expand Down Expand Up @@ -154,6 +159,7 @@ impl<T: Trait> Module<T> {
),
withdrawal_id: 0, // only for withdrawal
state,
application_state: None,
};
records.push(info);
}
Expand Down

0 comments on commit 206af6c

Please sign in to comment.