casper_client/rpcs/v1_4_5/
get_dictionary_item.rs1use 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#[derive(Serialize, Deserialize, Debug)]
14#[serde(deny_unknown_fields)]
15pub enum DictionaryItemIdentifier {
16 AccountNamedKey {
18 key: String,
21 dictionary_name: String,
23 dictionary_item_key: String,
25 },
26 ContractNamedKey {
28 key: String,
31 dictionary_name: String,
33 dictionary_item_key: String,
35 },
36 URef {
38 seed_uref: URef,
40 dictionary_item_key: String,
42 },
43 Dictionary(String),
47}
48
49impl DictionaryItemIdentifier {
50 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 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 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 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#[derive(Serialize, Deserialize, Debug)]
120#[serde(deny_unknown_fields)]
121pub struct GetDictionaryItemResult {
122 pub api_version: ProtocolVersion,
124 pub dictionary_key: String,
126 pub stored_value: StoredValue,
128 pub merkle_proof: String,
130}