11
11
12
12
use cairo_vm:: felt:: Felt252 ;
13
13
use starknet_in_rust:: {
14
- services:: api:: contract_classes:: deprecated_contract_class:: ContractClass ,
15
- testing:: state:: StarknetState ,
14
+ definitions:: { block_context:: BlockContext , constants:: TRANSACTION_VERSION } ,
15
+ services:: api:: contract_classes:: {
16
+ compiled_class:: CompiledClass , deprecated_contract_class:: ContractClass ,
17
+ } ,
18
+ state:: {
19
+ cached_state:: CachedState , in_memory_state_reader:: InMemoryStateReader , state_api:: State ,
20
+ } ,
21
+ transaction:: { Declare , Deploy , InvokeFunction , Transaction } ,
16
22
utils:: { calculate_sn_keccak, Address } ,
17
23
} ;
18
- use std:: path:: Path ;
24
+ use std:: { collections :: HashMap , path:: Path , sync :: Arc } ;
19
25
20
26
fn main ( ) {
21
27
// replace this with the path to your compiled contract
22
- let contract_path = "starknet_programs/factorial .json" ;
28
+ let contract_path = "starknet_programs/fibonacci .json" ;
23
29
24
30
// replace this with the name of your entrypoint
25
- let entry_point: & str = "factorial " ;
31
+ let entry_point: & str = "fib " ;
26
32
27
33
// replace this with the arguments for the entrypoint
28
34
let calldata: Vec < Felt252 > = [ 1 . into ( ) , 1 . into ( ) , 10 . into ( ) ] . to_vec ( ) ;
@@ -42,12 +48,21 @@ fn main() {
42
48
fn test_contract (
43
49
contract_path : impl AsRef < Path > ,
44
50
entry_point : & str ,
45
- calldata : Vec < Felt252 > ,
51
+ call_data : Vec < Felt252 > ,
46
52
) -> Vec < Felt252 > {
53
+ //* --------------------------------------------
54
+ //* Initialize needed variables
55
+ //* --------------------------------------------
56
+ let block_context = BlockContext :: default ( ) ;
57
+ let chain_id = block_context. starknet_os_config ( ) . chain_id ( ) . clone ( ) ;
58
+ let sender_address = Address ( 1 . into ( ) ) ;
59
+ let signature = vec ! [ ] ;
60
+
47
61
//* --------------------------------------------
48
62
//* Initialize state
49
63
//* --------------------------------------------
50
- let mut state = StarknetState :: new ( None ) ;
64
+ let state_reader = Arc :: new ( InMemoryStateReader :: default ( ) ) ;
65
+ let mut state = CachedState :: new ( state_reader, HashMap :: new ( ) ) ;
51
66
52
67
//* --------------------------------------------
53
68
//* Read contract from file
@@ -58,37 +73,74 @@ fn test_contract(
58
73
//* --------------------------------------------
59
74
//* Declare new contract class
60
75
//* --------------------------------------------
61
- state
62
- . declare ( contract_class. clone ( ) )
63
- . expect ( "Could not declare the contract class" ) ;
76
+ let declare_tx = Declare :: new (
77
+ contract_class. clone ( ) ,
78
+ chain_id. clone ( ) ,
79
+ sender_address,
80
+ 0 , // max fee
81
+ 0 . into ( ) ,
82
+ signature. clone ( ) ,
83
+ 0 . into ( ) , // nonce
84
+ )
85
+ . expect ( "couldn't create declare transaction" ) ;
86
+
87
+ declare_tx
88
+ . execute ( & mut state, & block_context)
89
+ . expect ( "could not declare the contract class" ) ;
64
90
65
91
//* --------------------------------------------
66
92
//* Deploy new contract class instance
67
93
//* --------------------------------------------
68
- let ( contract_address, _) = state
69
- . deploy ( contract_class, vec ! [ ] , Default :: default ( ) , None , 0 )
70
- . expect ( "Could not deploy contract" ) ;
94
+
95
+ let deploy = Deploy :: new (
96
+ Default :: default ( ) , // salt
97
+ contract_class. clone ( ) ,
98
+ vec ! [ ] , // call data
99
+ block_context. starknet_os_config ( ) . chain_id ( ) . clone ( ) ,
100
+ TRANSACTION_VERSION . clone ( ) ,
101
+ )
102
+ . unwrap ( ) ;
103
+
104
+ state
105
+ . set_contract_class (
106
+ & deploy. contract_hash ,
107
+ & CompiledClass :: Deprecated ( Arc :: new ( contract_class) ) ,
108
+ )
109
+ . unwrap ( ) ;
110
+ let contract_address = deploy. contract_address . clone ( ) ;
111
+
112
+ let tx = Transaction :: Deploy ( deploy) ;
113
+
114
+ tx. execute ( & mut state, & block_context, 0 )
115
+ . expect ( "could not deploy contract" ) ;
71
116
72
117
//* --------------------------------------------
73
118
//* Execute contract entrypoint
74
119
//* --------------------------------------------
75
120
let entry_point_selector = Felt252 :: from_bytes_be ( & calculate_sn_keccak ( entry_point. as_bytes ( ) ) ) ;
76
121
77
- let caller_address = Address :: default ( ) ;
78
-
79
- let callinfo = state
80
- . execute_entry_point_raw (
81
- contract_address,
82
- entry_point_selector,
83
- calldata,
84
- caller_address,
85
- )
86
- . expect ( "Could not execute entry point" ) ;
122
+ let invoke_tx = InvokeFunction :: new (
123
+ contract_address,
124
+ entry_point_selector,
125
+ 0 ,
126
+ TRANSACTION_VERSION . clone ( ) ,
127
+ call_data,
128
+ signature,
129
+ chain_id,
130
+ Some ( 0 . into ( ) ) ,
131
+ )
132
+ . unwrap ( ) ;
133
+
134
+ let tx = Transaction :: InvokeFunction ( invoke_tx) ;
135
+ let tx_exec_info = tx. execute ( & mut state, & block_context, 0 ) . unwrap ( ) ;
87
136
88
137
//* --------------------------------------------
89
138
//* Extract return values
90
139
//* --------------------------------------------
91
- callinfo. retdata
140
+ tx_exec_info
141
+ . call_info
142
+ . expect ( "call info should exist" )
143
+ . retdata
92
144
}
93
145
94
146
#[ test]
0 commit comments