diff --git a/pr-3226/pallet_gear_rpc/enum.Error.html b/pr-3226/pallet_gear_rpc/enum.Error.html index b783d2d634c..81c53501efa 100644 --- a/pr-3226/pallet_gear_rpc/enum.Error.html +++ b/pr-3226/pallet_gear_rpc/enum.Error.html @@ -1,10 +1,10 @@ -Error in pallet_gear_rpc - Rust
pub enum Error {
+Error in pallet_gear_rpc - Rust
pub enum Error {
     DecodeError,
     RuntimeError,
 }
Expand description

Error type of this RPC api.

Variants§

§

DecodeError

The transaction was not decodable.

§

RuntimeError

The call to runtime failed.

-

Trait Implementations§

source§

impl From<Error> for i64

source§

fn from(e: Error) -> i64

Converts to this type from the input type.

Auto Trait Implementations§

§

impl RefUnwindSafe for Error

§

impl Send for Error

§

impl Sync for Error

§

impl Unpin for Error

§

impl UnwindSafe for Error

Blanket Implementations§

source§

impl<T> Any for Twhere +

Trait Implementations§

source§

impl From<Error> for i64

source§

fn from(e: Error) -> i64

Converts to this type from the input type.

Auto Trait Implementations§

§

impl RefUnwindSafe for Error

§

impl Send for Error

§

impl Sync for Error

§

impl Unpin for Error

§

impl UnwindSafe for Error

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CheckedConversion for T

§

fn checked_from<T>(t: T) -> Option<Self>where diff --git a/pr-3226/pallet_gear_rpc/index.html b/pr-3226/pallet_gear_rpc/index.html index 490ec3c2858..7d61ba8b180 100644 --- a/pr-3226/pallet_gear_rpc/index.html +++ b/pr-3226/pallet_gear_rpc/index.html @@ -1,2 +1,2 @@ -pallet_gear_rpc - Rust

Crate pallet_gear_rpc

source ·
Expand description

RPC interface for the gear module.

+pallet_gear_rpc - Rust

Crate pallet_gear_rpc

source ·
Expand description

RPC interface for the gear module.

Structs

Enums

