forked from paritytech/substrate
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Impl Mining Asset RPC (paritytech#179)
* Impl Mining Asset RPC * . * Rename XTypeAssetPowerMap to FixedAssetPowerOf
- Loading branch information
1 parent
56fc1f6
commit c3430f6
Showing
14 changed files
with
430 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
} | ||
} |
Oops, something went wrong.