Skip to content

Commit

Permalink
reputation score of account
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed Jul 4, 2024
1 parent 6bb5549 commit c6f8a78
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 48 deletions.
100 changes: 100 additions & 0 deletions custom-pallets/shared-storage/src/extras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,104 @@ impl<T: Config> Pallet<T> {
PositiveExternalityScore::<T>::insert(address, score);
Ok(())
}

pub fn set_department_reputation_score(
address: T::AccountId,
department: Vec<u8>,
score: i64,
) -> DispatchResult {
ReputationScoreOfAccount::<T>::mutate(address, |reputation_score| {
if let Some(reputation_score) = reputation_score.as_mut() {
reputation_score.add_department(department.clone(), score);
} else {
*reputation_score = Some(ReputationScore::new());
reputation_score
.as_mut()
.unwrap()
.add_department(department.clone(), score);
}
});
Ok(())
}

pub fn update_department_reputation_score(
address: T::AccountId,
department: Vec<u8>,
score: i64,
) -> DispatchResult {
ReputationScoreOfAccount::<T>::mutate(address, |reputation_score| {
if let Some(reputation_score) = reputation_score.as_mut() {
reputation_score.update_department(department.clone(), score);
} else {
*reputation_score = Some(ReputationScore::new());
reputation_score
.as_mut()
.unwrap()
.update_department(department.clone(), score);
}
});
Ok(())
}

pub fn add_reputation_score_to_department(
address: T::AccountId,
department: Vec<u8>,
amount: i64,
) -> DispatchResult {
ReputationScoreOfAccount::<T>::mutate(address, |reputation_score| {
if let Some(reputation_score) = reputation_score.as_mut() {
reputation_score.add_score(department.clone(), amount);
} else {
*reputation_score = Some(ReputationScore::new());
reputation_score
.as_mut()
.unwrap()
.add_score(department.clone(), amount);
}
});
Ok(())
}

pub fn subtract_reputation_score_from_department(
address: T::AccountId,
department: Vec<u8>,
amount: i64,
) -> DispatchResult {
ReputationScoreOfAccount::<T>::mutate(address, |reputation_score| {
if let Some(reputation_score) = reputation_score.as_mut() {
reputation_score.subtract_score(department.clone(), amount);
} else {
*reputation_score = Some(ReputationScore::new());
reputation_score
.as_mut()
.unwrap()
.subtract_score(department.clone(), amount);
}
});
Ok(())
}

pub fn get_department_reputation_score(
address: T::AccountId,
department: Vec<u8>,
) -> Option<i64> {
ReputationScoreOfAccount::<T>::get(address)
.and_then(|reputation_score| reputation_score.get_department_score(department.clone()))
}

pub fn get_all_department_reputation_scores(address: T::AccountId) -> Vec<(Vec<u8>, i64)> {
ReputationScoreOfAccount::<T>::get(address)
.map(|reputation_score| {
reputation_score
.get_all_departments()
.iter()
.map(|(v, i)| (v.clone(), i.clone()))
.collect()
})
.unwrap_or_default()
}
pub fn get_total_reputation_score(address: T::AccountId) -> i64 {
ReputationScoreOfAccount::<T>::get(address)
.map_or(0, |reputation_score| reputation_score.get_total_score())
}
}
5 changes: 5 additions & 0 deletions custom-pallets/shared-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use frame_support::sp_runtime;
use frame_support::traits::BuildGenesisConfig;
use frame_system::pallet_prelude::*;
use sp_std::prelude::*;
use types::ReputationScore;

type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
type Score = i64;
Expand Down Expand Up @@ -63,6 +64,10 @@ pub mod pallet {

// Keep winning representatives of department in shared storage

#[pallet::storage]
#[pallet::getter(fn reputation_score)]
pub type ReputationScoreOfAccount<T:Config> = StorageMap<_, Blake2_128Concat, T::AccountId, ReputationScore>;

#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub approved_citizen_address: Vec<T::AccountId>,
Expand Down
54 changes: 6 additions & 48 deletions custom-pallets/shared-storage/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ 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)]

/// Reputation scores that can be used for schelling game.
#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo)]
pub struct ReputationScore {
pub departments: BTreeMap<Vec<u8>, i64>,
pub total_score: i64,
Expand Down Expand Up @@ -41,10 +43,10 @@ impl ReputationScore {
self.departments.get(&department).copied()
}

pub fn get_all_departments(&self) -> Vec<(Vec<u8>, &i64)> {
pub fn get_all_departments(&self) -> Vec<(Vec<u8>, i64)> {
self.departments
.iter()
.map(|(v, i)| (v.clone(), i))
.map(|(v, i)| (v.clone(), i.clone()))
.collect()
}

Expand Down Expand Up @@ -72,48 +74,4 @@ impl ReputationScore {
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<T: Config> {
id: T::AccountId,
reputation_score: ReputationScore,
}

impl<T: Config> Account<T> {
pub fn new(id: T::AccountId) -> Self {
Account {
id: id,
reputation_score: ReputationScore::new(),
}
}

pub fn add_department_score(&mut self, department: Vec<u8>, score: i64) {
self.reputation_score.add_department(department, score);
}

pub fn update_department_score(&mut self, department: Vec<u8>, score: i64) {
self.reputation_score.update_department(department, score);
}

pub fn get_department_score(&self, department: Vec<u8>) -> Option<i64> {
self.reputation_score.get_department_score(department)
}

pub fn get_all_department_scores(&self) -> Vec<(Vec<u8>, &i64)> {
self.reputation_score.get_all_departments()
}

pub fn add_score(&mut self, department: Vec<u8>, amount: i64) {
self.reputation_score.add_score(department, amount);
}

pub fn subtract_score(&mut self, department: Vec<u8>, amount: i64) -> bool {
self.reputation_score.subtract_score(department, amount)
}

pub fn get_total_reputation_score(&self) -> i64 {
self.reputation_score.get_total_score()
}
}
}

0 comments on commit c6f8a78

Please sign in to comment.