  • Error type of this RPC api.

Traits

\ No newline at end of file diff --git a/pr-3226/pallet_gear_rpc/struct.Gear.html b/pr-3226/pallet_gear_rpc/struct.Gear.html index 24406337c2c..24be7b1ef82 100644 --- a/pr-3226/pallet_gear_rpc/struct.Gear.html +++ b/pr-3226/pallet_gear_rpc/struct.Gear.html @@ -1,9 +1,9 @@ Gear in pallet_gear_rpc - Rust

Struct pallet_gear_rpc::Gear

source ·
pub struct Gear<C, P> { /* private fields */ }
Expand description

A struct that implements the GearApi.

Implementations§

source§

impl<C, P> Gear<C, P>

source

pub fn new(client: Arc<C>, allowance_multiplier: u64) -> Self

Creates a new instance of the Gear Rpc helper.

-

Trait Implementations§

source§

impl<C, Block> GearApiServer<<Block as Block>::Hash, Result<u64, Vec<u8, Global>>> for Gear<C, Block>where +

Trait Implementations§

source§

impl<C, Block> GearApiServer<<Block as Block>::Hash, Result<u64, Vec<u8, Global>>> for Gear<C, Block>where Block: BlockT, C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>, - C::Api: GearRuntimeApi<Block>,

source§

fn get_init_create_gas_spent( + C::Api: GearRuntimeApi<Block>,

source§

fn get_init_create_gas_spent( &self, source: H256, code_id: H256, @@ -11,7 +11,7 @@ value: u128, allow_other_panics: bool, at: Option<<Block as BlockT>::Hash> -) -> RpcResult<GasInfo>

source§

fn get_init_upload_gas_spent( +) -> RpcResult<GasInfo>

source§

fn get_init_upload_gas_spent( &self, source: H256, code: Bytes, @@ -19,7 +19,7 @@ value: u128, allow_other_panics: bool, at: Option<<Block as BlockT>::Hash> -) -> RpcResult<GasInfo>

source§

fn get_handle_gas_spent( +) -> RpcResult<GasInfo>

source§

fn get_handle_gas_spent( &self, source: H256, dest: H256, @@ -27,7 +27,7 @@ value: u128, allow_other_panics: bool, at: Option<<Block as BlockT>::Hash> -) -> RpcResult<GasInfo>

source§

fn get_reply_gas_spent( +) -> RpcResult<GasInfo>

source§

fn get_reply_gas_spent( &self, source: H256, message_id: H256, @@ -35,16 +35,16 @@ value: u128, allow_other_panics: bool, at: Option<<Block as BlockT>::Hash> -) -> RpcResult<GasInfo>

source§

fn read_state( +) -> RpcResult<GasInfo>

source§

fn read_state( &self, program_id: H256, payload: Bytes, at: Option<<Block as BlockT>::Hash> -) -> RpcResult<Bytes>

source§

fn read_state_batch( +) -> RpcResult<Bytes>

source§

fn read_state_batch( &self, batch_id_payload: Vec<(H256, Bytes)>, at: Option<<Block as BlockT>::Hash> -) -> RpcResult<Vec<Bytes>>

source§

fn read_state_using_wasm( +) -> RpcResult<Vec<Bytes>>

source§

fn read_state_using_wasm( &self, program_id: H256, payload: Bytes, @@ -52,14 +52,14 @@ wasm: Bytes, argument: Option<Bytes>, at: Option<<Block as BlockT>::Hash> -) -> RpcResult<Bytes>

source§

fn read_state_using_wasm_batch( +) -> RpcResult<Bytes>

source§

fn read_state_using_wasm_batch( &self, batch_id_payload: Vec<(H256, Bytes)>, fn_name: Bytes, wasm: Bytes, argument: Option<Bytes>, at: Option<<Block as BlockT>::Hash> -) -> RpcResult<Vec<Bytes>>

source§

fn read_metahash( +) -> RpcResult<Vec<Bytes>>

source§

fn read_metahash( &self, program_id: H256, at: Option<<Block as BlockT>::Hash> diff --git a/pr-3226/pallet_gear_rpc/trait.GearApiServer.html b/pr-3226/pallet_gear_rpc/trait.GearApiServer.html index 50b16d02e51..5a3ee3aa669 100644 --- a/pr-3226/pallet_gear_rpc/trait.GearApiServer.html +++ b/pr-3226/pallet_gear_rpc/trait.GearApiServer.html @@ -138,7 +138,7 @@ ) -> RpcResult<H256>

Provided Methods§

source

fn into_rpc(self) -> RpcModule<Self>where BlockHash: Send + Sync + 'static + DeserializeOwned, ResponseType: Send + Sync + 'static,

Collects all the methods and subscriptions defined in the trait and adds them into a single RpcModule.

-

Implementors§

source§

impl<C, Block> GearApiServer<<Block as Block>::Hash, Result<u64, Vec<u8, Global>>> for Gear<C, Block>where +

Implementors§

source§

impl<C, Block> GearApiServer<<Block as Block>::Hash, Result<u64, Vec<u8, Global>>> for Gear<C, Block>where Block: BlockT, C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>, C::Api: GearRuntimeApi<Block>,

\ No newline at end of file diff --git a/pr-3226/src/pallet_gear_rpc/lib.rs.html b/pr-3226/src/pallet_gear_rpc/lib.rs.html index 7832e2dd4cc..3b95c253927 100644 --- a/pr-3226/src/pallet_gear_rpc/lib.rs.html +++ b/pr-3226/src/pallet_gear_rpc/lib.rs.html @@ -581,195 +581,6 @@ 581 582 583 -584 -585 -586 -587 -588 -589 -590 -591 -592 -593 -594 -595 -596 -597 -598 -599 -600 -601 -602 -603 -604 -605 -606 -607 -608 -609 -610 -611 -612 -613 -614 -615 -616 -617 -618 -619 -620 -621 -622 -623 -624 -625 -626 -627 -628 -629 -630 -631 -632 -633 -634 -635 -636 -637 -638 -639 -640 -641 -642 -643 -644 -645 -646 -647 -648 -649 -650 -651 -652 -653 -654 -655 -656 -657 -658 -659 -660 -661 -662 -663 -664 -665 -666 -667 -668 -669 -670 -671 -672 -673 -674 -675 -676 -677 -678 -679 -680 -681 -682 -683 -684 -685 -686 -687 -688 -689 -690 -691 -692 -693 -694 -695 -696 -697 -698 -699 -700 -701 -702 -703 -704 -705 -706 -707 -708 -709 -710 -711 -712 -713 -714 -715 -716 -717 -718 -719 -720 -721 -722 -723 -724 -725 -726 -727 -728 -729 -730 -731 -732 -733 -734 -735 -736 -737 -738 -739 -740 -741 -742 -743 -744 -745 -746 -747 -748 -749 -750 -751 -752 -753 -754 -755 -756 -757 -758 -759 -760 -761 -762 -763 -764 -765 -766 -767 -768 -769 -770 -771 -772
// This file is part of Gear.
 
 // Copyright (C) 2021-2023 Gear Technologies Inc.
@@ -945,6 +756,59 @@
 
         runtime_api_result.map_err(|e| runtime_error_into_rpc_error(String::from_utf8_lossy(&e)))
     }
+
+    fn get_api_version(&self, at_hash: <Block as BlockT>::Hash) -> Result<u32, CallError> {
+        self.client
+            .runtime_api()
+            .api_version::<dyn GearRuntimeApi<Block>>(at_hash)
+            .map_err(|e| map_err(e, "Failed to get gear runtime api version"))?
+            .ok_or_else(|| {
+                CallError::Custom(ErrorObject::owned(
+                    8000,
+                    "Gear runtime api wasn't found in the runtime",
+                    None::<String>,
+                ))
+            })
+    }
+
+    fn calculate_gas_info(
+        &self,
+        at_hash: <Block as BlockT>::Hash,
+        source: H256,
+        kind: HandleKind,
+        payload: Vec<u8>,
+        value: u128,
+        allow_other_panics: bool,
+        min_limit: Option<u64>,
+    ) -> RpcResult<GasInfo> {
+        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.
@@ -955,17 +819,17 @@
     RuntimeError,
 }
 
-impl From<Error> for i64 {
-    fn from(e: Error) -> i64 {
-        match e {
-            Error::RuntimeError => 1,
-            Error::DecodeError => 2,
+impl From<Error> for i64 {
+    fn from(e: Error) -> i64 {
+        match e {
+            Error::RuntimeError => 1,
+            Error::DecodeError => 2,
         }
     }
 }
 
 fn map_err(error: impl ToString, desc: &'static str) -> CallError {
-    CallError::Custom(ErrorObject::owned(8000, desc, Some(error.to_string())))
+    CallError::Custom(ErrorObject::owned(8000, desc, Some(error.to_string())))
 }
 
 #[async_trait]
@@ -984,72 +848,25 @@
         allow_other_panics: bool,
         at: Option<<Block as BlockT>::Hash>,
     ) -> RpcResult<GasInfo> {
-        let at_hash = at.unwrap_or_else(|| self.client.info().best_hash);
+        let at_hash = at.unwrap_or_else(|| self.client.info().best_hash);
 
-        let api_version = self
-            .client
-            .runtime_api()
-            .api_version::<dyn GearRuntimeApi<Block>>(at_hash)
-            .map_err(|e| map_err(e, "Failed to get gear runtime api version"))?
-            .ok_or_else(|| {
-                CallError::Custom(ErrorObject::owned(
-                    8000,
-                    "Gear runtime api wasn't found in the runtime",
-                    None::<String>,
-                ))
-            })?;
+        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)?;
 
-        if api_version < 2 {
-            let GasInfo { min_limit, .. } = self.run_with_api_copy(|api| {
-                #[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,
-                    None,
-                )
-            })?;
-            self.run_with_api_copy(|api| {
-                #[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,
-                    Some(min_limit),
-                )
-            })
-        } else {
-            let GasInfo { min_limit, .. } = self.run_with_api_copy(|api| {
-                api.calculate_gas_info(
-                    at_hash,
-                    source,
-                    HandleKind::InitByHash(CodeId::from_origin(code_id)),
-                    payload.to_vec(),
-                    value,
-                    allow_other_panics,
-                    None,
-                    Some(self.allowance_multiplier),
-                )
-            })?;
-            self.run_with_api_copy(|api| {
-                api.calculate_gas_info(
-                    at_hash,
-                    source,
-                    HandleKind::InitByHash(CodeId::from_origin(code_id)),
-                    payload.to_vec(),
-                    value,
-                    allow_other_panics,
-                    Some(min_limit),
-                    Some(self.allowance_multiplier),
-                )
-            })
-        }
+        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(
@@ -1061,72 +878,25 @@
         allow_other_panics: bool,
         at: Option<<Block as BlockT>::Hash>,
     ) -> RpcResult<GasInfo> {
-        let at_hash = at.unwrap_or_else(|| self.client.info().best_hash);
+        let at_hash = at.unwrap_or_else(|| self.client.info().best_hash);
 
-        let api_version = self
-            .client
-            .runtime_api()
-            .api_version::<dyn GearRuntimeApi<Block>>(at_hash)
-            .map_err(|e| map_err(e, "Failed to get gear runtime api version"))?
-            .ok_or_else(|| {
-                CallError::Custom(ErrorObject::owned(
-                    8000,
-                    "Gear runtime api wasn't found in the runtime",
-                    None::<String>,
-                ))
-            })?;
+        let GasInfo { min_limit, .. } = self.calculate_gas_info(
+            at_hash,
+            source,
+            HandleKind::Init(code.to_vec()),
+            payload.to_vec(),
+            value,
+            allow_other_panics,
+            None)?;
 
-        if api_version < 2 {
-            let GasInfo { min_limit, .. } = self.run_with_api_copy(|api| {
-                #[allow(deprecated)]
-                api.calculate_gas_info_before_version_2(
-                    at_hash,
-                    source,
-                    HandleKind::Init(code.to_vec()),
-                    payload.to_vec(),
-                    value,
-                    allow_other_panics,
-                    None,
-                )
-            })?;
-            self.run_with_api_copy(|api| {
-                #[allow(deprecated)]
-                api.calculate_gas_info_before_version_2(
-                    at_hash,
-                    source,
-                    HandleKind::Init(code.to_vec()),
-                    payload.to_vec(),
-                    value,
-                    allow_other_panics,
-                    Some(min_limit),
-                )
-            })
-        } else {
-            let GasInfo { min_limit, .. } = self.run_with_api_copy(|api| {
-                api.calculate_gas_info(
-                    at_hash,
-                    source,
-                    HandleKind::Init(code.to_vec()),
-                    payload.to_vec(),
-                    value,
-                    allow_other_panics,
-                    None,
-                    Some(self.allowance_multiplier),
-                )
-            })?;
-            self.run_with_api_copy(|api| {
-                api.calculate_gas_info(
-                    at_hash,
-                    source,
-                    HandleKind::Init(code.to_vec()),
-                    payload.to_vec(),
-                    value,
-                    allow_other_panics,
-                    Some(min_limit),
-                    Some(self.allowance_multiplier),
-                )
-            })
-        }
+        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(
@@ -1138,72 +908,27 @@
         allow_other_panics: bool,
         at: Option<<Block as BlockT>::Hash>,
     ) -> RpcResult<GasInfo> {
-        let at_hash = at.unwrap_or_else(|| self.client.info().best_hash);
+        let at_hash = at.unwrap_or_else(|| self.client.info().best_hash);
 
-        let api_version = self
-            .client
-            .runtime_api()
-            .api_version::<dyn GearRuntimeApi<Block>>(at_hash)
-            .map_err(|e| map_err(e, "Failed to get gear runtime api version"))?
-            .ok_or_else(|| {
-                CallError::Custom(ErrorObject::owned(
-                    8000,
-                    "Gear runtime api wasn't found in the runtime",
-                    None::<String>,
-                ))
-            })?;
+        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,
+        )?;
 
-        if api_version < 2 {
-            let GasInfo { min_limit, .. } = self.run_with_api_copy(|api| {
-                #[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,
-                    None,
-                )
-            })?;
-            self.run_with_api_copy(|api| {
-                #[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,
-                    Some(min_limit),
-                )
-            })
-        } else {
-            let GasInfo { min_limit, .. } = self.run_with_api_copy(|api| {
-                api.calculate_gas_info(
-                    at_hash,
-                    source,
-                    HandleKind::Handle(ProgramId::from_origin(dest)),
-                    payload.to_vec(),
-                    value,
-                    allow_other_panics,
-                    None,
-                    Some(self.allowance_multiplier),
-                )
-            })?;
-            self.run_with_api_copy(|api| {
-                api.calculate_gas_info(
-                    at_hash,
-                    source,
-                    HandleKind::Handle(ProgramId::from_origin(dest)),
-                    payload.to_vec(),
-                    value,
-                    allow_other_panics,
-                    Some(min_limit),
-                    Some(self.allowance_multiplier),
-                )
-            })
-        }
+        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(
@@ -1215,84 +940,31 @@
         allow_other_panics: bool,
         at: Option<<Block as BlockT>::Hash>,
     ) -> RpcResult<GasInfo> {
-        let at_hash = at.unwrap_or_else(|| self.client.info().best_hash);
+        let at_hash = at.unwrap_or_else(|| self.client.info().best_hash);
 
-        let api_version = self
-            .client
-            .runtime_api()
-            .api_version::<dyn GearRuntimeApi<Block>>(at_hash)
-            .map_err(|e| map_err(e, "Failed to get gear runtime api version"))?
-            .ok_or_else(|| {
-                CallError::Custom(ErrorObject::owned(
-                    8000,
-                    "Gear runtime api wasn't found in the runtime",
-                    None::<String>,
-                ))
-            })?;
+        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)?;
 
