From 99c6b086eb1ee44451510be7b03bcb02db17a99e Mon Sep 17 00:00:00 2001 From: 1xstj <106580853+1xstj@users.noreply.github.com> Date: Tue, 17 Dec 2024 09:24:55 +0530 Subject: [PATCH] more tests passing --- .../src/functions/deposit.rs | 2 +- pallets/multi-asset-delegation/src/mock.rs | 88 +++++++++++-------- .../src/tests/delegate.rs | 83 ++++++++--------- .../src/tests/deposit.rs | 2 +- .../src/tests/operator.rs | 6 +- 5 files changed, 100 insertions(+), 81 deletions(-) diff --git a/pallets/multi-asset-delegation/src/functions/deposit.rs b/pallets/multi-asset-delegation/src/functions/deposit.rs index 08cff321..0f3b572b 100644 --- a/pallets/multi-asset-delegation/src/functions/deposit.rs +++ b/pallets/multi-asset-delegation/src/functions/deposit.rs @@ -55,7 +55,7 @@ impl Pallet { &Self::pallet_account(), amount, Preservation::Expendable, - ); + )?; }, Asset::Erc20(asset_address) => { let sender = evm_sender.ok_or(Error::::ERC20TransferFailed)?; diff --git a/pallets/multi-asset-delegation/src/mock.rs b/pallets/multi-asset-delegation/src/mock.rs index 99c4f2f5..a82c6812 100644 --- a/pallets/multi-asset-delegation/src/mock.rs +++ b/pallets/multi-asset-delegation/src/mock.rs @@ -36,6 +36,7 @@ use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; use serde_json::json; use sp_core::{sr25519, H160}; +use sp_keyring::AccountKeyring; use sp_keystore::{testing::MemoryKeystore, KeystoreExt, KeystorePtr}; use sp_runtime::{ testing::UintAuthorityId, @@ -52,9 +53,6 @@ pub type Balance = u128; type Nonce = u32; pub type AssetId = u128; -// AssetIds for tests -pub const VDOT: AssetId = 1; - // AccountIds for tests pub const ALICE: u8 = 1; pub const BOB: u8 = 2; @@ -302,7 +300,7 @@ parameter_types! { pub const MinOperatorBondAmount: u64 = 10_000; pub const BondDuration: u32 = 10; pub PID: PalletId = PalletId(*b"PotStake"); - pub SlashedAmountRecipient : AccountId = ALICE.into(); + pub SlashedAmountRecipient : AccountId = AccountKeyring::Alice.into(); #[derive(PartialEq, Eq, Clone, Copy, Debug, Encode, Decode, MaxEncodedLen, TypeInfo)] pub const MaxDelegatorBlueprints : u32 = 50; #[derive(PartialEq, Eq, Clone, Copy, Debug, Encode, Decode, MaxEncodedLen, TypeInfo)] @@ -399,18 +397,31 @@ 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 TNT: AssetId = 0; 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 // our desired mockup. pub fn new_test_ext_raw_authorities() -> sp_io::TestExternalities { let mut t = frame_system::GenesisConfig::::default().build_storage().unwrap(); // We use default for brevity, but you can configure as desired if needed. - let authorities = vec![mock_pub_key(1), mock_pub_key(2), mock_pub_key(3)]; - let balances: Vec<_> = authorities.iter().map(|i| (i.clone(), 20_000_u128)).collect(); + let authorities: Vec = vec![ + AccountKeyring::Alice.into(), + AccountKeyring::Bob.into(), + AccountKeyring::Charlie.into(), + ]; + let mut balances: Vec<_> = authorities.iter().map(|i| (i.clone(), 200_000_u128)).collect(); + + // Add test accounts with enough balance + let test_accounts = vec![ + AccountKeyring::Dave.into(), + AccountKeyring::Eve.into(), + MultiAssetDelegation::pallet_account(), + ]; + balances.extend(test_accounts.iter().map(|i: &AccountId| (i.clone(), 1_000_000_u128))); + pallet_balances::GenesisConfig:: { balances } .assimilate_storage(&mut t) .unwrap(); @@ -464,34 +475,41 @@ pub fn new_test_ext_raw_authorities() -> sp_io::TestExternalities { evm_config.assimilate_storage(&mut t).unwrap(); - let assets_config = pallet_assets::GenesisConfig:: { - assets: vec![ - (USDC, authorities[0].clone(), true, 100_000), // 1 cent. - (WETH, authorities[1].clone(), true, 100), // 100 wei. - (WBTC, authorities[2].clone(), true, 100), // 100 satoshi. - ], - metadata: vec![ - (USDC, Vec::from(b"USD Coin"), Vec::from(b"USDC"), 6), - (WETH, Vec::from(b"Wrapped Ether"), Vec::from(b"WETH"), 18), - (WBTC, Vec::from(b"Wrapped Bitcoin"), Vec::from(b"WBTC"), 18), - ], - accounts: vec![ - (USDC, authorities[0].clone(), 1_000_000 * 10u128.pow(6)), - (WETH, authorities[0].clone(), 100 * 10u128.pow(18)), - (WBTC, authorities[0].clone(), 50 * 10u128.pow(18)), - // - (USDC, authorities[1].clone(), 1_000_000 * 10u128.pow(6)), - (WETH, authorities[1].clone(), 100 * 10u128.pow(18)), - (WBTC, authorities[1].clone(), 50 * 10u128.pow(18)), - // - (USDC, authorities[2].clone(), 1_000_000 * 10u128.pow(6)), - (WETH, authorities[2].clone(), 100 * 10u128.pow(18)), - (WBTC, authorities[2].clone(), 50 * 10u128.pow(18)), - ], - next_asset_id: Some(4), - }; - - assets_config.assimilate_storage(&mut t).unwrap(); + // let assets_config = pallet_assets::GenesisConfig:: { + // assets: vec![ + // (USDC, authorities[0].clone(), true, 100_000), // 1 cent. + // (WETH, authorities[1].clone(), true, 100), // 100 wei. + // (WBTC, authorities[2].clone(), true, 100), // 100 satoshi. + // (VDOT, authorities[0].clone(), true, 100), + // ], + // metadata: vec![ + // (USDC, Vec::from(b"USD Coin"), Vec::from(b"USDC"), 6), + // (WETH, Vec::from(b"Wrapped Ether"), Vec::from(b"WETH"), 18), + // (WBTC, Vec::from(b"Wrapped Bitcoin"), Vec::from(b"WBTC"), 18), + // (VDOT, Vec::from(b"VeChain"), Vec::from(b"VDOT"), 18), + // ], + // accounts: vec![ + // (USDC, authorities[0].clone(), 1_000_000 * 10u128.pow(6)), + // (WETH, authorities[0].clone(), 100 * 10u128.pow(18)), + // (WBTC, authorities[0].clone(), 50 * 10u128.pow(18)), + // // + // (USDC, authorities[1].clone(), 1_000_000 * 10u128.pow(6)), + // (WETH, authorities[1].clone(), 100 * 10u128.pow(18)), + // (WBTC, authorities[1].clone(), 50 * 10u128.pow(18)), + // // + // (USDC, authorities[2].clone(), 1_000_000 * 10u128.pow(6)), + // (WETH, authorities[2].clone(), 100 * 10u128.pow(18)), + // (WBTC, authorities[2].clone(), 50 * 10u128.pow(18)), + + // // + // (VDOT, authorities[0].clone(), 1_000_000 * 10u128.pow(6)), + // (VDOT, authorities[1].clone(), 1_000_000 * 10u128.pow(6)), + // (VDOT, authorities[2].clone(), 1_000_000 * 10u128.pow(6)), + // ], + // next_asset_id: Some(4), + // }; + + // assets_config.assimilate_storage(&mut t).unwrap(); let mut ext = sp_io::TestExternalities::new(t); ext.register_extension(KeystoreExt(Arc::new(MemoryKeystore::new()) as KeystorePtr)); ext.execute_with(|| System::set_block_number(1)); diff --git a/pallets/multi-asset-delegation/src/tests/delegate.rs b/pallets/multi-asset-delegation/src/tests/delegate.rs index 1f6940c8..0a35b4f8 100644 --- a/pallets/multi-asset-delegation/src/tests/delegate.rs +++ b/pallets/multi-asset-delegation/src/tests/delegate.rs @@ -41,7 +41,7 @@ fn delegate_should_work() { // Deposit first assert_ok!(MultiAssetDelegation::deposit( RuntimeOrigin::signed(who.clone()), - asset_id, + asset_id.clone(), amount, None )); @@ -49,7 +49,7 @@ fn delegate_should_work() { assert_ok!(MultiAssetDelegation::delegate( RuntimeOrigin::signed(who.clone()), operator.clone(), - asset_id, + asset_id.clone(), amount, Default::default() )); @@ -64,7 +64,7 @@ fn delegate_should_work() { assert_eq!(delegation.asset_id, asset_id); // Check the operator metadata - let operator_metadata = MultiAssetDelegation::operator_info(operator).unwrap(); + let operator_metadata = MultiAssetDelegation::operator_info(operator.clone()).unwrap(); assert_eq!(operator_metadata.delegation_count, 1); assert_eq!(operator_metadata.delegations.len(), 1); let operator_delegation = &operator_metadata.delegations[0]; @@ -93,14 +93,14 @@ fn schedule_delegator_unstake_should_work() { // Deposit and delegate first assert_ok!(MultiAssetDelegation::deposit( RuntimeOrigin::signed(who.clone()), - asset_id, + asset_id.clone(), amount, None )); assert_ok!(MultiAssetDelegation::delegate( RuntimeOrigin::signed(who.clone()), operator.clone(), - asset_id, + asset_id.clone(), amount, Default::default() )); @@ -108,20 +108,20 @@ fn schedule_delegator_unstake_should_work() { assert_ok!(MultiAssetDelegation::schedule_delegator_unstake( RuntimeOrigin::signed(who.clone()), operator.clone(), - asset_id, + asset_id.clone(), amount, )); // Assert // Check the delegator metadata - let metadata = MultiAssetDelegation::delegators(who).unwrap(); + let metadata = MultiAssetDelegation::delegators(who.clone()).unwrap(); assert!(!metadata.delegator_unstake_requests.is_empty()); let request = &metadata.delegator_unstake_requests[0]; assert_eq!(request.asset_id, asset_id); assert_eq!(request.amount, amount); // Check the operator metadata - let operator_metadata = MultiAssetDelegation::operator_info(operator).unwrap(); + let operator_metadata = MultiAssetDelegation::operator_info(operator.clone()).unwrap(); assert_eq!(operator_metadata.delegation_count, 0); assert_eq!(operator_metadata.delegations.len(), 0); }); @@ -146,21 +146,21 @@ fn execute_delegator_unstake_should_work() { // Deposit, delegate and schedule unstake first assert_ok!(MultiAssetDelegation::deposit( RuntimeOrigin::signed(who.clone()), - asset_id, + asset_id.clone(), amount, None )); assert_ok!(MultiAssetDelegation::delegate( RuntimeOrigin::signed(who.clone()), operator.clone(), - asset_id, + asset_id.clone(), amount, Default::default() )); assert_ok!(MultiAssetDelegation::schedule_delegator_unstake( RuntimeOrigin::signed(who.clone()), operator.clone(), - asset_id, + asset_id.clone(), amount, )); @@ -172,7 +172,7 @@ fn execute_delegator_unstake_should_work() { ),)); // Assert - let metadata = MultiAssetDelegation::delegators(who).unwrap(); + let metadata = MultiAssetDelegation::delegators(who.clone()).unwrap(); assert!(metadata.delegator_unstake_requests.is_empty()); assert!(metadata.deposits.get(&asset_id).is_some()); assert_eq!(metadata.deposits.get(&asset_id).unwrap(), &amount); @@ -198,14 +198,14 @@ fn cancel_delegator_unstake_should_work() { // Deposit, delegate and schedule unstake first assert_ok!(MultiAssetDelegation::deposit( RuntimeOrigin::signed(who.clone()), - asset_id, + asset_id.clone(), amount, None )); assert_ok!(MultiAssetDelegation::delegate( RuntimeOrigin::signed(who.clone()), operator.clone(), - asset_id, + asset_id.clone(), amount, Default::default() )); @@ -213,37 +213,37 @@ fn cancel_delegator_unstake_should_work() { assert_ok!(MultiAssetDelegation::schedule_delegator_unstake( RuntimeOrigin::signed(who.clone()), operator.clone(), - asset_id, + asset_id.clone(), amount, )); // Assert // Check the delegator metadata - let metadata = MultiAssetDelegation::delegators(who).unwrap(); + let metadata = MultiAssetDelegation::delegators(who.clone()).unwrap(); assert!(!metadata.delegator_unstake_requests.is_empty()); let request = &metadata.delegator_unstake_requests[0]; assert_eq!(request.asset_id, asset_id); assert_eq!(request.amount, amount); // Check the operator metadata - let operator_metadata = MultiAssetDelegation::operator_info(operator).unwrap(); + let operator_metadata = MultiAssetDelegation::operator_info(operator.clone()).unwrap(); assert_eq!(operator_metadata.delegation_count, 0); assert_eq!(operator_metadata.delegations.len(), 0); assert_ok!(MultiAssetDelegation::cancel_delegator_unstake( RuntimeOrigin::signed(who.clone()), operator.clone(), - asset_id, + asset_id.clone(), amount )); // Assert // Check the delegator metadata - let metadata = MultiAssetDelegation::delegators(who).unwrap(); + let metadata = MultiAssetDelegation::delegators(who.clone()).unwrap(); assert!(metadata.delegator_unstake_requests.is_empty()); // Check the operator metadata - let operator_metadata = MultiAssetDelegation::operator_info(operator).unwrap(); + let operator_metadata = MultiAssetDelegation::operator_info(operator.clone()).unwrap(); assert_eq!(operator_metadata.delegation_count, 1); assert_eq!(operator_metadata.delegations.len(), 1); let operator_delegation = &operator_metadata.delegations[0]; @@ -272,14 +272,14 @@ fn cancel_delegator_unstake_should_update_already_existing() { // Deposit, delegate and schedule unstake first assert_ok!(MultiAssetDelegation::deposit( RuntimeOrigin::signed(who.clone()), - asset_id, + asset_id.clone(), amount, None )); assert_ok!(MultiAssetDelegation::delegate( RuntimeOrigin::signed(who.clone()), operator.clone(), - asset_id, + asset_id.clone(), amount, Default::default() )); @@ -287,20 +287,20 @@ fn cancel_delegator_unstake_should_update_already_existing() { assert_ok!(MultiAssetDelegation::schedule_delegator_unstake( RuntimeOrigin::signed(who.clone()), operator.clone(), - asset_id, + asset_id.clone(), 10, )); // Assert // Check the delegator metadata - let metadata = MultiAssetDelegation::delegators(who).unwrap(); + let metadata = MultiAssetDelegation::delegators(who.clone()).unwrap(); assert!(!metadata.delegator_unstake_requests.is_empty()); let request = &metadata.delegator_unstake_requests[0]; assert_eq!(request.asset_id, asset_id); assert_eq!(request.amount, 10); // Check the operator metadata - let operator_metadata = MultiAssetDelegation::operator_info(operator).unwrap(); + let operator_metadata = MultiAssetDelegation::operator_info(operator.clone()).unwrap(); assert_eq!(operator_metadata.delegation_count, 1); assert_eq!(operator_metadata.delegations.len(), 1); let operator_delegation = &operator_metadata.delegations[0]; @@ -311,17 +311,17 @@ fn cancel_delegator_unstake_should_update_already_existing() { assert_ok!(MultiAssetDelegation::cancel_delegator_unstake( RuntimeOrigin::signed(who.clone()), operator.clone(), - asset_id, + asset_id.clone(), 10 )); // Assert // Check the delegator metadata - let metadata = MultiAssetDelegation::delegators(who).unwrap(); + let metadata = MultiAssetDelegation::delegators(who.clone()).unwrap(); assert!(metadata.delegator_unstake_requests.is_empty()); // Check the operator metadata - let operator_metadata = MultiAssetDelegation::operator_info(operator).unwrap(); + let operator_metadata = MultiAssetDelegation::operator_info(operator.clone()).unwrap(); assert_eq!(operator_metadata.delegation_count, 1); assert_eq!(operator_metadata.delegations.len(), 1); let operator_delegation = &operator_metadata.delegations[0]; @@ -349,7 +349,7 @@ fn delegate_should_fail_if_not_enough_balance() { assert_ok!(MultiAssetDelegation::deposit( RuntimeOrigin::signed(who.clone()), - asset_id, + asset_id.clone(), amount - 20, None )); @@ -358,7 +358,7 @@ fn delegate_should_fail_if_not_enough_balance() { MultiAssetDelegation::delegate( RuntimeOrigin::signed(who.clone()), operator.clone(), - asset_id, + asset_id.clone(), amount, Default::default() ), @@ -386,7 +386,7 @@ fn schedule_delegator_unstake_should_fail_if_no_delegation() { // Deposit first assert_ok!(MultiAssetDelegation::deposit( RuntimeOrigin::signed(who.clone()), - asset_id, + asset_id.clone(), amount, None )); @@ -395,7 +395,7 @@ fn schedule_delegator_unstake_should_fail_if_no_delegation() { MultiAssetDelegation::schedule_delegator_unstake( RuntimeOrigin::signed(who.clone()), operator.clone(), - asset_id, + asset_id.clone(), amount, ), Error::::NoActiveDelegation @@ -422,14 +422,14 @@ fn execute_delegator_unstake_should_fail_if_not_ready() { // Deposit, delegate and schedule unstake first assert_ok!(MultiAssetDelegation::deposit( RuntimeOrigin::signed(who.clone()), - asset_id, + asset_id.clone(), amount, None )); assert_ok!(MultiAssetDelegation::delegate( RuntimeOrigin::signed(who.clone()), operator.clone(), - asset_id, + asset_id.clone(), amount, Default::default() )); @@ -438,7 +438,7 @@ fn execute_delegator_unstake_should_fail_if_not_ready() { MultiAssetDelegation::cancel_delegator_unstake( RuntimeOrigin::signed(who.clone()), operator.clone(), - asset_id, + asset_id.clone(), amount ), Error::::NoBondLessRequest @@ -447,7 +447,7 @@ fn execute_delegator_unstake_should_fail_if_not_ready() { assert_ok!(MultiAssetDelegation::schedule_delegator_unstake( RuntimeOrigin::signed(who.clone()), operator.clone(), - asset_id, + asset_id.clone(), amount, )); @@ -478,7 +478,7 @@ fn delegate_should_not_create_multiple_on_repeat_delegation() { // Deposit first assert_ok!(MultiAssetDelegation::deposit( RuntimeOrigin::signed(who.clone()), - asset_id, + asset_id.clone(), amount + additional_amount, None )); @@ -487,7 +487,7 @@ fn delegate_should_not_create_multiple_on_repeat_delegation() { assert_ok!(MultiAssetDelegation::delegate( RuntimeOrigin::signed(who.clone()), operator.clone(), - asset_id, + asset_id.clone(), amount, Default::default() )); @@ -502,7 +502,7 @@ fn delegate_should_not_create_multiple_on_repeat_delegation() { assert_eq!(delegation.asset_id, asset_id); // Check the operator metadata - let operator_metadata = MultiAssetDelegation::operator_info(operator).unwrap(); + let operator_metadata = MultiAssetDelegation::operator_info(operator.clone()).unwrap(); assert_eq!(operator_metadata.delegation_count, 1); assert_eq!(operator_metadata.delegations.len(), 1); let operator_delegation = &operator_metadata.delegations[0]; @@ -514,7 +514,7 @@ fn delegate_should_not_create_multiple_on_repeat_delegation() { assert_ok!(MultiAssetDelegation::delegate( RuntimeOrigin::signed(who.clone()), operator.clone(), - asset_id, + asset_id.clone(), additional_amount, Default::default() )); @@ -529,7 +529,8 @@ fn delegate_should_not_create_multiple_on_repeat_delegation() { assert_eq!(updated_delegation.asset_id, asset_id); // Check the updated operator metadata - let updated_operator_metadata = MultiAssetDelegation::operator_info(operator).unwrap(); + let updated_operator_metadata = + MultiAssetDelegation::operator_info(operator.clone()).unwrap(); assert_eq!(updated_operator_metadata.delegation_count, 1); assert_eq!(updated_operator_metadata.delegations.len(), 1); let updated_operator_delegation = &updated_operator_metadata.delegations[0]; diff --git a/pallets/multi-asset-delegation/src/tests/deposit.rs b/pallets/multi-asset-delegation/src/tests/deposit.rs index 193e1e2b..9a99faa5 100644 --- a/pallets/multi-asset-delegation/src/tests/deposit.rs +++ b/pallets/multi-asset-delegation/src/tests/deposit.rs @@ -106,7 +106,7 @@ fn multiple_deposit_should_work() { // Assert let metadata = MultiAssetDelegation::delegators(who.clone()).unwrap(); - assert_eq!(metadata.deposits.get(&Asset::Custom(VDOT),), Some(&amount * 2)); + assert_eq!(metadata.deposits.get(&Asset::Custom(VDOT),), Some(&(amount * 2))); assert_eq!( System::events().last().unwrap().event, RuntimeEvent::MultiAssetDelegation(crate::Event::Deposited { diff --git a/pallets/multi-asset-delegation/src/tests/operator.rs b/pallets/multi-asset-delegation/src/tests/operator.rs index 08752228..7a53c260 100644 --- a/pallets/multi-asset-delegation/src/tests/operator.rs +++ b/pallets/multi-asset-delegation/src/tests/operator.rs @@ -83,11 +83,11 @@ fn join_operator_insufficient_bond() { #[test] fn join_operator_insufficient_funds() { new_test_ext().execute_with(|| { - let bond_amount = 15_000; // User 4 has only 5_000 + let bond_amount = 350_000; // User 4 has only 200_000 assert_noop!( MultiAssetDelegation::join_operators( - RuntimeOrigin::signed(Eve.to_account_id()), + RuntimeOrigin::signed(Alice.to_account_id()), bond_amount ), pallet_balances::Error::::InsufficientBalance @@ -259,7 +259,7 @@ fn operator_bond_more_not_an_operator() { fn operator_bond_more_insufficient_balance() { new_test_ext().execute_with(|| { let bond_amount = 10_000; - let additional_bond = 115_000; // Exceeds available balance + let additional_bond = 1150_000; // Exceeds available balance // Join operator first assert_ok!(MultiAssetDelegation::join_operators(