casper_client/rpcs/v2_0_0/
get_dictionary_item.rs

1use serde::{Deserialize, Serialize};
2
3use casper_types::{account::AccountHash, Digest, EntityAddr, HashAddr, Key, URef};
4
5pub use crate::rpcs::v1_6_0::get_dictionary_item::GetDictionaryItemResult;
6pub(crate) use crate::rpcs::v1_6_0::get_dictionary_item::GET_DICTIONARY_ITEM_METHOD;
7use crate::Error;
8
9/// The identifier for a dictionary item.
10#[derive(Serialize, Deserialize, Debug, Clone)]
11#[serde(deny_unknown_fields)]
12pub enum DictionaryItemIdentifier {
13    /// A dictionary item identified via an [`Key::Account`]'s named keys.
14    AccountNamedKey {
15        /// The [`Key::Account`] as a formatted string, identifying the account whose named keys
16        /// contains `dictionary_name`.
17        key: String,
18        /// The named key under which the dictionary seed `URef` is stored.
19        dictionary_name: String,
20        /// The key within the dictionary under which the item is held.
21        dictionary_item_key: String,
22    },
23    /// A dictionary item identified via a [`Key::Hash`]'s named keys.
24    ContractNamedKey {
25        /// The [`Key::Hash`] as a formatted string, identifying the contract whose named keys
26        /// contains `dictionary_name`.
27        key: String,
28        /// The named key under which the dictionary seed `URef` is stored.
29        dictionary_name: String,
30        /// The key within the dictionary under which the item is held.
31        dictionary_item_key: String,
32    },
33    /// A dictionary item identified via a [`Key::AddressableEntity`]'s named keys.
34    EntityNamedKey {
35        /// The [`Key::AddressableEntity`] as a formatted string, identifying the entity whose
36        /// named keys contain `dictionary_name`.
37        key: String,
38        /// The named key under which the dictionary seed `URef` is stored.
39        dictionary_name: String,
40        /// The key within the dictionary under which the item is held.
41        dictionary_item_key: String,
42    },
43    /// A dictionary item identified via its seed [`Key::URef`].
44    URef {
45        /// The dictionary's seed `URef`.
46        seed_uref: URef,
47        /// The key within the dictionary under which the item is held.
48        dictionary_item_key: String,
49    },
50    /// A dictionary item identified via its unique address derived from the dictionary's seed
51    /// `URef` and the item's key within the dictionary.  The key must be a `Key::Dictionary`
52    /// variant, as a formatted string.
53    Dictionary(String),
54}
55
56impl DictionaryItemIdentifier {
57    /// Returns a new `DictionaryItemIdentifier::AccountNamedKey` variant.
58    pub fn new_from_account_info(
59        account_hash: AccountHash,
60        dictionary_name: String,
61        dictionary_item_key: String,
62    ) -> Self {
63        DictionaryItemIdentifier::AccountNamedKey {
64            key: Key::Account(account_hash).to_formatted_string(),
65            dictionary_name,
66            dictionary_item_key,
67        }
68    }
69
70    /// Returns a new `DictionaryItemIdentifier::ContractNamedKey` variant.
71    pub fn new_from_contract_info(
72        contract_addr: HashAddr,
73        dictionary_name: String,
74        dictionary_item_key: String,
75    ) -> Self {
76        DictionaryItemIdentifier::ContractNamedKey {
77            key: Key::Hash(contract_addr).to_formatted_string(),
78            dictionary_name,
79            dictionary_item_key,
80        }
81    }
82
83    /// Returns a new `DictionaryItemIdentifier::EntityNamedKey` variant.
84    pub fn new_from_entity_info(
85        entity_addr: EntityAddr,
86        dictionary_name: String,
87        dictionary_item_key: String,
88    ) -> Self {
89        DictionaryItemIdentifier::EntityNamedKey {
90            key: Key::AddressableEntity(entity_addr).to_formatted_string(),
91            dictionary_name,
92            dictionary_item_key,
93        }
94    }
95
96    /// Returns a new `DictionaryItemIdentifier::URef` variant.
97    pub fn new_from_seed_uref(seed_uref: URef, dictionary_item_key: String) -> Self {
98        DictionaryItemIdentifier::URef {
99            seed_uref,
100            dictionary_item_key,
101        }
102    }
103
104    /// Returns a new `DictionaryItemIdentifier::Dictionary` variant.
105    pub fn new_from_item_key(item_key: Key) -> Result<Self, Error> {
106        if item_key.as_dictionary().is_some() {
107            Ok(DictionaryItemIdentifier::Dictionary(
108                item_key.to_formatted_string(),
109            ))
110        } else {
111            Err(Error::InvalidKeyVariant {
112                expected_variant: "Key::Dictionary".to_string(),
113                actual: item_key,
114            })
115        }
116    }
117}
118
119#[derive(Serialize, Deserialize, Debug)]
120#[serde(deny_unknown_fields)]
121pub(crate) struct GetDictionaryItemParams {
122    state_root_hash: Digest,
123    dictionary_identifier: DictionaryItemIdentifier,
124}
125
126impl GetDictionaryItemParams {
127    pub(crate) fn new(
128        state_root_hash: Digest,
129        dictionary_identifier: DictionaryItemIdentifier,
130    ) -> Self {
131        GetDictionaryItemParams {
132            state_root_hash,
133            dictionary_identifier,
134        }
135    }
136}