multiversx_sc_scenario/facade/world_tx/
scenario_tx_env.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use multiversx_chain_scenario_format::interpret_trait::InterpreterContext;
use multiversx_sc::types::{ManagedAddress, ManagedBuffer, TxEnv, TxEnvWithTxHash, H256};

use crate::{api::StaticApi, scenario_model::TxExpect, ScenarioWorld};

/// Designates a tx environment suitable for running scenarios locally.
pub trait ScenarioTxEnv: TxEnv {
    fn env_data(&self) -> &ScenarioTxEnvData;
}

/// The actual data required to run a scenario locally. This is the minimal environment needed to run txs.
#[derive(Default, Debug, Clone)]
pub struct ScenarioTxEnvData {
    pub interpreter_context: InterpreterContext,
    pub tx_hash: Option<H256>,
}

impl TxEnv for ScenarioTxEnvData {
    type Api = StaticApi;

    type RHExpect = TxExpect;

    fn resolve_sender_address(&self) -> ManagedAddress<Self::Api> {
        panic!("Explicit sender address expected")
    }

    fn default_gas_annotation(&self) -> multiversx_sc::types::ManagedBuffer<Self::Api> {
        ManagedBuffer::from("5,000,000")
    }

    fn default_gas_value(&self) -> u64 {
        5_000_000
    }
}

impl TxEnvWithTxHash for ScenarioTxEnvData {
    fn set_tx_hash(&mut self, tx_hash: H256) {
        assert!(self.tx_hash.is_none(), "tx hash set twice");
        self.tx_hash = Some(tx_hash);
    }

    fn take_tx_hash(&mut self) -> Option<H256> {
        core::mem::take(&mut self.tx_hash)
    }
}

impl ScenarioTxEnvData {
    pub fn interpreter_context(&self) -> InterpreterContext {
        self.interpreter_context.clone()
    }
}

impl ScenarioTxEnv for ScenarioTxEnvData {
    fn env_data(&self) -> &ScenarioTxEnvData {
        self
    }
}

impl ScenarioWorld {
    pub(crate) fn new_env_data(&self) -> ScenarioTxEnvData {
        ScenarioTxEnvData {
            interpreter_context: InterpreterContext::new()
                .with_dir(self.current_dir.clone())
                .with_allowed_missing_files(),
            tx_hash: None,
        }
    }
}

/// Provides a `run` method for transactions and steps.
pub trait ScenarioTxRun {
    type Returns;

    fn run(self) -> Self::Returns;
}