-        if api_version < 2 {
-            let GasInfo { min_limit, .. } = self.run_with_api_copy(|api| {
-                #[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,
-                    None,
-                )
-            })?;
-            self.run_with_api_copy(|api| {
-                #[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,
-                    Some(min_limit),
-                )
-            })
-        } else {
-            let GasInfo { min_limit, .. } = self.run_with_api_copy(|api| {
-                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,
-                    None,
-                    Some(self.allowance_multiplier),
-                )
-            })?;
-            self.run_with_api_copy(|api| {
-                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,
-                    Some(min_limit),
-                    Some(self.allowance_multiplier),
-                )
-            })
-        }
+        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(
@@ -1301,34 +973,23 @@
         payload: Bytes,
         at: Option<<Block as BlockT>::Hash>,
     ) -> RpcResult<Bytes> {
-        let at_hash = at.unwrap_or_else(|| self.client.info().best_hash);
+        let at_hash = at.unwrap_or_else(|| self.client.info().best_hash);
 
-        let api_version = self
-            .client
-            .runtime_api()
-            .api_version::<dyn GearRuntimeApi<Block>>(at_hash)
-            .map_err(|e| map_err(e, "Failed to get gear runtime api version"))?
-            .ok_or_else(|| {
-                CallError::Custom(ErrorObject::owned(
-                    8000,
-                    "Gear runtime api wasn't found in the runtime",
-                    None::<String>,
-                ))
-            })?;
+        let api_version = self.get_api_version(at_hash)?;
 
         if api_version < 2 {
-            self.run_with_api_copy(|api| {
+            self.run_with_api_copy(|api| {
                 #[allow(deprecated)]
-                api.read_state_before_version_2(at_hash, program_id, payload.to_vec())
+                api.read_state_before_version_2(at_hash, program_id, payload.to_vec())
             })
             .map(Bytes)
         } else {
-            self.run_with_api_copy(|api| {
-                api.read_state(
-                    at_hash,
-                    program_id,
-                    payload.to_vec(),
-                    Some(self.allowance_multiplier),
+            self.run_with_api_copy(|api| {
+                api.read_state(
+                    at_hash,
+                    program_id,
+                    payload.to_vec(),
+                    Some(self.allowance_multiplier),
                 )
             })
             .map(Bytes)
@@ -1349,38 +1010,32 @@
             .into());
         }
 
-        let at_hash = at.unwrap_or_else(|| self.client.info().best_hash);
+        let at_hash = at.unwrap_or_else(|| self.client.info().best_hash);
 
-        let api_version = self
-            .client
-            .runtime_api()
-            .api_version::<dyn GearRuntimeApi<Block>>(at_hash)
-            .map_err(|e| map_err(e, "Failed to get gear runtime api version"))?
-            .ok_or_else(|| {
-                CallError::Custom(ErrorObject::owned(
-                    8000,
-                    "Gear runtime api wasn't found in the runtime",
-                    None::<String>,
-                ))
-            })?;
+        let api_version = self.get_api_version(at_hash)?;
 
         if api_version < 2 {
-            batch_id_payload
+            batch_id_payload
                 .into_iter()
                 .map(|(program_id, payload)| {
-                    self.run_with_api_copy(|api| {
+                    self.run_with_api_copy(|api| {
                         #[allow(deprecated)]
-                        api.read_state_before_version_2(at_hash, program_id, payload.0)
+                        api.read_state_before_version_2(at_hash, program_id, payload.0)
                     })
                     .map(Bytes)
                 })
                 .collect()
         } else {
-            batch_id_payload
+            batch_id_payload
                 .into_iter()
                 .map(|(program_id, payload)| {
-                    self.run_with_api_copy(|api| {
-                        api.read_state(at_hash, program_id, payload.0, Some(self.allowance_multiplier))
+                    self.run_with_api_copy(|api| {
+                        api.read_state(
+                            at_hash,
+                            program_id,
+                            payload.0,
+                            Some(self.allowance_multiplier),
+                        )
                     })
                     .map(Bytes)
                 })
@@ -1397,46 +1052,35 @@
         argument: Option<Bytes>,
         at: Option<<Block as BlockT>::Hash>,
     ) -> RpcResult<Bytes> {
-        let at_hash = at.unwrap_or_else(|| self.client.info().best_hash);
+        let at_hash = at.unwrap_or_else(|| self.client.info().best_hash);
 
-        let api_version = self
-            .client
-            .runtime_api()
-            .api_version::<dyn GearRuntimeApi<Block>>(at_hash)
-            .map_err(|e| map_err(e, "Failed to get gear runtime api version"))?
-            .ok_or_else(|| {
-                CallError::Custom(ErrorObject::owned(
-                    8000,
-                    "Gear runtime api wasn't found in the runtime",
-                    None::<String>,
-                ))
-            })?;
+        let api_version = self.get_api_version(at_hash)?;
 
         if api_version < 2 {
-            self.run_with_api_copy(|api| {
+            self.run_with_api_copy(|api| {
                 #[allow(deprecated)]
-                api.read_state_using_wasm_before_version_2(
-                    at_hash,
-                    program_id,
-                    payload.to_vec(),
-                    fn_name.to_vec(),
-                    wasm.to_vec(),
-                    argument.map(|v| v.to_vec()),
+                api.read_state_using_wasm_before_version_2(
+                    at_hash,
+                    program_id,
+                    payload.to_vec(),
+                    fn_name.to_vec(),
+                    wasm.to_vec(),
+                    argument.map(|v| v.to_vec()),
                 )
-                .map(|r| r.map(Bytes))
+                .map(|r| r.map(Bytes))
             })
         } else {
-            self.run_with_api_copy(|api| {
-                api.read_state_using_wasm(
-                    at_hash,
-                    program_id,
-                    payload.to_vec(),
-                    fn_name.to_vec(),
-                    wasm.to_vec(),
-                    argument.map(|v| v.to_vec()),
-                    Some(self.allowance_multiplier),
+            self.run_with_api_copy(|api| {
+                api.read_state_using_wasm(
+                    at_hash,
+                    program_id,
+                    payload.to_vec(),
+                    fn_name.to_vec(),
+                    wasm.to_vec(),
+                    argument.map(|v| v.to_vec()),
+                    Some(self.allowance_multiplier),
                 )
-                .map(|r| r.map(Bytes))
+                .map(|r| r.map(Bytes))
             })
         }
     }
@@ -1458,54 +1102,43 @@
             .into());
         }
 
-        let at_hash = at.unwrap_or_else(|| self.client.info().best_hash);
+        let at_hash = at.unwrap_or_else(|| self.client.info().best_hash);
 
-        let api_version = self
-            .client
-            .runtime_api()
-            .api_version::<dyn GearRuntimeApi<Block>>(at_hash)
-            .map_err(|e| map_err(e, "Failed to get gear runtime api version"))?
-            .ok_or_else(|| {
-                CallError::Custom(ErrorObject::owned(
-                    8000,
-                    "Gear runtime api wasn't found in the runtime",
-                    None::<String>,
-                ))
-            })?;
+        let api_version = self.get_api_version(at_hash)?;
 
         if api_version < 2 {
-            batch_id_payload
+            batch_id_payload
                 .into_iter()
                 .map(|(program_id, payload)| {
-                    self.run_with_api_copy(|api| {
+                    self.run_with_api_copy(|api| {
                         #[allow(deprecated)]
-                        api.read_state_using_wasm_before_version_2(
-                            at_hash,
-                            program_id,
-                            payload.to_vec(),
-                            fn_name.clone().to_vec(),
-                            wasm.clone().to_vec(),
-                            argument.clone().map(|v| v.to_vec()),
+                        api.read_state_using_wasm_before_version_2(
+                            at_hash,
+                            program_id,
+                            payload.to_vec(),
+                            fn_name.clone().to_vec(),
+                            wasm.clone().to_vec(),
+                            argument.clone().map(|v| v.to_vec()),
                         )
-                        .map(|r| r.map(Bytes))
+                        .map(|r| r.map(Bytes))
                     })
                 })
                 .collect()
         } else {
-            batch_id_payload
+            batch_id_payload
                 .into_iter()
                 .map(|(program_id, payload)| {
-                    self.run_with_api_copy(|api| {
-                        api.read_state_using_wasm(
-                            at_hash,
-                            program_id,
-                            payload.to_vec(),
-                            fn_name.clone().to_vec(),
-                            wasm.clone().to_vec(),
-                            argument.clone().map(|v| v.to_vec()),
-                            Some(self.allowance_multiplier),
+                    self.run_with_api_copy(|api| {
+                        api.read_state_using_wasm(
+                            at_hash,
+                            program_id,
+                            payload.to_vec(),
+                            fn_name.clone().to_vec(),
+                            wasm.clone().to_vec(),
+                            argument.clone().map(|v| v.to_vec()),
+                            Some(self.allowance_multiplier),
                         )
-                        .map(|r| r.map(Bytes))
+                        .map(|r| r.map(Bytes))
                     })
                 })
                 .collect()
@@ -1517,27 +1150,16 @@
         program_id: H256,
         at: Option<<Block as BlockT>::Hash>,
     ) -> RpcResult<H256> {
-        let at_hash = at.unwrap_or_else(|| self.client.info().best_hash);
+        let at_hash = at.unwrap_or_else(|| self.client.info().best_hash);
 
-        let api_version = self
-            .client
-            .runtime_api()
-            .api_version::<dyn GearRuntimeApi<Block>>(at_hash)
-            .map_err(|e| map_err(e, "Failed to get gear runtime api version"))?
-            .ok_or_else(|| {
-                CallError::Custom(ErrorObject::owned(
-                    8000,
-                    "Gear runtime api wasn't found in the runtime",
-                    None::<String>,
-                ))
-            })?;
+        let api_version = self.get_api_version(at_hash)?;
 
         if api_version < 2 {
             #[allow(deprecated)]
-            self.run_with_api_copy(|api| api.read_metahash_before_version_2(at_hash, program_id))
+            self.run_with_api_copy(|api| api.read_metahash_before_version_2(at_hash, program_id))
         } else {
-            self.run_with_api_copy(|api| {
-                api.read_metahash(at_hash, program_id, Some(self.allowance_multiplier))
+            self.run_with_api_copy(|api| {
+                api.read_metahash(at_hash, program_id, Some(self.allowance_multiplier))
             })
         }
     }