gear_subxt/constants/
constants_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 super::ConstantAddress;
6use crate::{
7    client::OfflineClientT,
8    error::{Error, MetadataError},
9    metadata::DecodeWithMetadata,
10    Config,
11};
12use derivative::Derivative;
13
14/// A client for accessing constants.
15#[derive(Derivative)]
16#[derivative(Clone(bound = "Client: Clone"))]
17pub struct ConstantsClient<T, Client> {
18    client: Client,
19    _marker: std::marker::PhantomData<T>,
20}
21
22impl<T, Client> ConstantsClient<T, Client> {
23    /// Create a new [`ConstantsClient`].
24    pub fn new(client: Client) -> Self {
25        Self {
26            client,
27            _marker: std::marker::PhantomData,
28        }
29    }
30}
31
32impl<T: Config, Client: OfflineClientT<T>> ConstantsClient<T, Client> {
33    /// Run the validation logic against some constant address you'd like to access. Returns `Ok(())`
34    /// if the address is valid (or if it's not possible to check since the address has no validation hash).
35    /// Return an error if the address was not valid or something went wrong trying to validate it (ie
36    /// the pallet or constant in question do not exist at all).
37    pub fn validate<Address: ConstantAddress>(&self, address: &Address) -> Result<(), Error> {
38        if let Some(actual_hash) = address.validation_hash() {
39            let expected_hash = self
40                .client
41                .metadata()
42                .pallet_by_name_err(address.pallet_name())?
43                .constant_hash(address.constant_name())
44                .ok_or_else(|| {
45                    MetadataError::ConstantNameNotFound(address.constant_name().to_owned())
46                })?;
47            if actual_hash != expected_hash {
48                return Err(MetadataError::IncompatibleCodegen.into());
49            }
50        }
51        Ok(())
52    }
53
54    /// Access the constant at the address given, returning the type defined by this address.
55    /// This is probably used with addresses given from static codegen, although you can manually
56    /// construct your own, too.
57    pub fn at<Address: ConstantAddress>(
58        &self,
59        address: &Address,
60    ) -> Result<Address::Target, Error> {
61        let metadata = self.client.metadata();
62
63        // 1. Validate constant shape if hash given:
64        self.validate(address)?;
65
66        // 2. Attempt to decode the constant into the type given:
67        let constant = metadata
68            .pallet_by_name_err(address.pallet_name())?
69            .constant_by_name(address.constant_name())
70            .ok_or_else(|| {
71                MetadataError::ConstantNameNotFound(address.constant_name().to_owned())
72            })?;
73        let value = <Address::Target as DecodeWithMetadata>::decode_with_metadata(
74            &mut constant.value(),
75            constant.ty(),
76            &metadata,
77        )?;
78        Ok(value)
79    }
80}