Skip to content

Commit 951d9cb

Browse files
authored
✨ Set balance to u128 instead of config type on pallet_funding (#394)
## What? - Remove the `Balance` config type, and replace it for u128 ## Why? - Having to access our fundamental balance type through trait interfaces was making our code unnecessarily bloated. The only reason to have a generic balance type is if our pallet is written to be used in many other blockchains, which is not the case. - Now we can use directly u128, instead of having to convert with into() - We were already assuming 99% that it was u128 since it had to implement From<u128> ## How? - Replace BalanceOf<T> with Balance - Delete all unnecessary conversion - Remove the generic Balance types from the pallet funding types ## Testing? Normal tests ## Anything Else? This will now allow us to use abs_diff for the vesting api
1 parent 43eed61 commit 951d9cb

27 files changed

+313
-457
lines changed

integration-tests/penpal/src/xcm_config.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use super::{
3131
use crate::{BaseDeliveryFee, FeeAssetId, TransactionByteFee};
3232
use core::marker::PhantomData;
3333
use frame_support::{
34-
match_types, parameter_types,
34+
parameter_types,
3535
traits::{ConstU32, Contains, ContainsPair, Everything, EverythingBut, Get, Nothing},
3636
weights::Weight,
3737
};
@@ -44,12 +44,12 @@ use sp_runtime::traits::{AccountIdConversion, ConvertInto, Identity, TryConvertI
4444
use xcm::latest::prelude::*;
4545
use xcm_builder::{
4646
AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowHrmpNotificationsFromRelayChain,
47-
AllowKnownQueryResponses, AllowSubscriptionsFrom, AllowTopLevelPaidExecutionFrom, AsPrefixedGeneralIndex,
48-
ConvertedConcreteId, EnsureXcmOrigin, FixedWeightBounds, FrameTransactionalProcessor, FungibleAdapter,
49-
FungiblesAdapter, IsConcrete, LocalMint, NativeAsset, NoChecking, ParentAsSuperuser, ParentIsPreset,
50-
RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative,
51-
SignedToAccountId32, SovereignSignedViaLocation, StartsWith, TakeWeightCredit, TrailingSetTopicAsId,
52-
UsingComponents, WithComputedOrigin, WithUniqueTopic, XcmFeeManagerFromComponents, XcmFeeToAccount,
47+
AllowKnownQueryResponses, AllowSubscriptionsFrom, AsPrefixedGeneralIndex, ConvertedConcreteId, EnsureXcmOrigin,
48+
FixedWeightBounds, FrameTransactionalProcessor, FungibleAdapter, FungiblesAdapter, IsConcrete, LocalMint,
49+
NativeAsset, NoChecking, ParentAsSuperuser, ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative,
50+
SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation,
51+
StartsWith, TakeWeightCredit, TrailingSetTopicAsId, UsingComponents, WithComputedOrigin, WithUniqueTopic,
52+
XcmFeeManagerFromComponents, XcmFeeToAccount,
5353
};
5454
use xcm_executor::{traits::JustTry, XcmExecutor};
5555

integration-tests/src/tests/ct_migration.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ fn create_settled_project() -> (ProjectId, Vec<AccountId>) {
192192
fn full_pallet_migration_test() {
193193
polimec::set_prices();
194194
let (project_id, participants) = create_settled_project();
195-
let project_status =
195+
let _project_status =
196196
PolimecNet::execute_with(|| pallet_funding::ProjectsDetails::<PolimecRuntime>::get(project_id).unwrap().status);
197197

198198
mock_hrmp_establishment(project_id);
@@ -304,7 +304,7 @@ fn cannot_start_pallet_migration_with_unsettled_participations() {
304304

305305
let tups = vec![tup_1, tup_2, tup_3];
306306

307-
for (project_id, participants) in tups.into_iter() {
307+
for (project_id, _participants) in tups.into_iter() {
308308
PolimecNet::execute_with(|| {
309309
assert_noop!(
310310
PolimecFunding::do_start_pallet_migration(&ISSUER.into(), project_id, ParaId::from(6969u32)),

integration-tests/src/tests/e2e.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ fn ct_minted() {
423423
let mut inst = IntegrationInstantiator::new(None);
424424

425425
PolimecNet::execute_with(|| {
426-
let project_id = inst.create_settled_project(
426+
let _project_id = inst.create_settled_project(
427427
excel_project(),
428428
ISSUER.into(),
429429
None,
@@ -511,10 +511,6 @@ fn ct_migrated() {
511511

512512
// Migrate CTs
513513
let accounts = excel_ct_amounts().iter().map(|item| item.0.clone()).unique().collect::<Vec<_>>();
514-
let total_ct_sold = excel_ct_amounts().iter().fold(FixedU128::zero(), |acc, item| acc + item.1);
515-
let polimec_sov_acc = PenNet::sovereign_account_id_of((Parent, Parachain(polimec::PARA_ID)).into());
516-
let polimec_fund_balance = PenNet::account_data_of(polimec_sov_acc);
517-
518514
let names = names();
519515

520516
for account in accounts {

integration-tests/src/tests/governance.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ use frame_support::{
1818
use pallet_democracy::{AccountVote, Conviction, GetElectorate, ReferendumInfo, Vote};
1919
use pallet_vesting::VestingInfo;
2020
use polimec_runtime::{
21-
Balances, Council, Democracy, Elections, ParachainStaking, Preimage, RuntimeOrigin, TechnicalCommittee, Treasury,
22-
Vesting, PLMC,
21+
Balances, Council, Democracy, Elections, ParachainStaking, Preimage, RuntimeOrigin, Treasury, Vesting, PLMC,
2322
};
2423
use xcm_emulator::helpers::get_account_id_from_seed;
2524
generate_accounts!(PEPE, CARLOS,);

pallets/funding/src/benchmarking.rs

+26-85
Original file line numberDiff line numberDiff line change
@@ -62,38 +62,21 @@ where
6262
let metadata_hash = BoundedVec::try_from(IPFS_CID.as_bytes().to_vec()).unwrap();
6363
ProjectMetadata {
6464
token_information: CurrencyMetadata { name: bounded_name, symbol: bounded_symbol, decimals: CT_DECIMALS },
65-
mainnet_token_max_supply: BalanceOf::<T>::try_from(1_000_000 * CT_UNIT)
66-
.unwrap_or_else(|_| panic!("Failed to create BalanceOf")),
67-
total_allocation_size: BalanceOf::<T>::try_from(1_000_000 * CT_UNIT)
68-
.unwrap_or_else(|_| panic!("Failed to create BalanceOf")),
65+
mainnet_token_max_supply: 1_000_000u128 * CT_UNIT,
66+
total_allocation_size: 1_000_000u128 * CT_UNIT,
6967
auction_round_allocation_percentage: Percent::from_percent(50u8),
7068
minimum_price: PriceProviderOf::<T>::calculate_decimals_aware_price(10u128.into(), USD_DECIMALS, CT_DECIMALS)
7169
.unwrap(),
7270

7371
bidding_ticket_sizes: BiddingTicketSizes {
74-
professional: TicketSize::new(
75-
BalanceOf::<T>::try_from(5000 * USD_UNIT).unwrap_or_else(|_| panic!("Failed to create BalanceOf")),
76-
None,
77-
),
78-
institutional: TicketSize::new(
79-
BalanceOf::<T>::try_from(5000 * USD_UNIT).unwrap_or_else(|_| panic!("Failed to create BalanceOf")),
80-
None,
81-
),
72+
professional: TicketSize::new(5000u128 * USD_UNIT, None),
73+
institutional: TicketSize::new(5000u128 * USD_UNIT, None),
8274
phantom: Default::default(),
8375
},
8476
contributing_ticket_sizes: ContributingTicketSizes {
85-
retail: TicketSize::new(
86-
BalanceOf::<T>::try_from(USD_UNIT).unwrap_or_else(|_| panic!("Failed to create BalanceOf")),
87-
None,
88-
),
89-
professional: TicketSize::new(
90-
BalanceOf::<T>::try_from(USD_UNIT).unwrap_or_else(|_| panic!("Failed to create BalanceOf")),
91-
None,
92-
),
93-
institutional: TicketSize::new(
94-
BalanceOf::<T>::try_from(USD_UNIT).unwrap_or_else(|_| panic!("Failed to create BalanceOf")),
95-
None,
96-
),
77+
retail: TicketSize::new(USD_UNIT, None),
78+
professional: TicketSize::new(USD_UNIT, None),
79+
institutional: TicketSize::new(USD_UNIT, None),
9780
phantom: Default::default(),
9881
},
9982
participation_currencies: vec![AcceptedFundingAsset::USDT].try_into().unwrap(),
@@ -105,7 +88,6 @@ where
10588
pub fn default_evaluations<T: Config>() -> Vec<UserToUSDBalance<T>>
10689
where
10790
<T as Config>::Price: From<u128>,
108-
<T as Config>::Balance: From<u128>,
10991
T::Hash: From<H256>,
11092
{
11193
let threshold = <T as Config>::EvaluationSuccessThreshold::get();
@@ -134,7 +116,6 @@ where
134116
pub fn default_bids<T: Config>() -> Vec<BidParams<T>>
135117
where
136118
<T as Config>::Price: From<u128>,
137-
<T as Config>::Balance: From<u128>,
138119
T::Hash: From<H256>,
139120
{
140121
let default_project_metadata = default_project_metadata::<T>(account::<AccountIdOf<T>>("issuer", 0, 0));
@@ -156,7 +137,6 @@ pub fn full_bids<T>() -> Vec<BidParams<T>>
156137
where
157138
T: Config,
158139
<T as Config>::Price: From<u128>,
159-
<T as Config>::Balance: From<u128>,
160140
T::Hash: From<H256>,
161141
{
162142
let inst = BenchInstantiator::<T>::new(None);
@@ -175,7 +155,6 @@ where
175155
pub fn default_community_contributions<T: Config>() -> Vec<ContributionParams<T>>
176156
where
177157
<T as Config>::Price: From<u128>,
178-
<T as Config>::Balance: From<u128>,
179158
T::Hash: From<H256>,
180159
{
181160
let inst = BenchInstantiator::<T>::new(None);
@@ -201,7 +180,6 @@ where
201180
pub fn default_remainder_contributions<T: Config>() -> Vec<ContributionParams<T>>
202181
where
203182
<T as Config>::Price: From<u128>,
204-
<T as Config>::Balance: From<u128>,
205183
T::Hash: From<H256>,
206184
{
207185
let inst = BenchInstantiator::<T>::new(None);
@@ -289,13 +267,12 @@ pub fn string_account<AccountId: Decode>(
289267

290268
#[benchmarks(
291269
where
292-
T: Config + frame_system::Config<RuntimeEvent = <T as Config>::RuntimeEvent> + pallet_balances::Config<Balance = BalanceOf<T>> + sp_std::fmt::Debug,
270+
T: Config + frame_system::Config<RuntimeEvent = <T as Config>::RuntimeEvent> + pallet_balances::Config<Balance = Balance> + sp_std::fmt::Debug,
293271
<T as Config>::RuntimeEvent: TryInto<Event<T>> + Parameter + Member,
294272
<T as Config>::Price: From<u128>,
295-
<T as Config>::Balance: From<u128> + Into<u128>,
296273
T::Hash: From<H256>,
297274
<T as frame_system::Config>::AccountId: Into<<<T as frame_system::Config>::RuntimeOrigin as OriginTrait>::AccountId> + sp_std::fmt::Debug,
298-
<T as pallet_balances::Config>::Balance: Into<BalanceOf<T>>,
275+
<T as pallet_balances::Config>::Balance: Into<Balance>,
299276
)]
300277
mod benchmarks {
301278
use super::*;
@@ -327,7 +304,7 @@ mod benchmarks {
327304
let ct_treasury_account_deposit = T::ContributionTokenCurrency::deposit_required(0);
328305
inst.mint_plmc_to(vec![UserToPLMCBalance::new(
329306
issuer.clone(),
330-
ed * 2u64.into() + metadata_deposit + ct_treasury_account_deposit,
307+
ed * 2u128 + metadata_deposit + ct_treasury_account_deposit,
331308
)]);
332309
let jwt = get_mock_jwt_with_cid(
333310
issuer.clone(),
@@ -411,10 +388,8 @@ mod benchmarks {
411388
symbol: BoundedVec::try_from("CTESTv2".as_bytes().to_vec()).unwrap(),
412389
decimals: CT_DECIMALS - 2,
413390
},
414-
mainnet_token_max_supply: BalanceOf::<T>::try_from(200_000 * CT_UNIT)
415-
.unwrap_or_else(|_| panic!("Failed to create BalanceOf")),
416-
total_allocation_size: BalanceOf::<T>::try_from(200_000 * CT_UNIT)
417-
.unwrap_or_else(|_| panic!("Failed to create BalanceOf")),
391+
mainnet_token_max_supply: 200_000u128 * CT_UNIT,
392+
total_allocation_size: 200_000u128 * CT_UNIT,
418393
auction_round_allocation_percentage: Percent::from_percent(30u8),
419394
minimum_price: PriceProviderOf::<T>::calculate_decimals_aware_price(
420395
11u128.into(),
@@ -423,44 +398,14 @@ mod benchmarks {
423398
)
424399
.unwrap(),
425400
bidding_ticket_sizes: BiddingTicketSizes {
426-
professional: TicketSize::new(
427-
BalanceOf::<T>::try_from(5000 * USD_UNIT).unwrap_or_else(|_| panic!("Failed to create BalanceOf")),
428-
Some(
429-
BalanceOf::<T>::try_from(10_000 * USD_UNIT)
430-
.unwrap_or_else(|_| panic!("Failed to create BalanceOf")),
431-
),
432-
),
433-
institutional: TicketSize::new(
434-
BalanceOf::<T>::try_from(5000 * USD_UNIT).unwrap_or_else(|_| panic!("Failed to create BalanceOf")),
435-
Some(
436-
BalanceOf::<T>::try_from(10_000 * USD_UNIT)
437-
.unwrap_or_else(|_| panic!("Failed to create BalanceOf")),
438-
),
439-
),
401+
professional: TicketSize::new(5000 * USD_UNIT, Some(10_000 * USD_UNIT)),
402+
institutional: TicketSize::new(5000 * USD_UNIT, Some(10_000 * USD_UNIT)),
440403
phantom: Default::default(),
441404
},
442405
contributing_ticket_sizes: ContributingTicketSizes {
443-
retail: TicketSize::new(
444-
BalanceOf::<T>::try_from(5000 * USD_UNIT).unwrap_or_else(|_| panic!("Failed to create BalanceOf")),
445-
Some(
446-
BalanceOf::<T>::try_from(10_000 * USD_UNIT)
447-
.unwrap_or_else(|_| panic!("Failed to create BalanceOf")),
448-
),
449-
),
450-
professional: TicketSize::new(
451-
BalanceOf::<T>::try_from(5000 * USD_UNIT).unwrap_or_else(|_| panic!("Failed to create BalanceOf")),
452-
Some(
453-
BalanceOf::<T>::try_from(10_000 * USD_UNIT)
454-
.unwrap_or_else(|_| panic!("Failed to create BalanceOf")),
455-
),
456-
),
457-
institutional: TicketSize::new(
458-
BalanceOf::<T>::try_from(5000 * USD_UNIT).unwrap_or_else(|_| panic!("Failed to create BalanceOf")),
459-
Some(
460-
BalanceOf::<T>::try_from(10_000 * USD_UNIT)
461-
.unwrap_or_else(|_| panic!("Failed to create BalanceOf")),
462-
),
463-
),
406+
retail: TicketSize::new(5000 * USD_UNIT, Some(10_000 * USD_UNIT)),
407+
professional: TicketSize::new(5000 * USD_UNIT, Some(10_000 * USD_UNIT)),
408+
institutional: TicketSize::new(5000 * USD_UNIT, Some(10_000 * USD_UNIT)),
464409
phantom: Default::default(),
465410
},
466411
participation_currencies: vec![AcceptedFundingAsset::USDT, AcceptedFundingAsset::USDC].try_into().unwrap(),
@@ -518,9 +463,9 @@ mod benchmarks {
518463
// Storage
519464
let stored_details = ProjectsDetails::<T>::get(project_id).unwrap();
520465
assert_eq!(stored_details.status, ProjectStatus::EvaluationRound);
521-
let starting_evaluation_info = EvaluationRoundInfoOf::<T> {
522-
total_bonded_usd: Zero::zero(),
523-
total_bonded_plmc: Zero::zero(),
466+
let starting_evaluation_info = EvaluationRoundInfo {
467+
total_bonded_usd: Balance::zero(),
468+
total_bonded_plmc: Balance::zero(),
524469
evaluators_outcome: None,
525470
};
526471
assert_eq!(stored_details.evaluation_round_info, starting_evaluation_info);
@@ -706,10 +651,8 @@ mod benchmarks {
706651
whitelist_account!(bidder);
707652

708653
let mut project_metadata = default_project_metadata::<T>(issuer.clone());
709-
project_metadata.mainnet_token_max_supply =
710-
(100_000 * CT_UNIT).try_into().unwrap_or_else(|_| panic!("Failed to create BalanceOf"));
711-
project_metadata.total_allocation_size =
712-
(100_000 * CT_UNIT).try_into().unwrap_or_else(|_| panic!("Failed to create BalanceOf"));
654+
project_metadata.mainnet_token_max_supply = 100_000 * CT_UNIT;
655+
project_metadata.total_allocation_size = 100_000 * CT_UNIT;
713656
project_metadata.minimum_price = PriceProviderOf::<T>::calculate_decimals_aware_price(
714657
PriceOf::<T>::checked_from_rational(100, 1).unwrap(),
715658
USD_DECIMALS,
@@ -790,7 +733,7 @@ mod benchmarks {
790733
let auction_allocation =
791734
project_metadata.auction_round_allocation_percentage * project_metadata.total_allocation_size;
792735
let bucket_size = Percent::from_percent(10) * auction_allocation;
793-
ct_amount = bucket_size * (y as u128).into();
736+
ct_amount = bucket_size * (y as u128);
794737
usdt_for_filler_bidder = usdt_for_new_bidder;
795738
}
796739
let extrinsic_bid = BidParams::new(bidder.clone(), ct_amount, 1u8, AcceptedFundingAsset::USDT);
@@ -949,10 +892,8 @@ mod benchmarks {
949892
whitelist_account!(issuer);
950893

951894
let mut project_metadata = default_project_metadata::<T>(issuer.clone());
952-
project_metadata.mainnet_token_max_supply =
953-
BalanceOf::<T>::try_from(10_000_000 * CT_UNIT).unwrap_or_else(|_| panic!("Failed to create BalanceOf"));
954-
project_metadata.total_allocation_size =
955-
BalanceOf::<T>::try_from(10_000_000 * CT_UNIT).unwrap_or_else(|_| panic!("Failed to create BalanceOf"));
895+
project_metadata.mainnet_token_max_supply = 10_000_000 * CT_UNIT;
896+
project_metadata.total_allocation_size = 10_000_000 * CT_UNIT;
956897
project_metadata.auction_round_allocation_percentage = Percent::from_percent(100u8);
957898

958899
let project_id = inst.create_auctioning_project(
@@ -990,7 +931,7 @@ mod benchmarks {
990931
// This one needs to fill the remaining with the bucket, so that all "accepted" bids will take the CT from a rejected one
991932
let last_rejected_bid = BidParams::<T>::new(
992933
account::<AccountIdOf<T>>("bidder", 0, 420),
993-
auction_allocation - (min_bid_amount * CT_UNIT * (y as u128 - 1u128)).into(),
934+
auction_allocation - (min_bid_amount * CT_UNIT * (y as u128 - 1u128)),
994935
1u8,
995936
AcceptedFundingAsset::USDT,
996937
);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ impl<T: Config> Pallet<T> {
3838
status: ProjectStatus::Application,
3939
round_duration: BlockNumberPair::new(None, None),
4040
remaining_contribution_tokens: project_metadata.total_allocation_size,
41-
funding_amount_reached_usd: BalanceOf::<T>::zero(),
42-
evaluation_round_info: EvaluationRoundInfoOf::<T> {
41+
funding_amount_reached_usd: Balance::zero(),
42+
evaluation_round_info: EvaluationRoundInfo {
4343
total_bonded_usd: Zero::zero(),
4444
total_bonded_plmc: Zero::zero(),
4545
evaluators_outcome: None,

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl<T: Config> Pallet<T> {
115115
pub fn do_evaluate(
116116
evaluator: &AccountIdOf<T>,
117117
project_id: ProjectId,
118-
usd_amount: BalanceOf<T>,
118+
usd_amount: Balance,
119119
did: Did,
120120
whitelisted_policy: Cid,
121121
) -> DispatchResultWithPostInfo {
@@ -157,7 +157,7 @@ impl<T: Config> Pallet<T> {
157157
remaining_bond_to_reach_threshold
158158
};
159159

160-
let late_usd_amount = usd_amount.checked_sub(&early_usd_amount).ok_or(Error::<T>::BadMath)?;
160+
let late_usd_amount = usd_amount.checked_sub(early_usd_amount).ok_or(Error::<T>::BadMath)?;
161161

162162
let new_evaluation = EvaluationInfoOf::<T> {
163163
id: evaluation_id,

0 commit comments

Comments
 (0)