gear_subxt/client/
offline_client.rs

1// Copyright 2019-2023 Parity Technologies (UK) Ltd.
2// This file is dual-licensed as Apache-2.0 or GPL-3.0.
3// see LICENSE for license details.
4
5use crate::{
6    blocks::BlocksClient, constants::ConstantsClient, events::EventsClient,
7    rpc::types::RuntimeVersion, runtime_api::RuntimeApiClient, storage::StorageClient,
8    tx::TxClient, Config, Metadata,
9};
10use derivative::Derivative;
11use std::sync::Arc;
12
13/// A trait representing a client that can perform
14/// offline-only actions.
15pub trait OfflineClientT<T: Config>: Clone + Send + Sync + 'static {
16    /// Return the provided [`Metadata`].
17    fn metadata(&self) -> Metadata;
18    /// Return the provided genesis hash.
19    fn genesis_hash(&self) -> T::Hash;
20    /// Return the provided [`RuntimeVersion`].
21    fn runtime_version(&self) -> RuntimeVersion;
22
23    /// Work with transactions.
24    fn tx(&self) -> TxClient<T, Self> {
25        TxClient::new(self.clone())
26    }
27
28    /// Work with events.
29    fn events(&self) -> EventsClient<T, Self> {
30        EventsClient::new(self.clone())
31    }
32
33    /// Work with storage.
34    fn storage(&self) -> StorageClient<T, Self> {
35        StorageClient::new(self.clone())
36    }
37
38    /// Access constants.
39    fn constants(&self) -> ConstantsClient<T, Self> {
40        ConstantsClient::new(self.clone())
41    }
42
43    /// Work with blocks.
44    fn blocks(&self) -> BlocksClient<T, Self> {
45        BlocksClient::new(self.clone())
46    }
47
48    /// Work with runtime API.
49    fn runtime_api(&self) -> RuntimeApiClient<T, Self> {
50        RuntimeApiClient::new(self.clone())
51    }
52}
53
54/// A client that is capable of performing offline-only operations.
55/// Can be constructed as long as you can populate the required fields.
56#[derive(Derivative)]
57#[derivative(Debug(bound = ""), Clone(bound = ""))]
58pub struct OfflineClient<T: Config> {
59    inner: Arc<Inner<T>>,
60}
61
62#[derive(Derivative)]
63#[derivative(Debug(bound = ""), Clone(bound = ""))]
64struct Inner<T: Config> {
65    genesis_hash: T::Hash,
66    runtime_version: RuntimeVersion,
67    metadata: Metadata,
68}
69
70impl<T: Config> OfflineClient<T> {
71    /// Construct a new [`OfflineClient`], providing
72    /// the necessary runtime and compile-time arguments.
73    pub fn new(
74        genesis_hash: T::Hash,
75        runtime_version: RuntimeVersion,
76        metadata: impl Into<Metadata>,
77    ) -> OfflineClient<T> {
78        OfflineClient {
79            inner: Arc::new(Inner {
80                genesis_hash,
81                runtime_version,
82                metadata: metadata.into(),
83            }),
84        }
85    }
86
87    /// Return the genesis hash.
88    pub fn genesis_hash(&self) -> T::Hash {
89        self.inner.genesis_hash
90    }
91
92    /// Return the runtime version.
93    pub fn runtime_version(&self) -> RuntimeVersion {
94        self.inner.runtime_version.clone()
95    }
96
97    /// Return the [`Metadata`] used in this client.
98    pub fn metadata(&self) -> Metadata {
99        self.inner.metadata.clone()
100    }
101
102    // Just a copy of the most important trait methods so that people
103    // don't need to import the trait for most things:
104
105    /// Work with transactions.
106    pub fn tx(&self) -> TxClient<T, Self> {
107        <Self as OfflineClientT<T>>::tx(self)
108    }
109
110    /// Work with events.
111    pub fn events(&self) -> EventsClient<T, Self> {
112        <Self as OfflineClientT<T>>::events(self)
113    }
114
115    /// Work with storage.
116    pub fn storage(&self) -> StorageClient<T, Self> {
117        <Self as OfflineClientT<T>>::storage(self)
118    }
119
120    /// Access constants.
121    pub fn constants(&self) -> ConstantsClient<T, Self> {
122        <Self as OfflineClientT<T>>::constants(self)
123    }
124}
125
126impl<T: Config> OfflineClientT<T> for OfflineClient<T> {
127    fn genesis_hash(&self) -> T::Hash {
128        self.genesis_hash()
129    }
130    fn runtime_version(&self) -> RuntimeVersion {
131        self.runtime_version()
132    }
133    fn metadata(&self) -> Metadata {
134        self.metadata()
135    }
136}
137
138// For ergonomics; cloning a client is deliberately fairly cheap (via Arc),
139// so this allows users to pass references to a client rather than explicitly
140// cloning. This is partly for consistency with OnlineClient, which can be
141// easily converted into an OfflineClient for ergonomics.
142impl<'a, T: Config> From<&'a OfflineClient<T>> for OfflineClient<T> {
143    fn from(c: &'a OfflineClient<T>) -> Self {
144        c.clone()
145    }
146}