Skip to content

Commit f048206

Browse files
committed
General Cleanup
1 parent 98db241 commit f048206

26 files changed

+721
-1071
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/2_evaluation.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@ impl<T: Config> Pallet<T> {
4545
// * Branch in possible project paths *
4646
// Successful path
4747
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);
48+
ProjectsInAuctionRound::<T>::insert(project_id, ());
5249
Self::transition_project(
5350
project_id,
5451
project_details,

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

+1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ impl<T: Config> Pallet<T> {
197197
let project_metadata = ProjectsMetadata::<T>::get(project_id).ok_or(Error::<T>::ProjectMetadataNotFound)?;
198198
let bucket = Buckets::<T>::get(project_id).ok_or(Error::<T>::BucketNotFound)?;
199199
let mut ct_amount_oversubscribed = CTAmountOversubscribed::<T>::get(project_id);
200+
200201
ensure!(ct_amount_oversubscribed > Zero::zero(), Error::<T>::NoBidsOversubscribed);
201202

202203
// Determine the current cutoff

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

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

54
impl<T: Config> Pallet<T> {
65
#[transactional]
@@ -20,10 +19,7 @@ impl<T: Config> Pallet<T> {
2019
);
2120
ensure!(ct_amount_oversubscribed.is_zero(), Error::<T>::OversubscribedBidsRemaining);
2221

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));
22+
ProjectsInAuctionRound::<T>::remove(project_id);
2723

2824
let auction_allocation_size = project_metadata.total_allocation_size;
2925

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;

pallets/funding/src/instantiator/calculations.rs

+1-105
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,6 @@ impl<
2525
self.execute(|| T::FundingCurrency::minimum_balance(asset_id))
2626
}
2727

28-
pub fn get_funding_asset_unit(&mut self, asset_id: AssetIdOf<T>) -> Balance {
29-
self.execute(|| {
30-
let decimals = T::FundingCurrency::decimals(asset_id);
31-
10u128.pow(decimals as u32)
32-
})
33-
}
34-
35-
pub fn get_ct_account_deposit(&self) -> Balance {
36-
<T as crate::Config>::ContributionTokenCurrency::deposit_required(One::one())
37-
}
38-
3928
pub fn calculate_evaluation_plmc_spent(
4029
&mut self,
4130
evaluations: Vec<EvaluationParams<T>>,
@@ -72,7 +61,7 @@ impl<
7261
while !amount_to_bid.is_zero() {
7362
let bid_amount = if amount_to_bid <= bucket.amount_left { amount_to_bid } else { bucket.amount_left };
7463
output.push((
75-
BidParams::from((bid.bidder.clone(), bid.investor_type.clone(), bid_amount, bid.mode, bid.asset)),
64+
BidParams::from((bid.bidder.clone(), bid.investor_type, bid_amount, bid.mode, bid.asset)),
7665
bucket.current_price,
7766
));
7867
bucket.update(bid_amount);
@@ -170,18 +159,6 @@ impl<
170159
output.merge_accounts(MergeOperation::Add)
171160
}
172161

173-
pub fn calculate_auction_plmc_spent_post_wap(
174-
&mut self,
175-
bids: &Vec<BidParams<T>>,
176-
project_metadata: ProjectMetadataOf<T>,
177-
) -> Vec<UserToPLMCBalance<T>> {
178-
let plmc_charged =
179-
self.calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket(bids, project_metadata.clone(), None);
180-
let plmc_returned = self.calculate_auction_plmc_returned_from_all_bids_made(bids, project_metadata.clone());
181-
182-
plmc_charged.subtract_accounts(plmc_returned)
183-
}
184-
185162
pub fn calculate_auction_funding_asset_charged_with_given_price(
186163
&mut self,
187164
bids: &Vec<BidParams<T>>,
@@ -273,45 +250,6 @@ impl<
273250
output.merge_accounts(MergeOperation::Add)
274251
}
275252

276-
pub fn calculate_auction_funding_asset_spent_post_wap(
277-
&mut self,
278-
bids: &Vec<BidParams<T>>,
279-
project_metadata: ProjectMetadataOf<T>,
280-
) -> Vec<UserToFundingAsset<T>> {
281-
let funding_asset_charged = self.calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket(
282-
bids,
283-
project_metadata.clone(),
284-
None,
285-
);
286-
let funding_asset_returned =
287-
self.calculate_auction_funding_asset_returned_from_all_bids_made(bids, project_metadata.clone());
288-
289-
funding_asset_charged.subtract_accounts(funding_asset_returned)
290-
}
291-
292-
/// Filters the bids that would be rejected after the auction ends.
293-
pub fn filter_bids_after_auction(&self, bids: Vec<BidParams<T>>, total_cts: Balance) -> Vec<BidParams<T>> {
294-
let mut filtered_bids: Vec<BidParams<T>> = Vec::new();
295-
let sorted_bids = bids;
296-
let mut total_cts_left = total_cts;
297-
for bid in sorted_bids {
298-
if total_cts_left >= bid.amount {
299-
total_cts_left.saturating_reduce(bid.amount);
300-
filtered_bids.push(bid);
301-
} else if !total_cts_left.is_zero() {
302-
filtered_bids.push(BidParams::from((
303-
bid.bidder.clone(),
304-
bid.investor_type,
305-
total_cts_left,
306-
bid.mode,
307-
bid.asset,
308-
)));
309-
total_cts_left = Zero::zero();
310-
}
311-
}
312-
filtered_bids
313-
}
314-
315253
pub fn add_otm_fee_to(
316254
&mut self,
317255
balance: &mut Balance,
@@ -351,24 +289,6 @@ impl<
351289
*balance += funding_asset_bond;
352290
}
353291

354-
pub fn generic_map_merge_reduce<M: Clone, K: Ord + Clone, S: Clone>(
355-
&self,
356-
mappings: Vec<Vec<M>>,
357-
key_extractor: impl Fn(&M) -> K,
358-
initial_state: S,
359-
merge_reduce: impl Fn(&M, S) -> S,
360-
) -> Vec<(K, S)> {
361-
let mut output = BTreeMap::new();
362-
for mut map in mappings {
363-
for item in map.drain(..) {
364-
let key = key_extractor(&item);
365-
let new_state = merge_reduce(&item, output.get(&key).cloned().unwrap_or(initial_state.clone()));
366-
output.insert(key, new_state);
367-
}
368-
}
369-
output.into_iter().collect()
370-
}
371-
372292
/// Merge the given mappings into one mapping, where the values are merged using the given
373293
/// merge operation.
374294
///
@@ -604,23 +524,6 @@ impl<
604524
balances
605525
}
606526

