From aa78fe218014f653f883173a6e8d610964d56fbe Mon Sep 17 00:00:00 2001
From: PG Herveou <pgherveou@gmail.com>
Date: Wed, 17 Apr 2024 18:43:56 +0200
Subject: [PATCH] Contracts: Refactor test builder (#4158)

- Moved `substrate/frame/contracts/src/tests/builder.rs` into a pub
test_utils module, so we can use that in the
`pallet-contracts-mock-network` tests
- Refactor xcm tests to use XCM builders, and simplify the use case for
xcm-send
---
 .../frame/contracts/mock-network/src/lib.rs   |   3 +-
 .../frame/contracts/mock-network/src/tests.rs | 168 +++++--------
 substrate/frame/contracts/src/lib.rs          |   1 +
 substrate/frame/contracts/src/test_utils.rs   |  30 +++
 .../frame/contracts/src/test_utils/builder.rs | 220 ++++++++++++++++++
 substrate/frame/contracts/src/tests.rs        |  43 +++-
 .../frame/contracts/src/tests/builder.rs      | 219 -----------------
 7 files changed, 346 insertions(+), 338 deletions(-)
 create mode 100644 substrate/frame/contracts/src/test_utils.rs
 create mode 100644 substrate/frame/contracts/src/test_utils/builder.rs
 delete mode 100644 substrate/frame/contracts/src/tests/builder.rs

diff --git a/substrate/frame/contracts/mock-network/src/lib.rs b/substrate/frame/contracts/mock-network/src/lib.rs
index 8a17a3f2fa7..20ded0f4a0b 100644
--- a/substrate/frame/contracts/mock-network/src/lib.rs
+++ b/substrate/frame/contracts/mock-network/src/lib.rs
@@ -23,6 +23,7 @@ pub mod relay_chain;
 mod tests;
 
 use crate::primitives::{AccountId, UNITS};
+pub use pallet_contracts::test_utils::{ALICE, BOB};
 use sp_runtime::BuildStorage;
 use xcm::latest::prelude::*;
 use xcm_executor::traits::ConvertLocation;
@@ -31,8 +32,6 @@ use xcm_simulator::{decl_test_network, decl_test_parachain, decl_test_relay_chai
 
 // Accounts
 pub const ADMIN: sp_runtime::AccountId32 = sp_runtime::AccountId32::new([0u8; 32]);
-pub const ALICE: sp_runtime::AccountId32 = sp_runtime::AccountId32::new([1u8; 32]);
-pub const BOB: sp_runtime::AccountId32 = sp_runtime::AccountId32::new([2u8; 32]);
 
 // Balances
 pub const INITIAL_BALANCE: u128 = 1_000_000_000 * UNITS;
diff --git a/substrate/frame/contracts/mock-network/src/tests.rs b/substrate/frame/contracts/mock-network/src/tests.rs
index 39aa9bebc0f..5632f75e787 100644
--- a/substrate/frame/contracts/mock-network/src/tests.rs
+++ b/substrate/frame/contracts/mock-network/src/tests.rs
@@ -22,45 +22,31 @@ use crate::{
 	relay_chain, MockNet, ParaA, ParachainBalances, Relay, ALICE, BOB, INITIAL_BALANCE,
 };
 use codec::{Decode, Encode};
-use frame_support::{
-	pallet_prelude::Weight,
-	traits::{fungibles::Mutate, Currency},
-};
-use pallet_balances::{BalanceLock, Reasons};
-use pallet_contracts::{Code, CollectEvents, DebugInfo, Determinism};
+use frame_support::traits::{fungibles::Mutate, Currency};
+use pallet_contracts::{test_utils::builder::*, Code};
 use pallet_contracts_fixtures::compile_module;
 use pallet_contracts_uapi::ReturnErrorCode;
 use xcm::{v4::prelude::*, VersionedLocation, VersionedXcm};
 use xcm_simulator::TestExt;
 
-type ParachainContracts = pallet_contracts::Pallet<parachain::Runtime>;
-
 macro_rules! assert_return_code {
 	( $x:expr , $y:expr $(,)? ) => {{
 		assert_eq!(u32::from_le_bytes($x.data[..].try_into().unwrap()), $y as u32);
 	}};
 }
 
+fn bare_call(dest: sp_runtime::AccountId32) -> BareCallBuilder<parachain::Runtime> {
+	BareCallBuilder::<parachain::Runtime>::bare_call(ALICE, dest)
+}
+
 /// Instantiate the tests contract, and fund it with some balance and assets.
 fn instantiate_test_contract(name: &str) -> AccountId {
 	let (wasm, _) = compile_module::<Runtime>(name).unwrap();
 
 	// Instantiate contract.
 	let contract_addr = ParaA::execute_with(|| {
-		ParachainContracts::bare_instantiate(
-			ALICE,
-			0,
-			Weight::MAX,
-			None,
-			Code::Upload(wasm),
-			vec![],
-			vec![],
-			DebugInfo::UnsafeDebug,
-			CollectEvents::Skip,
-		)
-		.result
-		.unwrap()
-		.account_id
+		BareInstantiateBuilder::<parachain::Runtime>::bare_instantiate(ALICE, Code::Upload(wasm))
+			.build_and_unwrap_account_id()
 	});
 
 	// Funds contract account with some balance and assets.
@@ -85,27 +71,18 @@ fn test_xcm_execute() {
 	// Execute XCM instructions through the contract.
 	ParaA::execute_with(|| {
 		let amount: u128 = 10 * CENTS;
+		let assets: Asset = (Here, amount).into();
+		let beneficiary = AccountId32 { network: None, id: BOB.clone().into() };
 
 		// The XCM used to transfer funds to Bob.
-		let message: Xcm<()> = Xcm(vec![
-			WithdrawAsset(vec![(Here, amount).into()].into()),
-			DepositAsset {
-				assets: All.into(),
-				beneficiary: AccountId32 { network: None, id: BOB.clone().into() }.into(),
-			},
-		]);
-
-		let result = ParachainContracts::bare_call(
-			ALICE,
-			contract_addr.clone(),
-			0,
-			Weight::MAX,
-			None,
-			VersionedXcm::V4(message).encode().encode(),
-			DebugInfo::UnsafeDebug,
-			CollectEvents::UnsafeCollect,
-			Determinism::Enforced,
-		);
+		let message: Xcm<()> = Xcm::builder_unsafe()
+			.withdraw_asset(assets.clone())
+			.deposit_asset(assets, beneficiary)
+			.build();
+
+		let result = bare_call(contract_addr.clone())
+			.data(VersionedXcm::V4(message).encode().encode())
+			.build();
 
 		assert_eq!(result.gas_consumed, result.gas_required);
 		assert_return_code!(&result.result.unwrap(), ReturnErrorCode::Success);
@@ -127,29 +104,22 @@ fn test_xcm_execute_incomplete() {
 
 	// Execute XCM instructions through the contract.
 	ParaA::execute_with(|| {
+		let assets: Asset = (Here, amount).into();
+		let beneficiary = AccountId32 { network: None, id: BOB.clone().into() };
+
 		// The XCM used to transfer funds to Bob.
-		let message: Xcm<()> = Xcm(vec![
-			WithdrawAsset(vec![(Here, amount).into()].into()),
+		let message: Xcm<()> = Xcm::builder_unsafe()
+			.withdraw_asset(assets.clone())
 			// This will fail as the contract does not have enough balance to complete both
 			// withdrawals.
-			WithdrawAsset(vec![(Here, INITIAL_BALANCE).into()].into()),
-			DepositAsset {
-				assets: All.into(),
-				beneficiary: AccountId32 { network: None, id: BOB.clone().into() }.into(),
-			},
-		]);
-
-		let result = ParachainContracts::bare_call(
-			ALICE,
-			contract_addr.clone(),
-			0,
-			Weight::MAX,
-			None,
-			VersionedXcm::V4(message).encode().encode(),
-			DebugInfo::UnsafeDebug,
-			CollectEvents::UnsafeCollect,
-			Determinism::Enforced,
-		);
+			.withdraw_asset((Here, INITIAL_BALANCE))
+			.buy_execution(assets.clone(), Unlimited)
+			.deposit_asset(assets, beneficiary)
+			.build();
+
+		let result = bare_call(contract_addr.clone())
+			.data(VersionedXcm::V4(message).encode().encode())
+			.build();
 
 		assert_eq!(result.gas_consumed, result.gas_required);
 		assert_return_code!(&result.result.unwrap(), ReturnErrorCode::XcmExecutionFailed);
@@ -175,28 +145,16 @@ fn test_xcm_execute_reentrant_call() {
 		});
 
 		// The XCM used to transfer funds to Bob.
-		let message: Xcm<parachain::RuntimeCall> = Xcm(vec![
-			Transact {
-				origin_kind: OriginKind::Native,
-				require_weight_at_most: 1_000_000_000.into(),
-				call: transact_call.encode().into(),
-			},
-			ExpectTransactStatus(MaybeErrorCode::Success),
-		]);
-
-		let result = ParachainContracts::bare_call(
-			ALICE,
-			contract_addr.clone(),
-			0,
-			Weight::MAX,
-			None,
-			VersionedXcm::V4(message).encode().encode(),
-			DebugInfo::UnsafeDebug,
-			CollectEvents::UnsafeCollect,
-			Determinism::Enforced,
-		);
+		let message: Xcm<parachain::RuntimeCall> = Xcm::builder_unsafe()
+			.transact(OriginKind::Native, 1_000_000_000, transact_call.encode())
+			.expect_transact_status(MaybeErrorCode::Success)
+			.build();
 
-		assert_return_code!(&result.result.unwrap(), ReturnErrorCode::XcmExecutionFailed);
+		let result = bare_call(contract_addr.clone())
+			.data(VersionedXcm::V4(message).encode().encode())
+			.build_and_unwrap_result();
+
+		assert_return_code!(&result, ReturnErrorCode::XcmExecutionFailed);
 
 		// Funds should not change hands as the XCM transact failed.
 		assert_eq!(ParachainBalances::free_balance(BOB), INITIAL_BALANCE);
@@ -207,40 +165,36 @@ fn test_xcm_execute_reentrant_call() {
 fn test_xcm_send() {
 	MockNet::reset();
 	let contract_addr = instantiate_test_contract("xcm_send");
+	let amount = 1_000 * CENTS;
 	let fee = parachain::estimate_message_fee(4); // Accounts for the `DescendOrigin` instruction added by `send_xcm`
 
-	// Send XCM instructions through the contract, to lock some funds on the relay chain.
+	// Send XCM instructions through the contract, to transfer some funds from the contract
+	// derivative account to Alice on the relay chain.
 	ParaA::execute_with(|| {
-		let dest = Location::from(Parent);
-		let dest = VersionedLocation::V4(dest);
-
-		let message: Xcm<()> = Xcm(vec![
-			WithdrawAsset((Here, fee).into()),
-			BuyExecution { fees: (Here, fee).into(), weight_limit: WeightLimit::Unlimited },
-			LockAsset { asset: (Here, 5 * CENTS).into(), unlocker: (Parachain(1)).into() },
-		]);
-		let message = VersionedXcm::V4(message);
-		let exec = ParachainContracts::bare_call(
-			ALICE,
-			contract_addr.clone(),
-			0,
-			Weight::MAX,
-			None,
-			(dest, message.encode()).encode(),
-			DebugInfo::UnsafeDebug,
-			CollectEvents::UnsafeCollect,
-			Determinism::Enforced,
-		);
+		let dest = VersionedLocation::V4(Parent.into());
+		let assets: Asset = (Here, amount).into();
+		let beneficiary = AccountId32 { network: None, id: ALICE.clone().into() };
+
+		let message: Xcm<()> = Xcm::builder()
+			.withdraw_asset(assets.clone())
+			.buy_execution((Here, fee), Unlimited)
+			.deposit_asset(assets, beneficiary)
+			.build();
+
+		let result = bare_call(contract_addr.clone())
+			.data((dest, VersionedXcm::V4(message).encode()).encode())
+			.build_and_unwrap_result();
 
-		let mut data = &exec.result.unwrap().data[..];
+		let mut data = &result.data[..];
 		XcmHash::decode(&mut data).expect("Failed to decode xcm_send message_id");
 	});
 
 	Relay::execute_with(|| {
-		// Check if the funds are locked on the relay chain.
+		let derived_contract_addr = &parachain_account_sovereign_account_id(1, contract_addr);
 		assert_eq!(
-			relay_chain::Balances::locks(&parachain_account_sovereign_account_id(1, contract_addr)),
-			vec![BalanceLock { id: *b"py/xcmlk", amount: 5 * CENTS, reasons: Reasons::All }]
+			INITIAL_BALANCE - amount,
+			relay_chain::Balances::free_balance(derived_contract_addr)
 		);
+		assert_eq!(INITIAL_BALANCE + amount - fee, relay_chain::Balances::free_balance(ALICE));
 	});
 }
diff --git a/substrate/frame/contracts/src/lib.rs b/substrate/frame/contracts/src/lib.rs
index edc4c872bfc..b381fd2dc4f 100644
--- a/substrate/frame/contracts/src/lib.rs
+++ b/substrate/frame/contracts/src/lib.rs
@@ -101,6 +101,7 @@ mod wasm;
 pub mod chain_extension;
 pub mod debug;
 pub mod migration;
+pub mod test_utils;
 pub mod weights;
 
 #[cfg(test)]
diff --git a/substrate/frame/contracts/src/test_utils.rs b/substrate/frame/contracts/src/test_utils.rs
new file mode 100644
index 00000000000..564b2d2e3bd
--- /dev/null
+++ b/substrate/frame/contracts/src/test_utils.rs
@@ -0,0 +1,30 @@
+// This file is part of Substrate.
+
+// Copyright (C) Parity Technologies (UK) Ltd.
+// SPDX-License-Identifier: Apache-2.0
+
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// 	http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+//! Shared utilities for testing contracts.
+//! This is not part of the tests module because it is made public for other crates to use.
+#![cfg(feature = "std")]
+use frame_support::weights::Weight;
+pub use sp_runtime::AccountId32;
+
+pub const ALICE: AccountId32 = AccountId32::new([1u8; 32]);
+pub const BOB: AccountId32 = AccountId32::new([2u8; 32]);
+pub const CHARLIE: AccountId32 = AccountId32::new([3u8; 32]);
+pub const DJANGO: AccountId32 = AccountId32::new([4u8; 32]);
+
+pub const GAS_LIMIT: Weight = Weight::from_parts(100_000_000_000, 3 * 1024 * 1024);
+pub mod builder;
diff --git a/substrate/frame/contracts/src/test_utils/builder.rs b/substrate/frame/contracts/src/test_utils/builder.rs
new file mode 100644
index 00000000000..94540eca5b4
--- /dev/null
+++ b/substrate/frame/contracts/src/test_utils/builder.rs
@@ -0,0 +1,220 @@
+// This file is part of Substrate.
+
+// Copyright (C) Parity Technologies (UK) Ltd.
+// SPDX-License-Identifier: Apache-2.0
+
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// 	http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+use super::GAS_LIMIT;
+use crate::{
+	AccountIdLookupOf, AccountIdOf, BalanceOf, Code, CodeHash, CollectEvents, Config,
+	ContractExecResult, ContractInstantiateResult, DebugInfo, Determinism, EventRecordOf,
+	ExecReturnValue, InstantiateReturnValue, OriginFor, Pallet, Weight,
+};
+use codec::{Encode, HasCompact};
+use core::fmt::Debug;
+use frame_support::pallet_prelude::DispatchResultWithPostInfo;
+use paste::paste;
+use scale_info::TypeInfo;
+
+/// Helper macro to generate a builder for contract API calls.
+macro_rules! builder {
+	// Entry point to generate a builder for the given method.
+	(
+		$method:ident($($field:ident: $type:ty,)*) -> $result:ty;
+        $($extra:item)*
+	) => {
+		paste!{
+			builder!([< $method:camel Builder >], $method($($field: $type,)* ) -> $result; $($extra)*);
+		}
+	};
+	// Generate the builder struct and its methods.
+	(
+		$name:ident,
+		$method:ident($($field:ident: $type:ty,)*) -> $result:ty;
+        $($extra:item)*
+	) => {
+		#[doc = concat!("A builder to construct a ", stringify!($method), " call")]
+		pub struct $name<T: Config> {
+			$($field: $type,)*
+		}
+
+		#[allow(dead_code)]
+		impl<T: Config> $name<T>
+		where
+			<BalanceOf<T> as HasCompact>::Type: Clone + Eq + PartialEq + Debug + TypeInfo + Encode,
+		{
+			$(
+				#[doc = concat!("Set the ", stringify!($field))]
+				pub fn $field(mut self, value: $type) -> Self {
+					self.$field = value;
+					self
+				}
+			)*
+
+			#[doc = concat!("Build the ", stringify!($method), " call")]
+			pub fn build(self) -> $result {
+				Pallet::<T>::$method(
+					$(self.$field,)*
+				)
+			}
+
+            $($extra)*
+		}
+	}
+}
+
+builder!(
+	instantiate_with_code(
+		origin: OriginFor<T>,
+		value: BalanceOf<T>,
+		gas_limit: Weight,
+		storage_deposit_limit: Option<<BalanceOf<T> as codec::HasCompact>::Type>,
+		code: Vec<u8>,
+		data: Vec<u8>,
+		salt: Vec<u8>,
+	) -> DispatchResultWithPostInfo;
+
+	/// Create an [`InstantiateWithCodeBuilder`] with default values.
+	pub fn instantiate_with_code(origin: OriginFor<T>, code: Vec<u8>) -> Self {
+		Self {
+			origin: origin,
+			value: 0u32.into(),
+			gas_limit: GAS_LIMIT,
+			storage_deposit_limit: None,
+			code,
+			data: vec![],
+			salt: vec![],
+		}
+	}
+);
+
+builder!(
+	instantiate(
+		origin: OriginFor<T>,
+		value: BalanceOf<T>,
+		gas_limit: Weight,
+		storage_deposit_limit: Option<<BalanceOf<T> as codec::HasCompact>::Type>,
+		code_hash: CodeHash<T>,
+		data: Vec<u8>,
+		salt: Vec<u8>,
+	) -> DispatchResultWithPostInfo;
+
+	/// Create an [`InstantiateBuilder`] with default values.
+	pub fn instantiate(origin: OriginFor<T>, code_hash: CodeHash<T>) -> Self {
+		Self {
+			origin,
+			value: 0u32.into(),
+			gas_limit: GAS_LIMIT,
+			storage_deposit_limit: None,
+			code_hash,
+			data: vec![],
+			salt: vec![],
+		}
+	}
+);
+
+builder!(
+	bare_instantiate(
+		origin: AccountIdOf<T>,
+		value: BalanceOf<T>,
+		gas_limit: Weight,
+		storage_deposit_limit: Option<BalanceOf<T>>,
+		code: Code<CodeHash<T>>,
+		data: Vec<u8>,
+		salt: Vec<u8>,
+		debug: DebugInfo,
+		collect_events: CollectEvents,
+	) -> ContractInstantiateResult<AccountIdOf<T>, BalanceOf<T>, EventRecordOf<T>>;
+
+	/// Build the instantiate call and unwrap the result.
+	pub fn build_and_unwrap_result(self) -> InstantiateReturnValue<AccountIdOf<T>> {
+		self.build().result.unwrap()
+	}
+
+	/// Build the instantiate call and unwrap the account id.
+	pub fn build_and_unwrap_account_id(self) -> AccountIdOf<T> {
+		self.build().result.unwrap().account_id
+	}
+
+	pub fn bare_instantiate(origin: AccountIdOf<T>, code: Code<CodeHash<T>>) -> Self {
+		Self {
+			origin,
+			value: 0u32.into(),
+			gas_limit: GAS_LIMIT,
+			storage_deposit_limit: None,
+			code,
+			data: vec![],
+			salt: vec![],
+			debug: DebugInfo::Skip,
+			collect_events: CollectEvents::Skip,
+		}
+	}
+);
+
+builder!(
+	call(
+		origin: OriginFor<T>,
+		dest: AccountIdLookupOf<T>,
+		value: BalanceOf<T>,
+		gas_limit: Weight,
+		storage_deposit_limit: Option<<BalanceOf<T> as codec::HasCompact>::Type>,
+		data: Vec<u8>,
+	) -> DispatchResultWithPostInfo;
+
+	/// Create a [`CallBuilder`] with default values.
+	pub fn call(origin: OriginFor<T>, dest: AccountIdLookupOf<T>) -> Self {
+		CallBuilder {
+			origin,
+			dest,
+			value: 0u32.into(),
+			gas_limit: GAS_LIMIT,
+			storage_deposit_limit: None,
+			data: vec![],
+		}
+	}
+);
+
+builder!(
+	bare_call(
+		origin: AccountIdOf<T>,
+		dest: AccountIdOf<T>,
+		value: BalanceOf<T>,
+		gas_limit: Weight,
+		storage_deposit_limit: Option<BalanceOf<T>>,
+		data: Vec<u8>,
+		debug: DebugInfo,
+		collect_events: CollectEvents,
+		determinism: Determinism,
+	) -> ContractExecResult<BalanceOf<T>, EventRecordOf<T>>;
+
+	/// Build the call and unwrap the result.
+	pub fn build_and_unwrap_result(self) -> ExecReturnValue {
+		self.build().result.unwrap()
+	}
+
+	/// Create a [`BareCallBuilder`] with default values.
+	pub fn bare_call(origin: AccountIdOf<T>, dest: AccountIdOf<T>) -> Self {
+		Self {
+			origin,
+			dest,
+			value: 0u32.into(),
+			gas_limit: GAS_LIMIT,
+			storage_deposit_limit: None,
+			data: vec![],
+			debug: DebugInfo::Skip,
+			collect_events: CollectEvents::Skip,
+			determinism: Determinism::Enforced,
+		}
+	}
+);
diff --git a/substrate/frame/contracts/src/tests.rs b/substrate/frame/contracts/src/tests.rs
index 57b804a51e4..8fe845fcf0f 100644
--- a/substrate/frame/contracts/src/tests.rs
+++ b/substrate/frame/contracts/src/tests.rs
@@ -15,7 +15,6 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-mod builder;
 mod pallet_dummy;
 mod test_debug;
 
@@ -98,7 +97,6 @@ macro_rules! assert_refcount {
 }
 
 pub mod test_utils {
-
 	use super::{Contracts, DepositPerByte, DepositPerItem, Hash, SysConfig, Test};
 	use crate::{
 		exec::AccountIdOf, BalanceOf, CodeHash, CodeInfo, CodeInfoOf, Config, ContractInfo,
@@ -166,6 +164,38 @@ pub mod test_utils {
 	}
 }
 
+mod builder {
+	use super::Test;
+	use crate::{
+		test_utils::{builder::*, AccountId32, ALICE},
+		tests::RuntimeOrigin,
+		AccountIdLookupOf, Code, CodeHash,
+	};
+
+	pub fn bare_instantiate(code: Code<CodeHash<Test>>) -> BareInstantiateBuilder<Test> {
+		BareInstantiateBuilder::<Test>::bare_instantiate(ALICE, code)
+	}
+
+	pub fn bare_call(dest: AccountId32) -> BareCallBuilder<Test> {
+		BareCallBuilder::<Test>::bare_call(ALICE, dest)
+	}
+
+	pub fn instantiate_with_code(code: Vec<u8>) -> InstantiateWithCodeBuilder<Test> {
+		InstantiateWithCodeBuilder::<Test>::instantiate_with_code(
+			RuntimeOrigin::signed(ALICE),
+			code,
+		)
+	}
+
+	pub fn instantiate(code_hash: CodeHash<Test>) -> InstantiateBuilder<Test> {
+		InstantiateBuilder::<Test>::instantiate(RuntimeOrigin::signed(ALICE), code_hash)
+	}
+
+	pub fn call(dest: AccountIdLookupOf<Test>) -> CallBuilder<Test> {
+		CallBuilder::<Test>::call(RuntimeOrigin::signed(ALICE), dest)
+	}
+}
+
 impl Test {
 	pub fn set_unstable_interface(unstable_interface: bool) {
 		UNSTABLE_INTERFACE.with(|v| *v.borrow_mut() = unstable_interface);
@@ -2439,14 +2469,7 @@ fn failed_deposit_charge_should_roll_back_call() {
 				transfer_proxy_call,
 			);
 
-			<Pallet<Test>>::call(
-				RuntimeOrigin::signed(ALICE),
-				addr_caller.clone(),
-				0,
-				GAS_LIMIT,
-				None,
-				data.encode(),
-			)
+			builder::call(addr_caller).data(data.encode()).build()
 		})
 	};
 
diff --git a/substrate/frame/contracts/src/tests/builder.rs b/substrate/frame/contracts/src/tests/builder.rs
deleted file mode 100644
index 08d12503a29..00000000000
--- a/substrate/frame/contracts/src/tests/builder.rs
+++ /dev/null
@@ -1,219 +0,0 @@
-// This file is part of Substrate.
-
-// Copyright (C) Parity Technologies (UK) Ltd.
-// SPDX-License-Identifier: Apache-2.0
-
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// 	http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-use super::{AccountId32, Test, ALICE, GAS_LIMIT};
-use crate::{
-	tests::RuntimeOrigin, AccountIdLookupOf, AccountIdOf, BalanceOf, Code, CodeHash, CollectEvents,
-	ContractExecResult, ContractInstantiateResult, DebugInfo, Determinism, EventRecordOf,
-	ExecReturnValue, OriginFor, Pallet, Weight,
-};
-use codec::Compact;
-use frame_support::pallet_prelude::DispatchResultWithPostInfo;
-use paste::paste;
-
-/// Helper macro to generate a builder for contract API calls.
-macro_rules! builder {
-	// Entry point to generate a builder for the given method.
-	(
-		$method:ident($($field:ident: $type:ty,)*) -> $result:ty
-	) => {
-		paste!{
-			builder!([< $method:camel Builder >], $method($($field: $type,)* ) -> $result);
-		}
-	};
-	// Generate the builder struct and its methods.
-	(
-		$name:ident,
-		$method:ident(
-			$($field:ident: $type:ty,)*
-		) -> $result:ty
-	) => {
-		#[doc = concat!("A builder to construct a ", stringify!($method), " call")]
-		pub struct $name {
-			$($field: $type,)*
-		}
-
-		#[allow(dead_code)]
-		impl $name
-		{
-			$(
-				#[doc = concat!("Set the ", stringify!($field))]
-				pub fn $field(mut self, value: $type) -> Self {
-					self.$field = value;
-					self
-				}
-			)*
-
-			#[doc = concat!("Build the ", stringify!($method), " call")]
-			pub fn build(self) -> $result {
-				Pallet::<Test>::$method(
-					$(self.$field,)*
-				)
-			}
-		}
-	}
-}
-
-builder!(
-	instantiate_with_code(
-		origin: OriginFor<Test>,
-		value: BalanceOf<Test>,
-		gas_limit: Weight,
-		storage_deposit_limit: Option<Compact<BalanceOf<Test>>>,
-		code: Vec<u8>,
-		data: Vec<u8>,
-		salt: Vec<u8>,
-	) -> DispatchResultWithPostInfo
-);
-
-builder!(
-	instantiate(
-		origin: OriginFor<Test>,
-		value: BalanceOf<Test>,
-		gas_limit: Weight,
-		storage_deposit_limit: Option<Compact<BalanceOf<Test>>>,
-		code_hash: CodeHash<Test>,
-		data: Vec<u8>,
-		salt: Vec<u8>,
-	) -> DispatchResultWithPostInfo
-);
-
-builder!(
-	bare_instantiate(
-		origin: AccountIdOf<Test>,
-		value: BalanceOf<Test>,
-		gas_limit: Weight,
-		storage_deposit_limit: Option<BalanceOf<Test>>,
-		code: Code<CodeHash<Test>>,
-		data: Vec<u8>,
-		salt: Vec<u8>,
-		debug: DebugInfo,
-		collect_events: CollectEvents,
-	) -> ContractInstantiateResult<AccountIdOf<Test>, BalanceOf<Test>, EventRecordOf<Test>>
-);
-
-builder!(
-	call(
-		origin: OriginFor<Test>,
-		dest: AccountIdLookupOf<Test>,
-		value: BalanceOf<Test>,
-		gas_limit: Weight,
-		storage_deposit_limit: Option<Compact<BalanceOf<Test>>>,
-		data: Vec<u8>,
-	) -> DispatchResultWithPostInfo
-);
-
-builder!(
-	bare_call(
-		origin: AccountIdOf<Test>,
-		dest: AccountIdOf<Test>,
-		value: BalanceOf<Test>,
-		gas_limit: Weight,
-		storage_deposit_limit: Option<BalanceOf<Test>>,
-		data: Vec<u8>,
-		debug: DebugInfo,
-		collect_events: CollectEvents,
-		determinism: Determinism,
-	) -> ContractExecResult<BalanceOf<Test>, EventRecordOf<Test>>
-);
-
-/// Create a [`BareInstantiateBuilder`] with default values.
-pub fn bare_instantiate(code: Code<CodeHash<Test>>) -> BareInstantiateBuilder {
-	BareInstantiateBuilder {
-		origin: ALICE,
-		value: 0,
-		gas_limit: GAS_LIMIT,
-		storage_deposit_limit: None,
-		code,
-		data: vec![],
-		salt: vec![],
-		debug: DebugInfo::Skip,
-		collect_events: CollectEvents::Skip,
-	}
-}
-
-impl BareInstantiateBuilder {
-	/// Build the instantiate call and unwrap the result.
-	pub fn build_and_unwrap_result(self) -> crate::InstantiateReturnValue<AccountIdOf<Test>> {
-		self.build().result.unwrap()
-	}
-
-	/// Build the instantiate call and unwrap the account id.
-	pub fn build_and_unwrap_account_id(self) -> AccountIdOf<Test> {
-		self.build().result.unwrap().account_id
-	}
-}
-
-/// Create a [`BareCallBuilder`] with default values.
-pub fn bare_call(dest: AccountId32) -> BareCallBuilder {
-	BareCallBuilder {
-		origin: ALICE,
-		dest,
-		value: 0,
-		gas_limit: GAS_LIMIT,
-		storage_deposit_limit: None,
-		data: vec![],
-		debug: DebugInfo::Skip,
-		collect_events: CollectEvents::Skip,
-		determinism: Determinism::Enforced,
-	}
-}
-
-impl BareCallBuilder {
-	/// Build the call and unwrap the result.
-	pub fn build_and_unwrap_result(self) -> ExecReturnValue {
-		self.build().result.unwrap()
-	}
-}
-
-/// Create an [`InstantiateWithCodeBuilder`] with default values.
-pub fn instantiate_with_code(code: Vec<u8>) -> InstantiateWithCodeBuilder {
-	InstantiateWithCodeBuilder {
-		origin: RuntimeOrigin::signed(ALICE),
-		value: 0,
-		gas_limit: GAS_LIMIT,
-		storage_deposit_limit: None,
-		code,
-		data: vec![],
-		salt: vec![],
-	}
-}
-
-/// Create an [`InstantiateBuilder`] with default values.
-pub fn instantiate(code_hash: CodeHash<Test>) -> InstantiateBuilder {
-	InstantiateBuilder {
-		origin: RuntimeOrigin::signed(ALICE),
-		value: 0,
-		gas_limit: GAS_LIMIT,
-		storage_deposit_limit: None,
-		code_hash,
-		data: vec![],
-		salt: vec![],
-	}
-}
-
-/// Create a [`CallBuilder`] with default values.
-pub fn call(dest: AccountIdLookupOf<Test>) -> CallBuilder {
-	CallBuilder {
-		origin: RuntimeOrigin::signed(ALICE),
-		dest,
-		value: 0,
-		gas_limit: GAS_LIMIT,
-		storage_deposit_limit: None,
-		data: vec![],
-	}
-}
-- 
GitLab