From 257d55d01aabc000d330ec96fd533b761e132324 Mon Sep 17 00:00:00 2001 From: Amiya Behera Date: Mon, 20 Jan 2025 20:46:16 +0530 Subject: [PATCH] user staked --- README.md | 2 + .../positive-externality-rpc/src/lib.rs | 57 +++++++++++++++++++ .../src/lib.rs | 2 + .../positive-externality/src/extras.rs | 24 ++++++++ .../schelling-game-shared/src/extras.rs | 20 +++++++ .../schelling-game-shared/src/share_link.rs | 13 +++++ runtime/src/lib.rs | 7 +++ traits/trait-schelling-game-shared/src/lib.rs | 3 + 8 files changed, 128 insertions(+) diff --git a/README.md b/README.md index 00ee857..d16dde2 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,9 @@ RUST_BACKTRACE=1 ./target/release/node-template -ldebug --dev ``` Code lines +Jan 2025 Shivarthu: 23586 +Client: 17699 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 bc49496..2376540 100644 --- a/custom-pallets/positive-externality/positive-externality-rpc/src/lib.rs +++ b/custom-pallets/positive-externality/positive-externality-rpc/src/lib.rs @@ -88,6 +88,23 @@ pub trait PositiveExternalityApi { at: Option, ) -> RpcResult>>; + #[method(name = "positiveexternality_has_user_staked")] + fn has_user_staked( + &self, + user_to_calculate: AccountId, + who: AccountId, + at: Option, + ) -> RpcResult; + + #[method(name = "positiveexternality_user_staked_value")] + fn user_staked_value( + &self, + user_to_calculate: AccountId, + who: AccountId, + at: Option, + ) -> RpcResult; + + } @@ -348,5 +365,45 @@ where Ok(res) } + fn has_user_staked( + &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.has_user_staked(at, user_to_calculate, who); + + fn map_err(error: impl ToString, desc: &'static str) -> ErrorObjectOwned { + 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 user_staked_value( + &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.user_staked_value(at, user_to_calculate, who); + + fn map_err(error: impl ToString, desc: &'static str) -> ErrorObjectOwned { + 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-runtime-api/src/lib.rs b/custom-pallets/positive-externality/positive-externality-runtime-api/src/lib.rs index 56e8e6c..01614fb 100644 --- a/custom-pallets/positive-externality/positive-externality-runtime-api/src/lib.rs +++ b/custom-pallets/positive-externality/positive-externality-runtime-api/src/lib.rs @@ -19,5 +19,7 @@ sp_api::decl_runtime_apis! { fn paginate_posts_by_address_latest(user: AccountId, page: u64, page_size: u64) -> Option>; fn validation_list_length() -> u64; fn validation_list_latest(page: u64, page_size: u64) -> Option>; + fn has_user_staked(user_to_calculate: AccountId, who: AccountId) -> bool; + fn user_staked_value(user_to_calculate: AccountId, who: AccountId) -> u64; } } diff --git a/custom-pallets/positive-externality/src/extras.rs b/custom-pallets/positive-externality/src/extras.rs index 32769a6..77632ec 100644 --- a/custom-pallets/positive-externality/src/extras.rs +++ b/custom-pallets/positive-externality/src/extras.rs @@ -167,6 +167,30 @@ impl Pallet { result } + pub fn has_user_staked(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::has_user_staked(key, who); + result + } + +pub fn user_staked_value(user_to_calculate: T::AccountId, who: 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(), + }; + + let result = T::SchellingGameSharedSource::user_staked_value(key, who); + result +} + // Block code end pub fn post_by_address_length(user: T::AccountId) -> u64 { diff --git a/custom-pallets/schelling-game-shared/src/extras.rs b/custom-pallets/schelling-game-shared/src/extras.rs index ed7bbfd..b3a8813 100644 --- a/custom-pallets/schelling-game-shared/src/extras.rs +++ b/custom-pallets/schelling-game-shared/src/extras.rs @@ -268,6 +268,26 @@ impl Pallet { } } + pub(super) fn has_user_staked(key:SumTreeNameType, who: AccountIdOf + ) -> bool { + let stake_of = T::SortitionSumGameSource::stake_of_link(key.clone(), who.clone()).unwrap(); + + match stake_of { + Some(_) => true, + None => false + } + } + + pub(super) fn user_staked_value(key:SumTreeNameType, who: AccountIdOf + ) -> u64 { + let stake_of = T::SortitionSumGameSource::stake_of_link(key.clone(), who.clone()).unwrap(); + + match stake_of { + Some(value) => value, + None => 0 + } + } + // Improvements: Set stake to zero after a juror is drawn, so that they are not drawn again. Store the stake in storage map in DrawnJurors, and use it in get_incentives_helper pub(super) fn draw_jurors_helper( key: SumTreeNameType, diff --git a/custom-pallets/schelling-game-shared/src/share_link.rs b/custom-pallets/schelling-game-shared/src/share_link.rs index ad79bb0..da5a264 100644 --- a/custom-pallets/schelling-game-shared/src/share_link.rs +++ b/custom-pallets/schelling-game-shared/src/share_link.rs @@ -157,6 +157,19 @@ impl SchellingGameSharedLink for Pallet { Self::apply_jurors_helper(key, phase_data, who, stake) } + fn has_user_staked( + key: Self::SumTreeName, + who: Self::AccountId, + + ) -> bool { + Self::has_user_staked(key, who) + } + + fn user_staked_value(key: Self::SumTreeName, + who: Self::AccountId) -> u64 { + Self::user_staked_value(key, who) + } + /// Draw Jurors /// Ensure `Period` is `Drawing` /// `iterations` is number of jurors drawn per call diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index fd66ccc..c06607e 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -715,6 +715,13 @@ impl_runtime_apis! { PositiveExternality::validation_list_latest(page, page_size) } + fn has_user_staked(user_to_calculate: AccountId, who: AccountId) -> bool { + PositiveExternality::has_user_staked(user_to_calculate, who) + } + fn user_staked_value(user_to_calculate: AccountId, who: AccountId) -> u64 { + PositiveExternality::user_staked_value(user_to_calculate, who) + } + } impl project_tips_runtime_api::ProjectTipsApi for Runtime { diff --git a/traits/trait-schelling-game-shared/src/lib.rs b/traits/trait-schelling-game-shared/src/lib.rs index 9ed0ade..a136c4a 100644 --- a/traits/trait-schelling-game-shared/src/lib.rs +++ b/traits/trait-schelling-game-shared/src/lib.rs @@ -162,4 +162,7 @@ pub trait SchellingGameSharedLink { fn set_new_mean_value(key: Self::SumTreeName) -> DispatchResult; fn add_to_incentives_count(key: Self::SumTreeName, who: Self::AccountId) -> DispatchResult; + + fn has_user_staked(key: Self::SumTreeName, who: Self::AccountId ) -> bool; + fn user_staked_value(key: Self::SumTreeName, who: Self::AccountId) -> u64; }