@@ -20,6 +20,36 @@ pub enum CheatSpan {
20
20
TargetCalls : usize ,
21
21
}
22
22
23
+ /// Enum used to specify the call data that should be matched when mocking a contract call.
24
+ #[derive(Copy , Drop , PartialEq , Clone , Debug )]
25
+ pub enum MockCallData {
26
+ /// Matches any call data.
27
+ Any ,
28
+ /// Matches the specified serialized call data.
29
+ Values : Span <felt252 >,
30
+ }
31
+
32
+ impl MockCallDataSerde of Serde <MockCallData > {
33
+ fn deserialize (ref serialized : Span <felt252 >) -> Option <MockCallData > {
34
+ let value : Option <Option <Span <felt252 >>> = Serde :: deserialize (ref serialized );
35
+
36
+ match value {
37
+ Option :: None => Option :: None ,
38
+ Option :: Some (call_data ) => match call_data {
39
+ Option :: None => Option :: Some (MockCallData :: Any ),
40
+ Option :: Some (data ) => Option :: Some (MockCallData :: Values (data )),
41
+ },
42
+ }
43
+ }
44
+
45
+ fn serialize (self : @ MockCallData , ref output : Array <felt252 >) {
46
+ match self {
47
+ MockCallData :: Any => Option :: <Span <felt252 >>:: None . serialize (ref output ),
48
+ MockCallData :: Values (data ) => Option :: Some (* data ). serialize (ref output ),
49
+ }
50
+ }
51
+ }
52
+
23
53
pub fn test_selector () -> felt252 {
24
54
// Result of selector!("TEST_CONTRACT_SELECTOR") since `selector!` macro requires dependency on
25
55
// `starknet`.
@@ -43,13 +73,17 @@ pub fn test_address() -> ContractAddress {
43
73
/// - `ret_data` - data to return by the function `function_selector`
44
74
/// - `n_times` - number of calls to mock the function for
45
75
pub fn mock_call <T , impl TSerde : core :: serde :: Serde <T >, impl TDestruct : Destruct <T >>(
46
- contract_address : ContractAddress , function_selector : felt252 , ret_data : T , n_times : u32
76
+ contract_address : ContractAddress ,
77
+ function_selector : felt252 ,
78
+ call_data : MockCallData ,
79
+ ret_data : T ,
80
+ n_times : u32
47
81
) {
48
82
assert! (n_times > 0 , " cannot mock_call 0 times, n_times argument must be greater than 0" );
49
83
50
84
let contract_address_felt : felt252 = contract_address . into ();
51
85
let mut inputs = array! [contract_address_felt , function_selector ];
52
-
86
+ call_data . serialize ( ref inputs );
53
87
CheatSpan :: TargetCalls (n_times ). serialize (ref inputs );
54
88
55
89
let mut ret_data_arr = ArrayTrait :: new ();
@@ -66,13 +100,17 @@ pub fn mock_call<T, impl TSerde: core::serde::Serde<T>, impl TDestruct: Destruct
66
100
/// - `contract_address` - targeted contracts' address
67
101
/// - `function_selector` - hashed name of the target function (can be obtained with `selector!`
68
102
/// macro)
103
+ /// - `call_data` - matching call data
69
104
/// - `ret_data` - data to be returned by the function
70
105
pub fn start_mock_call <T , impl TSerde : core :: serde :: Serde <T >, impl TDestruct : Destruct <T >>(
71
- contract_address : ContractAddress , function_selector : felt252 , ret_data : T
106
+ contract_address : ContractAddress ,
107
+ function_selector : felt252 ,
108
+ call_data : MockCallData ,
109
+ ret_data : T
72
110
) {
73
111
let contract_address_felt : felt252 = contract_address . into ();
74
112
let mut inputs = array! [contract_address_felt , function_selector ];
75
-
113
+ call_data . serialize ( ref inputs );
76
114
CheatSpan :: Indefinite . serialize (ref inputs );
77
115
78
116
let mut ret_data_arr = ArrayTrait :: new ();
@@ -87,12 +125,16 @@ pub fn start_mock_call<T, impl TSerde: core::serde::Serde<T>, impl TDestruct: De
87
125
/// address.
88
126
/// - `contract_address` - targeted contracts' address
89
127
/// - `function_selector` - hashed name of the target function (can be obtained with `selector!`
128
+ /// - `call_data` - matching call data
90
129
/// macro)
91
- pub fn stop_mock_call (contract_address : ContractAddress , function_selector : felt252 ) {
130
+ pub fn stop_mock_call (
131
+ contract_address : ContractAddress , function_selector : felt252 , call_data : MockCallData
132
+ ) {
92
133
let contract_address_felt : felt252 = contract_address . into ();
93
- execute_cheatcode_and_deserialize :: <
94
- ' stop_mock_call' , ()
95
- >(array! [contract_address_felt , function_selector ]. span ());
134
+ let mut inputs = array! [contract_address_felt , function_selector ];
135
+ call_data . serialize (ref inputs );
136
+
137
+ execute_cheatcode_and_deserialize :: <' stop_mock_call' , ()>(inputs . span ());
96
138
}
97
139
98
140
#[derive(Drop , Serde , PartialEq , Debug )]
0 commit comments