Skip to content

Commit ea242c4

Browse files
JuaniRioslrazovic
authored andcommittedMar 5, 2025
General Cleanup
1 parent 5208715 commit ea242c4

27 files changed

+857
-1181
lines changed
 

‎pallets/funding/src/benchmarking.rs

+7-14
Original file line numberDiff line numberDiff line change
@@ -542,21 +542,14 @@ mod benchmarks {
542542

543543
// Storage
544544
for (bid_params, price) in extrinsic_bids_post_bucketing.clone() {
545-
let bid_filter = BidInfoFilter::<T> {
546-
id: None,
547-
project_id: Some(project_id),
548-
bidder: Some(bidder.clone()),
549-
status: Some(BidStatus::YetUnknown),
550-
original_ct_amount: Some(bid_params.amount),
551-
original_ct_usd_price: Some(price),
552-
funding_asset: Some(AcceptedFundingAsset::USDT),
553-
funding_asset_amount_locked: None,
554-
mode: Some(bid_params.mode),
555-
plmc_bond: None,
556-
when: None,
557-
};
558545
Bids::<T>::iter_prefix_values(project_id)
559-
.find(|stored_bid| bid_filter.matches_bid(stored_bid))
546+
.find(|stored_bid| {
547+
stored_bid.bidder == bidder.clone() &&
548+
stored_bid.original_ct_amount == bid_params.amount &&
549+
stored_bid.original_ct_usd_price == price &&
550+
stored_bid.funding_asset == AcceptedFundingAsset::USDT &&
551+
stored_bid.mode == bid_params.mode
552+
})
560553
.expect("bid not found");
561554
}
562555
}

‎pallets/funding/src/functions/1_application.rs

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use super::*;
55

