Skip to content

Commit

Permalink
Stage/test fix assets rpc (paritytech#219)
Browse files Browse the repository at this point in the history
* let rpc assets return all assets info
  • Loading branch information
Aton authored Jan 17, 2019
1 parent 732dfda commit caae7c8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
27 changes: 13 additions & 14 deletions rpc/src/chainx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ mod types;
use self::error::Result;
use self::types::{
ApplicationWrapper, AssetInfo, CertInfo, IntentionInfo, PageData, PairInfo, PseduIntentionInfo,
PseduNominationRecord, QuotationsList, WithdrawalState,
PseduNominationRecord, QuotationsList, TotalAssetInfo, WithdrawalState,
};
use chainx::error::ErrorKind::{OrderPairIDErr, PageIndexErr, PageSizeErr, QuotationssPieceErr};

Expand All @@ -67,7 +67,7 @@ build_rpc_trait! {
fn assets_of(&self, AccountId, u32, u32) -> Result<Option<PageData<AssetInfo>>>;

#[rpc(name = "chainx_getAssets")]
fn assets(&self, u32, u32) -> Result<Option<PageData<AssetInfo>>>;
fn assets(&self, u32, u32) -> Result<Option<PageData<TotalAssetInfo>>>;

#[rpc(name = "chainx_getVerifyAddress")]
fn verify_addr(&self, xassets::Token, xrecords::AddrStr, xassets::Memo) -> Result<Option<Vec<u8>>>;
Expand Down Expand Up @@ -349,7 +349,7 @@ where
into_pagedata(assets, page_index, page_size)
}

fn assets(&self, page_index: u32, page_size: u32) -> Result<Option<PageData<AssetInfo>>> {
fn assets(&self, page_index: u32, page_size: u32) -> Result<Option<PageData<TotalAssetInfo>>> {
let b = self.best_number()?;
let tokens: Result<Vec<Token>> = self
.client
Expand All @@ -372,14 +372,17 @@ where
bmap.extend(info.0.iter());
}

let asset = Self::get_asset(&state, &token)?;
let is_native = match asset {
Some(info) => match info.chain() {
Chain::ChainX => true,
_ => false,
},
let asset = match Self::get_asset(&state, &token)? {
Some(info) => info,
None => unreachable!("should not reach this branch, the token info must be exists"),
};
// let (is_native , asset) = match asset {
// Some(info) => match info.chain() {
// Chain::ChainX => (true, info),
// _ => (false, info),
// },
// None => unreachable!("should not reach this branch, the token info must be exists"),
// };

// PCX free balance
if token.as_slice() == xassets::Module::<Runtime>::TOKEN {
Expand All @@ -393,11 +396,7 @@ where
bmap.insert(xassets::AssetType::Free, free_issue);
}

assets.push(AssetInfo {
name: String::from_utf8_lossy(&token).into_owned(),
is_native,
details: CodecBTreeMap(bmap),
});
assets.push(TotalAssetInfo::new(asset, CodecBTreeMap(bmap)));
}

into_pagedata(assets, page_index, page_size)
Expand Down
28 changes: 28 additions & 0 deletions rpc/src/chainx/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,34 @@ pub struct AssetInfo {
pub details: CodecBTreeMap<AssetType, Balance>,
}

#[derive(Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TotalAssetInfo {
name: String,
is_native: bool,
chain: Chain,
precision: u16,
desc: String,
details: CodecBTreeMap<AssetType, Balance>,
}

impl TotalAssetInfo {
pub fn new(asset: Asset, details: CodecBTreeMap<AssetType, Balance>) -> TotalAssetInfo {
TotalAssetInfo {
name: String::from_utf8_lossy(&asset.token()).into_owned(),
is_native: if asset.chain() == Chain::ChainX {
true
} else {
false
},
chain: asset.chain(),
precision: asset.precision(),
desc: String::from_utf8_lossy(&asset.desc()).into_owned(),
details,
}
}
}

/// Intention info
#[derive(Debug, Default, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
Expand Down
Binary file not shown.
1 change: 1 addition & 0 deletions xrml/xassets/records/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ mod tests;
use codec::Codec;
use rstd::prelude::*;
use runtime_support::dispatch::Result;

use runtime_support::StorageValue;

use xr_primitives::XString;
Expand Down

0 comments on commit caae7c8

Please sign in to comment.