1use frame_support::{traits::fungible::Inspect, weights::Weight};
20use pallet_contracts::Code;
21use pallet_contracts_uapi::ReturnFlags;
22use parity_scale_codec::Decode;
23use sp_runtime::traits::Hash;
24
25type ContractBalanceOf<T> = <<T as pallet_contracts::Config>::Currency as Inspect<
26 <T as frame_system::Config>::AccountId,
27>>::Balance;
28
29pub fn load_wasm_module<T>(
34 fixture_name: &str,
35) -> std::io::Result<(Vec<u8>, <T::Hashing as Hash>::Output)>
36where
37 T: frame_system::Config,
38{
39 let fixture_path = ["../ink-contracts/", fixture_name, ".wasm"].concat();
40 let wasm_binary = std::fs::read(fixture_path)?;
41 let code_hash = T::Hashing::hash(&wasm_binary);
42 Ok((wasm_binary, code_hash))
43}
44
45pub fn deploy_wasm_contract<T: pallet_contracts::Config>(
48 contract_name: &str,
49 origin: T::AccountId,
50 value: ContractBalanceOf<T>,
51 gas_limit: Weight,
52 storage_deposit_limit: Option<ContractBalanceOf<T>>,
53 data: Vec<u8>,
54) -> (T::AccountId, <T::Hashing as Hash>::Output) {
55 let (code, hash) = load_wasm_module::<T>(contract_name).unwrap();
56 let outcome = pallet_contracts::Pallet::<T>::bare_instantiate(
57 origin,
58 value,
59 gas_limit,
60 storage_deposit_limit,
61 Code::Upload(code),
62 data,
63 vec![],
64 pallet_contracts::DebugInfo::Skip,
65 pallet_contracts::CollectEvents::Skip,
66 );
67
68 let result = outcome.result.unwrap();
70 assert!(
71 !result.result.did_revert(),
72 "deploy_contract: reverted - {:?}",
73 result
74 );
75 (result.account_id, hash)
76}
77
78pub fn call_wasm_contract_method<T: pallet_contracts::Config, V: Decode>(
81 origin: T::AccountId,
82 dest: T::AccountId,
83 value: ContractBalanceOf<T>,
84 gas_limit: Weight,
85 storage_deposit_limit: Option<ContractBalanceOf<T>>,
86 data: Vec<u8>,
87 debug: bool,
88) -> (V, ReturnFlags, Weight) {
89 let outcome = pallet_contracts::Pallet::<T>::bare_call(
90 origin,
91 dest,
92 value,
93 gas_limit,
94 storage_deposit_limit,
95 data,
96 pallet_contracts::DebugInfo::Skip,
97 pallet_contracts::CollectEvents::Skip,
98 pallet_contracts::Determinism::Enforced,
99 );
100
101 if debug {
102 println!(
103 "Contract debug buffer - {:?}",
104 String::from_utf8(outcome.debug_message.clone())
105 );
106 println!("Contract outcome - {outcome:?}");
107 }
108
109 let res = outcome.result.unwrap();
110 assert!(!res.did_revert(), "Contract reverted!");
112
113 let value = Result::<V, ()>::decode(&mut res.data.as_ref())
114 .expect("decoding failed")
115 .expect("ink! lang error");
116
117 (value, res.flags, outcome.gas_consumed)
118}