diff --git a/pallets/gear/rpc/src/lib.rs b/pallets/gear/rpc/src/lib.rs index 722b268f901..1a814db7c6b 100644 --- a/pallets/gear/rpc/src/lib.rs +++ b/pallets/gear/rpc/src/lib.rs @@ -187,6 +187,45 @@ where )) }) } + + fn calculate_gas_info( + &self, + at_hash: ::Hash, + source: H256, + kind: HandleKind, + payload: Vec, + value: u128, + allow_other_panics: bool, + min_limit: Option, + ) -> RpcResult { + let api_version = self.get_api_version(at_hash)?; + + self.run_with_api_copy(|api| { + if api_version < 2 { + #[allow(deprecated)] + api.calculate_gas_info_before_version_2( + at_hash, + source, + kind, + payload, + value, + allow_other_panics, + min_limit, + ) + } else { + api.calculate_gas_info( + at_hash, + source, + kind, + payload, + value, + allow_other_panics, + min_limit, + Some(self.allowance_multiplier), + ) + } + }) + } } /// Error type of this RPC api. @@ -228,38 +267,23 @@ where ) -> RpcResult { let at_hash = at.unwrap_or_else(|| self.client.info().best_hash); - let api_version = self.get_api_version(at_hash)?; - - let calculate_gas_info = |min_limit: Option| { - self.run_with_api_copy(|api| { - if api_version < 2 { - #[allow(deprecated)] - api.calculate_gas_info_before_version_2( - at_hash, - source, - HandleKind::InitByHash(CodeId::from_origin(code_id)), - payload.to_vec(), - value, - allow_other_panics, - min_limit, - ) - } else { - api.calculate_gas_info( - at_hash, - source, - HandleKind::InitByHash(CodeId::from_origin(code_id)), - payload.to_vec(), - value, - allow_other_panics, - min_limit, - Some(self.allowance_multiplier), - ) - } - }) - }; - - let GasInfo { min_limit, .. } = calculate_gas_info(None)?; - calculate_gas_info(Some(min_limit)) + let GasInfo { min_limit, .. } = self.calculate_gas_info( + at_hash, + source, + HandleKind::InitByHash(CodeId::from_origin(code_id)), + payload.to_vec(), + value, + allow_other_panics, + None)?; + + self.calculate_gas_info( + at_hash, + source, + HandleKind::InitByHash(CodeId::from_origin(code_id)), + payload.to_vec(), + value, + allow_other_panics, + Some(min_limit)) } fn get_init_upload_gas_spent( @@ -273,38 +297,23 @@ where ) -> RpcResult { let at_hash = at.unwrap_or_else(|| self.client.info().best_hash); - let api_version = self.get_api_version(at_hash)?; - - let calculate_gas_info = |min_limit: Option| { - self.run_with_api_copy(|api| { - if api_version < 2 { - #[allow(deprecated)] - api.calculate_gas_info_before_version_2( - at_hash, - source, - HandleKind::Init(code.to_vec()), - payload.to_vec(), - value, - allow_other_panics, - min_limit, - ) - } else { - api.calculate_gas_info( - at_hash, - source, - HandleKind::Init(code.to_vec()), - payload.to_vec(), - value, - allow_other_panics, - min_limit, - Some(self.allowance_multiplier), - ) - } - }) - }; - - let GasInfo { min_limit, .. } = calculate_gas_info(None)?; - calculate_gas_info(Some(min_limit)) + let GasInfo { min_limit, .. } = self.calculate_gas_info( + at_hash, + source, + HandleKind::Init(code.to_vec()), + payload.to_vec(), + value, + allow_other_panics, + None)?; + + self.calculate_gas_info( + at_hash, + source, + HandleKind::Init(code.to_vec()), + payload.to_vec(), + value, + allow_other_panics, + Some(min_limit)) } fn get_handle_gas_spent( @@ -318,38 +327,25 @@ where ) -> RpcResult { let at_hash = at.unwrap_or_else(|| self.client.info().best_hash); - let api_version = self.get_api_version(at_hash)?; - - let calculate_gas_info = |min_limit: Option| { - self.run_with_api_copy(|api| { - if api_version < 2 { - #[allow(deprecated)] - api.calculate_gas_info_before_version_2( - at_hash, - source, - HandleKind::Handle(ProgramId::from_origin(dest)), - payload.to_vec(), - value, - allow_other_panics, - min_limit, - ) - } else { - api.calculate_gas_info( - at_hash, - source, - HandleKind::Handle(ProgramId::from_origin(dest)), - payload.to_vec(), - value, - allow_other_panics, - min_limit, - Some(self.allowance_multiplier), - ) - } - }) - }; - - let GasInfo { min_limit, .. } = calculate_gas_info(None)?; - calculate_gas_info(Some(min_limit)) + let GasInfo { min_limit, .. } = self.calculate_gas_info( + at_hash, + source, + HandleKind::Handle(ProgramId::from_origin(dest)), + payload.to_vec(), + value, + allow_other_panics, + None, + )?; + + self.calculate_gas_info( + at_hash, + source, + HandleKind::Handle(ProgramId::from_origin(dest)), + payload.to_vec(), + value, + allow_other_panics, + Some(min_limit), + ) } fn get_reply_gas_spent( @@ -363,44 +359,29 @@ where ) -> RpcResult { let at_hash = at.unwrap_or_else(|| self.client.info().best_hash); - let api_version = self.get_api_version(at_hash)?; - - let calculate_gas_info = |min_limit: Option| { - self.run_with_api_copy(|api| { - if api_version < 2 { - #[allow(deprecated)] - api.calculate_gas_info_before_version_2( - at_hash, - source, - HandleKind::Reply( - MessageId::from_origin(message_id), - ReplyCode::Success(SuccessReplyReason::Manual), - ), - payload.to_vec(), - value, - allow_other_panics, - min_limit, - ) - } else { - api.calculate_gas_info( - at_hash, - source, - HandleKind::Reply( - MessageId::from_origin(message_id), - ReplyCode::Success(SuccessReplyReason::Manual), - ), - payload.to_vec(), - value, - allow_other_panics, - min_limit, - Some(self.allowance_multiplier), - ) - } - }) - }; - - let GasInfo { min_limit, .. } = calculate_gas_info(None)?; - calculate_gas_info(Some(min_limit)) + let GasInfo { min_limit, .. } = self.calculate_gas_info( + at_hash, + source, + HandleKind::Reply( + MessageId::from_origin(message_id), + ReplyCode::Success(SuccessReplyReason::Manual), + ), + payload.to_vec(), + value, + allow_other_panics, + None)?; + + self.calculate_gas_info( + at_hash, + source, + HandleKind::Reply( + MessageId::from_origin(message_id), + ReplyCode::Success(SuccessReplyReason::Manual), + ), + payload.to_vec(), + value, + allow_other_panics, + Some(min_limit)) } fn read_state(