-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reward pallet RPCs to estimate current reward, for both escrow and vault rewards. Co-authored-by: Gregory Hill <gregorydhill@outlook.com>
- Loading branch information
1 parent
8bf8392
commit 9f37048
Showing
19 changed files
with
327 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
authors = ["Interlay Ltd"] | ||
edition = "2021" | ||
name = "module-reward-rpc" | ||
version = '0.3.0' | ||
|
||
[dependencies] | ||
codec = { package = "parity-scale-codec", version = "3.0.0" } | ||
jsonrpsee = { version = "0.13.0", features = ["server", "macros"] } | ||
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24" } | ||
sp-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24" } | ||
sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24" } | ||
module-reward-rpc-runtime-api = { path = "runtime-api" } | ||
|
||
[dependencies.module-oracle-rpc-runtime-api] | ||
path = '../../oracle/rpc/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,23 @@ | ||
[package] | ||
authors = ["Interlay Ltd"] | ||
edition = "2021" | ||
name = "module-reward-rpc-runtime-api" | ||
version = '0.3.0' | ||
|
||
[dependencies] | ||
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive", "max-encoded-len"] } | ||
sp-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false } | ||
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false } | ||
|
||
[dependencies.module-oracle-rpc-runtime-api] | ||
default-features = false | ||
path = '../../../oracle/rpc/runtime-api' | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"codec/std", | ||
"frame-support/std", | ||
"sp-api/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,22 @@ | ||
//! Runtime API definition for the Reward Module. | ||
|
||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
use codec::Codec; | ||
use frame_support::dispatch::DispatchError; | ||
use module_oracle_rpc_runtime_api::BalanceWrapper; | ||
|
||
sp_api::decl_runtime_apis! { | ||
pub trait RewardApi<AccountId, VaultId, CurrencyId, Balance> where | ||
AccountId: Codec, | ||
VaultId: Codec, | ||
CurrencyId: Codec, | ||
Balance: Codec | ||
{ | ||
/// Get a given user's rewards due | ||
fn compute_escrow_reward(account_id: AccountId, currency_id: CurrencyId) -> Result<BalanceWrapper<Balance>, DispatchError>; | ||
|
||
/// Get a given vault's rewards due | ||
fn compute_vault_reward(vault_id: VaultId, currency_id: CurrencyId) -> Result<BalanceWrapper<Balance>, 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,117 @@ | ||
//! RPC interface for the Reward Module. | ||
|
||
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 sp_api::ProvideRuntimeApi; | ||
use sp_blockchain::HeaderBackend; | ||
use sp_runtime::{ | ||
generic::BlockId, | ||
traits::{Block as BlockT, MaybeDisplay, MaybeFromStr}, | ||
DispatchError, | ||
}; | ||
use std::sync::Arc; | ||
|
||
pub use module_reward_rpc_runtime_api::RewardApi as RewardRuntimeApi; | ||
|
||
#[rpc(client, server)] | ||
pub trait RewardApi<BlockHash, AccountId, VaultId, CurrencyId, Balance> | ||
where | ||
Balance: Codec + MaybeDisplay + MaybeFromStr, | ||
AccountId: Codec, | ||
VaultId: Codec, | ||
CurrencyId: Codec, | ||
{ | ||
#[method(name = "reward_computeEscrowReward")] | ||
fn compute_escrow_reward( | ||
&self, | ||
account_id: AccountId, | ||
currency_id: CurrencyId, | ||
at: Option<BlockHash>, | ||
) -> RpcResult<BalanceWrapper<Balance>>; | ||
|
||
#[method(name = "reward_computeVaultReward")] | ||
fn compute_vault_reward( | ||
&self, | ||
vault_id: VaultId, | ||
currency_id: CurrencyId, | ||
at: Option<BlockHash>, | ||
) -> RpcResult<BalanceWrapper<Balance>>; | ||
} | ||
|
||
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 [`RewardApi`]. | ||
pub struct Reward<C, B> { | ||
client: Arc<C>, | ||
_marker: std::marker::PhantomData<B>, | ||
} | ||
|
||
impl<C, B> Reward<C, B> { | ||
/// Create new `Reward` with the given reference to the client. | ||
pub fn new(client: Arc<C>) -> Self { | ||
Reward { | ||
client, | ||
_marker: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
fn handle_response<T, E: std::fmt::Debug>(result: Result<Result<T, DispatchError>, E>, msg: String) -> RpcResult<T> { | ||
result | ||
.map_err(|err| internal_err(format!("Runtime error: {:?}: {:?}", msg, err)))? | ||
.map_err(|err| internal_err(format!("Execution error: {:?}: {:?}", msg, err))) | ||
} | ||
|
||
#[async_trait] | ||
impl<C, Block, AccountId, VaultId, CurrencyId, Balance> | ||
RewardApiServer<<Block as BlockT>::Hash, AccountId, VaultId, CurrencyId, Balance> for Reward<C, Block> | ||
where | ||
Block: BlockT, | ||
C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>, | ||
C::Api: RewardRuntimeApi<Block, AccountId, VaultId, CurrencyId, Balance>, | ||
AccountId: Codec, | ||
VaultId: Codec, | ||
CurrencyId: Codec, | ||
Balance: Codec + MaybeDisplay + MaybeFromStr, | ||
{ | ||
fn compute_escrow_reward( | ||
&self, | ||
account_id: AccountId, | ||
currency_id: CurrencyId, | ||
at: Option<<Block as BlockT>::Hash>, | ||
) -> RpcResult<BalanceWrapper<Balance>> { | ||
let api = self.client.runtime_api(); | ||
let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); | ||
|
||
handle_response( | ||
api.compute_escrow_reward(&at, account_id, currency_id), | ||
"Unable to obtain the current reward".into(), | ||
) | ||
} | ||
|
||
fn compute_vault_reward( | ||
&self, | ||
vault_id: VaultId, | ||
currency_id: CurrencyId, | ||
at: Option<<Block as BlockT>::Hash>, | ||
) -> RpcResult<BalanceWrapper<Balance>> { | ||
let api = self.client.runtime_api(); | ||
let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); | ||
|
||
handle_response( | ||
api.compute_vault_reward(&at, vault_id, currency_id), | ||
"Unable to obtain the current reward".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
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
Oops, something went wrong.