Skip to main content

miden_protocol/asset/
asset_callbacks.rs

1use alloc::vec::Vec;
2
3use crate::Word;
4use crate::account::{StorageSlot, StorageSlotName};
5use crate::utils::sync::LazyLock;
6
7// CONSTANTS
8// ================================================================================================
9
10static ON_BEFORE_ASSET_ADDED_TO_ACCOUNT_SLOT_NAME: LazyLock<StorageSlotName> =
11    LazyLock::new(|| {
12        StorageSlotName::new("miden::protocol::faucet::callback::on_before_asset_added_to_account")
13            .expect("storage slot name should be valid")
14    });
15
16static ON_BEFORE_ASSET_ADDED_TO_NOTE_SLOT_NAME: LazyLock<StorageSlotName> = LazyLock::new(|| {
17    StorageSlotName::new("miden::protocol::faucet::callback::on_before_asset_added_to_note")
18        .expect("storage slot name should be valid")
19});
20
21// ASSET CALLBACKS
22// ================================================================================================
23
24/// Configures the callback procedure roots for asset callbacks.
25///
26/// ## Storage Layout
27///
28/// - [`Self::on_before_asset_added_to_account_slot()`]: Stores the procedure root of the
29///   `on_before_asset_added_to_account` callback. This storage slot is only added if the callback
30///   procedure root is not the empty word.
31/// - [`Self::on_before_asset_added_to_note_slot()`]: Stores the procedure root of the
32///   `on_before_asset_added_to_note` callback. This storage slot is only added if the callback
33///   procedure root is not the empty word.
34#[derive(Debug, Clone, Default, PartialEq, Eq)]
35pub struct AssetCallbacks {
36    on_before_asset_added_to_account: Word,
37    on_before_asset_added_to_note: Word,
38}
39
40impl AssetCallbacks {
41    // CONSTRUCTORS
42    // --------------------------------------------------------------------------------------------
43
44    /// Creates a new [`AssetCallbacks`] with all callbacks set to the empty word.
45    pub fn new() -> Self {
46        Self::default()
47    }
48
49    /// Sets the `on_before_asset_added_to_account` callback procedure root.
50    pub fn on_before_asset_added_to_account(mut self, proc_root: Word) -> Self {
51        self.on_before_asset_added_to_account = proc_root;
52        self
53    }
54
55    /// Sets the `on_before_asset_added_to_note` callback procedure root.
56    pub fn on_before_asset_added_to_note(mut self, proc_root: Word) -> Self {
57        self.on_before_asset_added_to_note = proc_root;
58        self
59    }
60
61    // PUBLIC ACCESSORS
62    // --------------------------------------------------------------------------------------------
63
64    /// Returns the [`StorageSlotName`] where the `on_before_asset_added_to_account` callback
65    /// procedure root is stored.
66    pub fn on_before_asset_added_to_account_slot() -> &'static StorageSlotName {
67        &ON_BEFORE_ASSET_ADDED_TO_ACCOUNT_SLOT_NAME
68    }
69
70    /// Returns the [`StorageSlotName`] where the `on_before_asset_added_to_note` callback
71    /// procedure root is stored.
72    pub fn on_before_asset_added_to_note_slot() -> &'static StorageSlotName {
73        &ON_BEFORE_ASSET_ADDED_TO_NOTE_SLOT_NAME
74    }
75
76    /// Returns the names of all protocol-reserved asset callback storage slots.
77    pub fn slot_names() -> [&'static StorageSlotName; 2] {
78        [
79            Self::on_before_asset_added_to_account_slot(),
80            Self::on_before_asset_added_to_note_slot(),
81        ]
82    }
83
84    /// Returns the procedure root of the `on_before_asset_added_to_account` callback.
85    pub fn on_before_asset_added_to_account_proc_root(&self) -> Word {
86        self.on_before_asset_added_to_account
87    }
88
89    /// Returns the procedure root of the `on_before_asset_added_to_note` callback.
90    pub fn on_before_asset_added_to_note_proc_root(&self) -> Word {
91        self.on_before_asset_added_to_note
92    }
93
94    pub fn into_storage_slots(self) -> Vec<StorageSlot> {
95        let mut slots = Vec::new();
96
97        if !self.on_before_asset_added_to_account.is_empty() {
98            slots.push(StorageSlot::with_value(
99                AssetCallbacks::on_before_asset_added_to_account_slot().clone(),
100                self.on_before_asset_added_to_account,
101            ));
102        }
103
104        if !self.on_before_asset_added_to_note.is_empty() {
105            slots.push(StorageSlot::with_value(
106                AssetCallbacks::on_before_asset_added_to_note_slot().clone(),
107                self.on_before_asset_added_to_note,
108            ));
109        }
110
111        slots
112    }
113}