Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
project
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed Nov 12, 2023
1 parent 9b54e37 commit c3e3f07
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 32 deletions.
2 changes: 1 addition & 1 deletion pallets/department-funding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
type BalanceOf<T> = <<T as Config>::Currency as Currency<AccountIdOf<T>>>::Balance;
pub type BlockNumberOf<T> = <T as frame_system::Config>::BlockNumber;
pub type SumTreeNameType<T> = SumTreeName<AccountIdOf<T>, BlockNumberOf<T>>;
type DeparmentId = u128;
type DeparmentId = u64;

#[frame_support::pallet]
pub mod pallet {
Expand Down
23 changes: 21 additions & 2 deletions pallets/project-tips/src/extras.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
use crate::*;
use types::{TippingName, TippingValue};
use types::{Project, TippingName, TippingValue};

impl<T: Config> Project<T> {
pub fn new(
project_id: ProjectId,
department_id: DepartmentId,
tipping_name: TippingName,
funding_needed: BalanceOf<T>,
project_leader: T::AccountId,
) -> Self {
Project {
created: new_who_and_when::<T>(project_leader.clone()),
project_id,
department_id,
tipping_name,
funding_needed,
project_leader,
}
}
}

impl<T: Config> Pallet<T> {
pub(super) fn get_phase_data() -> PhaseData<T> {
T::SchellingGameSharedSource::create_phase_data(50, 5, 3, 100, (100, 100))
}
pub fn ensure_min_stake_deparment(department_id: DeparmentId) -> DispatchResult {
pub fn ensure_min_stake_deparment(department_id: DepartmentId) -> DispatchResult {
let stake = DepartmentStakeBalance::<T>::get(department_id);
let min_stake = MinimumDepartmentStake::<T>::get();
// println!("stake {:?}", stake);
Expand Down
64 changes: 38 additions & 26 deletions pallets/project-tips/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use frame_support::sp_runtime::SaturatedConversion;
use frame_support::sp_std::prelude::*;
use frame_support::{
dispatch::{DispatchError, DispatchResult},
ensure
ensure,
};
use frame_support::{
traits::{Currency, ExistenceRequirement, Get, ReservableCurrency, WithdrawReasons},
Expand All @@ -34,16 +34,19 @@ use pallet_support::{
ensure_content_is_valid, new_who_and_when, remove_from_vec, Content, PositiveExternalityPostId,
WhoAndWhen, WhoAndWhenOf,
};
use schelling_game_shared::types::{Period, RangePoint, SchellingGameType, PhaseData};
use schelling_game_shared::types::{Period, PhaseData, RangePoint, SchellingGameType};
use schelling_game_shared_link::SchellingGameSharedLink;
use shared_storage_link::SharedStorageLink;
use sortition_sum_game::types::SumTreeName;
use types::TippingName;
use types::{Project, TippingName, TippingValue};
pub use types::PROJECT_ID;

type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
type BalanceOf<T> = <<T as Config>::Currency as Currency<AccountIdOf<T>>>::Balance;
pub type BlockNumberOf<T> = <T as frame_system::Config>::BlockNumber;
pub type SumTreeNameType<T> = SumTreeName<AccountIdOf<T>, BlockNumberOf<T>>;
type DeparmentId = u128;
type DepartmentId = u64;
type ProjectId = u64;

#[frame_support::pallet(dev_mode)]
pub mod pallet {
Expand All @@ -57,7 +60,7 @@ pub mod pallet {

/// Configure the pallet by specifying the parameters and types on which it depends.
#[pallet::config]
pub trait Config: frame_system::Config + schelling_game_shared::Config {
pub trait Config: frame_system::Config + schelling_game_shared::Config + pallet_timestamp::Config {
/// Because this pallet emits events, it depends on the runtime's definition of an event.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// Type representing the weight of this pallet
Expand All @@ -73,12 +76,10 @@ pub mod pallet {
RangePoint = RangePoint,
Period = Period,
PhaseData = PhaseData<Self>,

>;
type Currency: ReservableCurrency<Self::AccountId>;
}


// The pallet's runtime storage items.
// https://docs.substrate.io/main-docs/build/runtime-storage/
#[pallet::storage]
Expand All @@ -87,22 +88,30 @@ pub mod pallet {
// https://docs.substrate.io/main-docs/build/runtime-storage/#declaring-storage-items
pub type Something<T> = StorageValue<_, u32>;



#[pallet::type_value]
pub fn MinimumDepartmentStake<T: Config>() -> BalanceOf<T> {
10000u128.saturated_into::<BalanceOf<T>>()
}

#[pallet::type_value]
pub fn DefaultForNextProjectId() -> ProjectId {
PROJECT_ID
}

#[pallet::storage]
#[pallet::getter(fn next_project_id)]
pub type NextProjectId<T: Config> =
StorageValue<_, ProjectId, ValueQuery, DefaultForNextProjectId>;

#[pallet::storage]
#[pallet::getter(fn department_stake)]
pub type DepartmentStakeBalance<T: Config> =
StorageMap<_, Twox64Concat, DeparmentId, BalanceOf<T>, ValueQuery>;
StorageMap<_, Twox64Concat, DepartmentId, BalanceOf<T>, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn validation_department_block_number)]
pub type ValidationDepartmentBlock<T: Config> =
StorageMap<_, Blake2_128Concat, DeparmentId, BlockNumberOf<T>, ValueQuery>;
StorageMap<_, Blake2_128Concat, DepartmentId, BlockNumberOf<T>, ValueQuery>;

// Pallets use events to inform users when important changes are made.
// https://docs.substrate.io/main-docs/build/events-errors/
Expand All @@ -124,35 +133,39 @@ pub mod pallet {
LessThanMinStake,
CannotStakeNow,
ChoiceOutOfRange,
FundingMoreThanTippingValue,
}

// Dispatchable functions allows users to interact with the pallet and invoke state changes.
// These functions materialize as "extrinsics", which are often compared to transactions.
// Dispatchable functions must be annotated with a weight and must return a DispatchResult.
#[pallet::call]
impl<T: Config> Pallet<T> {

#[pallet::call_index(0)]
#[pallet::weight(0)]
pub fn add_project_stake(
origin: OriginFor<T>,
department_id: DeparmentId,
department_id: DepartmentId,
tipping_name: TippingName,
funding_needed: BalanceOf<T>,
) -> DispatchResult {
let who = ensure_signed(origin)?;

let tipping_value = Self::value_of_tipping_name(tipping_name);
let max_tipping_value = tipping_value.max_tipping_value;
let stake_required = tipping_value.stake_required;
let project_id = Self::next_project_id();

let new_project: Project<T> = Project::new(project_id, department_id, tipping_name, funding_needed, who.clone());



ensure!(funding_needed <= max_tipping_value, Error::<T>::FundingMoreThanTippingValue);
// Check user has done kyc
let _ = <T as pallet::Config>::Currency::withdraw(
&who,
tipping_value.stake_required,
stake_required,
WithdrawReasons::TRANSFER,
ExistenceRequirement::AllowDeath,
)?;

// let stake = DepartmentStakeBalance::<T>::get(department_id);
// let total_balance = stake.saturating_add(deposit);
// DepartmentStakeBalance::<T>::insert(department_id, total_balance);
Expand All @@ -178,8 +191,7 @@ pub mod pallet {
#[pallet::weight(0)]
pub fn apply_staking_period(
origin: OriginFor<T>,
department_id: DeparmentId,

department_id: DepartmentId,
) -> DispatchResult {
let who = ensure_signed(origin)?;

Expand Down Expand Up @@ -222,7 +234,7 @@ pub mod pallet {
#[pallet::weight(0)]
pub fn apply_jurors_positive_externality(
origin: OriginFor<T>,
department_id: DeparmentId,
department_id: DepartmentId,
stake: BalanceOf<T>,
) -> DispatchResult {
let who = ensure_signed(origin)?;
Expand All @@ -246,7 +258,7 @@ pub mod pallet {

#[pallet::call_index(3)]
#[pallet::weight(0)]
pub fn pass_period(origin: OriginFor<T>, department_id: DeparmentId) -> DispatchResult {
pub fn pass_period(origin: OriginFor<T>, department_id: DepartmentId) -> DispatchResult {
let _who = ensure_signed(origin)?;

let pe_block_number = <ValidationDepartmentBlock<T>>::get(department_id);
Expand All @@ -267,7 +279,7 @@ pub mod pallet {
#[pallet::weight(0)]
pub fn draw_jurors_positive_externality(
origin: OriginFor<T>,
department_id: DeparmentId,
department_id: DepartmentId,
iterations: u64,
) -> DispatchResult {
let _who = ensure_signed(origin)?;
Expand All @@ -290,7 +302,7 @@ pub mod pallet {
// Stop drawn juror to unstake ✔️
#[pallet::call_index(5)]
#[pallet::weight(0)]
pub fn unstaking(origin: OriginFor<T>, department_id: DeparmentId) -> DispatchResult {
pub fn unstaking(origin: OriginFor<T>, department_id: DepartmentId) -> DispatchResult {
let who = ensure_signed(origin)?;
let pe_block_number = <ValidationDepartmentBlock<T>>::get(department_id);

Expand All @@ -307,7 +319,7 @@ pub mod pallet {
#[pallet::weight(0)]
pub fn commit_vote(
origin: OriginFor<T>,
department_id: DeparmentId,
department_id: DepartmentId,
vote_commit: [u8; 32],
) -> DispatchResult {
let who = ensure_signed(origin)?;
Expand All @@ -326,7 +338,7 @@ pub mod pallet {
#[pallet::weight(0)]
pub fn reveal_vote(
origin: OriginFor<T>,
department_id: DeparmentId,
department_id: DepartmentId,
choice: i64,
salt: Vec<u8>,
) -> DispatchResult {
Expand All @@ -347,7 +359,7 @@ pub mod pallet {

#[pallet::call_index(8)]
#[pallet::weight(0)]
pub fn get_incentives(origin: OriginFor<T>, department_id: DeparmentId) -> DispatchResult {
pub fn get_incentives(origin: OriginFor<T>, department_id: DepartmentId) -> DispatchResult {
let _who = ensure_signed(origin)?;
let pe_block_number = <ValidationDepartmentBlock<T>>::get(department_id);
let key = SumTreeName::DepartmentScore {
Expand Down
14 changes: 13 additions & 1 deletion pallets/project-tips/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate as pallet_template;
use frame_support::{traits::{ConstU16, ConstU64, GenesisBuild}};
use frame_support::{parameter_types, traits::{ConstU16, ConstU64, GenesisBuild}};
use sp_core::H256;
use sp_runtime::{
testing::Header,
Expand All @@ -21,6 +21,7 @@ frame_support::construct_runtime!(
System: frame_system,
TemplateModule: pallet_template,
Balances: pallet_balances,
Timestamp: pallet_timestamp,
SharedStorage:shared_storage,
SchellingGameShared: schelling_game_shared,
SortitionSumGame: sortition_sum_game,
Expand Down Expand Up @@ -54,6 +55,17 @@ impl frame_system::Config for Test {
type AccountData = pallet_balances::AccountData<u64>; // New code
}

parameter_types! {
pub const MinimumPeriod: u64 = 5;
}

impl pallet_timestamp::Config for Test {
type Moment = u64;
type OnTimestampSet = ();
type MinimumPeriod = MinimumPeriod;
type WeightInfo = ();
}


impl shared_storage::Config for Test {
type RuntimeEvent = RuntimeEvent;
Expand Down
17 changes: 17 additions & 0 deletions pallets/project-tips/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ use codec::{Decode, Encode, EncodeLike, MaxEncodedLen};
use frame_support::pallet_prelude::*;
use scale_info::TypeInfo;

pub const PROJECT_ID: ProjectId = 1;




#[derive(Encode, Decode, Clone, Copy, Eq, PartialEq, RuntimeDebug, TypeInfo)]
pub enum TippingName {
SmallTipper,
Expand All @@ -18,3 +23,15 @@ pub struct TippingValue<Balance> {
pub stake_required: Balance,
}

#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo)]
pub struct Project<T: Config> {
pub created: WhoAndWhenOf<T>,
pub project_id: ProjectId,
pub department_id: DepartmentId,
pub tipping_name: TippingName,
pub funding_needed: BalanceOf<T>,
pub project_leader: T::AccountId,
}



4 changes: 2 additions & 2 deletions pallets/sortition-sum-game/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ type CitizenId = u64;
pub enum SumTreeName<AccountId, BlockNumber> {
ProfileValidation { citizen_address: AccountId, block_number: BlockNumber},
PositiveExternality {user_address: AccountId, block_number: BlockNumber },
DepartmentScore {department_id: u128, block_number: BlockNumber},
ProjectTips { project_id: u128, block_number: BlockNumber },
DepartmentScore {department_id: u64, block_number: BlockNumber},
ProjectTips { project_id: u64, block_number: BlockNumber },
}


Expand Down

0 comments on commit c3e3f07

Please sign in to comment.