multiversx_sc_scenario/facade/
scenario_world_register.rs

1use crate::{
2    api::DebugApi,
3    executor::debug::ContractContainer,
4    multiversx_sc::{
5        api,
6        contract_base::{CallableContractBuilder, ContractAbiProvider},
7    },
8    scenario_format::interpret_trait::InterpreterContext,
9    scenario_model::BytesValue,
10    ScenarioWorld,
11};
12use multiversx_chain_scenario_format::interpret_trait::InterpretableFrom;
13
14use multiversx_sc_meta_lib::contract::sc_config::ContractVariant;
15
16use super::expr::RegisterCodeSource;
17
18impl ScenarioWorld {
19    pub fn interpreter_context(&self) -> InterpreterContext {
20        InterpreterContext::default()
21            .with_dir(self.current_dir.clone())
22            .with_allowed_missing_files()
23    }
24
25    /// Convenient way of creating a code expression based on the current context
26    /// (i.e. with the paths resolved, as configured).
27    pub fn code_expression(&self, path: &str) -> BytesValue {
28        BytesValue::interpret_from(path, &self.interpreter_context())
29    }
30
31    pub fn register_contract_container(
32        &mut self,
33        expression: impl RegisterCodeSource,
34        contract_container: ContractContainer,
35    ) {
36        let contract_bytes = expression.into_code(self.new_env_data());
37        self.get_mut_debugger_backend()
38            .vm_runner
39            .contract_map_ref
40            .lock()
41            .register_contract(contract_bytes, contract_container);
42    }
43
44    /// Links a contract path in a test to a contract implementation.
45    pub fn register_contract<B: CallableContractBuilder>(
46        &mut self,
47        expression: impl RegisterCodeSource,
48        contract_builder: B,
49    ) {
50        self.register_contract_container(
51            expression,
52            ContractContainer::new(contract_builder.new_contract_obj::<DebugApi>(), None, false),
53        )
54    }
55
56    #[deprecated(
57        since = "0.37.0",
58        note = "Got renamed to `register_contract`, but not completely removed, in order to ease test migration. Please replace with `register_contract`."
59    )]
60    pub fn register_contract_builder<B: CallableContractBuilder>(
61        &mut self,
62        expression: &str,
63        contract_builder: B,
64    ) {
65        self.register_contract(expression, contract_builder)
66    }
67
68    /// Links a contract path in a test to a multi-contract output.
69    ///
70    /// This simulates the effects of building such a contract with only part of the endpoints.
71    pub fn register_partial_contract<Abi, B>(
72        &mut self,
73        expression: impl RegisterCodeSource,
74        contract_builder: B,
75        sub_contract_name: &str,
76    ) where
77        Abi: ContractAbiProvider,
78        B: CallableContractBuilder,
79    {
80        let multi_contract_config =
81            multiversx_sc_meta_lib::multi_contract_config::<Abi>(self.current_dir.as_path());
82        let contract_variant = multi_contract_config.find_contract(sub_contract_name);
83        self.register_contract_variant(expression, contract_builder, contract_variant);
84    }
85
86    /// Links a contract path in a test to a multi-contract output.
87    ///
88    /// This simulates the effects of building such a contract with only part of the endpoints.
89    pub fn register_contract_variant<B>(
90        &mut self,
91        expression: impl RegisterCodeSource,
92        contract_builder: B,
93        contract_variant: &ContractVariant,
94    ) where
95        B: CallableContractBuilder,
96    {
97        let contract_obj = if contract_variant.settings.external_view {
98            contract_builder.new_contract_obj::<api::ExternalViewApi<DebugApi>>()
99        } else {
100            contract_builder.new_contract_obj::<DebugApi>()
101        };
102
103        self.register_contract_container(
104            expression,
105            ContractContainer::new(
106                contract_obj,
107                Some(contract_variant.all_exported_function_names()),
108                contract_variant.settings.panic_message,
109            ),
110        );
111    }
112}