miden_objects/conversion/
account_patch.rs1use alloc::borrow::ToOwned;
2
3use miden_protocol::Word;
4use miden_protocol::account::{
5 AccountCode,
6 AccountCodePatch,
7 AccountCodeUpgrade,
8 AccountPatch,
9 AccountStoragePatch,
10 AccountUpdateDetails,
11 AccountVaultPatch,
12 StorageMapPatch,
13 StorageMapPatchEntries,
14 StorageSlotPatch,
15 StorageValuePatch,
16};
17
18use crate::proto;
19
20#[cfg(test)]
21mod tests;
22
23impl From<&AccountCode> for proto::account::AccountCode {
27 fn from(code: &AccountCode) -> Self {
28 Self {
29 mast: Some(code.mast().as_ref().into()),
30 procedure_roots: code.procedure_roots().map(Into::into).collect(),
31 }
32 }
33}
34
35impl From<AccountCode> for proto::account::AccountCode {
36 fn from(code: AccountCode) -> Self {
37 Self::from(&code)
38 }
39}
40
41impl From<&AccountCodeUpgrade> for proto::account::AccountCodeUpgrade {
42 fn from(upgrade: &AccountCodeUpgrade) -> Self {
43 Self { code: Some(upgrade.code().into()) }
44 }
45}
46
47impl From<&StorageValuePatch> for proto::account::StorageValuePatch {
51 fn from(patch: &StorageValuePatch) -> Self {
52 use proto::account::storage_value_patch::Operation;
53
54 let operation = match patch {
55 StorageValuePatch::Create { value } => Operation::Create(value.into()),
56 StorageValuePatch::Update { value } => Operation::Update(value.into()),
57 StorageValuePatch::Remove => Operation::Remove(()),
58 };
59 Self { operation: Some(operation) }
60 }
61}
62
63impl From<&StorageMapPatch> for proto::account::StorageMapPatch {
64 fn from(patch: &StorageMapPatch) -> Self {
65 use proto::account::storage_map_patch::Operation;
66
67 let operation = match patch {
68 StorageMapPatch::Create { entries } => Operation::Create(entries.into()),
69 StorageMapPatch::Update { entries } => Operation::Update(entries.into()),
70 StorageMapPatch::Remove => Operation::Remove(()),
71 };
72 Self { operation: Some(operation) }
73 }
74}
75
76impl From<&StorageMapPatchEntries> for proto::account::StorageMapPatchEntries {
77 fn from(entries: &StorageMapPatchEntries) -> Self {
78 Self {
79 entries: entries
80 .as_map()
81 .iter()
82 .map(|(key, value)| proto::account::StorageMapEntry {
83 key: Some(Word::from(*key).into()),
84 value: Some(value.into()),
85 })
86 .collect(),
87 }
88 }
89}
90
91impl From<&AccountStoragePatch> for proto::account::AccountStoragePatch {
92 fn from(patch: &AccountStoragePatch) -> Self {
93 Self {
94 slots: patch
95 .slots()
96 .map(|(slot_name, slot_patch)| {
97 use proto::account::storage_slot_patch::Patch;
98
99 let patch = match slot_patch {
100 StorageSlotPatch::Value(value) => Patch::Value(value.into()),
101 StorageSlotPatch::Map(map) => Patch::Map(map.into()),
102 };
103 proto::account::StorageSlotPatch {
104 slot_name: slot_name.as_str().to_owned(),
105 patch: Some(patch),
106 }
107 })
108 .collect(),
109 }
110 }
111}
112
113impl From<&AccountVaultPatch> for proto::account::AccountVaultPatch {
117 fn from(patch: &AccountVaultPatch) -> Self {
118 Self {
119 entries: patch
120 .iter()
121 .map(|(asset_id, value)| proto::account::AccountVaultPatchEntry {
122 asset_id: Some(asset_id.to_word().into()),
123 value: Some((*value).into()),
124 })
125 .collect(),
126 }
127 }
128}
129
130impl From<&AccountCodePatch> for proto::account::AccountCodePatch {
131 fn from(patch: &AccountCodePatch) -> Self {
132 Self { code: patch.as_code().map(Into::into) }
133 }
134}
135
136impl From<&AccountPatch> for proto::account::AccountPatch {
137 fn from(patch: &AccountPatch) -> Self {
138 Self {
139 version: proto::account::AccountPatchVersion::V1 as i32,
140 account_id: Some(patch.id().into()),
141 storage: Some(patch.storage().into()),
142 vault: Some(patch.vault().into()),
143 code: Some(patch.code().into()),
144 final_nonce: patch.final_nonce().map(Into::into),
145 }
146 }
147}
148
149impl From<AccountPatch> for proto::account::AccountPatch {
150 fn from(patch: AccountPatch) -> Self {
151 Self::from(&patch)
152 }
153}
154
155impl From<&AccountUpdateDetails> for proto::account::AccountUpdateDetails {
156 fn from(details: &AccountUpdateDetails) -> Self {
157 use proto::account::account_update_details::Update;
158
159 let update = match details {
160 AccountUpdateDetails::Private => {
161 Update::Private(proto::account::PrivateAccountUpdate {})
162 },
163 AccountUpdateDetails::Public(patch) => Update::Public(patch.into()),
164 };
165 Self { update: Some(update) }
166 }
167}
168
169impl From<AccountUpdateDetails> for proto::account::AccountUpdateDetails {
170 fn from(details: AccountUpdateDetails) -> Self {
171 Self::from(&details)
172 }
173}