@@ -128,6 +128,8 @@ pub struct RpcBlockInfo {
128
128
pub block_timestamp : BlockTimestamp ,
129
129
/// The sequencer address of this block.
130
130
pub sequencer_address : ContractAddress ,
131
+ /// The transactions of this block.
132
+ pub transactions : Vec < Transaction > ,
131
133
}
132
134
133
135
#[ derive( Deserialize ) ]
@@ -299,6 +301,29 @@ impl<'de> Deserialize<'de> for TransactionTrace {
299
301
}
300
302
}
301
303
304
+ /// Freestanding deserialize method to avoid a new type.
305
+ fn deserialize_transaction_json ( transaction : serde_json:: Value ) -> serde_json:: Result < Transaction > {
306
+ let tx_type: String = serde_json:: from_value ( transaction[ "type" ] . clone ( ) ) ?;
307
+ let tx_version: String = serde_json:: from_value ( transaction[ "version" ] . clone ( ) ) ?;
308
+
309
+ match tx_type. as_str ( ) {
310
+ "INVOKE" => match tx_version. as_str ( ) {
311
+ "0x0" => Ok ( Transaction :: Invoke ( InvokeTransaction :: V0 (
312
+ serde_json:: from_value ( transaction) ?,
313
+ ) ) ) ,
314
+ "0x1" => Ok ( Transaction :: Invoke ( InvokeTransaction :: V1 (
315
+ serde_json:: from_value ( transaction) ?,
316
+ ) ) ) ,
317
+ x => Err ( serde:: de:: Error :: custom ( format ! (
318
+ "unimplemented invoke version: {x}"
319
+ ) ) ) ,
320
+ } ,
321
+ x => Err ( serde:: de:: Error :: custom ( format ! (
322
+ "unimplemented transaction type deserialization: {x}"
323
+ ) ) ) ,
324
+ }
325
+ }
326
+
302
327
impl RpcState {
303
328
/// Requests the transaction trace to the Feeder Gateway API.
304
329
/// It's useful for testing the transaction outputs like:
@@ -343,6 +368,25 @@ impl RpcState {
343
368
}
344
369
}
345
370
371
+ /// Gets the gas price of a given block.
372
+ pub fn get_gas_price ( & self , block_number : u64 ) -> serde_json:: Result < u128 > {
373
+ let chain_name = self . get_chain_name ( ) ;
374
+
375
+ let response = ureq:: get ( & format ! (
376
+ "https://{}.starknet.io/feeder_gateway/get_block" ,
377
+ chain_name
378
+ ) )
379
+ . query ( "blockNumber" , & block_number. to_string ( ) )
380
+ . call ( )
381
+ . unwrap ( ) ;
382
+
383
+ let res: serde_json:: Value = response. into_json ( ) . expect ( "should be json" ) ;
384
+
385
+ let gas_price_hex = res[ "gas_price" ] . as_str ( ) . unwrap ( ) ;
386
+ let gas_price = u128:: from_str_radix ( gas_price_hex. trim_start_matches ( "0x" ) , 16 ) . unwrap ( ) ;
387
+ Ok ( gas_price)
388
+ }
389
+
346
390
pub fn get_chain_name ( & self ) -> ChainId {
347
391
ChainId ( match self . chain {
348
392
RpcChain :: MainNet => "alpha-mainnet" . to_string ( ) ,
@@ -354,7 +398,7 @@ impl RpcState {
354
398
pub fn get_block_info ( & self ) -> RpcBlockInfo {
355
399
let get_block_info_params = ureq:: json!( {
356
400
"jsonrpc" : "2.0" ,
357
- "method" : "starknet_getBlockWithTxHashes " ,
401
+ "method" : "starknet_getBlockWithTxs " ,
358
402
"params" : [ self . block. to_value( ) . unwrap( ) ] ,
359
403
"id" : 1
360
404
} ) ;
@@ -363,6 +407,13 @@ impl RpcState {
363
407
let sequencer_address: StarkFelt =
364
408
serde_json:: from_value ( block_info[ "result" ] [ "sequencer_address" ] . clone ( ) ) . unwrap ( ) ;
365
409
410
+ let transactions: Vec < _ > = block_info[ "result" ] [ "transactions" ]
411
+ . as_array ( )
412
+ . unwrap ( )
413
+ . iter ( )
414
+ . filter_map ( |result| deserialize_transaction_json ( result. clone ( ) ) . ok ( ) )
415
+ . collect ( ) ;
416
+
366
417
RpcBlockInfo {
367
418
block_number : BlockNumber (
368
419
block_info[ "result" ] [ "block_number" ]
@@ -377,6 +428,7 @@ impl RpcState {
377
428
. unwrap ( ) ,
378
429
) ,
379
430
sequencer_address : ContractAddress ( sequencer_address. try_into ( ) . unwrap ( ) ) ,
431
+ transactions,
380
432
}
381
433
}
382
434
@@ -851,7 +903,6 @@ mod blockifier_transaction_tests {
851
903
tx_hash : & str ,
852
904
network : RpcChain ,
853
905
block_number : BlockNumber ,
854
- gas_price : u128 ,
855
906
) -> (
856
907
TransactionExecutionInfo ,
857
908
TransactionTrace ,
@@ -861,13 +912,15 @@ mod blockifier_transaction_tests {
861
912
862
913
// Instantiate the RPC StateReader and the CachedState
863
914
let rpc_reader = RpcStateReader ( RpcState :: new ( network, block_number. into ( ) ) ) ;
915
+ let gas_price = rpc_reader. 0 . get_gas_price ( block_number. 0 ) . unwrap ( ) ;
864
916
865
917
// Get values for block context before giving ownership of the reader
866
918
let chain_id = rpc_reader. 0 . get_chain_name ( ) ;
867
919
let RpcBlockInfo {
868
920
block_number,
869
921
block_timestamp,
870
922
sequencer_address,
923
+ ..
871
924
} = rpc_reader. 0 . get_block_info ( ) ;
872
925
873
926
// Get transaction before giving ownership of the reader
@@ -939,13 +992,20 @@ mod blockifier_transaction_tests {
939
992
use super :: * ;
940
993
941
994
#[ test]
942
- #[ ignore = "working on fixes" ]
995
+ fn test_get_gas_price ( ) {
996
+ let block = BlockValue :: Number ( BlockNumber ( 169928 ) ) ;
997
+ let rpc_state = RpcState :: new ( RpcChain :: MainNet , block) ;
998
+
999
+ let price = rpc_state. get_gas_price ( 169928 ) . unwrap ( ) ;
1000
+ assert_eq ! ( price, 22804578690 ) ;
1001
+ }
1002
+
1003
+ #[ test]
943
1004
fn test_recent_tx ( ) {
944
1005
let ( tx_info, trace, receipt) = execute_tx (
945
1006
"0x05d200ef175ba15d676a68b36f7a7b72c17c17604eda4c1efc2ed5e4973e2c91" ,
946
1007
RpcChain :: MainNet ,
947
1008
BlockNumber ( 169928 ) ,
948
- 17110275391107 ,
949
1009
) ;
950
1010
951
1011
let TransactionExecutionInfo {
0 commit comments