-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
463 additions
and
0 deletions.
There are no files selected for viewing
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,7 @@ | ||
[editor.lsp] | ||
display-inlay-hints = true | ||
|
||
|
||
[keys.normal.space] | ||
"H" = ":toggle lsp.display-inlay-hints" | ||
|
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
15 changes: 15 additions & 0 deletions
15
pallets/department-funding/department-funding-rpc/Cargo.toml
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,15 @@ | ||
[package] | ||
name = "department-funding-rpc" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
jsonrpsee = { version = "0.16.2", features = ["client-core", "server", "macros"] } | ||
sc-rpc = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" } | ||
sp-api = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" } | ||
sc-rpc-api = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" } | ||
sp-blockchain = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" } | ||
sp-runtime = { default-features = false, version = "7.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" } | ||
department-funding-runtime-api = { default-features= false, path="../department-funding-runtime-api"} |
261 changes: 261 additions & 0 deletions
261
pallets/department-funding/department-funding-rpc/src/lib.rs
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,261 @@ | ||
use jsonrpsee::{ | ||
core::{Error as JsonRpseeError, RpcResult}, | ||
proc_macros::rpc, | ||
types::error::{CallError, ErrorCode, ErrorObject}, | ||
}; | ||
use department_funding_runtime_api::DepartmentFundingApi as DepartmentFundingRuntimeApi; | ||
use sp_api::codec::Codec; | ||
use sp_api::ProvideRuntimeApi; | ||
use sp_blockchain::HeaderBackend; | ||
use sp_runtime::traits::Block as BlockT; | ||
use std::sync::Arc; | ||
type ChallengePostId = u64; | ||
type DepartmentRequiredFundId = u64; | ||
|
||
#[rpc(client, server)] | ||
pub trait DepartmentFundingApi<BlockHash, AccountId> { | ||
#[method(name = "departmentfunding_challengerevidence")] | ||
fn get_challengers_evidence( | ||
&self, | ||
department_required_fund_id: DepartmentRequiredFundId, | ||
offset: u64, | ||
limit: u16, | ||
at: Option<BlockHash>, | ||
) -> RpcResult<Vec<ChallengePostId>>; | ||
#[method(name = "departmentfunding_evidenceperiodendblock")] | ||
fn get_evidence_period_end_block( | ||
&self, | ||
department_required_fund_id: DepartmentRequiredFundId, | ||
at: Option<BlockHash>, | ||
) -> RpcResult<Option<u32>>; | ||
#[method(name = "departmentfunding_stakingperiodendblock")] | ||
fn get_staking_period_end_block( | ||
&self, | ||
department_required_fund_id: DepartmentRequiredFundId, | ||
at: Option<BlockHash>, | ||
) -> RpcResult<Option<u32>>; | ||
#[method(name = "departmentfunding_drawingperiodend")] | ||
fn get_drawing_period_end( | ||
&self, | ||
department_required_fund_id: DepartmentRequiredFundId, | ||
at: Option<BlockHash>, | ||
) -> RpcResult<(u64, u64, bool)>; | ||
#[method(name = "departmentfunding_commitendblock")] | ||
fn get_commit_period_end_block( | ||
&self, | ||
department_required_fund_id: DepartmentRequiredFundId, | ||
at: Option<BlockHash>, | ||
) -> RpcResult<Option<u32>>; | ||
#[method(name = "departmentfunding_voteendblock")] | ||
fn get_vote_period_end_block( | ||
&self, | ||
department_required_fund_id: DepartmentRequiredFundId, | ||
at: Option<BlockHash>, | ||
) -> RpcResult<Option<u32>>; | ||
#[method(name = "departmentfunding_selectedjuror")] | ||
fn selected_as_juror( | ||
&self, | ||
department_required_fund_id: DepartmentRequiredFundId, | ||
who: AccountId, | ||
at: Option<BlockHash>, | ||
) -> RpcResult<bool>; | ||
} | ||
|
||
/// A struct that implements the `SumStorageApi`. | ||
pub struct DepartmentFunding<C, M> { | ||
// If you have more generics, no need to SumStorage<C, M, N, P, ...> | ||
// just use a tuple like SumStorage<C, (M, N, P, ...)> | ||
client: Arc<C>, | ||
_marker: std::marker::PhantomData<M>, | ||
} | ||
|
||
impl<C, M> DepartmentFunding<C, M> { | ||
/// Create new `SumStorage` instance with the given reference to the client. | ||
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, | ||
Error::DecodeError => 2, | ||
} | ||
} | ||
} | ||
|
||
|
||
impl<C, Block, AccountId> DepartmentFundingApiServer<<Block as BlockT>::Hash, AccountId> for DepartmentFunding<C, Block> | ||
where | ||
Block: BlockT, | ||
AccountId: Codec, | ||
C: Send + Sync + 'static, | ||
C: ProvideRuntimeApi<Block>, | ||
C: HeaderBackend<Block>, | ||
C::Api: DepartmentFundingRuntimeApi<Block, AccountId>, | ||
{ | ||
fn get_challengers_evidence( | ||
&self, | ||
department_required_fund_id: DepartmentRequiredFundId, | ||
offset: u64, | ||
limit: u16, | ||
at: Option<Block::Hash>, | ||
) -> RpcResult<Vec<ChallengePostId>> { | ||
let api = self.client.runtime_api(); | ||
let at = at.unwrap_or_else(|| | ||
// If the block hash is not supplied assume the best block. | ||
self.client.info().best_hash); | ||
|
||
let runtime_api_result = | ||
api.get_challengers_evidence(at, department_required_fund_id, offset, limit); | ||
fn map_err(error: impl ToString, desc: &'static str) -> CallError { | ||
CallError::Custom(ErrorObject::owned( | ||
Error::RuntimeError.into(), | ||
desc, | ||
Some(error.to_string()), | ||
)) | ||
} | ||
let res = runtime_api_result.map_err(|e| map_err(e, "Unable to query dispatch info."))?; | ||
Ok(res) | ||
} | ||
fn get_evidence_period_end_block( | ||
&self, | ||
department_required_fund_id: DepartmentRequiredFundId, | ||
at: Option<Block::Hash>, | ||
) -> RpcResult<Option<u32>> { | ||
let api = self.client.runtime_api(); | ||
let at = at.unwrap_or_else(|| | ||
// If the block hash is not supplied assume the best block. | ||
self.client.info().best_hash); | ||
|
||
let runtime_api_result = api.get_evidence_period_end_block(at, department_required_fund_id); | ||
fn map_err(error: impl ToString, desc: &'static str) -> CallError { | ||
CallError::Custom(ErrorObject::owned( | ||
Error::RuntimeError.into(), | ||
desc, | ||
Some(error.to_string()), | ||
)) | ||
} | ||
let res = runtime_api_result.map_err(|e| map_err(e, "Unable to query dispatch info."))?; | ||
Ok(res) | ||
} | ||
fn get_staking_period_end_block( | ||
&self, | ||
department_required_fund_id: DepartmentRequiredFundId, | ||
at: Option<Block::Hash>, | ||
) -> RpcResult<Option<u32>> { | ||
let api = self.client.runtime_api(); | ||
let at = at.unwrap_or_else(|| | ||
// If the block hash is not supplied assume the best block. | ||
self.client.info().best_hash); | ||
|
||
let runtime_api_result = api.get_staking_period_end_block(at, department_required_fund_id); | ||
fn map_err(error: impl ToString, desc: &'static str) -> CallError { | ||
CallError::Custom(ErrorObject::owned( | ||
Error::RuntimeError.into(), | ||
desc, | ||
Some(error.to_string()), | ||
)) | ||
} | ||
let res = runtime_api_result.map_err(|e| map_err(e, "Unable to query dispatch info."))?; | ||
Ok(res) | ||
} | ||
fn get_drawing_period_end( | ||
&self, | ||
department_required_fund_id: DepartmentRequiredFundId, | ||
at: Option<Block::Hash>, | ||
) -> RpcResult<(u64, u64, bool)> { | ||
let api = self.client.runtime_api(); | ||
let at = at.unwrap_or_else(|| | ||
// If the block hash is not supplied assume the best block. | ||
self.client.info().best_hash); | ||
|
||
let runtime_api_result = api.get_drawing_period_end(at, department_required_fund_id); | ||
fn map_err(error: impl ToString, desc: &'static str) -> CallError { | ||
CallError::Custom(ErrorObject::owned( | ||
Error::RuntimeError.into(), | ||
desc, | ||
Some(error.to_string()), | ||
)) | ||
} | ||
let res = runtime_api_result.map_err(|e| map_err(e, "Unable to query dispatch info."))?; | ||
Ok(res) | ||
} | ||
|
||
fn get_commit_period_end_block( | ||
&self, | ||
department_required_fund_id: DepartmentRequiredFundId, | ||
at: Option<Block::Hash>, | ||
) -> RpcResult<Option<u32>> { | ||
let api = self.client.runtime_api(); | ||
let at = at.unwrap_or_else(|| | ||
// If the block hash is not supplied assume the best block. | ||
self.client.info().best_hash); | ||
|
||
let runtime_api_result = api.get_commit_period_end_block(at, department_required_fund_id); | ||
fn map_err(error: impl ToString, desc: &'static str) -> CallError { | ||
CallError::Custom(ErrorObject::owned( | ||
Error::RuntimeError.into(), | ||
desc, | ||
Some(error.to_string()), | ||
)) | ||
} | ||
let res = runtime_api_result.map_err(|e| map_err(e, "Unable to query dispatch info."))?; | ||
Ok(res) | ||
} | ||
|
||
fn get_vote_period_end_block( | ||
&self, | ||
department_required_fund_id: DepartmentRequiredFundId, | ||
at: Option<Block::Hash>, | ||
) -> RpcResult<Option<u32>> { | ||
let api = self.client.runtime_api(); | ||
let at = at.unwrap_or_else(|| | ||
// If the block hash is not supplied assume the best block. | ||
self.client.info().best_hash); | ||
|
||
let runtime_api_result = api.get_vote_period_end_block(at, department_required_fund_id); | ||
fn map_err(error: impl ToString, desc: &'static str) -> CallError { | ||
CallError::Custom(ErrorObject::owned( | ||
Error::RuntimeError.into(), | ||
desc, | ||
Some(error.to_string()), | ||
)) | ||
} | ||
let res = runtime_api_result.map_err(|e| map_err(e, "Unable to query dispatch info."))?; | ||
Ok(res) | ||
} | ||
|
||
fn selected_as_juror( | ||
&self, | ||
department_required_fund_id: DepartmentRequiredFundId, | ||
who: AccountId, | ||
at: Option<Block::Hash>, | ||
) -> RpcResult<bool> { | ||
let api = self.client.runtime_api(); | ||
let at = at.unwrap_or_else(|| | ||
// If the block hash is not supplied assume the best block. | ||
self.client.info().best_hash); | ||
|
||
let runtime_api_result = api.selected_as_juror(at, department_required_fund_id, who); | ||
fn map_err(error: impl ToString, desc: &'static str) -> CallError { | ||
CallError::Custom(ErrorObject::owned( | ||
Error::RuntimeError.into(), | ||
desc, | ||
Some(error.to_string()), | ||
)) | ||
} | ||
let res = runtime_api_result.map_err(|e| map_err(e, "Unable to query dispatch info."))?; | ||
Ok(res) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
pallets/department-funding/department-funding-runtime-api/Cargo.toml
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,17 @@ | ||
[package] | ||
name = "department-funding-runtime-api" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
sp-api = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" } | ||
frame-support = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42"} | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"sp-api/std", | ||
"frame-support/std", | ||
] |
20 changes: 20 additions & 0 deletions
20
pallets/department-funding/department-funding-runtime-api/src/lib.rs
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 @@ | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
// use frame_support::sp_std::{vec::Vec}; | ||
// or | ||
use frame_support::sp_std::{prelude::*}; | ||
use sp_api::codec::Codec; | ||
type ChallengePostId = u64; | ||
type DepartmentRequiredFundId= u64; | ||
|
||
sp_api::decl_runtime_apis! { | ||
pub trait DepartmentFundingApi<AccountId> where AccountId: Codec { | ||
fn get_challengers_evidence(department_required_fund_id: DepartmentRequiredFundId, offset: u64, limit: u16) -> Vec<ChallengePostId>; | ||
fn get_evidence_period_end_block(department_required_fund_id: DepartmentRequiredFundId) -> Option<u32>; | ||
fn get_staking_period_end_block(department_required_fund_id: DepartmentRequiredFundId) -> Option<u32>; | ||
fn get_drawing_period_end(department_required_fund_id: DepartmentRequiredFundId) -> (u64, u64, bool); | ||
fn get_commit_period_end_block(department_required_fund_id: DepartmentRequiredFundId) -> Option<u32>; | ||
fn get_vote_period_end_block(department_required_fund_id: DepartmentRequiredFundId) -> Option<u32>; | ||
fn selected_as_juror(department_required_fund_id: DepartmentRequiredFundId, who: AccountId) -> bool; | ||
} | ||
} |
Oops, something went wrong.