Skip to content

Commit

Permalink
user staked
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed Jan 20, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 5dfbee0 commit 257d55d
Showing 8 changed files with 128 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -93,7 +93,9 @@ RUST_BACKTRACE=1 ./target/release/node-template -ldebug --dev
```

Code lines
Jan 2025
Shivarthu: 23586
Client: 17699



Original file line number Diff line number Diff line change
@@ -88,6 +88,23 @@ pub trait PositiveExternalityApi<BlockHash, AccountId> {
at: Option<BlockHash>,
) -> RpcResult<Option<Vec<AccountId>>>;

#[method(name = "positiveexternality_has_user_staked")]
fn has_user_staked(
&self,
user_to_calculate: AccountId,
who: AccountId,
at: Option<BlockHash>,
) -> RpcResult<bool>;

#[method(name = "positiveexternality_user_staked_value")]
fn user_staked_value(
&self,
user_to_calculate: AccountId,
who: AccountId,
at: Option<BlockHash>,
) -> RpcResult<u64>;




}
@@ -348,5 +365,45 @@ where
Ok(res)
}

fn has_user_staked(
&self,
user_to_calculate: AccountId,
who: AccountId,
at: Option<Block::Hash>,
) -> RpcResult<bool> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(||
// If the block hash is not supplied assume the best block.
self.client.info().best_hash);

let runtime_api_result = api.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<Block::Hash>,
) -> RpcResult<u64> {
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)
}


}
Original file line number Diff line number Diff line change
@@ -19,5 +19,7 @@ sp_api::decl_runtime_apis! {
fn paginate_posts_by_address_latest(user: AccountId, page: u64, page_size: u64) -> Option<Vec<u64>>;
fn validation_list_length() -> u64;
fn validation_list_latest(page: u64, page_size: u64) -> Option<Vec<AccountId>>;
fn has_user_staked(user_to_calculate: AccountId, who: AccountId) -> bool;
fn user_staked_value(user_to_calculate: AccountId, who: AccountId) -> u64;
}
}
24 changes: 24 additions & 0 deletions custom-pallets/positive-externality/src/extras.rs
Original file line number Diff line number Diff line change
@@ -167,6 +167,30 @@ impl<T: Config> Pallet<T> {
result
}

pub fn has_user_staked(user_to_calculate: T::AccountId, who: T::AccountId) -> bool {
let pe_block_number = <ValidationBlock<T>>::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 = <ValidationBlock<T>>::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 {
20 changes: 20 additions & 0 deletions custom-pallets/schelling-game-shared/src/extras.rs
Original file line number Diff line number Diff line change
@@ -268,6 +268,26 @@ impl<T: Config> Pallet<T> {
}
}

pub(super) fn has_user_staked(key:SumTreeNameType<T>, who: AccountIdOf<T>
) -> 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<T>, who: AccountIdOf<T>
) -> 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<T>,
13 changes: 13 additions & 0 deletions custom-pallets/schelling-game-shared/src/share_link.rs
Original file line number Diff line number Diff line change
@@ -157,6 +157,19 @@ impl<T: Config> SchellingGameSharedLink for Pallet<T> {
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
7 changes: 7 additions & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<Block, AccountId> for Runtime {
3 changes: 3 additions & 0 deletions traits/trait-schelling-game-shared/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 257d55d

Please sign in to comment.