Skip to content

Commit

Permalink
cleanup clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
1xstj committed Dec 17, 2024
1 parent 44500a5 commit b2815d3
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 295 deletions.
137 changes: 39 additions & 98 deletions pallets/multi-asset-delegation/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,6 @@ pub type Balance = u128;
type Nonce = u32;
pub type AssetId = u128;

// AccountIds for tests
pub const ALICE: u8 = 1;
pub const BOB: u8 = 2;
pub const CHARLIE: u8 = 3;
pub const DAVE: u8 = 4;
pub const EVE: u8 = 5;

#[frame_support::derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
impl frame_system::Config for Runtime {
type BaseCallFilter = frame_support::traits::Everything;
Expand Down Expand Up @@ -378,27 +371,19 @@ pub fn account_id_to_address(account_id: AccountId) -> H160 {
H160::from_slice(&AsRef::<[u8; 32]>::as_ref(&account_id)[0..20])
}

pub fn address_to_account_id(address: H160) -> AccountId {
use pallet_evm::AddressMapping;
<Runtime as pallet_evm::Config>::AddressMapping::into_account_id(address)
}

pub fn mock_authorities(vec: Vec<u8>) -> Vec<AccountId> {
vec.into_iter().map(|id| mock_pub_key(id)).collect()
}
// pub fn address_to_account_id(address: H160) -> AccountId {
// use pallet_evm::AddressMapping;
// <Runtime as pallet_evm::Config>::AddressMapping::into_account_id(address)
// }

pub fn new_test_ext() -> sp_io::TestExternalities {
new_test_ext_raw_authorities()
}

pub const MBSM: H160 = H160([0x12; 20]);
pub const CGGMP21_BLUEPRINT: H160 = H160([0x21; 20]);
pub const HOOKS_TEST: H160 = H160([0x22; 20]);
pub const USDC_ERC20: H160 = H160([0x23; 20]);

pub const USDC: AssetId = 1;
pub const WETH: AssetId = 2;
pub const WBTC: AssetId = 3;
// pub const USDC: AssetId = 1;
// pub const WETH: AssetId = 2;
// pub const WBTC: AssetId = 3;
pub const VDOT: AssetId = 4;

// This function basically just builds a genesis storage key/value store according to
Expand Down Expand Up @@ -427,24 +412,6 @@ pub fn new_test_ext_raw_authorities() -> sp_io::TestExternalities {

let mut evm_accounts = BTreeMap::new();

let create_contract = |bytecode: &str, address: H160| {
let mut raw_hex = bytecode.replace("0x", "").replace("\n", "");
// fix odd length
if raw_hex.len() % 2 != 0 {
raw_hex = format!("0{}", raw_hex);
}
let code = hex::decode(raw_hex).unwrap();
evm_accounts.insert(
address,
fp_evm::GenesisAccount {
code,
storage: Default::default(),
nonce: Default::default(),
balance: Default::default(),
},
);
};

for i in 1..=authorities.len() {
evm_accounts.insert(
mock_address(i as u8),
Expand Down Expand Up @@ -616,61 +583,35 @@ macro_rules! evm_log {
};
}

/// Asserts that the EVM logs are as expected.
#[track_caller]
pub fn assert_evm_logs(expected: &[fp_evm::Log]) {
assert_evm_events_contains(expected.iter().cloned().collect())
}

/// Asserts that the EVM events are as expected.
#[track_caller]
fn assert_evm_events_contains(expected: Vec<fp_evm::Log>) {
let actual: Vec<fp_evm::Log> = System::events()
.iter()
.filter_map(|e| match e.event {
RuntimeEvent::EVM(pallet_evm::Event::Log { ref log }) => Some(log.clone()),
_ => None,
})
.collect();

// Check if `expected` is a subset of `actual`
let mut any_matcher = false;
for evt in expected {
if !actual.contains(&evt) {
panic!("Events don't match\nactual: {actual:?}\nexpected: {evt:?}");
} else {
any_matcher = true;
}
}

// At least one event should be present
if !any_matcher {
panic!("No events found");
}
}

// Checks events against the latest. A contiguous set of events must be
// provided. They must include the most recent RuntimeEvent, but do not have to include
// every past RuntimeEvent.
#[track_caller]
pub fn assert_events(mut expected: Vec<RuntimeEvent>) {
let mut actual: Vec<RuntimeEvent> = System::events()
.iter()
.filter_map(|e| match e.event {
RuntimeEvent::MultiAssetDelegation(_) => Some(e.event.clone()),
_ => None,
})
.collect();

expected.reverse();
for evt in expected {
let next = actual.pop().expect("RuntimeEvent expected");
match (&next, &evt) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
panic!("Events don't match\nactual: {actual:#?}\nexpected: {evt:#?}");
}
},
};
}
}
// /// Asserts that the EVM logs are as expected.
// #[track_caller]
// pub fn assert_evm_logs(expected: &[fp_evm::Log]) {
// assert_evm_events_contains(expected.iter().cloned().collect())
// }

