Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧑‍💻 Restructure instantiator into modules #245

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,279 changes: 0 additions & 3,279 deletions pallets/funding/src/instantiator.rs

This file was deleted.

967 changes: 967 additions & 0 deletions pallets/funding/src/instantiator/async_features.rs

Large diffs are not rendered by default.

563 changes: 563 additions & 0 deletions pallets/funding/src/instantiator/calculations.rs

Large diffs are not rendered by default.

1,075 changes: 1,075 additions & 0 deletions pallets/funding/src/instantiator/chain_interactions.rs

Large diffs are not rendered by default.

115 changes: 115 additions & 0 deletions pallets/funding/src/instantiator/macros.rs
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
}
};
}
64 changes: 64 additions & 0 deletions pallets/funding/src/instantiator/mod.rs
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;
26 changes: 26 additions & 0 deletions pallets/funding/src/instantiator/traits.rs
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;
}
Loading