Skip to content

Commit

Permalink
Impl Mining Asset RPC (paritytech#179)
Browse files Browse the repository at this point in the history
* Impl Mining Asset RPC

* .

* Rename XTypeAssetPowerMap to FixedAssetPowerOf
  • Loading branch information
liuchengxu authored Aug 11, 2020
1 parent 56fc1f6 commit c3430f6
Show file tree
Hide file tree
Showing 14 changed files with 430 additions and 18 deletions.
33 changes: 33 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ members = [
"xpallets/dex/spot/rpc",
"xpallets/dex/spot/rpc/runtime-api",
"xpallets/mining/asset",
"xpallets/mining/asset/rpc",
"xpallets/mining/asset/rpc/runtime-api",
"xpallets/mining/staking",
"xpallets/mining/staking/rpc",
"xpallets/mining/staking/rpc/runtime-api",
Expand Down
4 changes: 2 additions & 2 deletions primitives/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const MAXIMUM_MEMO_LEN: u8 = 128;
#[inline]
pub fn xss_check(input: &[u8]) -> DispatchResult {
if input.contains(&b'<') || input.contains(&b'>') {
Err(DispatchError::Other(
return Err(DispatchError::Other(
"'<' and '>' are not allowed, which could be abused off-chain.",
))?;
));
}
Ok(())
}
Expand Down
2 changes: 2 additions & 0 deletions rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ xpallet-dex-spot-rpc = { path = "../xpallets/dex/spot/rpc" }
xpallet-dex-spot-rpc-runtime-api = { path = "../xpallets/dex/spot/rpc/runtime-api" }
xpallet-mining-staking-rpc = { path = "../xpallets/mining/staking/rpc" }
xpallet-mining-staking-rpc-runtime-api = { path = "../xpallets/mining/staking/rpc/runtime-api" }
xpallet-mining-asset-rpc = { path = "../xpallets/mining/asset/rpc" }
xpallet-mining-asset-rpc-runtime-api = { path = "../xpallets/mining/asset/rpc/runtime-api" }
xpallet-contracts-rpc = { path = "../xpallets/contracts/rpc" }
xpallet-contracts-rpc-runtime-api = { path = "../xpallets/contracts/rpc/runtime-api" }
xpallet-gateway-records-rpc = { path = "../xpallets/gateway/records/rpc" }
Expand Down
11 changes: 11 additions & 0 deletions rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ where
xpallet_mining_staking_rpc_runtime_api::XStakingApi<Block, AccountId, Balance, BlockNumber>,
<Client<BE, E, Block, RA> as ProvideRuntimeApi<Block>>::Api:
xpallet_dex_spot_rpc_runtime_api::XSpotApi<Block, AccountId, Balance, BlockNumber, Balance>,
<Client<BE, E, Block, RA> as ProvideRuntimeApi<Block>>::Api:
xpallet_mining_asset_rpc_runtime_api::XMiningAssetApi<
Block,
AccountId,
Balance,
BlockNumber,
>,
<Client<BE, E, Block, RA> as ProvideRuntimeApi<Block>>::Api:
xpallet_gateway_records_rpc_runtime_api::XGatewayRecordsApi<
Block,
Expand All @@ -99,6 +106,7 @@ where
use xpallet_dex_spot_rpc::{XSpot, XSpotApi};
use xpallet_gateway_common_rpc::{XGatewayCommon, XGatewayCommonApi};
use xpallet_gateway_records_rpc::{XGatewayRecords, XGatewayRecordsApi};
use xpallet_mining_asset_rpc::{XMiningAsset, XMiningAssetApi};
use xpallet_mining_staking_rpc::{XStaking, XStakingApi};

let mut io = jsonrpc_core::IoHandler::default();
Expand All @@ -120,6 +128,9 @@ where
io.extend_with(ContractsApi::to_delegate(Contracts::new(client.clone())));
io.extend_with(XStakingApi::to_delegate(XStaking::new(client.clone())));
io.extend_with(XSpotApi::to_delegate(XSpot::new(client.clone())));
io.extend_with(XMiningAssetApi::to_delegate(XMiningAsset::new(
client.clone(),
)));
io.extend_with(XGatewayRecordsApi::to_delegate(XGatewayRecords::new(
client.clone(),
)));
Expand Down
2 changes: 2 additions & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ xpallet-dex-spot-rpc-runtime-api = { path = "../xpallets/dex/spot/rpc/runtime-ap
xpallet-mining-staking = { path = "../xpallets/mining/staking", default-features = false }
xpallet-mining-staking-rpc-runtime-api = { path = "../xpallets/mining/staking/rpc/runtime-api", default-features = false }
xpallet-mining-asset = { path = "../xpallets/mining/asset", default-features = false }
xpallet-mining-asset-rpc-runtime-api = { path = "../xpallets/mining/asset/rpc/runtime-api", default-features = false }

[build-dependencies]
wasm-builder-runner = { package = "substrate-wasm-builder-runner", git = "https://github.com/paritytech/substrate.git", tag = "v2.0.0-rc4", default-features = false }
Expand Down Expand Up @@ -156,4 +157,5 @@ std = [
"xpallet-mining-staking/std",
"xpallet-mining-staking-rpc-runtime-api/std",
"xpallet-mining-asset/std",
"xpallet-mining-asset-rpc-runtime-api/std",
]
15 changes: 15 additions & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use pallet_transaction_payment_rpc_runtime_api::RuntimeDispatchInfo;

use xpallet_contracts_rpc_runtime_api::ContractExecResult;
use xpallet_dex_spot::{Depth, FullPairInfo, RpcOrder, TradingPairId};
use xpallet_mining_asset::{MiningAssetInfo, RpcMinerLedger};
use xpallet_mining_staking::{RpcNominatorLedger, ValidatorInfo};
use xpallet_support::{RpcBalance, RpcPrice};

Expand Down Expand Up @@ -1036,6 +1037,20 @@ impl_runtime_apis! {
}
}

impl xpallet_mining_asset_rpc_runtime_api::XMiningAssetApi<Block, AccountId, Balance, BlockNumber> for Runtime {
fn mining_assets() -> Vec<MiningAssetInfo<AccountId, RpcBalance<Balance>, BlockNumber>> {
XMiningAsset::mining_assets()
}

fn mining_dividend(who: AccountId) -> BTreeMap<AssetId, RpcBalance<Balance>> {
XMiningAsset::mining_dividend(who)
}

fn miner_ledger(who: AccountId) -> BTreeMap<AssetId, RpcMinerLedger<BlockNumber>> {
XMiningAsset::miner_ledger(who)
}
}

impl xpallet_gateway_records_rpc_runtime_api::XGatewayRecordsApi<Block, AccountId, Balance, BlockNumber> for Runtime {
fn withdrawal_list() -> BTreeMap<u32, Withdrawal<AccountId, Balance, BlockNumber>> {
XGatewayRecords::withdrawal_list()
Expand Down
22 changes: 22 additions & 0 deletions xpallets/mining/asset/rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "xpallet-mining-asset-rpc"
version = "0.1.0"
authors = ["ChainX community <https://www.chainx.org>"]
edition = "2018"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "1.3.1" }
sp-api = { git = "https://github.com/paritytech/substrate.git", tag = "v2.0.0-rc4" }
sp-std = { git = "https://github.com/paritytech/substrate.git", tag = "v2.0.0-rc4" }
sp-runtime = { git = "https://github.com/paritytech/substrate.git", tag = "v2.0.0-rc4" }
sp-blockchain = { git = "https://github.com/paritytech/substrate.git", tag = "v2.0.0-rc4" }
jsonrpc-core = "14.2.0"
jsonrpc-derive = "14.2.1"
jsonrpc-core-client = "14.2.0"
xpallet-mining-asset = { path = "../" }
xpallet-mining-asset-rpc-runtime-api = { path = "./runtime-api" }
chainx-primitives = { path = "../../../../primitives" }
xpallet-support = { path = "../../../support" }
31 changes: 31 additions & 0 deletions xpallets/mining/asset/rpc/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[package]
name = "xpallet-mining-asset-rpc-runtime-api"
version = "0.1.0"
authors = ["ChainX community <https://www.chainx.org>"]
edition = "2018"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "1.3.1", default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate.git", tag = "v2.0.0-rc4", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate.git", tag = "v2.0.0-rc4", default-features = false }

chainx-primitives = { path = "../../../../../primitives", default-features = false }

xpallet-support = { path = "../../../../support", default-features = false }
xpallet-mining-asset = { path = "../../", default-features = false }

[features]
default = ["std"]
std = [
"codec/std",
"sp-api/std",
"sp-std/std",

"chainx-primitives/std",

"xpallet-mining-asset/std",
"xpallet-support/std",
]
28 changes: 28 additions & 0 deletions xpallets/mining/asset/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! Runtime API definition required by ChainX RPC extensions.

#![cfg_attr(not(feature = "std"), no_std)]

use chainx_primitives::AssetId;
use codec::Codec;
use sp_std::collections::btree_map::BTreeMap;
use sp_std::prelude::*;
use xpallet_mining_asset::{MiningAssetInfo, RpcMinerLedger};
use xpallet_support::RpcBalance;

sp_api::decl_runtime_apis! {
/// The API to query mining asset info.
pub trait XMiningAssetApi<AccountId, Balance, BlockNumber> where
AccountId: Codec,
Balance: Codec,
BlockNumber: Codec,
{
/// Get overall information about all mining assets.
fn mining_assets() -> Vec<MiningAssetInfo<AccountId, RpcBalance<Balance>, BlockNumber>>;

/// Get the asset mining dividends info given the asset miner AccountId.
fn mining_dividend(who: AccountId) -> BTreeMap<AssetId, RpcBalance<Balance>>;

/// Get the mining ledger details given the asset miner AccountId.
fn miner_ledger(who: AccountId) -> BTreeMap<AssetId, RpcMinerLedger<BlockNumber>>;
}
}
130 changes: 130 additions & 0 deletions xpallets/mining/asset/rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
//! RPC interface for the transaction payment module.

use chainx_primitives::AssetId;
use codec::Codec;
use jsonrpc_core::{Error as RpcError, ErrorCode, Result};
use jsonrpc_derive::rpc;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_runtime::{generic::BlockId, traits::Block as BlockT};
use sp_std::collections::btree_map::BTreeMap;
use std::sync::Arc;
use xpallet_mining_asset::{MiningAssetInfo, RpcMinerLedger};
use xpallet_mining_asset_rpc_runtime_api::XMiningAssetApi as XMiningAssetRuntimeApi;
use xpallet_support::RpcBalance;

/// XMiningAsset RPC methods.
#[rpc]
pub trait XMiningAssetApi<BlockHash, AccountId, RpcBalance, BlockNumber> {
/// Get overall information about all mining assets.
#[rpc(name = "xminingasset_getMiningAssets")]
fn mining_assets(
&self,
at: Option<BlockHash>,
) -> Result<Vec<MiningAssetInfo<AccountId, RpcBalance, BlockNumber>>>;

/// Get the asset mining dividends info given the asset miner AccountId.
#[rpc(name = "xminingasset_getDividendByAccount")]
fn mining_dividend(
&self,
who: AccountId,
at: Option<BlockHash>,
) -> Result<BTreeMap<AssetId, RpcBalance>>;

/// Get the mining ledger details given the asset miner AccountId.
#[rpc(name = "xminingasset_getMinerLedgerByAccount")]
fn miner_ledger(
&self,
who: AccountId,
at: Option<BlockHash>,
) -> Result<BTreeMap<AssetId, RpcMinerLedger<BlockNumber>>>;
}

/// A struct that implements the [`XMiningAssetApi`].
pub struct XMiningAsset<C, B> {
client: Arc<C>,
_marker: std::marker::PhantomData<B>,
}

impl<C, B> XMiningAsset<C, B> {
/// Create new `XMiningAsset` with the given reference to the client.
pub fn new(client: Arc<C>) -> Self {
Self {
client,
_marker: Default::default(),
}
}
}

impl<C, Block, AccountId, Balance, BlockNumber>
XMiningAssetApi<<Block as BlockT>::Hash, AccountId, RpcBalance<Balance>, BlockNumber>
for XMiningAsset<C, Block>
where
Block: BlockT,
C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
C::Api: XMiningAssetRuntimeApi<Block, AccountId, Balance, BlockNumber>,
AccountId: Codec,
Balance: Codec,
BlockNumber: Codec,
{
fn mining_assets(
&self,
at: Option<<Block as BlockT>::Hash>,
) -> Result<Vec<MiningAssetInfo<AccountId, RpcBalance<Balance>, BlockNumber>>> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));
Ok(api.mining_assets(&at).map_err(runtime_error_into_rpc_err)?)
}

