diff --git a/crates/e2e/src/drink_client.rs b/crates/e2e/src/drink_client.rs index fb68dd4daa..bdc7220545 100644 --- a/crates/e2e/src/drink_client.rs +++ b/crates/e2e/src/drink_client.rs @@ -94,8 +94,8 @@ where BalanceOf: From, { pub fn new>(contracts: impl IntoIterator) -> Self { - let mut sandbox = Sandbox::new().expect("Failed to initialize Drink! sandbox"); - Self::fund_accounts(&mut sandbox); + let sandbox = Sandbox::::default(); + Self::fund_accounts(&sandbox); Self { sandbox, @@ -104,7 +104,7 @@ where } } - fn fund_accounts(sandbox: &mut Sandbox) { + fn fund_accounts(sandbox: &Sandbox) { const TOKENS: u128 = 1_000_000_000_000_000; let accounts = [ @@ -119,7 +119,7 @@ where ] .map(|kp| kp.public_key().0) .map(From::from); - for account in accounts.into_iter() { + for account in accounts.iter() { sandbox .mint_into(account, TOKENS.into()) .unwrap_or_else(|_| panic!("Failed to mint {} tokens", TOKENS)); @@ -147,7 +147,7 @@ where let (pair, seed) = Pair::generate(); self.sandbox - .mint_into(pair.public().0.into(), amount) + .mint_into(&pair.public().0.into(), amount) .expect("Failed to mint tokens"); Keypair::from_seed(seed).expect("Failed to create keypair") @@ -267,8 +267,8 @@ where ) -> Result, Self::Error> { let code = self.contracts.load_code(contract_name); let data = constructor_exec_input(constructor.clone()); - let result = self.sandbox.dry_run(|r| { - r.deploy_contract( + let result = self.sandbox.dry_run(|| { + self.sandbox.deploy_contract( code, value, data, @@ -389,8 +389,8 @@ where let exec_input = Encode::encode(message.clone().params().exec_input()); let account_id = (*account_id.as_ref()).into(); - let result = self.sandbox.dry_run(|r| { - r.call_contract( + let result = self.sandbox.dry_run(|| { + self.sandbox.call_contract( account_id, value, exec_input, @@ -456,20 +456,75 @@ where /// Exposes preset sandbox configurations to be used in tests. pub mod preset { - use drink::impl_sandbox_config; - pub mod mock_network { - use super::*; + use drink::{ + frame_system, + minimal, + runtime::{ + AccountIdFor, + RuntimeMetadataPrefixed, + }, + Extension, + SandboxConfig, + }; + use sp_runtime::traits::Dispatchable; - use pallet_contracts_mock_network::parachain::Runtime as ParachainRuntime; pub use pallet_contracts_mock_network::*; - impl_sandbox_config!( - #[doc = "A Sandbox configuration for the parachain runtime of pallet-contracts-mock-network."] - pub struct Config { - runtime: ParachainRuntime; - default_balance: INITIAL_BALANCE; - default_actor: ALICE; + + pub struct Config; + impl SandboxConfig for Config { + type Runtime = parachain::Runtime; + + fn execute_with(execute: impl FnOnce() -> T) -> T { + ParaA::execute_with(execute) } - ); + + fn dry_run(action: impl FnOnce() -> T) -> T { + EXT_PARAA.with(|v| { + let backend_backup = v.borrow_mut().as_backend(); + let result = action(); + + let mut v = v.borrow_mut(); + v.commit_all().expect("Failed to commit changes"); + v.backend = backend_backup; + result + }) + } + + fn register_extension(ext: E) { + EXT_PARAA.with(|v| v.borrow_mut().register_extension(ext)); + } + + fn initialize_block( + height: frame_system::pallet_prelude::BlockNumberFor, + parent_hash: ::Hash, + ) { + minimal::BlockBuilder::::initialize_block( + height, + parent_hash, + ) + } + + fn finalize_block( + height: frame_system::pallet_prelude::BlockNumberFor, + ) -> ::Hash { + minimal::BlockBuilder::::finalize_block(height) + } + + fn default_actor() -> AccountIdFor { + ALICE + } + + fn get_metadata() -> RuntimeMetadataPrefixed { + parachain::Runtime::metadata() + } + + fn convert_account_to_origin( + account: AccountIdFor, + ) -> <::RuntimeCall as Dispatchable>::RuntimeOrigin + { + Some(account).into() + } + } } } diff --git a/integration-tests/contract-xcm/lib.rs b/integration-tests/contract-xcm/lib.rs index 97155c546f..50c6f0d5d4 100644 --- a/integration-tests/contract-xcm/lib.rs +++ b/integration-tests/contract-xcm/lib.rs @@ -222,11 +222,18 @@ mod contract_xcm { #[ink_e2e::test(backend(runtime_only(runtime = mock_network::Config)))] async fn xcm_send_works(mut client: Client) -> E2EResult<()> { + use frame_support::traits::{ + fungibles::Mutate, + tokens::currency::Currency, + }; use mock_network::{ + parachain, parachain_account_sovereign_account_id, relay_chain, + ParaA, Relay, TestExt, + INITIAL_BALANCE, }; use pallet_balances::{ BalanceLock, @@ -240,27 +247,42 @@ mod contract_xcm { .submit() .await .expect("instantiate failed"); - let mut call_builder = contract.call_builder::(); + let account_id: &[u8; 32] = contract.account_id.as_ref(); + let account_id: [u8; 32] = account_id.clone(); + let account_id = account_id.into(); + + ParaA::execute_with(|| { + parachain::Balances::make_free_balance_be(&account_id, INITIAL_BALANCE); + parachain::Assets::mint_into(0u32.into(), &account_id, INITIAL_BALANCE) + .unwrap(); + }); + + Relay::execute_with(|| { + let sovereign_account = + parachain_account_sovereign_account_id(1u32, account_id.clone()); + relay_chain::Balances::make_free_balance_be( + &sovereign_account, + INITIAL_BALANCE, + ); + }); + + let mut call_builder = contract.call_builder::(); let message = call_builder.lock_funds_to_relay(500_000, 8_000); let call_res = client.call(&ink_e2e::alice(), &message).submit().await?; assert!(call_res.return_value().is_ok()); // Check if the funds are locked on the relay chain. - let account_id: &[u8; 32] = contract.account_id.as_ref(); - let account_id: [u8; 32] = account_id.clone(); - let account_id = account_id.into(); - dbg!(&account_id); - Relay::execute_with(|| { + use relay_chain::System; assert_eq!( relay_chain::Balances::locks( ¶chain_account_sovereign_account_id(1, account_id) ), vec![BalanceLock { id: *b"py/xcmlk", - amount: 5_000, + amount: 500_000, reasons: Reasons::All }] );