Skip to content

Commit 07e2f57

Browse files
committed
General Cleanup
1 parent 960eac5 commit 07e2f57

22 files changed

+787
-1062
lines changed

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

pallets/funding/src/instantiator/macros.rs

-45
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,6 @@ macro_rules! assert_close_enough {
4040
};
4141
}
4242

43-
#[macro_export]
44-
macro_rules! call_and_is_ok {
45-
($inst: expr, $( $call: expr ),* ) => {
46-
$inst.execute(|| {
47-
$(
48-
let result = $call;
49-
assert!(result.is_ok(), "Call failed: {:?}", result);
50-
)*
51-
})
52-
};
53-
}
5443
#[macro_export]
5544
macro_rules! find_event {
5645
($runtime:ty, $pattern:pat, $($field_name:ident == $field_value:expr),+) => {
@@ -77,37 +66,3 @@ macro_rules! find_event {
7766
}
7867
};
7968
}
80-
81-
#[macro_export]
82-
macro_rules! extract_from_event {
83-
($env: expr, $pattern:pat, $field:ident) => {
84-
$env.execute(|| {
85-
let events = System::events();
86-
87-
events.iter().find_map(|event_record| {
88-
if let frame_system::EventRecord { event: RuntimeEvent::PolimecFunding($pattern), .. } = event_record {
89-
Some($field.clone())
90-
} else {
91-
None
92-
}
93-
})
94-
})
95-
};
96-
}
97-
98-
#[macro_export]
99-
macro_rules! define_names {
100-
($($name:ident: $id:expr, $label:expr);* $(;)?) => {
101-
$(
102-
pub const $name: AccountId = $id;
103-
)*
104-
105-
pub fn names() -> std::collections::HashMap<AccountId, &'static str> {
106-
let mut names = std::collections::HashMap::new();
107-
$(
108-
names.insert($name, $label);
109-
)*
110-
names
111-
}
112-
};
113-
}

pallets/funding/src/instantiator/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use frame_support::{
2323
metadata::Inspect as MetadataInspect, roles::Inspect as RolesInspect, Inspect as FungiblesInspect,
2424
Mutate as FungiblesMutate,
2525
},
26-
AccountTouch, Get, OnFinalize, OnIdle, OnInitialize,
26+
Get, OnFinalize, OnIdle, OnInitialize,
2727
},
2828
weights::Weight,
2929
Parameter,

0 commit comments

Comments
 (0)