casper_client/rpcs/v2_0_0/
get_dictionary_item.rs1use 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#[derive(Serialize, Deserialize, Debug, Clone)]
11#[serde(deny_unknown_fields)]
12pub enum DictionaryItemIdentifier {
13 AccountNamedKey {
15 key: String,
18 dictionary_name: String,
20 dictionary_item_key: String,
22 },
23 ContractNamedKey {
25 key: String,
28 dictionary_name: String,
30 dictionary_item_key: String,
32 },
33 EntityNamedKey {
35 key: String,
38 dictionary_name: String,
40 dictionary_item_key: String,
42 },
43 URef {
45 seed_uref: URef,
47 dictionary_item_key: String,
49 },
50 Dictionary(String),
54}
55
56impl DictionaryItemIdentifier {
57 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 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 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 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 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}