607-
pub fn calculate_total_reward_for_evaluation(
608-
&self,
609-
evaluation: EvaluationInfoOf<T>,
610-
reward_info: RewardInfo,
611-
) -> Balance {
612-
let early_reward_weight =
613-
Perquintill::from_rational(evaluation.early_usd_amount, reward_info.early_evaluator_total_bonded_usd);
614-
let normal_reward_weight = Perquintill::from_rational(
615-
evaluation.late_usd_amount.saturating_add(evaluation.early_usd_amount),
616-
reward_info.normal_evaluator_total_bonded_usd,
617-
);
618-
let early_evaluators_rewards = early_reward_weight * reward_info.early_evaluator_reward_pot;
619-
let normal_evaluators_rewards = normal_reward_weight * reward_info.normal_evaluator_reward_pot;
620-
621-
early_evaluators_rewards.saturating_add(normal_evaluators_rewards)
622-
}
623-
624527
// We assume a single bid can cover the whole first bucket. Make sure the ticket sizes allow this.
625528
pub fn generate_bids_from_bucket(
626529
&self,
@@ -677,13 +580,6 @@ impl<
677580
bids
678581
}
679582

680-
pub fn remainder_round_block(&self) -> BlockNumberFor<T> {
681-
T::EvaluationRoundDuration::get() +
682-
T::AuctionRoundDuration::get() +
683-
T::CommunityRoundDuration::get() +
684-
One::one()
685-
}
686-
687583
#[cfg(feature = "std")]
688584
pub fn eth_key_and_sig_from(
689585
&mut self,

pallets/funding/src/instantiator/chain_interactions.rs

+8-58
Original file line numberDiff line numberDiff line change
@@ -107,25 +107,6 @@ impl<
107107
self.execute(|| <T as Config>::ContributionTokenCurrency::balance(project_id, &user))
108108
}
109109

110-
pub fn get_all_free_plmc_balances(&mut self) -> Vec<UserToPLMCBalance<T>> {
111-
let user_keys = self.execute(|| frame_system::Account::<T>::iter_keys().collect());
112-
self.get_free_plmc_balances_for(user_keys)
113-
}
114-
115-
pub fn get_all_reserved_plmc_balances(
116-
&mut self,
117-
reserve_type: <T as Config>::RuntimeHoldReason,
118-
) -> Vec<UserToPLMCBalance<T>> {
119-
let user_keys = self.execute(|| frame_system::Account::<T>::iter_keys().collect());
120-
self.get_reserved_plmc_balances_for(user_keys, reserve_type)
121-
}
122-
123-
pub fn get_all_free_funding_asset_balances(&mut self, asset_id: AssetIdOf<T>) -> Vec<UserToFundingAsset<T>> {
124-
let user_keys =
125-
self.execute(|| frame_system::Account::<T>::iter_keys().map(|a| (a, asset_id.clone())).collect());
126-
self.get_free_funding_asset_balances_for(user_keys)
127-
}
128-
129110
pub fn get_plmc_total_supply(&mut self) -> Balance {
130111
self.execute(<T as Config>::NativeCurrency::total_issuance)
131112
}
@@ -324,43 +305,11 @@ impl<
324305
self.do_reserved_plmc_assertions(expected_reserved_plmc_balances, HoldReason::Evaluation.into());
325306
}
326307

