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 procedure root of the `on_before_asset_added_to_account` callback.
77    pub fn on_before_asset_added_to_account_proc_root(&self) -> Word {
78        self.on_before_asset_added_to_account
79    }
80
81    /// Returns the procedure root of the `on_before_asset_added_to_note` callback.
82    pub fn on_before_asset_added_to_note_proc_root(&self) -> Word {
83        self.on_before_asset_added_to_note
84    }
85
86    pub fn into_storage_slots(self) -> Vec<StorageSlot> {
87        let mut slots = Vec::new();
88
89        if !self.on_before_asset_added_to_account.is_empty() {
90            slots.push(StorageSlot::with_value(
91                AssetCallbacks::on_before_asset_added_to_account_slot().clone(),
92                self.on_before_asset_added_to_account,
93            ));
94        }
95
96        if !self.on_before_asset_added_to_note.is_empty() {
97            slots.push(StorageSlot::with_value(
98                AssetCallbacks::on_before_asset_added_to_note_slot().clone(),
99                self.on_before_asset_added_to_note,
100            ));
101        }
102
103        slots
104    }
105}