Skip to content

Commit 969a25b

Browse files
authored
fix: more lints (#783)
1 parent daaae21 commit 969a25b

File tree

6 files changed

+180
-74
lines changed

6 files changed

+180
-74
lines changed

pallets/pallet-bonded-coins/src/curves/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ where
6565
match self {
6666
Curve::Polynomial(params) => params,
6767
Curve::SquareRoot(params) => params,
68-
Curve::LMSR(params) => params,
68+
Curve::Lmsr(params) => params,
6969
}
7070
}
7171
}

pallets/pallet-bonded-coins/src/lib.rs

+57-29
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ pub mod pallet {
4646
use sp_std::{
4747
default::Default,
4848
ops::{AddAssign, BitOrAssign, ShlAssign},
49+
prelude::*,
50+
vec::Vec,
4951
};
5052
use substrate_fixed::{
5153
traits::{Fixed, FixedSigned, FixedUnsigned, ToFixed},
@@ -102,14 +104,17 @@ pub mod pallet {
102104

103105
pub(crate) const LOG_TARGET: &str = "runtime::pallet-bonded-coins";
104106

105-
/// Configure the pallet by specifying the parameters and types on which it depends.
107+
/// Configure the pallet by specifying the parameters and types on which it
108+
/// depends.
106109
#[pallet::config]
107110
pub trait Config: frame_system::Config {
108-
/// Because this pallet emits events, it depends on the runtime's definition of an event.
111+
/// Because this pallet emits events, it depends on the runtime's
112+
/// definition of an event.
109113
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
110114
/// The currency used for storage deposits.
111115
type DepositCurrency: MutateHold<Self::AccountId, Reason = Self::RuntimeHoldReason>;
112-
/// A fungibles trait implementation to interact with currencies which can be used as collateral for minting bonded tokens.
116+
/// A fungibles trait implementation to interact with currencies which
117+
/// can be used as collateral for minting bonded tokens.
113118
type CollateralCurrencies: MutateFungibles<Self::AccountId>
114119
+ AccountTouch<CollateralAssetIdOf<Self>, Self::AccountId>
115120
+ FungiblesMetadata<Self::AccountId>;
@@ -133,20 +138,23 @@ pub mod pallet {
133138
#[pallet::constant]
134139
type DepositPerCurrency: Get<DepositCurrencyBalanceOf<Self>>;
135140

136-
/// The base deposit required to create a new pool, primarily to cover the ED of the pool account.
141+
/// The base deposit required to create a new pool, primarily to cover
142+
/// the ED of the pool account.
137143
#[pallet::constant]
138144
type BaseDeposit: Get<DepositCurrencyBalanceOf<Self>>;
139145

140146
/// The origin for most permissionless and priviledged operations.
141147
type DefaultOrigin: EnsureOrigin<Self::RuntimeOrigin, Success = Self::AccountId>;
142-
/// The dedicated origin for creating new bonded currency pools (typically permissionless).
148+
/// The dedicated origin for creating new bonded currency pools
149+
/// (typically permissionless).
143150
type PoolCreateOrigin: EnsureOrigin<Self::RuntimeOrigin, Success = Self::AccountId>;
144151
/// The origin for permissioned operations (force_* transactions).
145152
type ForceOrigin: EnsureOrigin<Self::RuntimeOrigin>;
146153
/// The type used for pool ids
147154
type PoolId: Parameter + MaxEncodedLen + From<[u8; 32]> + Into<Self::AccountId>;
148155

149-
/// The type used for asset ids. This is the type of the bonded currencies.
156+
/// The type used for asset ids. This is the type of the bonded
157+
/// currencies.
150158
type AssetId: Parameter + Member + FullCodec + MaxEncodedLen + Saturating + One + Default;
151159

152160
type RuntimeHoldReason: From<HoldReason>;
@@ -194,11 +202,13 @@ pub mod pallet {
194202
DestructionStarted {
195203
id: T::PoolId,
196204
},
197-
/// Collateral distribution to bonded token holders has been completed for this pool - no more tokens or no more collateral to distribute.
205+
/// Collateral distribution to bonded token holders has been completed
206+
/// for this pool (no more tokens or no more collateral to distribute).
198207
RefundComplete {
199208
id: T::PoolId,
200209
},
201-
/// A bonded token pool has been fully destroyed and all collateral and deposits have been refunded.
210+
/// A bonded token pool has been fully destroyed and all collateral and
211+
/// deposits have been refunded.
202212
Destroyed {
203213
id: T::PoolId,
204214
},
@@ -215,17 +225,23 @@ pub mod pallet {
215225
PoolUnknown,
216226
/// The pool has no associated bonded currency with the given index.
217227
IndexOutOfBounds,
218-
/// The pool does not hold collateral to be refunded, or has no remaining supply of tokens to exchange. Call start_destroy to intiate teardown.
228+
/// The pool does not hold collateral to be refunded, or has no
229+
/// remaining supply of tokens to exchange. Call start_destroy to
230+
/// intiate teardown.
219231
NothingToRefund,
220232
/// The user is not privileged to perform the requested operation.
221233
NoPermission,
222-
/// The pool is deactivated (i.e., in destroying or refunding state) and not available for use.
234+
/// The pool is deactivated (i.e., in destroying or refunding state) and
235+
/// not available for use.
223236
PoolNotLive,
224-
/// There are active accounts associated with this pool and thus it cannot be destroyed at this point.
237+
/// There are active accounts associated with this pool and thus it
238+
/// cannot be destroyed at this point.
225239
LivePool,
226240
/// This operation can only be made when the pool is in refunding state.
227241
NotRefunding,
228-
/// The number of currencies linked to a pool exceeds the limit parameter. Thrown by transactions that require specifying the number of a pool's currencies in order to determine weight limits upfront.
242+
/// The number of currencies linked to a pool exceeds the limit
243+
/// parameter. Thrown by transactions that require specifying the number
244+
/// of a pool's currencies in order to determine weight limits upfront.
229245
CurrencyCount,
230246
InvalidInput,
231247
Internal,
@@ -297,8 +313,8 @@ pub mod pallet {
297313
Ok(())
298314
})?;
299315

300-
// Touch the pool account in order to be able to transfer the collateral currency to it
301-
// This should also verify that the currency actually exists
316+
// Touch the pool account in order to be able to transfer the collateral
317+
// currency to it. This should also verify that the currency actually exists.
302318
T::CollateralCurrencies::touch(collateral_id.clone(), pool_account, &who)?;
303319

304320
Pools::<T>::set(
@@ -337,7 +353,7 @@ pub mod pallet {
337353

338354
let asset_id = pool_details
339355
.bonded_currencies
340-
.get(currency_idx as usize)
356+
.get(currency_idx.saturated_into::<usize>())
341357
.ok_or(Error::<T>::IndexOutOfBounds)?;
342358

343359
let pool_id_account = pool_id.into();
@@ -471,7 +487,8 @@ pub mod pallet {
471487
// fail if cost > max_cost
472488
ensure!(cost <= max_cost, Error::<T>::Slippage);
473489

474-
// Transfer the collateral. We do not want to kill the minter, so this operation can fail if the account is being reaped.
490+
// Transfer the collateral. We do not want to kill the minter, so this operation
491+
// can fail if the account is being reaped.
475492
T::CollateralCurrencies::transfer(
476493
pool_details.collateral_id,
477494
&who,
@@ -625,9 +642,11 @@ pub mod pallet {
625642

626643
let pool_account = pool_id.clone().into();
627644

628-
// Choosing total_balance over reducible_balance to ensure that all funds are distributed fairly;
629-
// in case of any locks present on the pool account, this could lead to refunds failing to execute though.
630-
// This case would have to be resolved by governance, either by removing locks or force_destroying the pool.
645+
// Choosing total_balance over reducible_balance to ensure that all funds are
646+
// distributed fairly; in case of any locks present on the pool account, this
647+
// could lead to refunds failing to execute. This case would have to be
648+
// resolved by governance, either by removing locks or force_destroying the
649+
// pool.
631650
let total_collateral_issuance =
632651
T::CollateralCurrencies::total_balance(pool_details.collateral_id.clone(), &pool_account);
633652

@@ -640,7 +659,8 @@ pub mod pallet {
640659
// remove any existing locks on the account prior to burning
641660
T::Fungibles::thaw(asset_id, &who).map_err(|freeze_error| freeze_error.into())?;
642661

643-
// With amount = max_value(), this trait implementation burns the reducible balance on the account and returns the actual amount burnt
662+
// With amount = max_value(), this trait implementation burns the reducible
663+
// balance on the account and returns the actual amount burnt
644664
let burnt = T::Fungibles::burn_from(
645665
asset_id.clone(),
646666
&who,
@@ -665,7 +685,8 @@ pub mod pallet {
665685
.checked_mul(&total_collateral_issuance)
666686
.ok_or(ArithmeticError::Overflow)? // TODO: do we need a fallback if this fails?
667687
.checked_div(&sum_of_issuances)
668-
.ok_or(Error::<T>::NothingToRefund)?; // should be impossible - how would we be able to burn funds if the sum of total supplies is 0?
688+
.ok_or(Error::<T>::NothingToRefund)?; // should be impossible - how would we be able to burn funds if the sum of total
689+
// supplies is 0?
669690

670691
if amount.is_zero()
671692
|| T::CollateralCurrencies::can_deposit(
@@ -677,8 +698,9 @@ pub mod pallet {
677698
.into_result()
678699
.is_err()
679700
{
680-
// funds are burnt but the collateral received is not sufficient to be deposited to the account
681-
// this is tolerated as otherwise we could have edge cases where it's impossible to refund at least some accounts
701+
// Funds are burnt but the collateral received is not sufficient to be deposited
702+
// to the account. This is tolerated as otherwise we could have edge cases where
703+
// it's impossible to refund at least some accounts.
682704
return Ok(());
683705
}
684706

@@ -690,7 +712,8 @@ pub mod pallet {
690712
Preservation::Expendable,
691713
)?; // TODO: check edge cases around existential deposit
692714

693-
// if collateral or total supply drops to zero, refunding is complete -> emit event
715+
// if collateral or total supply drops to zero, refunding is complete
716+
// -> emit event
694717
if sum_of_issuances <= burnt || total_collateral_issuance <= transferred {
695718
Self::deposit_event(Event::RefundComplete { id: pool_id });
696719
}
@@ -733,7 +756,8 @@ pub mod pallet {
733756

734757
for asset_id in pool_details.bonded_currencies {
735758
if T::Fungibles::asset_exists(asset_id.clone()) {
736-
// This would fail with an LiveAsset error if there are any accounts left on any currency
759+
// This would fail with an LiveAsset error if there are any accounts left on any
760+
// currency
737761
T::Fungibles::finish_destroy(asset_id)?;
738762
}
739763
}
@@ -892,7 +916,8 @@ pub mod pallet {
892916
let has_holders = pool_details.bonded_currencies.iter().any(|asset_id| {
893917
T::Fungibles::total_issuance(asset_id.clone()) > FungiblesBalanceOf::<T>::zero()
894918
});
895-
// destruction is only allowed when there are no holders or no collateral to distribute
919+
// destruction is only allowed when there are no holders or no collateral to
920+
// distribute
896921
ensure!(!has_holders, Error::<T>::LivePool);
897922
}
898923
}
@@ -905,11 +930,13 @@ pub mod pallet {
905930
new_pool_details.state.start_destroy();
906931
Pools::<T>::set(&pool_id, Some(new_pool_details));
907932

908-
// emit this event before the destruction started events are emitted by assets deactivation
933+
// emit this event before the destruction started events are emitted by assets
934+
// deactivation
909935
Self::deposit_event(Event::DestructionStarted { id: pool_id });
910936

911937
for asset_id in bonded_currencies {
912-
// Governance or other pallets using the fungibles trait can in theory destroy an asset without this pallet knowing, so we check if it's still around
938+
// Governance or other pallets using the fungibles trait can in theory destroy
939+
// an asset without this pallet knowing, so we check if it's still around
913940
if T::Fungibles::asset_exists(asset_id.clone()) {
914941
T::Fungibles::start_destroy(asset_id, None)?;
915942
}
@@ -935,7 +962,8 @@ pub mod pallet {
935962
}
936963

937964
fn get_currencies_number(pool_details: &PoolDetailsOf<T>) -> u32 {
938-
// bonded_currencies is a BoundedVec with maximum length MaxCurrencies, which is a u32; conversion to u32 must thus be lossless.
965+
// bonded_currencies is a BoundedVec with maximum length MaxCurrencies, which is
966+
// a u32; conversion to u32 must thus be lossless.
939967
pool_details.bonded_currencies.len().saturated_into()
940968
}
941969

pallets/pallet-bonded-coins/src/tests/curves/lmsr.rs

+61-20
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@ fn mint_first_coin() {
1717

1818
let passive_issuance = Float::from_num(0);
1919

20-
// Costs for existing supply: 100000000 * ln(e^(0/100000000) + e^(0/100000000)) = 69314718.055994530942
21-
// Costs for new supply: 100000000 * ln(e^(1/100000000) + e^(0/100000000)) = 69314718.555994532192
22-
// Costs to mint the first coin: 69314718.555994532192 - 69314718.055994530942 = 0.50000000124972321215 -> 0.50000000124972321215
20+
// Costs for existing supply:
21+
// 100000000 * ln(e^(0/100000000) + e^(0/100000000)) = 69314718.055994530942
22+
23+
// Costs for new supply:
24+
// 100000000 * ln(e^(1/100000000) + e^(0/100000000)) = 69314718.555994532192
25+
26+
// Costs to mint the first coin:
27+
// 69314718.555994532192 - 69314718.055994530942 = 0.50000000124972321215 ->
28+
// 0.50000000124972321215
2329
let costs = curve.calculate_costs(low, high, vec![passive_issuance]).unwrap();
2430

2531
let expected_costs = Float::from_str("0.50000000124972321215").unwrap();
@@ -39,9 +45,16 @@ fn high_supply_with_no_passive_issuance() {
3945

4046
let passive_issuance = Float::from_num(0);
4147

42-
// Costs for existing supply: 100000000 * ln(e^(100000000/100000000) + e^(0/100000000)) = 131326168.7518222834
43-
// Costs for new supply: 100000000 * ln(e^(100000100/100000000) + e^(0/100000000)) = 131326241.857689977
44-
// Costs to mint the first coin: 131326241.857689977 - 131326168.7518222834 = 73.1058676936
48+
// Costs for existing supply:
49+
// 100000000 * ln(e^(100000000/100000000) + e^(0/100000000)) =
50+
// 131326168.7518222834
51+
52+
// Costs for new supply:
53+
// 100000000 * ln(e^(100000100/100000000) + e^(0/100000000)) =
54+
// 131326241.857689977
55+
56+
// Costs to mint the first coin:
57+
// 131326241.857689977 - 131326168.7518222834 = 73.1058676936
4558
let costs = curve.calculate_costs(low, high, vec![passive_issuance]).unwrap();
4659

4760
let expected_costs = Float::from_str("73.1058676936").unwrap();
@@ -51,7 +64,8 @@ fn high_supply_with_no_passive_issuance() {
5164

5265
#[test]
5366
fn high_supply_with_passive_issuance() {
54-
// Create curve with liquidity parameter b=100_000_000, and passive issuance=1_000_000_000
67+
// Create curve with liquidity parameter b=100_000_000, and passive
68+
// issuance=1_000_000_000
5569
let m = Float::from_num(100_000_000);
5670
let curve = LMSRParameters { m };
5771

@@ -61,17 +75,26 @@ fn high_supply_with_passive_issuance() {
6175

6276
let passive_issuance = Float::from_num(1_000_000_000);
6377

64-
// Costs for existing supply: 100000000 * ln(e^(100000000/100000000) + e^(1000000000/100000000)) = 1000012340.2189723259
65-
// Costs for new supply: 100000000 * ln(e^(100000100/100000000) + e^(1000000000/100000000)) = 1000012340.2313117896
66-
// Costs to mint the first coin: 1000012340.2313117896 - 1000012340.2189723259 = 0.0123394637
78+
// Costs for existing supply:
79+
// 100000000 * ln(e^(100000000/100000000) + e^(1000000000/100000000)) =
80+
// 1000012340.2189723259
81+
82+
// Costs for new supply:
83+
// 100000000 * ln(e^(100000100/100000000) + e^(1000000000/100000000)) =
84+
// 1000012340.2313117896
85+
86+
// Costs to mint the first coin:
87+
// 1000012340.2313117896 - 1000012340.2189723259 = 0.0123394637
6788
let costs = curve.calculate_costs(low, high, vec![passive_issuance]).unwrap();
6889

6990
let expected_costs = Float::from_str("0.0123394637").unwrap();
7091

7192
assert_relative_eq(costs, expected_costs, Float::from_str("0.0000001").unwrap());
7293
}
7394

74-
// The main consequences of the low liquidity parameter is a lack of representable coins. e^40 goes beyond the representable range of the [Float] type.
95+
// The main consequences of the low liquidity parameter is a lack of
96+
// representable coins. e^40 goes beyond the representable range of the [Float]
97+
// type.
7598
#[test]
7699
fn low_liquidity_parameter() {
77100
// Create curve with liquidity parameter b=100, and passive issuance=0
@@ -84,9 +107,14 @@ fn low_liquidity_parameter() {
84107

85108
let passive_issuance = Float::from_num(0);
86109

87-
// Costs for existing supply: 100 * ln(e^(1000/100) + e^(0/100)) = 1000.0045398899216865
88-
// Costs for new supply: 100 * ln(e^(1100/100) + e^(0/100)) = 1100.0016701561318394
89-
// Costs to mint the first coin: 1100.0016701561318394 - 1000.0045398899216865 = 99.9971302662101529
110+
// Costs for existing supply:
111+
// 100 * ln(e^(1000/100) + e^(0/100)) = 1000.0045398899216865
112+
113+
// Costs for new supply:
114+
// 100 * ln(e^(1100/100) + e^(0/100)) = 1100.0016701561318394
115+
116+
// Costs to mint the first coin:
117+
// 1100.0016701561318394 - 1000.0045398899216865 = 99.9971302662101529
90118
let costs = curve.calculate_costs(low, high, vec![passive_issuance]).unwrap();
91119

92120
let expected_costs = Float::from_str("99.9971302662101529").unwrap();
@@ -107,9 +135,17 @@ fn mint_coin_with_existing_supply_and_no_passive_issuance() {
107135

108136
let passive_issuance = Float::from_num(0);
109137

110-
// Costs for existing supply: 100000000 * ln(e^(100/100000000) + e^(0/100000000)) = 69314768.056007030941723211624
111-
// Costs for new supply: 100000000 * ln(e^(101/100000000) + e^(0/100000000)) = 69314768.55600728219172321160383640
112-
// Costs to mint the first coin: 69314768.55600728219172321160383640 - 69314768.55600728219172321160383640 = 0.5000002412499999999798364
138+
// Costs for existing supply:
139+
// 100000000 * ln(e^(100/100000000) + e^(0/100000000)) =
140+
// 69314768.056007030941723211624
141+
142+
// Costs for new supply:
143+
// 100000000 * ln(e^(101/100000000) + e^(0/100000000)) =
144+
// 69314768.55600728219172321160383640
145+
146+
//Costs to mint the first coin:
147+
// 69314768.55600728219172321160383640 - 69314768.55600728219172321160383640 =
148+
// 0.5000002412499999999798364
113149
let costs = curve.calculate_costs(low, high, vec![passive_issuance]).unwrap();
114150

115151
let expected_costs = Float::from_str("0.50000024125").unwrap();
@@ -130,9 +166,14 @@ fn mint_coin_with_existing_supply_and_passive_issuance() {
130166

131167
let passive_issuance = Float::from_num(100);
132168

133-
// Costs for existing supply: 100000000 * ln(e^(100/100000000) + e^(100/100000000)) = 69314818.055994530942
134-
// Costs for new supply: 100000000 * ln(e^(101/100000000) + e^(100/100000000)) = 69314818.555994532192
135-
// Costs to mint the first coin: 69314818.555994532192 - 69314818.055994530942 = 0.50000000125
169+
// Costs for existing supply:
170+
// 100000000 * ln(e^(100/100000000) + e^(100/100000000)) = 69314818.055994530942
171+
172+
// Costs for new supply:
173+
// 100000000 * ln(e^(101/100000000) + e^(100/100000000)) = 69314818.555994532192
174+
175+
// Costs to mint the first coin:
176+
// 69314818.555994532192 - 69314818.055994530942 = 0.50000000125
136177
let costs = curve.calculate_costs(low, high, vec![passive_issuance]).unwrap();
137178

138179
let expected_costs = Float::from_str("0.50000000125").unwrap();

0 commit comments

Comments
 (0)