// /// Asserts that the EVM events are as expected.
// #[track_caller]
// fn assert_evm_events_contains(expected: Vec<fp_evm::Log>) {
// let actual: Vec<fp_evm::Log> = System::events()
// .iter()
// .filter_map(|e| match e.event {
// RuntimeEvent::EVM(pallet_evm::Event::Log { ref log }) => Some(log.clone()),
// _ => None,
// })
// .collect();

// // Check if `expected` is a subset of `actual`
// let mut any_matcher = false;
// for evt in expected {
// if !actual.contains(&evt) {
// panic!("Events don't match\nactual: {actual:?}\nexpected: {evt:?}");
// } else {
// any_matcher = true;
// }
// }

// // At least one event should be present
// if !any_matcher {
// panic!("No events found");
// }
// }
30 changes: 30 additions & 0 deletions pallets/multi-asset-delegation/src/tests/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,36 @@ fn deposit_should_work_for_fungible_asset() {
});
}

#[test]
fn deposit_should_work_for_evm_asset() {
new_test_ext().execute_with(|| {
// Arrange
let who: AccountId = Bob.into();
let amount = 200;

create_and_mint_tokens(VDOT, who.clone(), amount);

assert_ok!(MultiAssetDelegation::deposit(
RuntimeOrigin::signed(who.clone()),
Asset::Custom(VDOT),
amount,
None
));

// Assert
let metadata = MultiAssetDelegation::delegators(who.clone()).unwrap();
assert_eq!(metadata.deposits.get(&Asset::Custom(VDOT),), Some(&amount));
assert_eq!(
System::events().last().unwrap().event,
RuntimeEvent::MultiAssetDelegation(crate::Event::Deposited {
who: who.clone(),
amount,
asset_id: Asset::Custom(VDOT),
})
);
});
}

#[test]
fn multiple_deposit_should_work() {
new_test_ext().execute_with(|| {
Expand Down
2 changes: 1 addition & 1 deletion pallets/services/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use frame_support::{
construct_runtime, derive_impl, parameter_types,
traits::{AsEnsureOriginWithArg, ConstU128, ConstU32, OneSessionHandler},
};
use tangle_primitives::services::EvmRunner;
use frame_system::EnsureRoot;
use mock_evm::MockedEvmRunner;
use pallet_evm::GasWeightMapping;
Expand All @@ -39,6 +38,7 @@ use sp_runtime::{
AccountId32, BuildStorage, Perbill,
};
use tangle_primitives::services::Asset;
use tangle_primitives::services::EvmRunner;
use tangle_primitives::services::{EvmAddressMapping, EvmGasWeightMapping};

use core::ops::Mul;
Expand Down
Loading

0 comments on commit b2815d3

Please sign in to comment.