327-
pub fn finalized_bids_assertions(
328-
&mut self,
329-
project_id: ProjectId,
330-
bid_expectations: Vec<BidInfoFilter<T>>,
331-
expected_ct_sold: Balance,
332-
) {
333-
let project_metadata = self.get_project_metadata(project_id);
334-
let project_details = self.get_project_details(project_id);
335-
let project_bids = self.execute(|| Bids::<T>::iter_prefix_values(project_id).collect::<Vec<_>>());
336-
337-
for filter in bid_expectations {
338-
let _found_bid = project_bids.iter().find(|bid| filter.matches_bid(bid)).unwrap();
339-
}
340-
341-
// Remaining CTs are updated
342-
assert_eq!(
343-
project_details.remaining_contribution_tokens,
344-
project_metadata.total_allocation_size - expected_ct_sold,
345-
"Remaining CTs are incorrect"
346-
);
347-
}
348-
349308
pub fn assert_plmc_free_balance(&mut self, account_id: AccountIdOf<T>, expected_balance: Balance) {
350309
let real_balance = self.get_free_plmc_balance_for(account_id.clone());
351310
assert_eq!(real_balance, expected_balance, "Unexpected PLMC balance for user {:?}", account_id);
352311
}
353312

354-
pub fn assert_plmc_held_balance(
355-
&mut self,
356-
account_id: AccountIdOf<T>,
357-
expected_balance: Balance,
358-
hold_reason: <T as Config>::RuntimeHoldReason,
359-
) {
360-
let real_balance = self.get_reserved_plmc_balance_for(account_id.clone(), hold_reason);
361-
assert_eq!(real_balance, expected_balance, "Unexpected PLMC balance for user {:?}", account_id);
362-
}
363-
364313
pub fn assert_funding_asset_free_balance(
365314
&mut self,
366315
account_id: AccountIdOf<T>,
@@ -465,6 +414,12 @@ impl<
465414
self.mint_funding_asset_to(necessary_funding_assets);
466415
}
467416

417+
pub fn mint_necessary_tokens_for_evaluations(&mut self, evaluations: Vec<EvaluationParams<T>>) {
418+
let plmc_required = self.calculate_evaluation_plmc_spent(evaluations.clone());
419+
self.mint_plmc_ed_if_required(plmc_required.accounts());
420+
self.mint_plmc_to(plmc_required);
421+
}
422+
468423
pub fn bid_for_users(&mut self, project_id: ProjectId, bids: Vec<BidParams<T>>) -> DispatchResultWithPostInfo {
469424
let project_policy = self.get_project_metadata(project_id).policy_ipfs_cid.unwrap();
470425

@@ -514,10 +469,6 @@ impl<
514469
self.execute(|| Bids::<T>::iter_prefix_values(project_id).collect())
515470
}
516471

517-
pub fn get_bid(&mut self, bid_id: u32) -> BidInfoOf<T> {
518-
self.execute(|| Bids::<T>::iter_values().find(|bid| bid.id == bid_id).unwrap())
519-
}
520-
521472
// Used to check all the USDT/USDC/DOT was paid to the issuer funding account
522473
pub fn assert_total_funding_paid_out(&mut self, project_id: ProjectId, bids: Vec<BidInfoOf<T>>) {
523474
let project_metadata = self.get_project_metadata(project_id);
@@ -805,9 +756,7 @@ impl<
805756

806757
let status = self.go_to_next_state(project_id);
807758

808-
if status == ProjectStatus::FundingSuccessful {
809-
self.test_ct_not_created_for(project_id);
810-
} else if status == ProjectStatus::FundingFailed {
759+
if status == ProjectStatus::FundingSuccessful || status == ProjectStatus::FundingFailed {
811760
self.test_ct_not_created_for(project_id);
812761
} else {
813762
panic!("Project should be in FundingSuccessful or FundingFailed status");
@@ -834,6 +783,7 @@ impl<
834783
);
835784

836785
assert!(matches!(self.go_to_next_state(project_id), ProjectStatus::SettlementStarted(_)));
786+
self.test_ct_created_for(project_id);
837787

838788
self.settle_project(project_id, mark_as_settled);
839789

0 commit comments

Comments
 (0)