Skip to content

Commit

Permalink
department-funding-rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed Mar 26, 2024
1 parent d8ac784 commit ac67145
Show file tree
Hide file tree
Showing 8 changed files with 463 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .helix/config.toml
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"

23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ profile-validation-runtime-api = {path="../pallets/profile-validation/profile-va
profile-validation-rpc = { path="../pallets/profile-validation/profile-validation-rpc", default-features = false}


# Department funding rpc
department-funding-runtime-api = {path="../pallets/department-funding/department-funding-runtime-api", default-features = false}
department-funding-rpc= { path="../pallets/department-funding/department-funding-rpc", default-features = false}



[build-dependencies]
substrate-build-script-utils = { version = "3.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }

Expand Down
15 changes: 15 additions & 0 deletions pallets/department-funding/department-funding-rpc/Cargo.toml
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 pallets/department-funding/department-funding-rpc/src/lib.rs
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)
}
}
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",
]
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;
}
}
Loading

0 comments on commit ac67145

Please sign in to comment.