casper_client/rpcs/v1_4_5/
get_dictionary_item.rs

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