-
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.
Expose parachain-staking methods to RPC in all runtimes (#350)
* exposed get_unclaimed_staking_rewards and get_staking_rates as rpc calls * rollback on regenerating specs, fmt and fixes * modify author of parachain-staking rpc modules * bump down edition in new cargo.toml * merge with main, bump spacewalk versions * added untracked benchmarking file * Revert "added untracked benchmarking file" This reverts commit 0bec85d. * fix import
- Loading branch information
Showing
18 changed files
with
227 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
authors = ["Pendulum"] | ||
edition = "2021" | ||
name = "module-pallet-staking-rpc" | ||
version = "1.0.0" | ||
|
||
[dependencies] | ||
codec = {package = "parity-scale-codec", version = "3.0.0"} | ||
jsonrpsee = {version = "0.16.0", features = ["server", "macros"]} | ||
module-oracle-rpc-runtime-api = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "d05b0015d15ca39cc780889bcc095335e9862a36"} | ||
module-pallet-staking-rpc-runtime-api = {path = "runtime-api"} | ||
sp-api = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40"} | ||
sp-blockchain = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40"} | ||
sp-runtime = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40"} |
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,26 @@ | ||
[package] | ||
authors = ["Pendulum"] | ||
edition = "2021" | ||
name = "module-pallet-staking-rpc-runtime-api" | ||
version = "1.0.0" | ||
|
||
[dependencies] | ||
frame-support = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false} | ||
sp-api = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false} | ||
sp-std = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false} | ||
parity-scale-codec = {version = "3.1.5", default-features = false, features = ["derive"]} | ||
scale-info = {version = "2.1.1", default-features = false, features = ["derive"]} | ||
module-oracle-rpc-runtime-api = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "d05b0015d15ca39cc780889bcc095335e9862a36"} | ||
serde = {version = "1.0.142", default-features = false, optional = true, features = ["derive"]} | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"serde", | ||
"frame-support/std", | ||
"scale-info/std", | ||
"sp-api/std", | ||
"sp-std/std", | ||
"parity-scale-codec/std", | ||
"module-oracle-rpc-runtime-api/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,30 @@ | ||
//! Runtime API definition for Parachain Staking. | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
use frame_support::{scale_info::TypeInfo, sp_runtime::Perquintill}; | ||
use module_oracle_rpc_runtime_api::BalanceWrapper; | ||
use parity_scale_codec::{Codec, Decode, Encode, MaxEncodedLen}; | ||
use sp_std::fmt::Debug; | ||
|
||
#[cfg(feature = "std")] | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Decode, Encode, TypeInfo, MaxEncodedLen, PartialEq, Eq, Debug)] | ||
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] | ||
pub struct StakingRates { | ||
pub collator_staking_rate: Perquintill, | ||
pub collator_reward_rate: Perquintill, | ||
pub delegator_staking_rate: Perquintill, | ||
pub delegator_reward_rate: Perquintill, | ||
} | ||
|
||
sp_api::decl_runtime_apis! { | ||
pub trait ParachainStakingApi<AccountId, Balance> | ||
where | ||
AccountId: Codec, | ||
Balance: Codec | ||
{ | ||
fn get_unclaimed_staking_rewards(account: AccountId) -> BalanceWrapper<Balance>; | ||
fn get_staking_rates() -> StakingRates; | ||
} | ||
} |
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,85 @@ | ||
//! RPC interface for the parachain staking pallet. | ||
use codec::Codec; | ||
use jsonrpsee::{ | ||
core::{async_trait, Error as JsonRpseeError, RpcResult}, | ||
proc_macros::rpc, | ||
types::error::{CallError, ErrorCode, ErrorObject}, | ||
}; | ||
use module_oracle_rpc_runtime_api::BalanceWrapper; | ||
use module_pallet_staking_rpc_runtime_api::{ | ||
ParachainStakingApi as ParachainStakingRuntimeApi, StakingRates, | ||
}; | ||
use sp_api::ProvideRuntimeApi; | ||
use sp_blockchain::HeaderBackend; | ||
use sp_runtime::traits::{Block as BlockT, MaybeDisplay, MaybeFromStr}; | ||
use std::sync::Arc; | ||
|
||
#[rpc(client, server)] | ||
pub trait ParachainStakingApi<BlockHash, AccountId, Balance> | ||
where | ||
Balance: Codec + MaybeDisplay + MaybeFromStr, | ||
AccountId: Codec, | ||
{ | ||
#[method(name = "staking_getUnclaimedStakingRewards")] | ||
fn get_unclaimed_staking_rewards( | ||
&self, | ||
account: AccountId, | ||
at: Option<BlockHash>, | ||
) -> RpcResult<BalanceWrapper<Balance>>; | ||
|
||
#[method(name = "staking_getStakingRates")] | ||
fn get_staking_rates(&self, at: Option<BlockHash>) -> RpcResult<StakingRates>; | ||
} | ||
|
||
fn internal_err<T: ToString>(message: T) -> JsonRpseeError { | ||
JsonRpseeError::Call(CallError::Custom(ErrorObject::owned( | ||
ErrorCode::InternalError.code(), | ||
message.to_string(), | ||
None::<()>, | ||
))) | ||
} | ||
|
||
/// A struct that implements the [`Staking`]. | ||
pub struct Staking<C, B> { | ||
client: Arc<C>, | ||
_marker: std::marker::PhantomData<B>, | ||
} | ||
|
||
impl<C, B> Staking<C, B> { | ||
/// Create new `Staking` with the given reference to the client. | ||
pub fn new(client: Arc<C>) -> Self { | ||
Staking { client, _marker: Default::default() } | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl<C, Block, AccountId, Balance> | ||
ParachainStakingApiServer<<Block as BlockT>::Hash, AccountId, Balance> for Staking<C, Block> | ||
where | ||
Block: BlockT, | ||
C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>, | ||
C::Api: ParachainStakingRuntimeApi<Block, AccountId, Balance>, | ||
Balance: Codec + MaybeDisplay + MaybeFromStr, | ||
AccountId: Codec, | ||
{ | ||
fn get_unclaimed_staking_rewards( | ||
&self, | ||
account: AccountId, | ||
at: Option<<Block as BlockT>::Hash>, | ||
) -> RpcResult<BalanceWrapper<Balance>> { | ||
let api = self.client.runtime_api(); | ||
let at = at.unwrap_or_else(|| self.client.info().best_hash); | ||
|
||
api.get_unclaimed_staking_rewards(at, account) | ||
.map_err(|_e| internal_err(format!("Unable to get unclaimed staking rewards"))) | ||
} | ||
|
||
fn get_staking_rates(&self, at: Option<<Block as BlockT>::Hash>) -> RpcResult<StakingRates> { | ||
let api = self.client.runtime_api(); | ||
let at = at.unwrap_or_else(|| self.client.info().best_hash); | ||
|
||
api.get_staking_rates(at) | ||
.map_err(|_e| internal_err(format!("Unable to get staking rates"))) | ||
} | ||
} |
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 was deleted.
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
Oops, something went wrong.