Skip to main content

miden_objects/conversion/
account_patch.rs

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