gear_subxt/storage/
storage_client.rs1use super::{
6 storage_type::{validate_storage_address, Storage},
7 utils, StorageAddress,
8};
9
10use crate::{
11 client::{OfflineClientT, OnlineClientT},
12 error::Error,
13 Config,
14};
15use derivative::Derivative;
16use std::{future::Future, marker::PhantomData};
17
18#[derive(Derivative)]
20#[derivative(Clone(bound = "Client: Clone"))]
21pub struct StorageClient<T, Client> {
22 client: Client,
23 _marker: PhantomData<T>,
24}
25
26impl<T, Client> StorageClient<T, Client> {
27 pub fn new(client: Client) -> Self {
29 Self {
30 client,
31 _marker: PhantomData,
32 }
33 }
34}
35
36impl<T, Client> StorageClient<T, Client>
37where
38 T: Config,
39 Client: OfflineClientT<T>,
40{
41 pub fn validate<Address: StorageAddress>(&self, address: &Address) -> Result<(), Error> {
46 let metadata = self.client.metadata();
47 let pallet_metadata = metadata.pallet_by_name_err(address.pallet_name())?;
48 validate_storage_address(address, pallet_metadata)
49 }
50
51 pub fn address_root_bytes<Address: StorageAddress>(&self, address: &Address) -> Vec<u8> {
54 utils::storage_address_root_bytes(address)
55 }
56
57 pub fn address_bytes<Address: StorageAddress>(
63 &self,
64 address: &Address,
65 ) -> Result<Vec<u8>, Error> {
66 utils::storage_address_bytes(address, &self.client.metadata())
67 }
68}
69
70impl<T, Client> StorageClient<T, Client>
71where
72 T: Config,
73 Client: OnlineClientT<T>,
74{
75 pub fn at(&self, block_hash: T::Hash) -> Storage<T, Client> {
77 Storage::new(self.client.clone(), block_hash)
78 }
79
80 pub fn at_latest(
82 &self,
83 ) -> impl Future<Output = Result<Storage<T, Client>, Error>> + Send + 'static {
84 let client = self.client.clone();
87 async move {
88 let block_hash = client
90 .rpc()
91 .block_hash(None)
92 .await?
93 .expect("didn't pass a block number; qed");
94
95 Ok(Storage::new(client, block_hash))
96 }
97 }
98}