From 481cdc93e976f0882b1e3a655ef7ce1dd8fe062b Mon Sep 17 00:00:00 2001 From: Amiya Behera Date: Wed, 27 Mar 2024 14:30:27 +0530 Subject: [PATCH] rpc --- Cargo.lock | 50 +++- Cargo.toml | 2 +- node/Cargo.toml | 8 + .../src/extras.rs | 71 ----- .../Cargo.toml | 2 +- .../README.md | 0 .../positive-externality-rpc/Cargo.toml | 15 + .../positive-externality-rpc/src/lib.rs | 260 +++++++++++++++++ .../Cargo.toml | 17 ++ .../src/lib.rs | 20 ++ .../src/benchmarking.rs | 0 pallets/positive-externality/src/extras.rs | 183 ++++++++++++ .../src/lib.rs | 0 .../src/mock.rs | 0 .../src/tests.rs | 0 .../src/types.rs | 0 .../src/weights.rs | 0 .../project-tips/project-tips-rpc/Cargo.toml | 15 + .../project-tips/project-tips-rpc/src/lib.rs | 262 ++++++++++++++++++ .../project-tips-runtime-api/Cargo.toml | 17 ++ .../project-tips-runtime-api/src/lib.rs | 22 ++ pallets/project-tips/src/extras.rs | 90 ++++++ runtime/node-runtime/Cargo.toml | 4 +- runtime/node-runtime/src/lib.rs | 6 +- 24 files changed, 964 insertions(+), 80 deletions(-) delete mode 100644 pallets/positive-externality-validation/src/extras.rs rename pallets/{positive-externality-validation => positive-externality}/Cargo.toml (98%) rename pallets/{positive-externality-validation => positive-externality}/README.md (100%) create mode 100644 pallets/positive-externality/positive-externality-rpc/Cargo.toml create mode 100644 pallets/positive-externality/positive-externality-rpc/src/lib.rs create mode 100644 pallets/positive-externality/positive-externality-runtime-api/Cargo.toml create mode 100644 pallets/positive-externality/positive-externality-runtime-api/src/lib.rs rename pallets/{positive-externality-validation => positive-externality}/src/benchmarking.rs (100%) create mode 100644 pallets/positive-externality/src/extras.rs rename pallets/{positive-externality-validation => positive-externality}/src/lib.rs (100%) rename pallets/{positive-externality-validation => positive-externality}/src/mock.rs (100%) rename pallets/{positive-externality-validation => positive-externality}/src/tests.rs (100%) rename pallets/{positive-externality-validation => positive-externality}/src/types.rs (100%) rename pallets/{positive-externality-validation => positive-externality}/src/weights.rs (100%) create mode 100644 pallets/project-tips/project-tips-rpc/Cargo.toml create mode 100644 pallets/project-tips/project-tips-rpc/src/lib.rs create mode 100644 pallets/project-tips/project-tips-runtime-api/Cargo.toml create mode 100644 pallets/project-tips/project-tips-runtime-api/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 1dac883..cc7097e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4524,8 +4524,12 @@ dependencies = [ "node-template-runtime", "pallet-transaction-payment", "pallet-transaction-payment-rpc", + "positive-externality-rpc", + "positive-externality-runtime-api", "profile-validation-rpc", "profile-validation-runtime-api", + "project-tips-rpc", + "project-tips-runtime-api", "sc-basic-authorship", "sc-cli", "sc-client-api", @@ -4579,7 +4583,7 @@ dependencies = [ "pallet-transaction-payment", "pallet-transaction-payment-rpc-runtime-api", "parity-scale-codec", - "positive-externality-validation", + "positive-externality", "profile-validation", "profile-validation-runtime-api", "project-tips", @@ -5447,7 +5451,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "767eb9f07d4a5ebcb39bbf2d452058a93c011373abf6832e24194a1c3f004794" [[package]] -name = "positive-externality-validation" +name = "positive-externality" version = "4.0.0-dev" dependencies = [ "frame-benchmarking", @@ -5469,6 +5473,27 @@ dependencies = [ "sp-runtime", ] +[[package]] +name = "positive-externality-rpc" +version = "0.1.0" +dependencies = [ + "jsonrpsee", + "positive-externality-runtime-api", + "sc-rpc", + "sc-rpc-api", + "sp-api", + "sp-blockchain", + "sp-runtime", +] + +[[package]] +name = "positive-externality-runtime-api" +version = "0.1.0" +dependencies = [ + "frame-support", + "sp-api", +] + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -5659,6 +5684,27 @@ dependencies = [ "sp-runtime", ] +[[package]] +name = "project-tips-rpc" +version = "0.1.0" +dependencies = [ + "jsonrpsee", + "project-tips-runtime-api", + "sc-rpc", + "sc-rpc-api", + "sp-api", + "sp-blockchain", + "sp-runtime", +] + +[[package]] +name = "project-tips-runtime-api" +version = "0.1.0" +dependencies = [ + "frame-support", + "sp-api", +] + [[package]] name = "prometheus" version = "0.13.3" diff --git a/Cargo.toml b/Cargo.toml index a524f62..7bc97dc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ members = [ "pallets/shared-storage", "pallets/spaces", "pallets/profile-validation", - "pallets/positive-externality-validation", + "pallets/positive-externality", "pallets/support", "pallets/election", "pallets/department-funding", diff --git a/node/Cargo.toml b/node/Cargo.toml index f2fde94..223fb79 100644 --- a/node/Cargo.toml +++ b/node/Cargo.toml @@ -74,6 +74,14 @@ profile-validation-rpc = { path="../pallets/profile-validation/profile-validatio 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} +# Postive exterality rpc +positive-externality-runtime-api = {path="../pallets/positive-externality/positive-externality-runtime-api", default-features = false} +positive-externality-rpc= { path="../pallets/positive-externality/positive-externality-rpc", default-features = false} + +# Project tip rpc +project-tips-runtime-api = {path="../pallets/project-tips/project-tips-runtime-api", default-features = false} +project-tips-rpc= { path="../pallets/project-tips/project-tips-rpc", default-features = false} + [build-dependencies] diff --git a/pallets/positive-externality-validation/src/extras.rs b/pallets/positive-externality-validation/src/extras.rs deleted file mode 100644 index e914ea2..0000000 --- a/pallets/positive-externality-validation/src/extras.rs +++ /dev/null @@ -1,71 +0,0 @@ -use frame_support::dispatch::DispatchResult; - -use super::*; - -impl PositiveExternalityPost { - pub fn new(id: PositiveExternalityPostId, created_by: T::AccountId, content: Content) -> Self { - PositiveExternalityPost { - id, - created: new_who_and_when::(created_by.clone()), - edited: false, - owner: created_by, - content, - hidden: false, - upvotes_count: 0, - downvotes_count: 0, - } - } - - pub fn ensure_owner(&self, account: &T::AccountId) -> DispatchResult { - ensure!(self.is_owner(account), Error::::NotAPostOwner); - Ok(()) - } - - pub fn is_owner(&self, account: &T::AccountId) -> bool { - self.owner == *account - } -} - -impl Pallet { - - pub(super) fn get_phase_data() -> PhaseData { - T::SchellingGameSharedSource::create_phase_data(50, 5, 3, 100, (100, 100)) - } - - pub fn ensure_validation_on_positive_externality(account: T::AccountId) -> DispatchResult { - let bool_data = ValidatePositiveExternality::::get(account); - ensure!(bool_data == true, Error::::ValidationPositiveExternalityIsOff); - - Ok(()) - } - - pub fn ensure_min_stake_positive_externality(account: T::AccountId) -> DispatchResult { - let stake = PositiveExternalityStakeBalance::::get(account); - let min_stake = MinimumPositiveExternalityStake::::get(); - // println!("stake {:?}", stake); - // println!("min stake {:?}", min_stake); - ensure!(stake >= min_stake, Error::::LessThanMinStake); - - Ok(()) - } - - pub(super) fn u64_to_balance_saturated(input: u64) -> BalanceOf { - input.saturated_into::>() - } - - pub(super) fn u64_to_block_saturated(input: u64) -> BlockNumberOf { - input.saturated_into::>() - } - - pub(super) fn get_drawn_jurors(user_to_calculate: T::AccountId) -> Vec<(T::AccountId, u64)> { - let pe_block_number = - >::get(user_to_calculate.clone()); - - let key = SumTreeName::PositiveExternality { - user_address: user_to_calculate, - block_number: pe_block_number.clone(), - }; - - T::SchellingGameSharedSource::get_drawn_jurors(key) - } -} diff --git a/pallets/positive-externality-validation/Cargo.toml b/pallets/positive-externality/Cargo.toml similarity index 98% rename from pallets/positive-externality-validation/Cargo.toml rename to pallets/positive-externality/Cargo.toml index 876e8b6..4c9f646 100644 --- a/pallets/positive-externality-validation/Cargo.toml +++ b/pallets/positive-externality/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "positive-externality-validation" +name = "positive-externality" version = "4.0.0-dev" description = "FRAME pallet template for defining custom runtime logic." authors = ["Substrate DevHub "] diff --git a/pallets/positive-externality-validation/README.md b/pallets/positive-externality/README.md similarity index 100% rename from pallets/positive-externality-validation/README.md rename to pallets/positive-externality/README.md diff --git a/pallets/positive-externality/positive-externality-rpc/Cargo.toml b/pallets/positive-externality/positive-externality-rpc/Cargo.toml new file mode 100644 index 0000000..5ac5119 --- /dev/null +++ b/pallets/positive-externality/positive-externality-rpc/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "positive-externality-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" } +positive-externality-runtime-api = { default-features= false, path="../positive-externality-runtime-api"} diff --git a/pallets/positive-externality/positive-externality-rpc/src/lib.rs b/pallets/positive-externality/positive-externality-rpc/src/lib.rs new file mode 100644 index 0000000..ca52719 --- /dev/null +++ b/pallets/positive-externality/positive-externality-rpc/src/lib.rs @@ -0,0 +1,260 @@ +use jsonrpsee::{ + core::{Error as JsonRpseeError, RpcResult}, + proc_macros::rpc, + types::error::{CallError, ErrorCode, ErrorObject}, +}; +use positive_externality_runtime_api::PositiveExternalityApi as PositiveExternalityRuntimeApi; +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; + +#[rpc(client, server)] +pub trait PositiveExternalityApi { + #[method(name = "positiveexternality_challengerevidence")] + fn get_challengers_evidence( + &self, + user_to_calculate: AccountId, + offset: u64, + limit: u16, + 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, +} + +impl PositiveExternality { + /// 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, +} + +impl From for i32 { + fn from(e: Error) -> i32 { + match e { + Error::RuntimeError => 1, + Error::DecodeError => 2, + } + } +} + + +impl PositiveExternalityApiServer<::Hash, AccountId> for PositiveExternality +where + Block: BlockT, + AccountId: Codec, + C: Send + Sync + 'static, + C: ProvideRuntimeApi, + C: HeaderBackend, + C::Api: PositiveExternalityRuntimeApi, +{ + fn get_challengers_evidence( + &self, + user_to_calculate: 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, user_to_calculate, 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, + 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(|| + // 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(|| + // 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) + } + + 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) + } + + 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) + } + + 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) + } +} diff --git a/pallets/positive-externality/positive-externality-runtime-api/Cargo.toml b/pallets/positive-externality/positive-externality-runtime-api/Cargo.toml new file mode 100644 index 0000000..14d17ce --- /dev/null +++ b/pallets/positive-externality/positive-externality-runtime-api/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "positive-externality-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", +] diff --git a/pallets/positive-externality/positive-externality-runtime-api/src/lib.rs b/pallets/positive-externality/positive-externality-runtime-api/src/lib.rs new file mode 100644 index 0000000..543e2dd --- /dev/null +++ b/pallets/positive-externality/positive-externality-runtime-api/src/lib.rs @@ -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; + + +sp_api::decl_runtime_apis! { + pub trait PositiveExternalityApi where AccountId: Codec { + fn get_challengers_evidence(user_to_calculate: AccountId, offset: u64, limit: u16) -> Vec; + fn get_evidence_period_end_block(user_to_calculate: AccountId) -> Option; + fn get_staking_period_end_block(user_to_calculate: AccountId) -> Option; + fn get_drawing_period_end(user_to_calculate: AccountId) -> (u64, u64, bool); + fn get_commit_period_end_block(user_to_calculate: AccountId) -> Option; + fn get_vote_period_end_block(user_to_calculate: AccountId) -> Option; + fn selected_as_juror(user_to_calculate: AccountId, who: AccountId) -> bool; + } +} diff --git a/pallets/positive-externality-validation/src/benchmarking.rs b/pallets/positive-externality/src/benchmarking.rs similarity index 100% rename from pallets/positive-externality-validation/src/benchmarking.rs rename to pallets/positive-externality/src/benchmarking.rs diff --git a/pallets/positive-externality/src/extras.rs b/pallets/positive-externality/src/extras.rs new file mode 100644 index 0000000..dea660d --- /dev/null +++ b/pallets/positive-externality/src/extras.rs @@ -0,0 +1,183 @@ +use frame_support::dispatch::DispatchResult; + +use super::*; + +impl PositiveExternalityPost { + pub fn new(id: PositiveExternalityPostId, created_by: T::AccountId, content: Content) -> Self { + PositiveExternalityPost { + id, + created: new_who_and_when::(created_by.clone()), + edited: false, + owner: created_by, + content, + hidden: false, + upvotes_count: 0, + downvotes_count: 0, + } + } + + pub fn ensure_owner(&self, account: &T::AccountId) -> DispatchResult { + ensure!(self.is_owner(account), Error::::NotAPostOwner); + Ok(()) + } + + pub fn is_owner(&self, account: &T::AccountId) -> bool { + self.owner == *account + } +} + +impl Pallet { + + pub(super) fn get_phase_data() -> PhaseData { + T::SchellingGameSharedSource::create_phase_data(50, 5, 3, 100, (100, 100)) + } + + pub fn ensure_validation_on_positive_externality(account: T::AccountId) -> DispatchResult { + let bool_data = ValidatePositiveExternality::::get(account); + ensure!(bool_data == true, Error::::ValidationPositiveExternalityIsOff); + + Ok(()) + } + + pub fn ensure_min_stake_positive_externality(account: T::AccountId) -> DispatchResult { + let stake = PositiveExternalityStakeBalance::::get(account); + let min_stake = MinimumPositiveExternalityStake::::get(); + // println!("stake {:?}", stake); + // println!("min stake {:?}", min_stake); + ensure!(stake >= min_stake, Error::::LessThanMinStake); + + Ok(()) + } + + pub(super) fn u64_to_balance_saturated(input: u64) -> BalanceOf { + input.saturated_into::>() + } + + pub(super) fn u64_to_block_saturated(input: u64) -> BlockNumberOf { + input.saturated_into::>() + } + + pub(super) fn get_drawn_jurors(user_to_calculate: T::AccountId) -> Vec<(T::AccountId, u64)> { + let pe_block_number = + >::get(user_to_calculate.clone()); + + let key = SumTreeName::PositiveExternality { + user_address: user_to_calculate, + block_number: pe_block_number.clone(), + }; + + T::SchellingGameSharedSource::get_drawn_jurors(key) + } + + // Block code start + + pub fn get_evidence_period_end_block(user_to_calculate: T::AccountId) -> Option { + let now = >::block_number(); + + let pe_block_number = + >::get(user_to_calculate.clone()); + + let key = SumTreeName::PositiveExternality { + user_address: user_to_calculate, + block_number: pe_block_number.clone(), + }; + + + let phase_data = Self::get_phase_data(); + + let result = T::SchellingGameSharedSource::get_evidence_period_end_block_helper_link( + key, phase_data, now, + ); + result + + + } + + + pub fn get_staking_period_end_block(user_to_calculate: T::AccountId) -> Option { + let now = >::block_number(); + + let pe_block_number = + >::get(user_to_calculate.clone()); + + let key = SumTreeName::PositiveExternality { + user_address: user_to_calculate, + block_number: pe_block_number.clone(), + }; + + let phase_data = Self::get_phase_data(); + + let result = T::SchellingGameSharedSource::get_staking_period_end_block_helper_link( + key, phase_data, now, + ); + result + } + + pub fn get_drawing_period_end(user_to_calculate: T::AccountId) -> (u64, u64, bool) { + let pe_block_number = + >::get(user_to_calculate.clone()); + + let key = SumTreeName::PositiveExternality { + user_address: user_to_calculate, + block_number: pe_block_number.clone(), + }; + let phase_data = Self::get_phase_data(); + + let result = + T::SchellingGameSharedSource::get_drawing_period_end_helper_link(key, phase_data); + result + } + + pub fn get_commit_period_end_block(user_to_calculate: T::AccountId) -> Option { + let now = >::block_number(); + + let pe_block_number = + >::get(user_to_calculate.clone()); + + let key = SumTreeName::PositiveExternality { + user_address: user_to_calculate, + block_number: pe_block_number.clone(), + }; + + let phase_data = Self::get_phase_data(); + + let result = T::SchellingGameSharedSource::get_commit_period_end_block_helper_link( + key, phase_data, now, + ); + result + } + + pub fn get_vote_period_end_block(user_to_calculate: T::AccountId) -> Option { + let now = >::block_number(); + + let pe_block_number = + >::get(user_to_calculate.clone()); + + let key = SumTreeName::PositiveExternality { + user_address: user_to_calculate, + block_number: pe_block_number.clone(), + }; + + let phase_data = Self::get_phase_data(); + + let result = T::SchellingGameSharedSource::get_vote_period_end_block_helper_link( + key, phase_data, now, + ); + result + } + + pub fn selected_as_juror(user_to_calculate: T::AccountId, who: T::AccountId) -> bool { + let pe_block_number = + >::get(user_to_calculate.clone()); + + let key = SumTreeName::PositiveExternality { + user_address: user_to_calculate, + block_number: pe_block_number.clone(), + }; + + let result = T::SchellingGameSharedSource::selected_as_juror_helper_link(key, who); + result + } + + // Block code end +} diff --git a/pallets/positive-externality-validation/src/lib.rs b/pallets/positive-externality/src/lib.rs similarity index 100% rename from pallets/positive-externality-validation/src/lib.rs rename to pallets/positive-externality/src/lib.rs diff --git a/pallets/positive-externality-validation/src/mock.rs b/pallets/positive-externality/src/mock.rs similarity index 100% rename from pallets/positive-externality-validation/src/mock.rs rename to pallets/positive-externality/src/mock.rs diff --git a/pallets/positive-externality-validation/src/tests.rs b/pallets/positive-externality/src/tests.rs similarity index 100% rename from pallets/positive-externality-validation/src/tests.rs rename to pallets/positive-externality/src/tests.rs diff --git a/pallets/positive-externality-validation/src/types.rs b/pallets/positive-externality/src/types.rs similarity index 100% rename from pallets/positive-externality-validation/src/types.rs rename to pallets/positive-externality/src/types.rs diff --git a/pallets/positive-externality-validation/src/weights.rs b/pallets/positive-externality/src/weights.rs similarity index 100% rename from pallets/positive-externality-validation/src/weights.rs rename to pallets/positive-externality/src/weights.rs diff --git a/pallets/project-tips/project-tips-rpc/Cargo.toml b/pallets/project-tips/project-tips-rpc/Cargo.toml new file mode 100644 index 0000000..1878c33 --- /dev/null +++ b/pallets/project-tips/project-tips-rpc/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "project-tips-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" } +project-tips-runtime-api = { default-features= false, path="../project-tips-runtime-api"} diff --git a/pallets/project-tips/project-tips-rpc/src/lib.rs b/pallets/project-tips/project-tips-rpc/src/lib.rs new file mode 100644 index 0000000..a8c6709 --- /dev/null +++ b/pallets/project-tips/project-tips-rpc/src/lib.rs @@ -0,0 +1,262 @@ +use jsonrpsee::{ + core::{Error as JsonRpseeError, RpcResult}, + proc_macros::rpc, + types::error::{CallError, ErrorCode, ErrorObject}, +}; +use project_tips_runtime_api::ProjectTipsApi as ProjectTipsRuntimeApi; +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 ProjectId = u64; + +#[rpc(client, server)] +pub trait ProjectTipsApi { + #[method(name = "projecttips_challengerevidence")] + fn get_challengers_evidence( + &self, + project_id: ProjectId, + offset: u64, + limit: u16, + 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, +} + +impl ProjectTips { + /// 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, +} + +impl From for i32 { + fn from(e: Error) -> i32 { + match e { + Error::RuntimeError => 1, + Error::DecodeError => 2, + } + } +} + + +impl ProjectTipsApiServer<::Hash, AccountId> for ProjectTips +where + Block: BlockT, + AccountId: Codec, + C: Send + Sync + 'static, + C: ProvideRuntimeApi, + C: HeaderBackend, + C::Api: ProjectTipsRuntimeApi, +{ + fn get_challengers_evidence( + &self, + project_id: ProjectId, + 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, project_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, + 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(|| + // 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(|| + // 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(|| + // 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(|| + // 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(|| + // 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) + } +} diff --git a/pallets/project-tips/project-tips-runtime-api/Cargo.toml b/pallets/project-tips/project-tips-runtime-api/Cargo.toml new file mode 100644 index 0000000..fee90dc --- /dev/null +++ b/pallets/project-tips/project-tips-runtime-api/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "project-tips-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", +] diff --git a/pallets/project-tips/project-tips-runtime-api/src/lib.rs b/pallets/project-tips/project-tips-runtime-api/src/lib.rs new file mode 100644 index 0000000..44be8bb --- /dev/null +++ b/pallets/project-tips/project-tips-runtime-api/src/lib.rs @@ -0,0 +1,22 @@ +#![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 ProjectId = u64; + + +sp_api::decl_runtime_apis! { + pub trait ProjectTipsApi where AccountId: Codec { + fn get_challengers_evidence(project_id: ProjectId, offset: u64, limit: u16) -> Vec; + fn get_evidence_period_end_block(project_id: ProjectId) -> Option; + fn get_staking_period_end_block(project_id: ProjectId) -> Option; + fn get_drawing_period_end(project_id: ProjectId) -> (u64, u64, bool); + fn get_commit_period_end_block(project_id: ProjectId) -> Option; + fn get_vote_period_end_block(project_id: ProjectId) -> Option; + fn selected_as_juror(project_id: ProjectId, who: AccountId) -> bool; + } +} diff --git a/pallets/project-tips/src/extras.rs b/pallets/project-tips/src/extras.rs index 1c85fbe..261d103 100644 --- a/pallets/project-tips/src/extras.rs +++ b/pallets/project-tips/src/extras.rs @@ -91,4 +91,94 @@ impl Pallet { }, } } + + + // Block code start + + pub fn get_evidence_period_end_block(project_id: ProjectId) -> Option { + let now = >::block_number(); + + let block_number = Self::get_block_number_of_schelling_game(project_id).unwrap(); + + let key = SumTreeName::ProjectTips { project_id, block_number: block_number.clone() }; + + + let phase_data = Self::get_phase_data(); + + let result = T::SchellingGameSharedSource::get_evidence_period_end_block_helper_link( + key, phase_data, now, + ); + result + + + } + + + pub fn get_staking_period_end_block(project_id: ProjectId) -> Option { + let now = >::block_number(); + + let block_number = Self::get_block_number_of_schelling_game(project_id).unwrap(); + + let key = SumTreeName::ProjectTips { project_id, block_number: block_number.clone() }; + + let phase_data = Self::get_phase_data(); + + let result = T::SchellingGameSharedSource::get_staking_period_end_block_helper_link( + key, phase_data, now, + ); + result + } + + pub fn get_drawing_period_end(project_id: ProjectId) -> (u64, u64, bool) { + let block_number = Self::get_block_number_of_schelling_game(project_id).unwrap(); + + let key = SumTreeName::ProjectTips { project_id, block_number: block_number.clone() }; + let phase_data = Self::get_phase_data(); + + let result = + T::SchellingGameSharedSource::get_drawing_period_end_helper_link(key, phase_data); + result + } + + pub fn get_commit_period_end_block(project_id: ProjectId) -> Option { + let now = >::block_number(); + + let block_number = Self::get_block_number_of_schelling_game(project_id).unwrap(); + + let key = SumTreeName::ProjectTips { project_id, block_number: block_number.clone() }; + + let phase_data = Self::get_phase_data(); + + let result = T::SchellingGameSharedSource::get_commit_period_end_block_helper_link( + key, phase_data, now, + ); + result + } + + pub fn get_vote_period_end_block(project_id: ProjectId) -> Option { + let now = >::block_number(); + + let block_number = Self::get_block_number_of_schelling_game(project_id).unwrap(); + + let key = SumTreeName::ProjectTips { project_id, block_number: block_number.clone() }; + + let phase_data = Self::get_phase_data(); + + let result = T::SchellingGameSharedSource::get_vote_period_end_block_helper_link( + key, phase_data, now, + ); + result + } + + pub fn selected_as_juror(project_id: ProjectId, who: T::AccountId) -> bool { + let block_number = Self::get_block_number_of_schelling_game(project_id).unwrap(); + + let key = SumTreeName::ProjectTips { project_id, block_number: block_number.clone() }; + + let result = T::SchellingGameSharedSource::selected_as_juror_helper_link(key, who); + result + } + + // Block code end + } diff --git a/runtime/node-runtime/Cargo.toml b/runtime/node-runtime/Cargo.toml index eb9ba91..918ff1c 100644 --- a/runtime/node-runtime/Cargo.toml +++ b/runtime/node-runtime/Cargo.toml @@ -62,7 +62,7 @@ profile-validation = {default-features=false, path="../../pallets/profile-valida # profile-validation-runtime-api = {default-features=false, path="../pallets/profile-validation/profile-validation-runtime-api"} shared-storage = {default-features=false, path="../../pallets/shared-storage"} profile-validation-runtime-api = {default-features=false, path="../../pallets/profile-validation/profile-validation-runtime-api"} -positive-externality-validation = {default-features = false, path="../../pallets/positive-externality-validation"} +positive-externality = {default-features = false, path="../../pallets/positive-externality"} department-funding = {default-features = false, path="../../pallets/department-funding"} project-tips = {default-features = false, path="../../pallets/project-tips"} @@ -113,7 +113,7 @@ std = [ "profile-validation/std", # "shared-storage/std", "profile-validation-runtime-api/std", - "positive-externality-validation/std", + "positive-externality/std", "shared-storage/std", ] runtime-benchmarks = [ diff --git a/runtime/node-runtime/src/lib.rs b/runtime/node-runtime/src/lib.rs index 476e5d8..aa8bd70 100644 --- a/runtime/node-runtime/src/lib.rs +++ b/runtime/node-runtime/src/lib.rs @@ -306,9 +306,9 @@ impl shared_storage::Config for Runtime { type WeightInfo = shared_storage::weights::SubstrateWeight; } -impl positive_externality_validation::Config for Runtime { +impl positive_externality::Config for Runtime { type RuntimeEvent = RuntimeEvent; - type WeightInfo = positive_externality_validation::weights::SubstrateWeight; + type WeightInfo = positive_externality::weights::SubstrateWeight; type SharedStorageSource = SharedStorage; type Currency = Balances; type SchellingGameSharedSource = SchellingGameShared; @@ -352,7 +352,7 @@ construct_runtime!( SchellingGameShared: schelling_game_shared, ProfileValidation: profile_validation, SharedStorage: shared_storage, - PositiveExternalityValidation: positive_externality_validation, + PositiveExternality: positive_externality, DepartmentFunding: department_funding, ProjectTips: project_tips, }