-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
π§βπ» Restructure instantiator into modules (#245)
## What? - Move the instantiator code from a single file to multiple submodules ## Why? - Was too big to read ## How? - Separate into different files inside the `instantiator` module ## Testing? Normal tests to see if there's no compiler errors
- Loading branch information
Showing
8 changed files
with
3,262 additions
and
3,279 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
1,075 changes: 1,075 additions & 0 deletions
1,075
pallets/funding/src/instantiator/chain_interactions.rs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
use super::*; | ||
|
||
#[macro_export] | ||
/// Example: | ||
/// ``` | ||
/// use pallet_funding::assert_close_enough; | ||
/// use sp_arithmetic::Perquintill; | ||
/// | ||
/// let real = 98u64; | ||
/// let desired = 100u64; | ||
/// assert_close_enough!(real, desired, Perquintill::from_float(0.98)); | ||
/// // This would fail | ||
/// // assert_close_enough!(real, desired, Perquintill::from_float(0.99)); | ||
/// ``` | ||
macro_rules! assert_close_enough { | ||
// Match when a message is provided | ||
($real:expr, $desired:expr, $min_percentage:expr, $msg:expr) => { | ||
let actual_percentage; | ||
if $real <= $desired { | ||
actual_percentage = Perquintill::from_rational($real, $desired); | ||
} else { | ||
actual_percentage = Perquintill::from_rational($desired, $real); | ||
} | ||
assert!(actual_percentage >= $min_percentage, $msg); | ||
}; | ||
// Match when no message is provided | ||
($real:expr, $desired:expr, $min_percentage:expr) => { | ||
let actual_percentage; | ||
if $real <= $desired { | ||
actual_percentage = Perquintill::from_rational($real, $desired); | ||
} else { | ||
actual_percentage = Perquintill::from_rational($desired, $real); | ||
} | ||
assert!( | ||
actual_percentage >= $min_percentage, | ||
"Actual percentage too low for the set minimum: {:?} < {:?} for {:?} and {:?}", | ||
actual_percentage, | ||
$min_percentage, | ||
$real, | ||
$desired | ||
); | ||
}; | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! call_and_is_ok { | ||
($inst: expr, $( $call: expr ),* ) => { | ||
$inst.execute(|| { | ||
$( | ||
let result = $call; | ||
assert!(result.is_ok(), "Call failed: {:?}", result); | ||
)* | ||
}) | ||
}; | ||
} | ||
#[macro_export] | ||
macro_rules! find_event { | ||
($runtime:ty, $pattern:pat, $($field_name:ident == $field_value:expr),+) => { | ||
{ | ||
let events = frame_system::Pallet::<$runtime>::events(); | ||
events.iter().find_map(|event_record| { | ||
let runtime_event = event_record.event.clone(); | ||
let runtime_event = <<$runtime as crate::Config>::RuntimeEvent>::from(runtime_event); | ||
if let Ok(funding_event) = TryInto::<crate::Event<$runtime>>::try_into(runtime_event) { | ||
if let $pattern = funding_event { | ||
let mut is_match = true; | ||
$( | ||
is_match &= $field_name == $field_value; | ||
)+ | ||
if is_match { | ||
return Some(funding_event.clone()); | ||
} | ||
} | ||
None | ||
} else { | ||
None | ||
} | ||
}) | ||
} | ||
}; | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! extract_from_event { | ||
($env: expr, $pattern:pat, $field:ident) => { | ||
$env.execute(|| { | ||
let events = System::events(); | ||
|
||
events.iter().find_map(|event_record| { | ||
if let frame_system::EventRecord { event: RuntimeEvent::PolimecFunding($pattern), .. } = event_record { | ||
Some($field.clone()) | ||
} else { | ||
None | ||
} | ||
}) | ||
}) | ||
}; | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! define_names { | ||
($($name:ident: $id:expr, $label:expr);* $(;)?) => { | ||
$( | ||
pub const $name: AccountId = $id; | ||
)* | ||
|
||
pub fn names() -> std::collections::HashMap<AccountId, &'static str> { | ||
let mut names = std::collections::HashMap::new(); | ||
$( | ||
names.insert($name, $label); | ||
)* | ||
names | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Polimec Blockchain β https://www.polimec.org/ | ||
// Copyright (C) Polimec 2022. All rights reserved. | ||
|
||
// The Polimec Blockchain is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// The Polimec Blockchain is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
use crate::{ | ||
traits::{BondingRequirementCalculation, ProvideAssetPrice}, | ||
*, | ||
}; | ||
use frame_support::{ | ||
pallet_prelude::*, | ||
traits::{ | ||
fungible::{Inspect as FungibleInspect, InspectHold as FungibleInspectHold, Mutate as FungibleMutate}, | ||
fungibles::{ | ||
metadata::Inspect as MetadataInspect, roles::Inspect as RolesInspect, Inspect as FungiblesInspect, | ||
Mutate as FungiblesMutate, | ||
}, | ||
AccountTouch, Get, OnFinalize, OnIdle, OnInitialize, | ||
}, | ||
weights::Weight, | ||
Parameter, | ||
}; | ||
use frame_system::pallet_prelude::BlockNumberFor; | ||
use itertools::Itertools; | ||
use parity_scale_codec::Decode; | ||
use polimec_common::{credentials::InvestorType, migration_types::MigrationOrigin}; | ||
#[cfg(any(test, feature = "std", feature = "runtime-benchmarks"))] | ||
use polimec_common_test_utils::generate_did_from_account; | ||
use sp_arithmetic::{ | ||
traits::{SaturatedConversion, Saturating, Zero}, | ||
FixedPointNumber, Percent, Perquintill, | ||
}; | ||
use sp_runtime::{ | ||
traits::{Convert, Member, One}, | ||
DispatchError, | ||
}; | ||
use sp_std::{ | ||
cell::RefCell, | ||
collections::{btree_map::*, btree_set::*}, | ||
iter::zip, | ||
marker::PhantomData, | ||
}; | ||
|
||
pub mod macros; | ||
pub use macros::*; | ||
pub mod types; | ||
pub use types::*; | ||
pub mod traits; | ||
pub use traits::*; | ||
#[cfg(feature = "std")] | ||
pub mod async_features; | ||
pub mod calculations; | ||
pub mod chain_interactions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use super::*; | ||
|
||
pub trait Deposits<T: Config> { | ||
fn existential_deposits(&self) -> Vec<UserToPLMCBalance<T>>; | ||
} | ||
pub trait Accounts { | ||
type Account; | ||
|
||
fn accounts(&self) -> Vec<Self::Account>; | ||
} | ||
|
||
pub enum MergeOperation { | ||
Add, | ||
Subtract, | ||
} | ||
pub trait AccountMerge: Accounts + Sized { | ||
/// The inner type of the Vec implementing this Trait. | ||
type Inner; | ||
/// Merge accounts in the list based on the operation. | ||
fn merge_accounts(&self, ops: MergeOperation) -> Self; | ||
/// Subtract amount of the matching accounts in the other list from the current list. | ||
/// If the account is not present in the current list, it is ignored. | ||
fn subtract_accounts(&self, other_list: Self) -> Self; | ||
|
||
fn sum_accounts(&self, other_list: Self) -> Self; | ||
} |
Oops, something went wrong.