From 6bb55499e0b445c9d242eecc82358b6a0736d32b Mon Sep 17 00:00:00 2001 From: Amiya Behera Date: Thu, 4 Jul 2024 15:22:02 +0530 Subject: [PATCH] reputation score --- container-chains/nodes/simple/src/rpc.rs | 20 +- .../runtime-templates/simple/src/lib.rs | 195 +++++---- .../department-funding-rpc/src/lib.rs | 357 +++++++-------- .../positive-externality-rpc/src/lib.rs | 353 +++++++-------- .../profile-validation-rpc/src/lib.rs | 411 +++++++++--------- .../project-tips/project-tips-rpc/src/lib.rs | 359 +++++++-------- custom-pallets/shared-storage/src/lib.rs | 3 +- custom-pallets/shared-storage/src/types.rs | 119 +++++ 8 files changed, 974 insertions(+), 843 deletions(-) diff --git a/container-chains/nodes/simple/src/rpc.rs b/container-chains/nodes/simple/src/rpc.rs index 573c559..56a91ba 100644 --- a/container-chains/nodes/simple/src/rpc.rs +++ b/container-chains/nodes/simple/src/rpc.rs @@ -72,15 +72,15 @@ where C::Api: BlockBuilder, P: TransactionPool + Sync + Send + 'static, C::Api: profile_validation_runtime_api::ProfileValidationApi, - C::Api: department_funding_runtime_api::DepartmentFundingApi, - C::Api: positive_externality_runtime_api::PositiveExternalityApi, - C::Api: project_tips_runtime_api::ProjectTipsApi, + C::Api: department_funding_runtime_api::DepartmentFundingApi, + C::Api: positive_externality_runtime_api::PositiveExternalityApi, + C::Api: project_tips_runtime_api::ProjectTipsApi, { - use substrate_frame_rpc_system::{System, SystemApiServer}; use department_funding_rpc::DepartmentFundingApiServer; - use positive_externality_rpc::PositiveExternalityApiServer; - use profile_validation_rpc::ProfileValidationApiServer; - use project_tips_rpc::ProjectTipsApiServer; + use positive_externality_rpc::PositiveExternalityApiServer; + use profile_validation_rpc::ProfileValidationApiServer; + use project_tips_rpc::ProjectTipsApiServer; + use substrate_frame_rpc_system::{System, SystemApiServer}; let mut module = RpcExtension::new(()); let FullDeps { @@ -113,9 +113,9 @@ where } module.merge(profile_validation_rpc::ProfileValidation::new(client.clone()).into_rpc())?; - module.merge(department_funding_rpc::DepartmentFunding::new(client.clone()).into_rpc())?; - module.merge(positive_externality_rpc::PositiveExternality::new(client.clone()).into_rpc())?; - module.merge(project_tips_rpc::ProjectTips::new(client.clone()).into_rpc())?; + module.merge(department_funding_rpc::DepartmentFunding::new(client.clone()).into_rpc())?; + module.merge(positive_externality_rpc::PositiveExternality::new(client.clone()).into_rpc())?; + module.merge(project_tips_rpc::ProjectTips::new(client.clone()).into_rpc())?; Ok(module) } diff --git a/container-chains/runtime-templates/simple/src/lib.rs b/container-chains/runtime-templates/simple/src/lib.rs index 7991216..4330b8c 100644 --- a/container-chains/runtime-templates/simple/src/lib.rs +++ b/container-chains/runtime-templates/simple/src/lib.rs @@ -116,7 +116,6 @@ pub type SignedBlock = generic::SignedBlock; /// BlockId type as expected by this runtime. pub type BlockId = generic::BlockId; - pub type ChallengePostId = u64; pub type DepartmentRequiredFundId = u64; @@ -788,103 +787,103 @@ mod benches { impl_runtime_apis! { impl profile_validation_runtime_api::ProfileValidationApi for Runtime { - fn get_challengers_evidence(profile_user_account: AccountId, offset: u64, limit: u16) -> Vec { - ProfileValidation::get_challengers_evidence(profile_user_account, offset, limit) - } - - fn get_evidence_period_end_block(profile_user_account: AccountId) -> Option { - ProfileValidation::get_evidence_period_end_block(profile_user_account) - } - - fn get_staking_period_end_block(profile_user_account: AccountId) -> Option { - ProfileValidation::get_staking_period_end_block(profile_user_account) - } - fn get_drawing_period_end(profile_user_account: AccountId) -> (u64, u64, bool) { - ProfileValidation::get_drawing_period_end(profile_user_account) - } - fn get_commit_period_end_block(profile_user_account: AccountId) -> Option { - ProfileValidation::get_commit_period_end_block(profile_user_account) - } - - fn get_vote_period_end_block(profile_user_account: AccountId) -> Option { - ProfileValidation::get_vote_period_end_block(profile_user_account) - } - fn selected_as_juror(profile_user_account: AccountId, who: AccountId) -> bool { - ProfileValidation::selected_as_juror(profile_user_account, who) - } - } - - impl department_funding_runtime_api::DepartmentFundingApi for Runtime { - - fn get_evidence_period_end_block(department_required_fund_id: DepartmentRequiredFundId) -> Option { - DepartmentFunding::get_evidence_period_end_block(department_required_fund_id) - } - - fn get_staking_period_end_block(department_required_fund_id: DepartmentRequiredFundId) -> Option { - DepartmentFunding::get_staking_period_end_block(department_required_fund_id) - } - fn get_drawing_period_end(department_required_fund_id: DepartmentRequiredFundId) -> (u64, u64, bool) { - DepartmentFunding::get_drawing_period_end(department_required_fund_id) - } - fn get_commit_period_end_block(department_required_fund_id: DepartmentRequiredFundId) -> Option { - DepartmentFunding::get_commit_period_end_block(department_required_fund_id) - } - - fn get_vote_period_end_block(department_required_fund_id: DepartmentRequiredFundId) -> Option { - DepartmentFunding::get_vote_period_end_block(department_required_fund_id) - } - fn selected_as_juror(department_required_fund_id: DepartmentRequiredFundId, who: AccountId) -> bool { - DepartmentFunding::selected_as_juror(department_required_fund_id, who) - } - } - - impl positive_externality_runtime_api::PositiveExternalityApi for Runtime { - - fn get_evidence_period_end_block(user_to_calculate: AccountId) -> Option { - PositiveExternality::get_evidence_period_end_block(user_to_calculate) - } - - fn get_staking_period_end_block(user_to_calculate: AccountId) -> Option { - PositiveExternality::get_staking_period_end_block(user_to_calculate) - } - fn get_drawing_period_end(user_to_calculate: AccountId) -> (u64, u64, bool) { - PositiveExternality::get_drawing_period_end(user_to_calculate) - } - fn get_commit_period_end_block(user_to_calculate: AccountId) -> Option { - PositiveExternality::get_commit_period_end_block(user_to_calculate) - } - - fn get_vote_period_end_block(user_to_calculate: AccountId) -> Option { - PositiveExternality::get_vote_period_end_block(user_to_calculate) - } - fn selected_as_juror(user_to_calculate: AccountId, who: AccountId) -> bool { - PositiveExternality::selected_as_juror(user_to_calculate, who) - } - } - - impl project_tips_runtime_api::ProjectTipsApi for Runtime { - - fn get_evidence_period_end_block(project_id: ProjectId) -> Option { - ProjectTips::get_evidence_period_end_block(project_id) - } - - fn get_staking_period_end_block(project_id: ProjectId) -> Option { - ProjectTips::get_staking_period_end_block(project_id) - } - fn get_drawing_period_end(project_id: ProjectId) -> (u64, u64, bool) { - ProjectTips::get_drawing_period_end(project_id) - } - fn get_commit_period_end_block(project_id: ProjectId) -> Option { - ProjectTips::get_commit_period_end_block(project_id) - } - - fn get_vote_period_end_block(project_id: ProjectId) -> Option { - ProjectTips::get_vote_period_end_block(project_id) - } - fn selected_as_juror(project_id: ProjectId, who: AccountId) -> bool { - ProjectTips::selected_as_juror(project_id, who) - } - } + fn get_challengers_evidence(profile_user_account: AccountId, offset: u64, limit: u16) -> Vec { + ProfileValidation::get_challengers_evidence(profile_user_account, offset, limit) + } + + fn get_evidence_period_end_block(profile_user_account: AccountId) -> Option { + ProfileValidation::get_evidence_period_end_block(profile_user_account) + } + + fn get_staking_period_end_block(profile_user_account: AccountId) -> Option { + ProfileValidation::get_staking_period_end_block(profile_user_account) + } + fn get_drawing_period_end(profile_user_account: AccountId) -> (u64, u64, bool) { + ProfileValidation::get_drawing_period_end(profile_user_account) + } + fn get_commit_period_end_block(profile_user_account: AccountId) -> Option { + ProfileValidation::get_commit_period_end_block(profile_user_account) + } + + fn get_vote_period_end_block(profile_user_account: AccountId) -> Option { + ProfileValidation::get_vote_period_end_block(profile_user_account) + } + fn selected_as_juror(profile_user_account: AccountId, who: AccountId) -> bool { + ProfileValidation::selected_as_juror(profile_user_account, who) + } + } + + impl department_funding_runtime_api::DepartmentFundingApi for Runtime { + + fn get_evidence_period_end_block(department_required_fund_id: DepartmentRequiredFundId) -> Option { + DepartmentFunding::get_evidence_period_end_block(department_required_fund_id) + } + + fn get_staking_period_end_block(department_required_fund_id: DepartmentRequiredFundId) -> Option { + DepartmentFunding::get_staking_period_end_block(department_required_fund_id) + } + fn get_drawing_period_end(department_required_fund_id: DepartmentRequiredFundId) -> (u64, u64, bool) { + DepartmentFunding::get_drawing_period_end(department_required_fund_id) + } + fn get_commit_period_end_block(department_required_fund_id: DepartmentRequiredFundId) -> Option { + DepartmentFunding::get_commit_period_end_block(department_required_fund_id) + } + + fn get_vote_period_end_block(department_required_fund_id: DepartmentRequiredFundId) -> Option { + DepartmentFunding::get_vote_period_end_block(department_required_fund_id) + } + fn selected_as_juror(department_required_fund_id: DepartmentRequiredFundId, who: AccountId) -> bool { + DepartmentFunding::selected_as_juror(department_required_fund_id, who) + } + } + + impl positive_externality_runtime_api::PositiveExternalityApi for Runtime { + + fn get_evidence_period_end_block(user_to_calculate: AccountId) -> Option { + PositiveExternality::get_evidence_period_end_block(user_to_calculate) + } + + fn get_staking_period_end_block(user_to_calculate: AccountId) -> Option { + PositiveExternality::get_staking_period_end_block(user_to_calculate) + } + fn get_drawing_period_end(user_to_calculate: AccountId) -> (u64, u64, bool) { + PositiveExternality::get_drawing_period_end(user_to_calculate) + } + fn get_commit_period_end_block(user_to_calculate: AccountId) -> Option { + PositiveExternality::get_commit_period_end_block(user_to_calculate) + } + + fn get_vote_period_end_block(user_to_calculate: AccountId) -> Option { + PositiveExternality::get_vote_period_end_block(user_to_calculate) + } + fn selected_as_juror(user_to_calculate: AccountId, who: AccountId) -> bool { + PositiveExternality::selected_as_juror(user_to_calculate, who) + } + } + + impl project_tips_runtime_api::ProjectTipsApi for Runtime { + + fn get_evidence_period_end_block(project_id: ProjectId) -> Option { + ProjectTips::get_evidence_period_end_block(project_id) + } + + fn get_staking_period_end_block(project_id: ProjectId) -> Option { + ProjectTips::get_staking_period_end_block(project_id) + } + fn get_drawing_period_end(project_id: ProjectId) -> (u64, u64, bool) { + ProjectTips::get_drawing_period_end(project_id) + } + fn get_commit_period_end_block(project_id: ProjectId) -> Option { + ProjectTips::get_commit_period_end_block(project_id) + } + + fn get_vote_period_end_block(project_id: ProjectId) -> Option { + ProjectTips::get_vote_period_end_block(project_id) + } + fn selected_as_juror(project_id: ProjectId, who: AccountId) -> bool { + ProjectTips::selected_as_juror(project_id, who) + } + } impl sp_api::Core for Runtime { fn version() -> RuntimeVersion { diff --git a/custom-pallets/department-funding/department-funding-rpc/src/lib.rs b/custom-pallets/department-funding/department-funding-rpc/src/lib.rs index ea134fe..cc0349d 100644 --- a/custom-pallets/department-funding/department-funding-rpc/src/lib.rs +++ b/custom-pallets/department-funding/department-funding-rpc/src/lib.rs @@ -1,8 +1,8 @@ use department_funding_runtime_api::DepartmentFundingApi as DepartmentFundingRuntimeApi; use jsonrpsee::{ - core::{Error as JsonRpseeError, RpcResult}, - proc_macros::rpc, - types::error::{CallError, ErrorCode, ErrorObject}, + core::{Error as JsonRpseeError, RpcResult}, + proc_macros::rpc, + types::error::{CallError, ErrorCode, ErrorObject}, }; use parity_scale_codec::Codec; use sp_api::ProvideRuntimeApi; @@ -14,215 +14,218 @@ type DepartmentRequiredFundId = u64; #[rpc(client, server)] pub trait DepartmentFundingApi { - #[method(name = "departmentfunding_evidenceperiodendblock")] - fn get_evidence_period_end_block( - &self, - department_required_fund_id: DepartmentRequiredFundId, - at: Option, - ) -> RpcResult>; - #[method(name = "departmentfunding_stakingperiodendblock")] - fn get_staking_period_end_block( - &self, - department_required_fund_id: DepartmentRequiredFundId, - at: Option, - ) -> RpcResult>; - #[method(name = "departmentfunding_drawingperiodend")] - fn get_drawing_period_end( - &self, - department_required_fund_id: DepartmentRequiredFundId, - at: Option, - ) -> RpcResult<(u64, u64, bool)>; - #[method(name = "departmentfunding_commitendblock")] - fn get_commit_period_end_block( - &self, - department_required_fund_id: DepartmentRequiredFundId, - at: Option, - ) -> RpcResult>; - #[method(name = "departmentfunding_voteendblock")] - fn get_vote_period_end_block( - &self, - department_required_fund_id: DepartmentRequiredFundId, - at: Option, - ) -> RpcResult>; - #[method(name = "departmentfunding_selectedjuror")] - fn selected_as_juror( - &self, - department_required_fund_id: DepartmentRequiredFundId, - who: AccountId, - at: Option, - ) -> RpcResult; + #[method(name = "departmentfunding_evidenceperiodendblock")] + fn get_evidence_period_end_block( + &self, + department_required_fund_id: DepartmentRequiredFundId, + at: Option, + ) -> RpcResult>; + #[method(name = "departmentfunding_stakingperiodendblock")] + fn get_staking_period_end_block( + &self, + department_required_fund_id: DepartmentRequiredFundId, + at: Option, + ) -> RpcResult>; + #[method(name = "departmentfunding_drawingperiodend")] + fn get_drawing_period_end( + &self, + department_required_fund_id: DepartmentRequiredFundId, + at: Option, + ) -> RpcResult<(u64, u64, bool)>; + #[method(name = "departmentfunding_commitendblock")] + fn get_commit_period_end_block( + &self, + department_required_fund_id: DepartmentRequiredFundId, + at: Option, + ) -> RpcResult>; + #[method(name = "departmentfunding_voteendblock")] + fn get_vote_period_end_block( + &self, + department_required_fund_id: DepartmentRequiredFundId, + at: Option, + ) -> RpcResult>; + #[method(name = "departmentfunding_selectedjuror")] + fn selected_as_juror( + &self, + department_required_fund_id: DepartmentRequiredFundId, + who: AccountId, + at: Option, + ) -> RpcResult; } /// A struct that implements the `SumStorageApi`. pub struct DepartmentFunding { - // If you have more generics, no need to SumStorage - // just use a tuple like SumStorage - client: Arc, - _marker: std::marker::PhantomData, + // If you have more generics, no need to SumStorage + // just use a tuple like SumStorage + client: Arc, + _marker: std::marker::PhantomData, } impl DepartmentFunding { - /// Create new `SumStorage` instance with the given reference to the client. - pub fn new(client: Arc) -> Self { - Self { client, _marker: Default::default() } - } + /// Create new `SumStorage` instance with the given reference to the client. + pub fn new(client: Arc) -> 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, + /// The transaction was not decodable. + DecodeError, + /// The call to runtime failed. + RuntimeError, } impl From for i32 { - fn from(e: Error) -> i32 { - match e { - Error::RuntimeError => 1, - Error::DecodeError => 2, - } - } + fn from(e: Error) -> i32 { + match e { + Error::RuntimeError => 1, + Error::DecodeError => 2, + } + } } impl DepartmentFundingApiServer<::Hash, AccountId> - for DepartmentFunding + for DepartmentFunding where - Block: BlockT, - AccountId: Codec, - C: Send + Sync + 'static, - C: ProvideRuntimeApi, - C: HeaderBackend, - C::Api: DepartmentFundingRuntimeApi, + Block: BlockT, + AccountId: Codec, + C: Send + Sync + 'static, + C: ProvideRuntimeApi, + C: HeaderBackend, + C::Api: DepartmentFundingRuntimeApi, { - fn get_evidence_period_end_block( - &self, - department_required_fund_id: DepartmentRequiredFundId, - at: Option, - ) -> RpcResult> { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| + fn get_evidence_period_end_block( + &self, + department_required_fund_id: DepartmentRequiredFundId, + at: Option, + ) -> RpcResult> { + 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, - ) -> RpcResult> { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| + 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, + ) -> RpcResult> { + 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, - ) -> RpcResult<(u64, u64, bool)> { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| + 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, + ) -> 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, - ) -> RpcResult> { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| + 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, + ) -> RpcResult> { + 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, - ) -> RpcResult> { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| + 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, + ) -> RpcResult> { + 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, - ) -> RpcResult { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| + 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, + ) -> RpcResult { + 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) - } + 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) + } } diff --git a/custom-pallets/positive-externality/positive-externality-rpc/src/lib.rs b/custom-pallets/positive-externality/positive-externality-rpc/src/lib.rs index 5de7fda..35527e4 100644 --- a/custom-pallets/positive-externality/positive-externality-rpc/src/lib.rs +++ b/custom-pallets/positive-externality/positive-externality-rpc/src/lib.rs @@ -1,10 +1,10 @@ use jsonrpsee::{ - core::{Error as JsonRpseeError, RpcResult}, - proc_macros::rpc, - types::error::{CallError, ErrorCode, ErrorObject}, + core::{Error as JsonRpseeError, RpcResult}, + proc_macros::rpc, + types::error::{CallError, ErrorCode, ErrorObject}, }; -use positive_externality_runtime_api::PositiveExternalityApi as PositiveExternalityRuntimeApi; use parity_scale_codec::Codec; +use positive_externality_runtime_api::PositiveExternalityApi as PositiveExternalityRuntimeApi; use sp_api::ProvideRuntimeApi; use sp_blockchain::HeaderBackend; use sp_runtime::traits::Block as BlockT; @@ -12,215 +12,218 @@ use std::sync::Arc; #[rpc(client, server)] pub trait PositiveExternalityApi { - #[method(name = "positiveexternality_evidenceperiodendblock")] - fn get_evidence_period_end_block( - &self, - user_to_calculate: AccountId, - at: Option, - ) -> RpcResult>; - #[method(name = "positiveexternality_stakingperiodendblock")] - fn get_staking_period_end_block( - &self, - user_to_calculate: AccountId, - at: Option, - ) -> RpcResult>; - #[method(name = "positiveexternality_drawingperiodend")] - fn get_drawing_period_end( - &self, - user_to_calculate: AccountId, - at: Option, - ) -> RpcResult<(u64, u64, bool)>; - #[method(name = "positiveexternality_commitendblock")] - fn get_commit_period_end_block( - &self, - user_to_calculate: AccountId, - at: Option, - ) -> RpcResult>; - #[method(name = "positiveexternality_voteendblock")] - fn get_vote_period_end_block( - &self, - user_to_calculate: AccountId, - at: Option, - ) -> RpcResult>; - #[method(name = "positiveexternality_selectedjuror")] - fn selected_as_juror( - &self, - user_to_calculate: AccountId, - who: AccountId, - at: Option, - ) -> RpcResult; + #[method(name = "positiveexternality_evidenceperiodendblock")] + fn get_evidence_period_end_block( + &self, + user_to_calculate: AccountId, + at: Option, + ) -> RpcResult>; + #[method(name = "positiveexternality_stakingperiodendblock")] + fn get_staking_period_end_block( + &self, + user_to_calculate: AccountId, + at: Option, + ) -> RpcResult>; + #[method(name = "positiveexternality_drawingperiodend")] + fn get_drawing_period_end( + &self, + user_to_calculate: AccountId, + at: Option, + ) -> RpcResult<(u64, u64, bool)>; + #[method(name = "positiveexternality_commitendblock")] + fn get_commit_period_end_block( + &self, + user_to_calculate: AccountId, + at: Option, + ) -> RpcResult>; + #[method(name = "positiveexternality_voteendblock")] + fn get_vote_period_end_block( + &self, + user_to_calculate: AccountId, + at: Option, + ) -> RpcResult>; + #[method(name = "positiveexternality_selectedjuror")] + fn selected_as_juror( + &self, + user_to_calculate: AccountId, + who: AccountId, + at: Option, + ) -> RpcResult; } /// A struct that implements the `SumStorageApi`. pub struct PositiveExternality { - // If you have more generics, no need to SumStorage - // just use a tuple like SumStorage - client: Arc, - _marker: std::marker::PhantomData, + // If you have more generics, no need to SumStorage + // just use a tuple like SumStorage + client: Arc, + _marker: std::marker::PhantomData, } impl PositiveExternality { - /// Create new `SumStorage` instance with the given reference to the client. - pub fn new(client: Arc) -> Self { - Self { client, _marker: Default::default() } - } + /// Create new `SumStorage` instance with the given reference to the client. + pub fn new(client: Arc) -> 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, + /// The transaction was not decodable. + DecodeError, + /// The call to runtime failed. + RuntimeError, } impl From for i32 { - fn from(e: Error) -> i32 { - match e { - Error::RuntimeError => 1, - Error::DecodeError => 2, - } - } + fn from(e: Error) -> i32 { + match e { + Error::RuntimeError => 1, + Error::DecodeError => 2, + } + } } impl PositiveExternalityApiServer<::Hash, AccountId> - for PositiveExternality + for PositiveExternality where - Block: BlockT, - AccountId: Codec, - C: Send + Sync + 'static, - C: ProvideRuntimeApi, - C: HeaderBackend, - C::Api: PositiveExternalityRuntimeApi, + Block: BlockT, + AccountId: Codec, + C: Send + Sync + 'static, + C: ProvideRuntimeApi, + C: HeaderBackend, + C::Api: PositiveExternalityRuntimeApi, { - fn get_evidence_period_end_block( - &self, - user_to_calculate: AccountId, - at: Option, - ) -> RpcResult> { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| + fn get_evidence_period_end_block( + &self, + user_to_calculate: AccountId, + at: Option, + ) -> RpcResult> { + 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, user_to_calculate); - 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, - user_to_calculate: AccountId, - at: Option, - ) -> RpcResult> { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| + let runtime_api_result = api.get_evidence_period_end_block(at, user_to_calculate); + 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, + user_to_calculate: AccountId, + at: Option, + ) -> RpcResult> { + 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, user_to_calculate); - 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, - user_to_calculate: AccountId, - at: Option, - ) -> RpcResult<(u64, u64, bool)> { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| + let runtime_api_result = api.get_staking_period_end_block(at, user_to_calculate); + 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, + user_to_calculate: AccountId, + at: Option, + ) -> 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, user_to_calculate); - 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) - } + let runtime_api_result = api.get_drawing_period_end(at, user_to_calculate); + 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, - user_to_calculate: AccountId, - at: Option, - ) -> RpcResult> { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| + fn get_commit_period_end_block( + &self, + user_to_calculate: AccountId, + at: Option, + ) -> RpcResult> { + 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, user_to_calculate); - 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) - } + let runtime_api_result = api.get_commit_period_end_block(at, user_to_calculate); + 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, - user_to_calculate: AccountId, - at: Option, - ) -> RpcResult> { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| + fn get_vote_period_end_block( + &self, + user_to_calculate: AccountId, + at: Option, + ) -> RpcResult> { + 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, user_to_calculate); - 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) - } + let runtime_api_result = api.get_vote_period_end_block(at, user_to_calculate); + 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, - user_to_calculate: AccountId, - who: AccountId, - at: Option, - ) -> RpcResult { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| + fn selected_as_juror( + &self, + user_to_calculate: AccountId, + who: AccountId, + at: Option, + ) -> RpcResult { + 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, user_to_calculate, 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) - } + let runtime_api_result = api.selected_as_juror(at, user_to_calculate, 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) + } } diff --git a/custom-pallets/profile-validation/profile-validation-rpc/src/lib.rs b/custom-pallets/profile-validation/profile-validation-rpc/src/lib.rs index d566f97..2e8c557 100644 --- a/custom-pallets/profile-validation/profile-validation-rpc/src/lib.rs +++ b/custom-pallets/profile-validation/profile-validation-rpc/src/lib.rs @@ -1,10 +1,10 @@ use jsonrpsee::{ - core::{Error as JsonRpseeError, RpcResult}, - proc_macros::rpc, - types::error::{CallError, ErrorCode, ErrorObject}, + core::{Error as JsonRpseeError, RpcResult}, + proc_macros::rpc, + types::error::{CallError, ErrorCode, ErrorObject}, }; -use profile_validation_runtime_api::ProfileValidationApi as ProfileValidationRuntimeApi; use parity_scale_codec::Codec; +use profile_validation_runtime_api::ProfileValidationApi as ProfileValidationRuntimeApi; use sp_api::ProvideRuntimeApi; use sp_blockchain::HeaderBackend; use sp_runtime::traits::Block as BlockT; @@ -14,249 +14,252 @@ type ChallengePostId = u64; #[rpc(client, server)] pub trait ProfileValidationApi { - #[method(name = "profilevalidation_challengerevidence")] - fn get_challengers_evidence( - &self, - profile_user_account: AccountId, - offset: u64, - limit: u16, - at: Option, - ) -> RpcResult>; + #[method(name = "profilevalidation_challengerevidence")] + fn get_challengers_evidence( + &self, + profile_user_account: AccountId, + offset: u64, + limit: u16, + at: Option, + ) -> RpcResult>; - #[method(name = "profilevalidation_evidenceperiodendblock")] - fn get_evidence_period_end_block( - &self, - profile_user_account: AccountId, - at: Option, - ) -> RpcResult>; - #[method(name = "profilevalidation_stakingperiodendblock")] - fn get_staking_period_end_block( - &self, - profile_user_account: AccountId, - at: Option, - ) -> RpcResult>; - #[method(name = "profilevalidation_drawingperiodend")] - fn get_drawing_period_end( - &self, - profile_user_account: AccountId, - at: Option, - ) -> RpcResult<(u64, u64, bool)>; - #[method(name = "profilevalidation_commitendblock")] - fn get_commit_period_end_block( - &self, - profile_user_account: AccountId, - at: Option, - ) -> RpcResult>; - #[method(name = "profilevalidation_voteendblock")] - fn get_vote_period_end_block( - &self, - profile_user_account: AccountId, - at: Option, - ) -> RpcResult>; - #[method(name = "profilevalidation_selectedjuror")] - fn selected_as_juror( - &self, - profile_user_account: AccountId, - who: AccountId, - at: Option, - ) -> RpcResult; + #[method(name = "profilevalidation_evidenceperiodendblock")] + fn get_evidence_period_end_block( + &self, + profile_user_account: AccountId, + at: Option, + ) -> RpcResult>; + #[method(name = "profilevalidation_stakingperiodendblock")] + fn get_staking_period_end_block( + &self, + profile_user_account: AccountId, + at: Option, + ) -> RpcResult>; + #[method(name = "profilevalidation_drawingperiodend")] + fn get_drawing_period_end( + &self, + profile_user_account: AccountId, + at: Option, + ) -> RpcResult<(u64, u64, bool)>; + #[method(name = "profilevalidation_commitendblock")] + fn get_commit_period_end_block( + &self, + profile_user_account: AccountId, + at: Option, + ) -> RpcResult>; + #[method(name = "profilevalidation_voteendblock")] + fn get_vote_period_end_block( + &self, + profile_user_account: AccountId, + at: Option, + ) -> RpcResult>; + #[method(name = "profilevalidation_selectedjuror")] + fn selected_as_juror( + &self, + profile_user_account: AccountId, + who: AccountId, + at: Option, + ) -> RpcResult; } /// A struct that implements the `SumStorageApi`. pub struct ProfileValidation { - // If you have more generics, no need to SumStorage - // just use a tuple like SumStorage - client: Arc, - _marker: std::marker::PhantomData, + // If you have more generics, no need to SumStorage + // just use a tuple like SumStorage + client: Arc, + _marker: std::marker::PhantomData, } impl ProfileValidation { - /// Create new `SumStorage` instance with the given reference to the client. - pub fn new(client: Arc) -> Self { - Self { client, _marker: Default::default() } - } + /// Create new `SumStorage` instance with the given reference to the client. + pub fn new(client: Arc) -> 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, + /// The transaction was not decodable. + DecodeError, + /// The call to runtime failed. + RuntimeError, } impl From for i32 { - fn from(e: Error) -> i32 { - match e { - Error::RuntimeError => 1, - Error::DecodeError => 2, - } - } + fn from(e: Error) -> i32 { + match e { + Error::RuntimeError => 1, + Error::DecodeError => 2, + } + } } impl ProfileValidationApiServer<::Hash, AccountId> - for ProfileValidation + for ProfileValidation where - Block: BlockT, - AccountId: Codec, - C: Send + Sync + 'static, - C: ProvideRuntimeApi, - C: HeaderBackend, - C::Api: ProfileValidationRuntimeApi, + Block: BlockT, + AccountId: Codec, + C: Send + Sync + 'static, + C: ProvideRuntimeApi, + C: HeaderBackend, + C::Api: ProfileValidationRuntimeApi, { - fn get_challengers_evidence( - &self, - profile_user_account: AccountId, - offset: u64, - limit: u16, - at: Option, - ) -> RpcResult> { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| + fn get_challengers_evidence( + &self, + profile_user_account: AccountId, + offset: u64, + limit: u16, + at: Option, + ) -> RpcResult> { + 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, profile_user_account, 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) - } + let runtime_api_result = + api.get_challengers_evidence(at, profile_user_account, 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, - profile_user_account: AccountId, - at: Option, - ) -> RpcResult> { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| + fn get_evidence_period_end_block( + &self, + profile_user_account: AccountId, + at: Option, + ) -> RpcResult> { + 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, profile_user_account); - 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, - profile_user_account: AccountId, - at: Option, - ) -> RpcResult> { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| + let runtime_api_result = api.get_evidence_period_end_block(at, profile_user_account); + 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, + profile_user_account: AccountId, + at: Option, + ) -> RpcResult> { + 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, profile_user_account); - 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, - profile_user_account: AccountId, - at: Option, - ) -> RpcResult<(u64, u64, bool)> { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| + let runtime_api_result = api.get_staking_period_end_block(at, profile_user_account); + 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, + profile_user_account: AccountId, + at: Option, + ) -> 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, profile_user_account); - 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) - } + let runtime_api_result = api.get_drawing_period_end(at, profile_user_account); + 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, - profile_user_account: AccountId, - at: Option, - ) -> RpcResult> { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| + fn get_commit_period_end_block( + &self, + profile_user_account: AccountId, + at: Option, + ) -> RpcResult> { + 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, profile_user_account); - 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) - } + let runtime_api_result = api.get_commit_period_end_block(at, profile_user_account); + 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, - profile_user_account: AccountId, - at: Option, - ) -> RpcResult> { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| + fn get_vote_period_end_block( + &self, + profile_user_account: AccountId, + at: Option, + ) -> RpcResult> { + 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, profile_user_account); - 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) - } + let runtime_api_result = api.get_vote_period_end_block(at, profile_user_account); + 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, - profile_user_account: AccountId, - who: AccountId, - at: Option, - ) -> RpcResult { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| + fn selected_as_juror( + &self, + profile_user_account: AccountId, + who: AccountId, + at: Option, + ) -> RpcResult { + 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, profile_user_account, 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) - } + let runtime_api_result = api.selected_as_juror(at, profile_user_account, 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) + } } diff --git a/custom-pallets/project-tips/project-tips-rpc/src/lib.rs b/custom-pallets/project-tips/project-tips-rpc/src/lib.rs index d360516..0bdcf77 100644 --- a/custom-pallets/project-tips/project-tips-rpc/src/lib.rs +++ b/custom-pallets/project-tips/project-tips-rpc/src/lib.rs @@ -1,10 +1,10 @@ use jsonrpsee::{ - core::{Error as JsonRpseeError, RpcResult}, - proc_macros::rpc, - types::error::{CallError, ErrorCode, ErrorObject}, + core::{Error as JsonRpseeError, RpcResult}, + proc_macros::rpc, + types::error::{CallError, ErrorCode, ErrorObject}, }; -use project_tips_runtime_api::ProjectTipsApi as ProjectTipsRuntimeApi; use parity_scale_codec::Codec; +use project_tips_runtime_api::ProjectTipsApi as ProjectTipsRuntimeApi; use sp_api::ProvideRuntimeApi; use sp_blockchain::HeaderBackend; use sp_runtime::traits::Block as BlockT; @@ -14,215 +14,218 @@ type ProjectId = u64; #[rpc(client, server)] pub trait ProjectTipsApi { - #[method(name = "projecttips_evidenceperiodendblock")] - fn get_evidence_period_end_block( - &self, - project_id: ProjectId, - at: Option, - ) -> RpcResult>; - #[method(name = "projecttips_stakingperiodendblock")] - fn get_staking_period_end_block( - &self, - project_id: ProjectId, - at: Option, - ) -> RpcResult>; - #[method(name = "projecttips_drawingperiodend")] - fn get_drawing_period_end( - &self, - project_id: ProjectId, - at: Option, - ) -> RpcResult<(u64, u64, bool)>; - #[method(name = "projecttips_commitendblock")] - fn get_commit_period_end_block( - &self, - project_id: ProjectId, - at: Option, - ) -> RpcResult>; - #[method(name = "projecttips_voteendblock")] - fn get_vote_period_end_block( - &self, - project_id: ProjectId, - at: Option, - ) -> RpcResult>; - #[method(name = "projecttips_selectedjuror")] - fn selected_as_juror( - &self, - project_id: ProjectId, - who: AccountId, - at: Option, - ) -> RpcResult; + #[method(name = "projecttips_evidenceperiodendblock")] + fn get_evidence_period_end_block( + &self, + project_id: ProjectId, + at: Option, + ) -> RpcResult>; + #[method(name = "projecttips_stakingperiodendblock")] + fn get_staking_period_end_block( + &self, + project_id: ProjectId, + at: Option, + ) -> RpcResult>; + #[method(name = "projecttips_drawingperiodend")] + fn get_drawing_period_end( + &self, + project_id: ProjectId, + at: Option, + ) -> RpcResult<(u64, u64, bool)>; + #[method(name = "projecttips_commitendblock")] + fn get_commit_period_end_block( + &self, + project_id: ProjectId, + at: Option, + ) -> RpcResult>; + #[method(name = "projecttips_voteendblock")] + fn get_vote_period_end_block( + &self, + project_id: ProjectId, + at: Option, + ) -> RpcResult>; + #[method(name = "projecttips_selectedjuror")] + fn selected_as_juror( + &self, + project_id: ProjectId, + who: AccountId, + at: Option, + ) -> RpcResult; } /// A struct that implements the `SumStorageApi`. pub struct ProjectTips { - // If you have more generics, no need to SumStorage - // just use a tuple like SumStorage - client: Arc, - _marker: std::marker::PhantomData, + // If you have more generics, no need to SumStorage + // just use a tuple like SumStorage + client: Arc, + _marker: std::marker::PhantomData, } impl ProjectTips { - /// Create new `SumStorage` instance with the given reference to the client. - pub fn new(client: Arc) -> Self { - Self { client, _marker: Default::default() } - } + /// Create new `SumStorage` instance with the given reference to the client. + pub fn new(client: Arc) -> 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, + /// The transaction was not decodable. + DecodeError, + /// The call to runtime failed. + RuntimeError, } impl From for i32 { - fn from(e: Error) -> i32 { - match e { - Error::RuntimeError => 1, - Error::DecodeError => 2, - } - } + fn from(e: Error) -> i32 { + match e { + Error::RuntimeError => 1, + Error::DecodeError => 2, + } + } } impl ProjectTipsApiServer<::Hash, AccountId> - for ProjectTips + for ProjectTips where - Block: BlockT, - AccountId: Codec, - C: Send + Sync + 'static, - C: ProvideRuntimeApi, - C: HeaderBackend, - C::Api: ProjectTipsRuntimeApi, + Block: BlockT, + AccountId: Codec, + C: Send + Sync + 'static, + C: ProvideRuntimeApi, + C: HeaderBackend, + C::Api: ProjectTipsRuntimeApi, { - fn get_evidence_period_end_block( - &self, - project_id: ProjectId, - at: Option, - ) -> RpcResult> { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| + fn get_evidence_period_end_block( + &self, + project_id: ProjectId, + at: Option, + ) -> RpcResult> { + 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, project_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, - project_id: ProjectId, - at: Option, - ) -> RpcResult> { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| + let runtime_api_result = api.get_evidence_period_end_block(at, project_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, + project_id: ProjectId, + at: Option, + ) -> RpcResult> { + 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, project_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, - project_id: ProjectId, - at: Option, - ) -> RpcResult<(u64, u64, bool)> { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| + let runtime_api_result = api.get_staking_period_end_block(at, project_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, + project_id: ProjectId, + at: Option, + ) -> 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, project_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, - project_id: ProjectId, - at: Option, - ) -> RpcResult> { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| + let runtime_api_result = api.get_drawing_period_end(at, project_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, + project_id: ProjectId, + at: Option, + ) -> RpcResult> { + 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, project_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, - project_id: ProjectId, - at: Option, - ) -> RpcResult> { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| + let runtime_api_result = api.get_commit_period_end_block(at, project_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, + project_id: ProjectId, + at: Option, + ) -> RpcResult> { + 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, project_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, - project_id: ProjectId, - who: AccountId, - at: Option, - ) -> RpcResult { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| + let runtime_api_result = api.get_vote_period_end_block(at, project_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, + project_id: ProjectId, + who: AccountId, + at: Option, + ) -> RpcResult { + 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, project_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) - } + let runtime_api_result = api.selected_as_juror(at, project_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) + } } diff --git a/custom-pallets/shared-storage/src/lib.rs b/custom-pallets/shared-storage/src/lib.rs index 9ad2c6c..047952a 100644 --- a/custom-pallets/shared-storage/src/lib.rs +++ b/custom-pallets/shared-storage/src/lib.rs @@ -15,7 +15,8 @@ mod tests; mod benchmarking; pub mod weights; pub use weights::*; -mod extras; +pub mod extras; +pub mod types; use frame_support::sp_runtime; use frame_support::traits::BuildGenesisConfig; diff --git a/custom-pallets/shared-storage/src/types.rs b/custom-pallets/shared-storage/src/types.rs index e69de29..120884e 100644 --- a/custom-pallets/shared-storage/src/types.rs +++ b/custom-pallets/shared-storage/src/types.rs @@ -0,0 +1,119 @@ +use super::*; +use frame_support::pallet_prelude::*; +use parity_scale_codec::{Decode, Encode}; +use scale_info::TypeInfo; +use sp_std::{collections::btree_map::BTreeMap, vec::Vec}; + +#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo)] + +pub struct ReputationScore { + pub departments: BTreeMap, i64>, + pub total_score: i64, +} + +impl ReputationScore { + pub fn new() -> ReputationScore { + ReputationScore { + departments: BTreeMap::new(), + total_score: 0, + } + } + + pub fn add_department(&mut self, department: Vec, score: i64) { + self.departments.insert(department, score); + self.total_score = self.total_score.checked_add(score).unwrap_or(i64::MAX); + } + + pub fn update_department(&mut self, department: Vec, score: i64) { + if let Some(existing_score) = self.departments.get_mut(&department) { + self.total_score = self + .total_score + .checked_sub(*existing_score) + .unwrap_or(i64::MIN); + *existing_score = score; + self.total_score = self.total_score.checked_add(score).unwrap_or(i64::MAX); + } else { + self.add_department(department, score); + } + } + + pub fn get_department_score(&self, department: Vec) -> Option { + self.departments.get(&department).copied() + } + + pub fn get_all_departments(&self) -> Vec<(Vec, &i64)> { + self.departments + .iter() + .map(|(v, i)| (v.clone(), i)) + .collect() + } + + pub fn add_score(&mut self, department: Vec, amount: i64) { + if let Some(score) = self.departments.get_mut(&department) { + *score = score.checked_add(amount).unwrap_or(i64::MAX); + self.total_score = self.total_score.checked_add(amount).unwrap_or(i64::MAX); + } + } + + pub fn subtract_score(&mut self, department: Vec, amount: i64) -> bool { + if let Some(score) = self.departments.get_mut(&department) { + if *score >= amount { + *score = score.checked_sub(amount).unwrap_or(0); + self.total_score = self.total_score.checked_sub(amount).unwrap_or(i64::MIN); + true + } else { + false + } + } else { + false + } + } + + pub fn get_total_score(&self) -> i64 { + self.total_score + } +} + +#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo)] +#[scale_info(skip_type_params(T))] +pub struct Account { + id: T::AccountId, + reputation_score: ReputationScore, +} + +impl Account { + pub fn new(id: T::AccountId) -> Self { + Account { + id: id, + reputation_score: ReputationScore::new(), + } + } + + pub fn add_department_score(&mut self, department: Vec, score: i64) { + self.reputation_score.add_department(department, score); + } + + pub fn update_department_score(&mut self, department: Vec, score: i64) { + self.reputation_score.update_department(department, score); + } + + pub fn get_department_score(&self, department: Vec) -> Option { + self.reputation_score.get_department_score(department) + } + + pub fn get_all_department_scores(&self) -> Vec<(Vec, &i64)> { + self.reputation_score.get_all_departments() + } + + pub fn add_score(&mut self, department: Vec, amount: i64) { + self.reputation_score.add_score(department, amount); + } + + pub fn subtract_score(&mut self, department: Vec, amount: i64) -> bool { + self.reputation_score.subtract_score(department, amount) + } + + pub fn get_total_reputation_score(&self) -> i64 { + self.reputation_score.get_total_score() + } +}