-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/uni-arts-chain/uni-arts-n…
…etwork into master
- Loading branch information
Showing
12 changed files
with
238 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "pallet-staking-rpc" | ||
version = "0.1.0" | ||
authors = ["yxf <[email protected]>"] | ||
edition = "2018" | ||
|
||
[package.metadata.docs.rs] | ||
targets = ["x86_64-unknown-linux-gnu"] | ||
|
||
[dependencies] | ||
codec = { package = "parity-scale-codec", version = "1.3.1" } | ||
jsonrpc-core = "15.0.0" | ||
jsonrpc-core-client = "15.0.0" | ||
jsonrpc-derive = "15.0.0" | ||
sp-core = { version = "2.0.0" } | ||
sp-rpc = { version = "2.0.0" } | ||
serde = { version = "1.0.101", features = ["derive"] } | ||
sp-runtime = { version = "2.0.0" } | ||
sp-api = { version = "2.0.0" } | ||
sp-blockchain = { version = "2.0.0" } | ||
pallet-staking-rpc-runtime-api = { version = "0.1.0", path = "./runtime-api" } |
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,29 @@ | ||
[package] | ||
name = "pallet-staking-rpc-runtime-api" | ||
version = "0.1.0" | ||
authors = ["yxf <[email protected]>"] | ||
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, features = ["derive"] } | ||
serde = { version = "1.0.101", optional = true, features = ["derive"] } | ||
sp-api = { version = "2.0.0", default-features = false } | ||
sp-std = { version = "2.0.0", default-features = false } | ||
sp-runtime = { version = "2.0.0", default-features = false } | ||
frame-support = { version = "2.0.0", default-features = false } | ||
|
||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"serde", | ||
"sp-api/std", | ||
"codec/std", | ||
"sp-std/std", | ||
"sp-runtime/std", | ||
"frame-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,38 @@ | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
use sp_std::prelude::*; | ||
use codec::{Encode, Decode}; | ||
#[cfg(feature = "std")] | ||
use serde::{Serialize, Deserialize, Serializer, Deserializer}; | ||
|
||
#[derive(Eq, PartialEq, Encode, Decode, Default)] | ||
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))] | ||
pub struct Reward<Balance>( | ||
#[cfg_attr(feature = "std", serde(bound(serialize = "Balance: std::fmt::Display")))] | ||
#[cfg_attr(feature = "std", serde(serialize_with = "serialize_as_string"))] | ||
#[cfg_attr(feature = "std", serde(bound(deserialize = "Balance: std::str::FromStr")))] | ||
#[cfg_attr(feature = "std", serde(deserialize_with = "deserialize_from_string"))] | ||
pub Balance | ||
); | ||
|
||
#[cfg(feature = "std")] | ||
fn serialize_as_string<S: Serializer, T: std::fmt::Display>(t: &T, serializer: S) -> Result<S::Ok, S::Error> { | ||
serializer.serialize_str(&t.to_string()) | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
fn deserialize_from_string<'de, D: Deserializer<'de>, T: std::str::FromStr>(deserializer: D) -> Result<T, D::Error> { | ||
let s = String::deserialize(deserializer)?; | ||
s.parse::<T>().map_err(|_| serde::de::Error::custom("Parse from string failed")) | ||
} | ||
|
||
sp_api::decl_runtime_apis! { | ||
pub trait StakingApi<AccountId, Balance> where | ||
AccountId: codec::Codec, | ||
Balance: codec::Codec | ||
{ | ||
fn staking_module_account_id() -> AccountId; | ||
fn pool_account_id(id: u32) -> AccountId; | ||
fn pending_rewards(pool_id: u32, account_id: AccountId) -> Balance; | ||
} | ||
} |
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,67 @@ | ||
use std::sync::Arc; | ||
use codec::{Codec}; | ||
use jsonrpc_derive::rpc; | ||
use jsonrpc_core::{Error as RpcError, ErrorCode, Result}; | ||
use sp_blockchain::HeaderBackend; | ||
use sp_runtime::{generic::BlockId, traits::{Block as BlockT, MaybeDisplay, MaybeFromStr}}; | ||
use sp_api::ProvideRuntimeApi; | ||
use pallet_staking_rpc_runtime_api::Reward; | ||
pub use pallet_staking_rpc_runtime_api::StakingApi as StakingRuntimeApi; | ||
|
||
#[rpc] | ||
pub trait StakingApi<AccountId, ResponseType> { | ||
#[rpc(name = "staking_pendingRewards")] | ||
fn pending_rewards( | ||
&self, | ||
account_id: AccountId | ||
) -> Result<ResponseType>; | ||
|
||
#[rpc(name = "staking_poolAccountId")] | ||
fn pool_account_id(&self) -> Result<AccountId>; | ||
} | ||
|
||
pub struct Staking<C, P> { | ||
client: Arc<C>, | ||
_marker: std::marker::PhantomData<P>, | ||
} | ||
|
||
impl<C, P> Staking<C, P> { | ||
pub fn new(client: Arc<C>) -> Self { | ||
Staking { client, _marker: Default::default() } | ||
} | ||
} | ||
|
||
|
||
impl<C, Block, AccountId, Balance> StakingApi<AccountId, Reward<Balance>> | ||
for Staking<C, Block> | ||
where | ||
Block: BlockT, | ||
C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>, | ||
C::Api: StakingRuntimeApi<Block, AccountId, Balance>, | ||
Balance: Codec + MaybeDisplay + MaybeFromStr + std::default::Default + std::fmt::Debug, | ||
AccountId: Codec | ||
{ | ||
|
||
fn pending_rewards(&self, account_id: AccountId) -> Result<Reward<Balance>> { | ||
let api = self.client.runtime_api(); | ||
let at = BlockId::hash(self.client.info().best_hash); | ||
api.pending_rewards(&at, 0, account_id).map_err(|e| RpcError { | ||
code: ErrorCode::InternalError, | ||
message: "Unable to query pending rewards".into(), | ||
data: Some(format!("{:?}", e).into()), | ||
}).map(|value| Reward(value)) | ||
|
||
// println!("balance = {:?}", balance); | ||
// Ok(Reward { value: Default::default() }) | ||
} | ||
|
||
fn pool_account_id(&self) -> Result<AccountId> { | ||
let api = self.client.runtime_api(); | ||
let at = BlockId::hash(self.client.info().best_hash); | ||
api.staking_module_account_id(&at).map_err(|e| RpcError { | ||
code: ErrorCode::InternalError, | ||
message: "Unable to query pool account_id".into(), | ||
data: Some(format!("{:?}", e).into()), | ||
}) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -21,4 +21,4 @@ std = [ | |
"sp-runtime/std", | ||
"sp-core/std", | ||
"sp-std/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
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