fn mining_dividend(
&self,
who: AccountId,
at: Option<<Block as BlockT>::Hash>,
) -> Result<BTreeMap<AssetId, RpcBalance<Balance>>> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));
Ok(api
.mining_dividend(&at, who)
.map_err(runtime_error_into_rpc_err)?)
}

fn miner_ledger(
&self,
who: AccountId,
at: Option<<Block as BlockT>::Hash>,
) -> Result<BTreeMap<AssetId, RpcMinerLedger<BlockNumber>>> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));
Ok(api
.miner_ledger(&at, who)
.map_err(runtime_error_into_rpc_err)?)
}
}

/// Error type of this RPC api.
pub enum Error {
/// The transaction was not decodable.
DecodeError,
/// The call to runtime failed.
RuntimeError,
}

impl From<Error> for i64 {
fn from(e: Error) -> i64 {
match e {
Error::RuntimeError => 1,
Error::DecodeError => 2,
}
}
}

const RUNTIME_ERROR: i64 = 1;

/// Converts a runtime trap into an RPC error.
fn runtime_error_into_rpc_err(err: impl std::fmt::Debug) -> RpcError {
RpcError {
code: ErrorCode::ServerError(RUNTIME_ERROR),
message: "Runtime trapped".into(),
data: Some(format!("{:?}", err).into()),
}
}
Loading

0 comments on commit c3430f6

Please sign in to comment.