@@ -110,7 +110,7 @@ crate::jsonrpc!(pub struct CkbRpcClient {
110110 pub fn calculate_dao_maximum_withdraw( & self , out_point: OutPoint , kind: DaoWithdrawingCalculationKind ) -> Capacity ;
111111} ) ;
112112
113- crate :: jsonrpc_async!( pub struct CkbRpcAyncClient {
113+ crate :: jsonrpc_async!( pub struct CkbRpcAsyncClient {
114114 // Chain
115115 pub fn get_block( & self , hash: H256 ) -> Option <BlockView >;
116116 pub fn get_block_by_number( & self , number: BlockNumber ) -> Option <BlockView >;
@@ -212,6 +212,15 @@ fn transform_cycles(cycles: Option<Vec<ckb_jsonrpc_types::Cycle>>) -> Vec<Cycle>
212212 . unwrap_or_default ( )
213213}
214214
215+ impl From < & CkbRpcClient > for CkbRpcAsyncClient {
216+ fn from ( value : & CkbRpcClient ) -> Self {
217+ Self {
218+ client : value. client . clone ( ) ,
219+ id : 0 . into ( ) ,
220+ }
221+ }
222+ }
223+
215224impl CkbRpcClient {
216225 pub fn get_packed_block ( & self , hash : H256 ) -> Result < Option < JsonBytes > , crate :: RpcError > {
217226 self . post ( "get_block" , ( hash, Some ( Uint32 :: from ( 0u32 ) ) ) )
@@ -386,3 +395,197 @@ impl CkbRpcClient {
386395 self . post :: < _ , Option < JsonBytes > > ( "get_fork_block" , ( block_hash, Some ( Uint32 :: from ( 0u32 ) ) ) )
387396 }
388397}
398+
399+ impl CkbRpcAsyncClient {
400+ pub async fn get_packed_block ( & self , hash : H256 ) -> Result < Option < JsonBytes > , crate :: RpcError > {
401+ self . post ( "get_block" , ( hash, Some ( Uint32 :: from ( 0u32 ) ) ) )
402+ . await
403+ }
404+
405+ // turn block response into BlockView and cycle vec
406+ fn transform_block_view_with_cycle (
407+ opt_resp : Option < BlockResponse > ,
408+ ) -> Result < Option < ( BlockView , Vec < Cycle > ) > , crate :: rpc:: RpcError > {
409+ opt_resp
410+ . map ( |resp| match resp {
411+ BlockResponse :: Regular ( block_view) => Ok ( ( block_view. get_value ( ) ?, vec ! [ ] ) ) ,
412+ BlockResponse :: WithCycles ( block_cycles) => {
413+ let cycles = transform_cycles ( block_cycles. cycles ) ;
414+ Ok ( ( block_cycles. block . get_value ( ) ?, cycles) )
415+ }
416+ } )
417+ . transpose ( )
418+ }
419+ /// Same as get_block except with parameter with_cycles and return BlockResponse
420+ pub async fn get_block_with_cycles (
421+ & self ,
422+ hash : H256 ,
423+ ) -> Result < Option < ( BlockView , Vec < Cycle > ) > , crate :: rpc:: RpcError > {
424+ let res = self
425+ . post :: < _ , Option < BlockResponse > > ( "get_block" , ( hash, None :: < u32 > , true ) )
426+ . await ?;
427+ Self :: transform_block_view_with_cycle ( res)
428+ }
429+
430+ // turn BlockResponse to JsonBytes and Cycle tuple
431+ fn blockresponse2bytes (
432+ opt_resp : Option < BlockResponse > ,
433+ ) -> Result < Option < ( JsonBytes , Vec < Cycle > ) > , crate :: rpc:: RpcError > {
434+ opt_resp
435+ . map ( |resp| match resp {
436+ BlockResponse :: Regular ( block_view) => Ok ( ( block_view. get_json_bytes ( ) ?, vec ! [ ] ) ) ,
437+ BlockResponse :: WithCycles ( block_cycles) => {
438+ let cycles = transform_cycles ( block_cycles. cycles ) ;
439+ Ok ( ( block_cycles. block . get_json_bytes ( ) ?, cycles) )
440+ }
441+ } )
442+ . transpose ( )
443+ }
444+
445+ pub async fn get_packed_block_with_cycles (
446+ & self ,
447+ hash : H256 ,
448+ ) -> Result < Option < ( JsonBytes , Vec < Cycle > ) > , crate :: rpc:: RpcError > {
449+ let res = self
450+ . post :: < _ , Option < BlockResponse > > ( "get_block" , ( hash, Some ( Uint32 :: from ( 0u32 ) ) , true ) )
451+ . await ?;
452+ Self :: blockresponse2bytes ( res)
453+ }
454+
455+ /// Same as get_block_by_number except with parameter with_cycles and return BlockResponse
456+ pub async fn get_packed_block_by_number (
457+ & self ,
458+ number : BlockNumber ,
459+ ) -> Result < Option < JsonBytes > , crate :: rpc:: RpcError > {
460+ self . post ( "get_block_by_number" , ( number, Some ( Uint32 :: from ( 0u32 ) ) ) )
461+ . await
462+ }
463+
464+ pub async fn get_block_by_number_with_cycles (
465+ & self ,
466+ number : BlockNumber ,
467+ ) -> Result < Option < ( BlockView , Vec < Cycle > ) > , crate :: rpc:: RpcError > {
468+ let res = self
469+ . post :: < _ , Option < BlockResponse > > ( "get_block_by_number" , ( number, None :: < u32 > , true ) )
470+ . await ?;
471+ Self :: transform_block_view_with_cycle ( res)
472+ }
473+
474+ pub async fn get_packed_block_by_number_with_cycles (
475+ & self ,
476+ number : BlockNumber ,
477+ ) -> Result < Option < ( JsonBytes , Vec < Cycle > ) > , crate :: rpc:: RpcError > {
478+ let res = self
479+ . post :: < _ , Option < BlockResponse > > (
480+ "get_block_by_number" ,
481+ ( number, Some ( Uint32 :: from ( 0u32 ) ) , true ) ,
482+ )
483+ . await ?;
484+ Self :: blockresponse2bytes ( res)
485+ }
486+
487+ pub async fn get_packed_header (
488+ & self ,
489+ hash : H256 ,
490+ ) -> Result < Option < JsonBytes > , crate :: rpc:: RpcError > {
491+ self . post :: < _ , Option < JsonBytes > > ( "get_header" , ( hash, Some ( Uint32 :: from ( 0u32 ) ) ) )
492+ . await
493+ }
494+
495+ pub async fn get_packed_header_by_number (
496+ & self ,
497+ number : BlockNumber ,
498+ ) -> Result < Option < JsonBytes > , crate :: rpc:: RpcError > {
499+ self . post :: < _ , Option < JsonBytes > > (
500+ "get_header_by_number" ,
501+ ( number, Some ( Uint32 :: from ( 0u32 ) ) ) ,
502+ )
503+ . await
504+ }
505+
506+ pub async fn get_live_cell_with_include_tx_pool (
507+ & self ,
508+ out_point : OutPoint ,
509+ with_data : bool ,
510+ include_tx_pool : bool ,
511+ ) -> Result < CellWithStatus , crate :: rpc:: RpcError > {
512+ self . post :: < _ , CellWithStatus > (
513+ "get_live_cell" ,
514+ ( out_point, with_data, Some ( include_tx_pool) ) ,
515+ )
516+ . await
517+ }
518+
519+ // get transaction with only_committed=true
520+ pub async fn get_only_committed_transaction (
521+ & self ,
522+ hash : H256 ,
523+ ) -> Result < TransactionWithStatusResponse , crate :: rpc:: RpcError > {
524+ self . post :: < _ , TransactionWithStatusResponse > (
525+ "get_transaction" ,
526+ ( hash, Some ( Uint32 :: from ( 2u32 ) ) , true ) ,
527+ )
528+ . await
529+ }
530+
531+ // get transaction with verbosity=0
532+ pub async fn get_packed_transaction (
533+ & self ,
534+ hash : H256 ,
535+ ) -> Result < TransactionWithStatusResponse , crate :: rpc:: RpcError > {
536+ self . post :: < _ , TransactionWithStatusResponse > (
537+ "get_transaction" ,
538+ ( hash, Some ( Uint32 :: from ( 0u32 ) ) ) ,
539+ )
540+ . await
541+ }
542+
543+ // get transaction with verbosity=0 and only_committed=true
544+ pub async fn get_only_committed_packed_transaction (
545+ & self ,
546+ hash : H256 ,
547+ ) -> Result < TransactionWithStatusResponse , crate :: rpc:: RpcError > {
548+ self . post :: < _ , TransactionWithStatusResponse > (
549+ "get_transaction" ,
550+ ( hash, Some ( Uint32 :: from ( 0u32 ) ) , true ) ,
551+ )
552+ . await
553+ }
554+
555+ // get transaction with verbosity=1, so the result transaction field is None
556+ pub async fn get_transaction_status (
557+ & self ,
558+ hash : H256 ,
559+ ) -> Result < TransactionWithStatusResponse , crate :: rpc:: RpcError > {
560+ self . post :: < _ , TransactionWithStatusResponse > (
561+ "get_transaction" ,
562+ ( hash, Some ( Uint32 :: from ( 1u32 ) ) ) ,
563+ )
564+ . await
565+ }
566+
567+ // get transaction with verbosity=1 and only_committed=true, so the result transaction field is None
568+ pub async fn get_only_committed_transaction_status (
569+ & self ,
570+ hash : H256 ,
571+ ) -> Result < TransactionWithStatusResponse , crate :: rpc:: RpcError > {
572+ self . post :: < _ , TransactionWithStatusResponse > (
573+ "get_transaction" ,
574+ ( hash, Some ( Uint32 :: from ( 1u32 ) ) , true ) ,
575+ )
576+ . await
577+ }
578+
579+ pub async fn get_packed_tip_header ( & self ) -> Result < JsonBytes , crate :: rpc:: RpcError > {
580+ self . post :: < _ , JsonBytes > ( "get_tip_header" , ( Some ( Uint32 :: from ( 0u32 ) ) , ) )
581+ . await
582+ }
583+
584+ pub async fn get_packed_fork_block (
585+ & self ,
586+ block_hash : H256 ,
587+ ) -> Result < Option < JsonBytes > , crate :: rpc:: RpcError > {
588+ self . post :: < _ , Option < JsonBytes > > ( "get_fork_block" , ( block_hash, Some ( Uint32 :: from ( 0u32 ) ) ) )
589+ . await
590+ }
591+ }
0 commit comments