Skip to content

Commit

Permalink
profile validation location
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed Dec 1, 2024
1 parent 82e044f commit f6003be
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 17 deletions.
8 changes: 7 additions & 1 deletion custom-pallets/profile-validation/src/extras.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use crate::*;

impl<T: Config> CitizenDetailsPost<T> {
pub fn new(citizen_id: CitizenId, created_by: T::AccountId, content: Content) -> Self {
pub fn new(
citizen_id: CitizenId,
created_by: T::AccountId,
content: Content,
location: LocationDetails,
) -> Self {
CitizenDetailsPost {
created: new_who_and_when::<T>(created_by.clone()),
content,
location,
citizen_id,
owner: created_by,
edited: false,
Expand Down
24 changes: 18 additions & 6 deletions custom-pallets/profile-validation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mod extras;
mod permissions;
mod types;

use crate::types::{ChallengeEvidencePost, ChallengerFundInfo, ProfileFundInfo};
use crate::types::{ChallengeEvidencePost, ChallengerFundInfo, LocationDetails, ProfileFundInfo};
use frame_support::sp_runtime::traits::AccountIdConversion;
use frame_support::sp_runtime::traits::{CheckedAdd, CheckedSub};
use frame_support::sp_runtime::SaturatedConversion;
Expand Down Expand Up @@ -268,15 +268,23 @@ pub mod pallet {
#[pallet::call_index(0)]
#[pallet::weight(0)]
pub fn add_citizen(origin: OriginFor<T>, content: Content) -> DispatchResult {
pub fn add_citizen(
origin: OriginFor<T>,
content: Content,
location: LocationDetails,
) -> DispatchResult {
let who = ensure_signed(origin)?;
let count = Self::next_citizen_id();
match <GetCitizenId<T>>::get(&who) {
Some(citizen_id) => {
let total_funded = <ProfileTotalFundCollected<T>>::get(who.clone());
if total_funded == 0u128.saturated_into::<BalanceOf<T>>() {
let new_post: CitizenDetailsPost<T> =
CitizenDetailsPost::new(citizen_id, who.clone(), content.clone());
let new_post: CitizenDetailsPost<T> = CitizenDetailsPost::new(
citizen_id,
who.clone(),
content.clone(),
location.clone(),
);
<CitizenProfile<T>>::insert(who.clone(), new_post);
Ok(())
} else {
Expand All @@ -286,8 +294,12 @@ pub mod pallet {
None => {
<GetCitizenId<T>>::insert(&who, count);

let new_post: CitizenDetailsPost<T> =
CitizenDetailsPost::new(count, who.clone(), content.clone());
let new_post: CitizenDetailsPost<T> = CitizenDetailsPost::new(
count,
who.clone(),
content.clone(),
location.clone(),
);

<CitizenProfile<T>>::insert(who.clone(), new_post);
NextCitizenId::<T>::mutate(|n| {
Expand Down
114 changes: 104 additions & 10 deletions custom-pallets/profile-validation/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::types::CitizenDetailsPost;
use crate::types::{CitizenDetailsPost, LocationDetails};
use crate::{mock::*, Error, Event};
use frame_support::{assert_noop, assert_ok};
use pallet_schelling_game_shared::types::Period;
Expand All @@ -16,11 +16,22 @@ fn add_citizen_profile_check() {
.as_bytes()
.to_vec(),
);
assert_ok!(ProfileValidation::add_citizen(RuntimeOrigin::signed(1), content.clone()));
let location: LocationDetails = LocationDetails {
country: "India".as_bytes().to_vec(),
state: "Odisha".as_bytes().to_vec(),
city: "Bhubaneswar".as_bytes().to_vec(),
street: None,
};
assert_ok!(ProfileValidation::add_citizen(
RuntimeOrigin::signed(1),
content.clone(),
location.clone()
));
let data = ProfileValidation::citizen_profile(1);
let profile = Some(CitizenDetailsPost::<Test> {
created: WhoAndWhen { account: 1, block: 1, time: 0 },
content,
location,
citizen_id: 1,
owner: 1,
edited: false,
Expand All @@ -35,11 +46,23 @@ fn add_citizen_profile_check() {
.as_bytes()
.to_vec(),
);
assert_ok!(ProfileValidation::add_citizen(RuntimeOrigin::signed(1), content.clone()));

let location: LocationDetails = LocationDetails {
country: "India".as_bytes().to_vec(),
state: "Odisha".as_bytes().to_vec(),
city: "Bhubaneswar".as_bytes().to_vec(),
street: None,
};
assert_ok!(ProfileValidation::add_citizen(
RuntimeOrigin::signed(1),
content.clone(),
location.clone()
));
let data = ProfileValidation::citizen_profile(1);
let profile = Some(CitizenDetailsPost::<Test> {
created: WhoAndWhen { account: 1, block: 5, time: 0 },
content,
location,
citizen_id: 1,
owner: 1,
edited: false,
Expand All @@ -61,11 +84,22 @@ fn check_fund_addition() {
.as_bytes()
.to_vec(),
);
assert_ok!(ProfileValidation::add_citizen(RuntimeOrigin::signed(1), content.clone()));
let location: LocationDetails = LocationDetails {
country: "India".as_bytes().to_vec(),
state: "Odisha".as_bytes().to_vec(),
city: "Bhubaneswar".as_bytes().to_vec(),
street: None,
};
assert_ok!(ProfileValidation::add_citizen(
RuntimeOrigin::signed(1),
content.clone(),
location.clone()
));
let data = ProfileValidation::citizen_profile(1);
let profile = Some(CitizenDetailsPost::<Test> {
created: WhoAndWhen { account: 1, block: 10, time: 0 },
content,
location,
citizen_id: 1,
owner: 1,
edited: false,
Expand All @@ -84,8 +118,18 @@ fn check_fund_addition() {
.as_bytes()
.to_vec(),
);
let location: LocationDetails = LocationDetails {
country: "India".as_bytes().to_vec(),
state: "Odisha".as_bytes().to_vec(),
city: "Bhubaneswar".as_bytes().to_vec(),
street: None,
};
assert_noop!(
ProfileValidation::add_citizen(RuntimeOrigin::signed(1), content.clone()),
ProfileValidation::add_citizen(
RuntimeOrigin::signed(1),
content.clone(),
location.clone()
),
Error::<Test>::NoMoreUpdates
);
let data = ProfileValidation::profile_fund_details(1, 3).unwrap();
Expand Down Expand Up @@ -124,7 +168,17 @@ fn challenge_evidence() {
.as_bytes()
.to_vec(),
);
assert_ok!(ProfileValidation::add_citizen(RuntimeOrigin::signed(1), content.clone()));
let location: LocationDetails = LocationDetails {
country: "India".as_bytes().to_vec(),
state: "Odisha".as_bytes().to_vec(),
city: "Bhubaneswar".as_bytes().to_vec(),
street: None,
};
assert_ok!(ProfileValidation::add_citizen(
RuntimeOrigin::signed(1),
content.clone(),
location.clone()
));
assert_ok!(ProfileValidation::add_profile_stake(RuntimeOrigin::signed(3), 1, 1000));
let key = SumTreeName::ProfileValidation { citizen_address: 1, block_number: 1 };
let period = SchellingGameShared::get_period(key.clone());
Expand Down Expand Up @@ -181,7 +235,17 @@ fn challenge_profile_after_time_for_staking_over_test() {
.as_bytes()
.to_vec(),
);
assert_ok!(ProfileValidation::add_citizen(RuntimeOrigin::signed(1), content.clone()));
let location: LocationDetails = LocationDetails {
country: "India".as_bytes().to_vec(),
state: "Odisha".as_bytes().to_vec(),
city: "Bhubaneswar".as_bytes().to_vec(),
street: None,
};
assert_ok!(ProfileValidation::add_citizen(
RuntimeOrigin::signed(1),
content.clone(),
location.clone()
));
assert_ok!(ProfileValidation::add_profile_stake(RuntimeOrigin::signed(3), 1, 1000));
let key = SumTreeName::ProfileValidation { citizen_address: 1, block_number: 1 };
let period = SchellingGameShared::get_period(key.clone());
Expand Down Expand Up @@ -225,7 +289,17 @@ fn return_profile_stake_test() {
.as_bytes()
.to_vec(),
);
assert_ok!(ProfileValidation::add_citizen(RuntimeOrigin::signed(1), content.clone()));
let location: LocationDetails = LocationDetails {
country: "India".as_bytes().to_vec(),
state: "Odisha".as_bytes().to_vec(),
city: "Bhubaneswar".as_bytes().to_vec(),
street: None,
};
assert_ok!(ProfileValidation::add_citizen(
RuntimeOrigin::signed(1),
content.clone(),
location.clone()
));
let balance = Balances::free_balance(3);
assert_eq!(300000, balance);
assert_ok!(ProfileValidation::add_profile_stake(RuntimeOrigin::signed(3), 1, 400));
Expand Down Expand Up @@ -279,7 +353,17 @@ fn schelling_game_test() {
.as_bytes()
.to_vec(),
);
assert_ok!(ProfileValidation::add_citizen(RuntimeOrigin::signed(1), content.clone()));
let location: LocationDetails = LocationDetails {
country: "India".as_bytes().to_vec(),
state: "Odisha".as_bytes().to_vec(),
city: "Bhubaneswar".as_bytes().to_vec(),
street: None,
};
assert_ok!(ProfileValidation::add_citizen(
RuntimeOrigin::signed(1),
content.clone(),
location.clone()
));
assert_ok!(ProfileValidation::add_profile_stake(RuntimeOrigin::signed(3), 1, 1000));
let challenge_content: Content = Content::IPFS(
"bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhabc"
Expand Down Expand Up @@ -443,7 +527,17 @@ fn test_draw_juror() {
.as_bytes()
.to_vec(),
);
assert_ok!(ProfileValidation::add_citizen(RuntimeOrigin::signed(1), content.clone()));
let location: LocationDetails = LocationDetails {
country: "India".as_bytes().to_vec(),
state: "Odisha".as_bytes().to_vec(),
city: "Bhubaneswar".as_bytes().to_vec(),
street: None,
};
assert_ok!(ProfileValidation::add_citizen(
RuntimeOrigin::signed(1),
content.clone(),
location.clone()
));

assert_ok!(ProfileValidation::add_profile_stake(RuntimeOrigin::signed(3), 1, 1000));
let challenge_content: Content = Content::IPFS(
Expand Down
10 changes: 10 additions & 0 deletions custom-pallets/profile-validation/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,23 @@ pub struct CitizenDetailsPost<T: Config> {
pub created: WhoAndWhenOf<T>,
pub content: Content,
pub citizen_id: CitizenId,
pub location: LocationDetails,
pub owner: T::AccountId,
pub edited: bool,
pub hidden: bool,
pub upvotes_count: u32,
pub downvotes_count: u32,
}

#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo)]
#[scale_info(skip_type_params(T))]
pub struct LocationDetails {
pub country: Vec<u8>,
pub state: Vec<u8>,
pub city: Vec<u8>,
pub street: Option<Vec<u8>>,
}

#[derive(
PartialEq, Eq, PartialOrd, Ord, Default, Clone, Encode, Decode, MaxEncodedLen, TypeInfo,
)]
Expand Down

0 comments on commit f6003be

Please sign in to comment.