-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(rpc): Add RPC call to query inflation and ROI #3290
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "pallet-gear-staking-rewards-rpc" | ||
version = "1.0.0" | ||
authors.workspace = true | ||
edition.workspace = true | ||
license.workspace = true | ||
homepage = "https://gear-tech.io" | ||
repository = "https://github.com/gear-tech/gear" | ||
|
||
[dependencies] | ||
jsonrpsee = { workspace = true, features = ["server", "macros"] } | ||
|
||
# Substrate packages | ||
sp-api.workspace = true | ||
sp-blockchain.workspace = true | ||
sp-runtime.workspace = true | ||
|
||
# Local packages | ||
pallet-gear-staking-rewards-rpc-runtime-api.workspace = true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "pallet-gear-staking-rewards-rpc-runtime-api" | ||
version = "1.0.0" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
authors.workspace = true | ||
edition.workspace = true | ||
license.workspace = true | ||
homepage = "https://gear-tech.io" | ||
repository = "https://github.com/gear-tech/gear" | ||
|
||
[dependencies] | ||
sp-api.workspace = true | ||
pallet-gear-staking-rewards.workspace = true | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"sp-api/std", | ||
"pallet-gear-staking-rewards/std", | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// This file is part of Gear. | ||
|
||
// Copyright (C) 2021-2023 Gear Technologies Inc. | ||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
pub use pallet_gear_staking_rewards::InflationInfo; | ||
|
||
sp_api::decl_runtime_apis! { | ||
pub trait GearStakingRewardsApi { | ||
/// Calculate token economics related data. | ||
fn inflation_info() -> InflationInfo; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// This file is part of Gear. | ||
|
||
// Copyright (C) 2021-2023 Gear Technologies Inc. | ||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
//! RPC interface for the gear module. | ||
|
||
use jsonrpsee::{ | ||
core::{Error as JsonRpseeError, RpcResult}, | ||
proc_macros::rpc, | ||
types::error::{CallError, ErrorObject}, | ||
}; | ||
pub use pallet_gear_staking_rewards_rpc_runtime_api::GearStakingRewardsApi as GearStakingRewardsRuntimeApi; | ||
use pallet_gear_staking_rewards_rpc_runtime_api::InflationInfo; | ||
use sp_api::ProvideRuntimeApi; | ||
use sp_blockchain::HeaderBackend; | ||
use sp_runtime::traits::Block as BlockT; | ||
use std::sync::Arc; | ||
|
||
#[rpc(server)] | ||
pub trait GearStakingRewardsApi<BlockHash, ResponseType> { | ||
#[method(name = "stakingRewards_inflationInfo")] | ||
fn query_inflation_info(&self, at: Option<BlockHash>) -> RpcResult<ResponseType>; | ||
} | ||
|
||
/// Provides RPC methods to query token economics related data. | ||
pub struct GearStakingRewards<C, P> { | ||
/// Shared reference to the client. | ||
client: Arc<C>, | ||
_marker: std::marker::PhantomData<P>, | ||
} | ||
|
||
impl<C, P> GearStakingRewards<C, P> { | ||
/// Creates a new instance of the GearStakingRewards Rpc helper. | ||
pub fn new(client: Arc<C>) -> Self { | ||
Self { | ||
client, | ||
_marker: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
/// 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 i32 { | ||
fn from(e: Error) -> i32 { | ||
match e { | ||
Error::RuntimeError => 1, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why? |
||
Error::DecodeError => 2, | ||
} | ||
} | ||
} | ||
|
||
impl<C, Block> GearStakingRewardsApiServer<<Block as BlockT>::Hash, InflationInfo> | ||
for GearStakingRewards<C, Block> | ||
where | ||
Block: BlockT, | ||
C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>, | ||
C::Api: GearStakingRewardsRuntimeApi<Block>, | ||
{ | ||
fn query_inflation_info(&self, at: Option<Block::Hash>) -> RpcResult<InflationInfo> { | ||
let api = self.client.runtime_api(); | ||
let at_hash = at.unwrap_or_else(|| self.client.info().best_hash); | ||
|
||
fn map_err(err: impl std::fmt::Debug, desc: &'static str) -> JsonRpseeError { | ||
CallError::Custom(ErrorObject::owned( | ||
Error::RuntimeError.into(), | ||
desc, | ||
Some(format!("{err:?}")), | ||
)) | ||
.into() | ||
} | ||
|
||
api.inflation_info(at_hash) | ||
.map_err(|e| map_err(e, "Unable to query inflation info")) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.