66
impl<T: Config> Pallet<T> {
7+
/// Make sure the data provided by the issuer on project creation makes sense.
78
fn project_validation(
89
project_metadata: &ProjectMetadataOf<T>,
910
issuer: AccountIdOf<T>,
@@ -53,6 +54,7 @@ impl<T: Config> Pallet<T> {
5354
Ok((project_details, bucket))
5455
}
5556

57+
/// Create a new project.
5658
#[transactional]
5759
pub fn do_create_project(
5860
issuer: &AccountIdOf<T>,
@@ -93,6 +95,7 @@ impl<T: Config> Pallet<T> {
9395
Ok(PostDispatchInfo { actual_weight: None, pays_fee: Pays::No })
9496
}
9597

98+
/// Edit the project information before starting the raise
9699
#[transactional]
97100
pub fn do_edit_project(
98101
issuer: AccountIdOf<T>,
@@ -121,6 +124,7 @@ impl<T: Config> Pallet<T> {
121124
Ok(PostDispatchInfo { actual_weight: None, pays_fee: Pays::No })
122125
}
123126

127+
/// Remove the project before the raise started.
124128
#[transactional]
125129
pub fn do_remove_project(issuer: AccountIdOf<T>, project_id: ProjectId, did: Did) -> DispatchResultWithPostInfo {
126130
// * Get variables *

‎pallets/funding/src/functions/2_evaluation.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use super::*;
33
use polimec_common::ProvideAssetPrice;
44
impl<T: Config> Pallet<T> {
5+
/// Start the evaluation round of a project. This is how the raise is started.
56
#[transactional]
67
pub fn do_start_evaluation(caller: AccountIdOf<T>, project_id: ProjectId) -> DispatchResultWithPostInfo {
78
// * Get variables *
@@ -29,6 +30,7 @@ impl<T: Config> Pallet<T> {
2930
Ok(PostDispatchInfo { actual_weight: None, pays_fee: Pays::No })
3031
}
3132

33+
/// End the evaluation round of a project, and start the auction round.
3234
#[transactional]
3335
pub fn do_end_evaluation(project_id: ProjectId) -> DispatchResult {
3436
// * Get variables *
@@ -45,10 +47,7 @@ impl<T: Config> Pallet<T> {
4547
// * Branch in possible project paths *
4648
// Successful path
4749
return if is_funded {
48-
let mut project_ids = ProjectsInAuctionRound::<T>::get().to_vec();
49-
project_ids.push(project_id);
50-
let project_ids = WeakBoundedVec::force_from(project_ids, None);
51-
ProjectsInAuctionRound::<T>::put(project_ids);
50+
ProjectsInAuctionRound::<T>::insert(project_id, ());
5251
Self::transition_project(
5352
project_id,
5453
project_details,
@@ -72,6 +71,7 @@ impl<T: Config> Pallet<T> {
7271
}
7372
}
7473

74+
/// Place an evaluation on a project
7575
#[transactional]
7676
pub fn do_evaluate(
7777
evaluator: &AccountIdOf<T>,

‎pallets/funding/src/functions/3_auction.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use super::*;
33

44
impl<T: Config> Pallet<T> {
5+
/// Place a bid on a project in the auction round
56
#[transactional]
67
pub fn do_bid(params: DoBidParams<T>) -> DispatchResult {
78
// * Get variables *
@@ -119,6 +120,8 @@ impl<T: Config> Pallet<T> {
119120
Ok(())
120121
}
121122

123+
/// Inner function to perform bids within a bucket. do_bid makes sure to split the bid into buckets and call this as
124+
/// many times as necessary
122125
#[transactional]
123126
fn do_perform_bid(do_perform_bid_params: DoPerformBidParams<T>) -> Result<BidInfoOf<T>, DispatchError> {
124127
let DoPerformBidParams {
@@ -192,11 +195,13 @@ impl<T: Config> Pallet<T> {
192195
Ok(new_bid)
193196
}
194197

198+
/// Process a bid that was outbid by a new bid. This will set it to Rejected so the user can get their funds back with `settle_bid` and bid again.
195199
pub fn do_process_next_oversubscribed_bid(project_id: ProjectId) -> DispatchResult {
196200
// Load and validate initial state
197201
let project_metadata = ProjectsMetadata::<T>::get(project_id).ok_or(Error::<T>::ProjectMetadataNotFound)?;
198202
let bucket = Buckets::<T>::get(project_id).ok_or(Error::<T>::BucketNotFound)?;
199203
let mut ct_amount_oversubscribed = CTAmountOversubscribed::<T>::get(project_id);
204+
200205
ensure!(ct_amount_oversubscribed > Zero::zero(), Error::<T>::NoBidsOversubscribed);
201206

202207
// Determine the current cutoff
@@ -206,21 +211,21 @@ impl<T: Config> Pallet<T> {
206211
if matches!(bid.status, BidStatus::PartiallyAccepted(_)) {
207212
cutoff
208213
} else {
209-
let (new_price, new_index) = Self::get_next_cutoff(project_id, bucket.delta_price, bid_price, bid_index)?;
214+
let (new_price, new_index) =
215+
Self::get_next_cutoff(project_id, bucket.delta_price, bid_price, bid_index)?;
210216
OutbidBidsCutoff { bid_price: new_price, bid_index: new_index }
211217
}
212218
},
213219
None => {
214220
let first_price = project_metadata.minimum_price;
215-
let first_bounds = BidsBucketBounds::<T>::get(project_id, first_price)
216-
.ok_or(Error::<T>::ImpossibleState)?;
221+
let first_bounds =
222+
BidsBucketBounds::<T>::get(project_id, first_price).ok_or(Error::<T>::ImpossibleState)?;
217223
OutbidBidsCutoff { bid_price: first_price, bid_index: first_bounds.last_bid_index }
218-
}
224+
},
219225
};
220226

221227
// Process the bid at the cutoff
222-
let mut bid = Bids::<T>::get(project_id, current_cutoff.bid_index)
223-
.ok_or(Error::<T>::ImpossibleState)?;
228+
let mut bid = Bids::<T>::get(project_id, current_cutoff.bid_index).ok_or(Error::<T>::ImpossibleState)?;
224229

225230
let bid_amount = match bid.status {
226231
BidStatus::PartiallyAccepted(amount) => amount,
@@ -244,6 +249,7 @@ impl<T: Config> Pallet<T> {
244249
Ok(())
245250
}
246251

252+
/// Get the next bid that should be processed by do_process_next_oversubscribed_bid
247253
pub fn get_next_cutoff(
248254
project_id: ProjectId,
249255
delta_price: PriceOf<T>,

‎pallets/funding/src/functions/5_funding_end.rs ‎pallets/funding/src/functions/4_funding_end.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#[allow(clippy::wildcard_imports)]
22
use super::*;
3-
use itertools::Itertools;
43

54
impl<T: Config> Pallet<T> {
5+
/// End the auction round and the fundraise. Check if the raise was successful or not.
66
#[transactional]
77
pub fn do_end_funding(project_id: ProjectId) -> DispatchResult {
88
// * Get variables *
@@ -20,10 +20,7 @@ impl<T: Config> Pallet<T> {
2020
);
2121
ensure!(ct_amount_oversubscribed.is_zero(), Error::<T>::OversubscribedBidsRemaining);
2222

23-
let mut project_ids = ProjectsInAuctionRound::<T>::get().to_vec();
24-
let (pos, _) = project_ids.iter().find_position(|id| **id == project_id).ok_or(Error::<T>::ImpossibleState)?;
25-
project_ids.remove(pos);
26-
ProjectsInAuctionRound::<T>::put(WeakBoundedVec::force_from(project_ids, None));
23+
ProjectsInAuctionRound::<T>::remove(project_id);
2724

2825
let auction_allocation_size = project_metadata.total_allocation_size;
2926

‎pallets/funding/src/functions/6_settlement.rs ‎pallets/funding/src/functions/5_settlement.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ use polimec_common::{
2121
use sp_runtime::{traits::Zero, Perquintill};
2222

2323
impl<T: Config> Pallet<T> {
24+
/// Start the settlement round. Now users can mint their contribution tokens or get their funds back, and the issuer
25+
/// will get the funds in their funding account.
2426
#[transactional]
2527
pub fn do_start_settlement(project_id: ProjectId) -> DispatchResult {
2628
let mut project_details = ProjectsDetails::<T>::get(project_id).ok_or(Error::<T>::ProjectDetailsNotFound)?;
@@ -104,6 +106,7 @@ impl<T: Config> Pallet<T> {
104106
Ok(())
105107
}
106108

109+
/// Settle an evaluation, by maybe minting CTs, and releasing the PLMC bond.
107110
pub fn do_settle_evaluation(evaluation: EvaluationInfoOf<T>, project_id: ProjectId) -> DispatchResult {
108111
let project_details = ProjectsDetails::<T>::get(project_id).ok_or(Error::<T>::ProjectDetailsNotFound)?;
109112

@@ -154,6 +157,9 @@ impl<T: Config> Pallet<T> {
154157
Ok(())
155158
}
156159

160+
/// Settle a bid. If bid was successful mint the CTs and release the PLMC bond (if multiplier > 1 and mode is Classic).
161+
/// If was unsuccessful, release the PLMC bond and refund the funds.
162+
/// If the project was successful, the issuer will get the funds.
157163
pub fn do_settle_bid(project_id: ProjectId, bid_id: u32) -> DispatchResult {
158164
let project_details = ProjectsDetails::<T>::get(project_id).ok_or(Error::<T>::ProjectDetailsNotFound)?;
159165
let project_metadata = ProjectsMetadata::<T>::get(project_id).ok_or(Error::<T>::ProjectMetadataNotFound)?;
@@ -261,6 +267,7 @@ impl<T: Config> Pallet<T> {
261267
}
262268
}
263269

270+
/// Mark a project as fully settled. Only once this is done we can mark migrations as completed.
264271
pub fn do_mark_project_as_settled(project_id: ProjectId) -> DispatchResult {
265272
let project_details = ProjectsDetails::<T>::get(project_id).ok_or(Error::<T>::ProjectDetailsNotFound)?;
266273
let outcome = match project_details.status {
@@ -288,6 +295,7 @@ impl<T: Config> Pallet<T> {
288295
Ok(())
289296
}
290297

298+
/// Helper function to Mint CTs and handle the payment of new storage with "touch"
291299
fn mint_contribution_tokens(
292300
project_id: ProjectId,
293301
participant: &AccountIdOf<T>,
@@ -300,6 +308,7 @@ impl<T: Config> Pallet<T> {
300308
Ok(())
301309
}
302310

311+
/// Helper function to release the funding asset to the participant
303312
fn release_funding_asset(
304313
project_id: ProjectId,
305314
participant: &AccountIdOf<T>,
@@ -313,7 +322,7 @@ impl<T: Config> Pallet<T> {
313322
T::FundingCurrency::transfer(asset.id(), &project_pot, participant, amount, Preservation::Expendable)?;
314323
Ok(())
315324
}
316-
325+
/// Helper function to release the PLMC bond to the participant
317326
fn release_participation_bond_for(participant: &AccountIdOf<T>, amount: Balance) -> DispatchResult {
318327
if amount.is_zero() {
319328
return Ok(());
@@ -323,6 +332,7 @@ impl<T: Config> Pallet<T> {
323332
Ok(())
324333
}
325334

335+
/// Set the PLMC release schedule if mode was `Classic`. Return the schedule either way.
326336
fn set_plmc_bond_release_with_mode(
327337
participant: AccountIdOf<T>,
328338
plmc_amount: Balance,
@@ -337,6 +347,7 @@ impl<T: Config> Pallet<T> {
337347
}
338348
}
339349

350+
/// Calculate the vesting info and add the PLMC release schedule to the user, or fully release the funds if possible.
340351
fn set_release_schedule_for(
341352
participant: &AccountIdOf<T>,
342353
plmc_amount: Balance,
@@ -361,6 +372,7 @@ impl<T: Config> Pallet<T> {
361372
Ok(vesting_info.duration)
362373
}
363374

375+
/// Slash an evaluator and transfer funds to the treasury.
364376
fn slash_evaluator(evaluation: &EvaluationInfoOf<T>) -> Result<Balance, DispatchError> {
365377
let slash_percentage = T::EvaluatorSlash::get();
366378
let treasury_account = T::BlockchainOperationTreasury::get();
@@ -384,6 +396,7 @@ impl<T: Config> Pallet<T> {
384396
Ok(evaluation.current_plmc_bond.saturating_sub(slashed_amount))
385397
}
386398

399+
/// Reward an evaluator and mint CTs.
387400
fn reward_evaluator(
388401
project_id: ProjectId,
389402
evaluation: &EvaluationInfoOf<T>,

‎pallets/funding/src/functions/7_ct_migration.rs ‎pallets/funding/src/functions/6_ct_migration.rs

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use xcm::v4::MaxPalletNameLen;
44

55
// Offchain migration functions
66
impl<T: Config> Pallet<T> {
7+
/// Mark a project as ready for offchain migration confirmations.
78
#[transactional]
89
pub fn do_start_offchain_migration(project_id: ProjectId, caller: AccountIdOf<T>) -> DispatchResultWithPostInfo {
910
let mut project_details = ProjectsDetails::<T>::get(project_id).ok_or(Error::<T>::ProjectDetailsNotFound)?;
@@ -24,6 +25,7 @@ impl<T: Config> Pallet<T> {
2425
Ok(PostDispatchInfo { actual_weight: None, pays_fee: Pays::No })
2526
}
2627

28+
/// Confirm a user's migrations as completed
2729
#[transactional]
2830
pub fn do_confirm_offchain_migration(
2931
project_id: ProjectId,

‎pallets/funding/src/functions/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ const QUERY_RESPONSE_TIME_WINDOW_BLOCKS: u32 = 20u32;
3131
mod application;
3232
#[path = "3_auction.rs"]
3333
mod auction;
34-
#[path = "7_ct_migration.rs"]
34+
#[path = "6_ct_migration.rs"]
3535
mod ct_migration;
3636
#[path = "2_evaluation.rs"]
3737
mod evaluation;
38-
#[path = "5_funding_end.rs"]
38+
#[path = "4_funding_end.rs"]
3939
mod funding_end;
4040
pub mod misc;
41-
#[path = "6_settlement.rs"]
41+
#[path = "5_settlement.rs"]
4242
mod settlement;
43+
44+
pub mod runtime_api;

0 commit comments

Comments
 (0)