-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add rpc to query user rewards (#878)
* add rewards rpc * working rpc * fixrewards rpc
- Loading branch information
Showing
13 changed files
with
298 additions
and
22 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,20 @@ | ||
[package] | ||
name = "pallet-rewards-rpc" | ||
version = { workspace = true } | ||
authors = { workspace = true } | ||
edition = { workspace = true } | ||
homepage = { workspace = true } | ||
license = { workspace = true } | ||
repository = { workspace = true } | ||
|
||
[package.metadata.docs.rs] | ||
targets = ["x86_64-unknown-linux-gnu"] | ||
|
||
[dependencies] | ||
jsonrpsee = { workspace = true, features = ["client-core", "server", "macros"] } | ||
pallet-rewards-rpc-runtime-api = { path = "./runtime-api", default-features = false } | ||
parity-scale-codec = { workspace = true } | ||
sp-api = { workspace = true } | ||
sp-blockchain = { workspace = true } | ||
sp-runtime = { workspace = true } | ||
tangle-primitives = { workspace = true } |
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 @@ | ||
[package] | ||
name = "pallet-rewards-rpc-runtime-api" | ||
version = { workspace = true } | ||
authors = { workspace = true } | ||
edition = { workspace = true } | ||
homepage = { workspace = true } | ||
license = { workspace = true } | ||
repository = { workspace = true } | ||
|
||
[package.metadata.docs.rs] | ||
targets = ["x86_64-unknown-linux-gnu"] | ||
|
||
[dependencies] | ||
parity-scale-codec = { version = "3.6.12", default-features = false, features = ["derive"] } | ||
sp-api = { workspace = true } | ||
sp-runtime = { workspace = true } | ||
sp-std = { workspace = true } | ||
tangle-primitives = { workspace = true, default-features = false } | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"parity-scale-codec/std", | ||
"sp-api/std", | ||
"sp-runtime/std", | ||
"tangle-primitives/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// This file is part of Tangle. | ||
// Copyright (C) 2022-2024 Tangle Foundation. | ||
// | ||
// Tangle 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. | ||
// | ||
// Tangle 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 Tangle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
//! Runtime API definition for rewards pallet. | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
#![allow(clippy::type_complexity)] | ||
use parity_scale_codec::Codec; | ||
use sp_runtime::{traits::MaybeDisplay, Serialize}; | ||
|
||
pub type BlockNumberOf<Block> = | ||
<<Block as sp_runtime::traits::HeaderProvider>::HeaderT as sp_runtime::traits::Header>::Number; | ||
|
||
sp_api::decl_runtime_apis! { | ||
pub trait RewardsApi<AccountId, AssetId, Balance> | ||
where | ||
AccountId: Codec + MaybeDisplay + Serialize, | ||
AssetId: Codec + MaybeDisplay + Serialize, | ||
Balance: Codec + MaybeDisplay + Serialize, | ||
{ | ||
/// Query all the rewards that this operator is providing along with their blueprints. | ||
/// | ||
/// ## Arguments | ||
/// - `operator`: The operator account id. | ||
/// ## Return | ||
/// - [`RpcRewardsWithBlueprint`]: A list of rewards with their blueprints. | ||
fn query_user_rewards( | ||
account_id: AccountId, | ||
asset_id: tangle_primitives::services::Asset<AssetId> | ||
) -> Result< | ||
Balance, | ||
sp_runtime::DispatchError, | ||
>; | ||
} | ||
} |
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,110 @@ | ||
// This file is part of Tangle. | ||
// Copyright (C) 2022-2024 Tangle Foundation. | ||
// | ||
// Tangle 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. | ||
// | ||
// Tangle 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 Tangle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#![allow(clippy::unnecessary_mut_passed)] | ||
#![allow(clippy::type_complexity)] | ||
|
||
use jsonrpsee::{ | ||
core::RpcResult, | ||
proc_macros::rpc, | ||
types::error::{ErrorObject, ErrorObjectOwned}, | ||
}; | ||
use parity_scale_codec::Codec; | ||
use sp_api::ProvideRuntimeApi; | ||
use sp_blockchain::HeaderBackend; | ||
use sp_runtime::{ | ||
traits::{Block as BlockT, MaybeDisplay}, | ||
Serialize, | ||
}; | ||
use std::sync::Arc; | ||
use tangle_primitives::Balance; | ||
|
||
pub use pallet_rewards_rpc_runtime_api::RewardsApi as RewardsRuntimeApi; | ||
|
||
/// RewardsClient RPC methods. | ||
#[rpc(client, server)] | ||
pub trait RewardsApi<BlockHash, AccountId, AssetId> | ||
where | ||
AccountId: Codec + MaybeDisplay + core::fmt::Debug + Send + Sync + 'static + Serialize, | ||
AssetId: Codec + MaybeDisplay + core::fmt::Debug + Send + Sync + 'static + Serialize, | ||
{ | ||
#[method(name = "rewards_queryUserRewards")] | ||
fn query_user_rewards( | ||
&self, | ||
account_id: AccountId, | ||
asset_id: tangle_primitives::services::Asset<AssetId>, | ||
at: Option<BlockHash>, | ||
) -> RpcResult<Balance>; | ||
} | ||
|
||
/// Provides RPC methods to query a dispatchable's class, weight and fee. | ||
pub struct RewardsClient<C, P> { | ||
/// Shared reference to the client. | ||
client: Arc<C>, | ||
_marker: std::marker::PhantomData<P>, | ||
} | ||
|
||
impl<C, P> RewardsClient<C, P> { | ||
/// Creates a new instance of the RewardsClient Rpc helper. | ||
pub fn new(client: Arc<C>) -> Self { | ||
Self { client, _marker: Default::default() } | ||
} | ||
} | ||
|
||
impl<C, Block, AccountId, AssetId> RewardsApiServer<<Block as BlockT>::Hash, AccountId, AssetId> | ||
for RewardsClient<C, Block> | ||
where | ||
Block: BlockT, | ||
AccountId: Codec + MaybeDisplay + core::fmt::Debug + Send + Sync + 'static + Serialize, | ||
AssetId: Codec + MaybeDisplay + core::fmt::Debug + Send + Sync + 'static + Serialize, | ||
C: HeaderBackend<Block> + ProvideRuntimeApi<Block> + Send + Sync + 'static, | ||
C::Api: RewardsRuntimeApi<Block, AccountId, AssetId, Balance>, | ||
{ | ||
fn query_user_rewards( | ||
&self, | ||
account_id: AccountId, | ||
asset_id: tangle_primitives::services::Asset<AssetId>, | ||
at: Option<<Block as BlockT>::Hash>, | ||
) -> RpcResult<Balance> { | ||
let api = self.client.runtime_api(); | ||
let at = at.unwrap_or_else(|| self.client.info().best_hash); | ||
|
||
match api.query_user_rewards(at, account_id, asset_id) { | ||
Ok(Ok(res)) => Ok(res), | ||
Ok(Err(e)) => Err(map_err(format!("{:?}", e), "Unable to query user rewards")), | ||
Err(e) => Err(map_err(format!("{:?}", e), "Unable to query user rewards")), | ||
} | ||
} | ||
} | ||
|
||
fn map_err(error: impl ToString, desc: &'static str) -> ErrorObjectOwned { | ||
ErrorObject::owned(Error::RuntimeError.into(), desc, Some(error.to_string())) | ||
} | ||
|
||
/// Error type of this RPC api. | ||
#[derive(Debug)] | ||
pub enum Error { | ||
/// The call to runtime failed. | ||
RuntimeError, | ||
} | ||
|
||
impl From<Error> for i32 { | ||
fn from(e: Error) -> i32 { | ||
match e { | ||
Error::RuntimeError => 1, | ||
} | ||
} | ||
} |
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
Oops, something went wrong.