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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use crate::{
num_bigint::BigUint,
scenario::model::{ScQueryStep, Step, TxExpect, TypedScQuery, TypedScQueryExecutor},
tx_execution::execute_sc_query,
tx_mock::{generate_tx_hash_dummy, TxInput, TxResult},
world_mock::BlockchainMock,
DebugApi,
};
use multiversx_sc::{
codec::{CodecFrom, PanicErrorHandler, TopEncodeMulti},
types::ContractCall,
};
use super::check_tx_output;
impl BlockchainMock {
pub fn perform_sc_query(&mut self, sc_query_step: ScQueryStep) -> &mut Self {
let _ = self.with_borrowed(|state| execute_and_check(state, &sc_query_step));
self.scenario_trace.steps.push(Step::ScQuery(sc_query_step));
self
}
pub fn perform_sc_query_expect_result<OriginalResult, RequestedResult>(
&mut self,
typed_sc_query: TypedScQuery<OriginalResult>,
) -> RequestedResult
where
OriginalResult: TopEncodeMulti,
RequestedResult: CodecFrom<OriginalResult>,
{
let mut sc_query_step: ScQueryStep = typed_sc_query.into();
let tx_result = self.with_borrowed(|state| execute_and_check(state, &sc_query_step));
let mut tx_expect = TxExpect::ok();
for raw_result in &tx_result.result_values {
let result_hex_string = format!("0x{}", hex::encode(raw_result));
tx_expect = tx_expect.result(result_hex_string.as_str());
}
sc_query_step = sc_query_step.expect(tx_expect);
self.scenario_trace.steps.push(Step::ScQuery(sc_query_step));
let mut raw_results = tx_result.result_values;
RequestedResult::multi_decode_or_handle_err(&mut raw_results, PanicErrorHandler).unwrap()
}
}
impl TypedScQueryExecutor for BlockchainMock {
fn execute_typed_sc_query<OriginalResult, RequestedResult>(
&mut self,
typed_sc_call: TypedScQuery<OriginalResult>,
) -> RequestedResult
where
OriginalResult: TopEncodeMulti,
RequestedResult: CodecFrom<OriginalResult>,
{
self.perform_sc_query_expect_result(typed_sc_call)
}
}
impl BlockchainMock {
pub fn quick_query<CC, RequestedResult>(&mut self, contract_call: CC) -> RequestedResult
where
CC: ContractCall<DebugApi>,
RequestedResult: CodecFrom<CC::OriginalResult>,
{
let sc_query_step = ScQueryStep::new().call(contract_call);
let tx_result = self.with_borrowed(|state| execute(state, &sc_query_step));
let mut raw_result = tx_result.result_values;
RequestedResult::multi_decode_or_handle_err(&mut raw_result, PanicErrorHandler).unwrap()
}
}
pub(crate) fn execute(
state: BlockchainMock,
sc_query_step: &ScQueryStep,
) -> (TxResult, BlockchainMock) {
let tx_input = TxInput {
from: sc_query_step.tx.to.to_address(),
to: sc_query_step.tx.to.to_address(),
egld_value: BigUint::from(0u32),
esdt_values: Vec::new(),
func_name: sc_query_step.tx.function.clone().into(),
args: sc_query_step
.tx
.arguments
.iter()
.map(|scen_arg| scen_arg.value.clone())
.collect(),
gas_limit: u64::MAX,
gas_price: 0u64,
tx_hash: generate_tx_hash_dummy(&sc_query_step.id),
..Default::default()
};
let (tx_result, state) = execute_sc_query(tx_input, state);
assert!(
tx_result.pending_calls.no_calls(),
"Can't query a view function that performs an async call"
);
(tx_result, state)
}
fn execute_and_check(
state: BlockchainMock,
sc_query_step: &ScQueryStep,
) -> (TxResult, BlockchainMock) {
let (tx_result, state) = execute(state, sc_query_step);
if let Some(tx_expect) = &sc_query_step.expect {
check_tx_output(&sc_query_step.id, tx_expect, &tx_result);
}
(tx_result, state)
}