astar_test_utils/
lib.rs

1// This file is part of Astar.
2
3// Copyright (C) Stake Technologies Pte.Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later
5
6// Astar is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// Astar is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with Astar. If not, see <http://www.gnu.org/licenses/>.
18
19use 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
29/// Load a given wasm module from wasm binary contents along
30/// with it's hash.
31///
32/// The fixture files are located under the `../ink-contracts/` directory.
33pub 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
45// Load and deploy the contract from wasm binary
46/// and check for successful deploy
47pub 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    // make sure it does not revert
69    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
78/// Call the wasm contract method and returns the decoded return
79/// values along with return flags and consumed weight
80pub 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    // check for revert
111    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}