Skip to content

Commit

Permalink
extrinsic #0 added
Browse files Browse the repository at this point in the history
  • Loading branch information
ndkazu committed Dec 30, 2024
1 parent 1051e52 commit 0f6bd3d
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 29 deletions.
Empty file.
32 changes: 32 additions & 0 deletions substrate/frame/opf/src/functions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Helper functions for OPF pallet.

pub use super::*;
impl<T: Config> Pallet<T> {

// Helper function for project registration
pub fn register_new(project_id: ProjectId<T>, amount: BalanceOf<T>) -> DispatchResult{
let submission_block = T::BlockNumberProvider::current_block_number();
let project_infos: ProjectInfo<T> = ProjectInfo { project_id, submission_block, amount};
let mut bounded = Projects::get();
let _ = bounded.try_push(project_infos);
Projects::put(bounded);
Ok(())
}
}
70 changes: 48 additions & 22 deletions substrate/frame/opf/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![cfg_attr(not(feature = "std"), no_std)]

pub use pallet::*;
mod types;
mod functions;
pub use pallet_scheduler as Schedule;
pub use types::*;



#[frame_support::pallet(dev_mode)]
pub mod pallet {
use super::*;
Expand All @@ -30,7 +46,7 @@ pub mod pallet {
+ fungible::Mutate<Self::AccountId>;

/// Provider for the block number.
type BlockNumberProvider: BlockNumberProvider<BlockNumber = BlockNumberFor<Self>>;
type BlockNumberProvider: BlockNumberProvider;

/// Treasury account Id
#[pallet::constant]
Expand All @@ -41,12 +57,12 @@ pub mod pallet {

/// The Scheduler.
type Scheduler: ScheduleAnon<
BlockNumberFor<Self>,
ProvidedBlockNumberFor<Self>,
CallOf<Self>,
PalletsOriginOf<Self>,
Hasher = Self::Hashing,
> + ScheduleNamed<
BlockNumberFor<Self>,
ProvidedBlockNumberFor<Self>,
CallOf<Self>,
PalletsOriginOf<Self>,
Hasher = Self::Hashing,
Expand All @@ -56,7 +72,7 @@ pub mod pallet {
/// After the period has ended, the votes are counted (STOP THE COUNT)
/// and then the funds are distributed into Spends.
#[pallet::constant]
type VotingPeriod: Get<BlockNumberFor<Self>>;
type VotingPeriod: Get<ProvidedBlockNumberFor<Self>>;

/// Maximum number projects that can be accepted by this pallet
#[pallet::constant]
Expand All @@ -66,11 +82,11 @@ pub mod pallet {
/// After the period has passed, a spend is thrown away
/// and the funds are available again for distribution in the pot.
#[pallet::constant]
type ClaimingPeriod: Get<BlockNumberFor<Self>>;
type ClaimingPeriod: Get<ProvidedBlockNumberFor<Self>>;

/// Period after which all the votes are reset.
#[pallet::constant]
type VoteValidityPeriod: Get<BlockNumberFor<Self>>;
type VoteValidityPeriod: Get<ProvidedBlockNumberFor<Self>>;


/// Weight information for extrinsics in this pallet.
Expand Down Expand Up @@ -100,48 +116,51 @@ pub mod pallet {
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// Reward successfully claimed
RewardClaimed { when: BlockNumberFor<T>, amount: BalanceOf<T>, project_id: ProjectId<T> },
RewardClaimed { when: ProvidedBlockNumberFor<T>, amount: BalanceOf<T>, project_id: ProjectId<T> },

/// A Spend was created
SpendCreated { when: BlockNumberFor<T>, amount: BalanceOf<T>, project_id: ProjectId<T> },
SpendCreated { when: ProvidedBlockNumberFor<T>, amount: BalanceOf<T>, project_id: ProjectId<T> },

/// Not yet in the claiming period
NotClaimingPeriod { project_id: ProjectId<T>, claiming_period: BlockNumberFor<T> },
NotClaimingPeriod { project_id: ProjectId<T>, claiming_period: ProvidedBlockNumberFor<T> },

/// Payment will be enacted for corresponding project
WillBeEnacted { project_id: ProjectId<T> },

/// Reward successfully assigned
RewardsAssigned { when: BlockNumberFor<T> },
RewardsAssigned { when: ProvidedBlockNumberFor<T> },

/// User's vote successfully submitted
VoteCasted { who: VoterId<T>, when: BlockNumberFor<T>, project_id: ProjectId<T> },
VoteCasted { who: VoterId<T>, when: ProvidedBlockNumberFor<T>, project_id: ProjectId<T> },

/// User's vote successfully removed
VoteRemoved { who: VoterId<T>, when: BlockNumberFor<T>, project_id: ProjectId<T> },
VoteRemoved { who: VoterId<T>, when: ProvidedBlockNumberFor<T>, project_id: ProjectId<T> },

/// Project added to whitelisted projects list
Projectlisted { when:ProvidedBlockNumberFor<T>, project_id: ProjectId<T> },

/// Project removed from whitelisted projects list
ProjectUnlisted { when: BlockNumberFor<T>, project_id: ProjectId<T> },
ProjectUnlisted { when:ProvidedBlockNumberFor<T>, project_id: ProjectId<T> },

/// Project Funding Accepted by voters
ProjectFundingAccepted {
project_id: ProjectId<T>,
when: BlockNumberFor<T>,
when:ProvidedBlockNumberFor<T>,
round_number: u32,
amount: BalanceOf<T>,
},

/// Project Funding rejected by voters
ProjectFundingRejected { when: BlockNumberFor<T>, project_id: ProjectId<T> },
ProjectFundingRejected { when:ProvidedBlockNumberFor<T>, project_id: ProjectId<T> },

/// A new voting round started
VotingRoundStarted { when: BlockNumberFor<T>, round_number: u32 },
VotingRoundStarted { when:ProvidedBlockNumberFor<T>, round_number: u32 },

/// The users voting period ended. Reward calculation will start.
VoteActionLocked { when: BlockNumberFor<T>, round_number: u32 },
VoteActionLocked { when:ProvidedBlockNumberFor<T>, round_number: u32 },

/// The voting round ended
VotingRoundEnded { when: BlockNumberFor<T>, round_number: u32 },
VotingRoundEnded { when:ProvidedBlockNumberFor<T>, round_number: u32 },
}

#[pallet::error]
Expand All @@ -168,7 +187,7 @@ pub mod pallet {

/*#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(n: BlockNumberFor<T>) -> Weight {
fn on_initialize(n:ProvidedBlockNumberFor<T>) -> Weight {
Self::begin_block(n)
}
}*/
Expand All @@ -178,7 +197,14 @@ pub mod pallet {

#[pallet::call_index(0)]
#[transactional]
pub fn register_project(origin: OriginFor<T>) -> DispatchResult{
pub fn register_project(origin: OriginFor<T>, project_id: ProjectId<T>, amount: BalanceOf<T>) -> DispatchResult{
let _caller = ensure_signed(origin)?;
let when = T::BlockNumberProvider::current_block_number();
Self::register_new(project_id.clone(), amount)?;
Self::deposit_event(Event::Projectlisted {
when,
project_id,
});
Ok(())
}

Expand Down
15 changes: 8 additions & 7 deletions substrate/frame/opf/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub type PalletsOriginOf<T> =
pub const DISTRIBUTION_ID: LockIdentifier = *b"distribu";
pub type RoundIndex = u32;
pub type VoterId<T> = AccountIdOf<T>;
pub type ProvidedBlockNumberFor<T> = <<T as Config>::BlockNumberProvider as BlockNumberProvider>::BlockNumber;

/// The state of the payment claim.
#[derive(Encode, Decode, Clone, PartialEq, Eq, MaxEncodedLen, RuntimeDebug, TypeInfo, Default)]
Expand All @@ -77,13 +78,13 @@ pub struct SpendInfo<T: Config> {
/// The asset amount of the spend.
pub amount: BalanceOf<T>,
/// The block number from which the spend can be claimed(24h after SpendStatus Creation).
pub valid_from: BlockNumberFor<T>,
pub valid_from:ProvidedBlockNumberFor<T>,
/// Corresponding project id
pub whitelisted_project: AccountIdOf<T>,
/// Has it been claimed?
pub claimed: bool,
/// Claim Expiration block
pub expire: BlockNumberFor<T>,
pub expire:ProvidedBlockNumberFor<T>,
}

impl<T: Config> SpendInfo<T> {
Expand All @@ -92,7 +93,7 @@ impl<T: Config> SpendInfo<T> {
let whitelisted_project = whitelisted.project_id.clone();
let claimed = false;
let valid_from =
<frame_system::Pallet<T>>::block_number();
T::BlockNumberProvider::current_block_number();
let expire = valid_from.clone().saturating_add(T::ClaimingPeriod::get());

let spend = SpendInfo { amount, valid_from, whitelisted_project, claimed, expire };
Expand All @@ -111,7 +112,7 @@ pub struct ProjectInfo<T: Config> {
pub project_id: ProjectId<T>,

/// Block at which the project was submitted for reward distribution
pub submission_block: BlockNumberFor<T>,
pub submission_block:ProvidedBlockNumberFor<T>,

/// Amount to be lock & pay for this project
pub amount: BalanceOf<T>,
Expand All @@ -131,7 +132,7 @@ pub struct VoteInfo<T: Config> {

pub conviction: Conviction,

pub funds_unlock_block: BlockNumberFor<T>,
pub funds_unlock_block:ProvidedBlockNumberFor<T>,
}

// If no conviction, user's funds are released at the end of the voting round
Expand Down Expand Up @@ -163,8 +164,8 @@ impl<T: Config> Default for VoteInfo<T> {
#[scale_info(skip_type_params(T))]
pub struct VotingRoundInfo<T: Config> {
pub round_number: u32,
pub round_starting_block: BlockNumberFor<T>,
pub round_ending_block: BlockNumberFor<T>,
pub round_starting_block:ProvidedBlockNumberFor<T>,
pub round_ending_block:ProvidedBlockNumberFor<T>,
pub total_positive_votes_amount: BalanceOf<T>,
pub total_negative_votes_amount: BalanceOf<T>,
}
Expand Down

0 comments on commit 0f6bd3d

Please sign in to comment.