gear_subxt/constants/
constant_address.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::{dynamic::DecodedValueThunk, metadata::DecodeWithMetadata};
6use std::borrow::Cow;
7
8/// This represents a constant address. Anything implementing this trait
9/// can be used to fetch constants.
10pub trait ConstantAddress {
11    /// The target type of the value that lives at this address.
12    type Target: DecodeWithMetadata;
13
14    /// The name of the pallet that the constant lives under.
15    fn pallet_name(&self) -> &str;
16
17    /// The name of the constant in a given pallet.
18    fn constant_name(&self) -> &str;
19
20    /// An optional hash which, if present, will be checked against
21    /// the node metadata to confirm that the return type matches what
22    /// we are expecting.
23    fn validation_hash(&self) -> Option<[u8; 32]> {
24        None
25    }
26}
27
28/// This represents the address of a constant.
29pub struct Address<ReturnTy> {
30    pallet_name: Cow<'static, str>,
31    constant_name: Cow<'static, str>,
32    constant_hash: Option<[u8; 32]>,
33    _marker: std::marker::PhantomData<ReturnTy>,
34}
35
36/// The type of address typically used to return dynamic constant values.
37pub type DynamicAddress = Address<DecodedValueThunk>;
38
39impl<ReturnTy> Address<ReturnTy> {
40    /// Create a new [`Address`] to use to look up a constant.
41    pub fn new(pallet_name: impl Into<String>, constant_name: impl Into<String>) -> Self {
42        Self {
43            pallet_name: Cow::Owned(pallet_name.into()),
44            constant_name: Cow::Owned(constant_name.into()),
45            constant_hash: None,
46            _marker: std::marker::PhantomData,
47        }
48    }
49
50    /// Create a new [`Address`] that will be validated
51    /// against node metadata using the hash given.
52    #[doc(hidden)]
53    pub fn new_static(
54        pallet_name: &'static str,
55        constant_name: &'static str,
56        hash: [u8; 32],
57    ) -> Self {
58        Self {
59            pallet_name: Cow::Borrowed(pallet_name),
60            constant_name: Cow::Borrowed(constant_name),
61            constant_hash: Some(hash),
62            _marker: std::marker::PhantomData,
63        }
64    }
65
66    /// Do not validate this constant prior to accessing it.
67    pub fn unvalidated(self) -> Self {
68        Self {
69            pallet_name: self.pallet_name,
70            constant_name: self.constant_name,
71            constant_hash: None,
72            _marker: self._marker,
73        }
74    }
75}
76
77impl<ReturnTy: DecodeWithMetadata> ConstantAddress for Address<ReturnTy> {
78    type Target = ReturnTy;
79
80    fn pallet_name(&self) -> &str {
81        &self.pallet_name
82    }
83
84    fn constant_name(&self) -> &str {
85        &self.constant_name
86    }
87
88    fn validation_hash(&self) -> Option<[u8; 32]> {
89        self.constant_hash
90    }
91}
92
93/// Construct a new dynamic constant lookup.
94pub fn dynamic(pallet_name: impl Into<String>, constant_name: impl Into<String>) -> DynamicAddress {
95    DynamicAddress::new(pallet_name, constant_name)
96}