@@ -2,7 +2,7 @@ extern crate alloc;
22
33use alloc:: format;
44
5- use frame_support:: dispatch:: { GetDispatchInfo , Pays , PostDispatchInfo } ;
5+ use frame_support:: dispatch:: { DispatchInfo , GetDispatchInfo , Pays , PostDispatchInfo } ;
66use frame_system:: RawOrigin ;
77use pallet_admin_utils:: { PrecompileEnable , PrecompileEnum } ;
88use pallet_evm:: {
@@ -71,33 +71,16 @@ pub(crate) trait PrecompileHandleExt: PrecompileHandle {
7171
7272 match call. dispatch ( R :: RuntimeOrigin :: from ( origin) ) {
7373 Ok ( post_info) => {
74- if post_info. pays_fee ( & info) == Pays :: Yes {
75- let actual_weight = post_info. actual_weight . unwrap_or ( info. call_weight ) ;
76- let cost =
77- <R as pallet_evm:: Config >:: GasWeightMapping :: weight_to_gas ( actual_weight) ;
78- self . record_cost ( cost) ?;
79-
80- self . refund_external_cost (
81- Some (
82- info. call_weight
83- . ref_time ( )
84- . saturating_sub ( actual_weight. ref_time ( ) ) ,
85- ) ,
86- Some (
87- info. call_weight
88- . proof_size ( )
89- . saturating_sub ( actual_weight. proof_size ( ) ) ,
90- ) ,
91- ) ;
92- }
93-
9474 log:: debug!( "Dispatch succeeded. Post info: {post_info:?}" ) ;
75+ self . charge_and_refund_after_dispatch :: < R , Call > ( & info, & post_info) ?;
9576
9677 Ok ( ( ) )
9778 }
9879 Err ( e) => {
9980 log:: error!( "Dispatch failed. Error: {e:?}" ) ;
10081 log:: warn!( "Returning error PrecompileFailure::Error" ) ;
82+ self . charge_and_refund_after_dispatch :: < R , Call > ( & info, & e. post_info ) ?;
83+
10184 Err ( PrecompileFailure :: Error {
10285 exit_status : ExitError :: Other (
10386 format ! ( "dispatch execution failed: {}" , <& ' static str >:: from( e) ) . into ( ) ,
@@ -106,6 +89,36 @@ pub(crate) trait PrecompileHandleExt: PrecompileHandle {
10689 }
10790 }
10891 }
92+
93+ fn charge_and_refund_after_dispatch < R , Call > (
94+ & mut self ,
95+ info : & DispatchInfo ,
96+ post_info : & PostDispatchInfo ,
97+ ) -> EvmResult < ( ) >
98+ where
99+ R : frame_system:: Config + pallet_evm:: Config ,
100+ {
101+ if post_info. pays_fee ( info) == Pays :: Yes {
102+ let actual_weight = post_info. actual_weight . unwrap_or ( info. call_weight ) ;
103+ let cost = <R as pallet_evm:: Config >:: GasWeightMapping :: weight_to_gas ( actual_weight) ;
104+ self . record_cost ( cost) ?;
105+
106+ self . refund_external_cost (
107+ Some (
108+ info. call_weight
109+ . ref_time ( )
110+ . saturating_sub ( actual_weight. ref_time ( ) ) ,
111+ ) ,
112+ Some (
113+ info. call_weight
114+ . proof_size ( )
115+ . saturating_sub ( actual_weight. proof_size ( ) ) ,
116+ ) ,
117+ ) ;
118+ }
119+
120+ Ok ( ( ) )
121+ }
109122}
110123
111124impl < T > PrecompileHandleExt for T where T : PrecompileHandle { }
0 commit comments