use alloc::vec::Vec;
use miden_protocol::account::AccountId;
use miden_protocol::assembly::Path;
use miden_protocol::asset::AssetAmount;
use miden_protocol::crypto::rand::FeltRng;
use miden_protocol::errors::NoteError;
use miden_protocol::note::{
Note,
NoteAssets,
NoteAttachment,
NoteAttachments,
NoteRecipient,
NoteScript,
NoteScriptRoot,
NoteStorage,
NoteTag,
NoteType,
PartialNoteMetadata,
};
use miden_protocol::utils::sync::LazyLock;
use miden_protocol::{Felt, Word};
use crate::StandardsLib;
use crate::note::NetworkAccountTarget;
use crate::note::costs::{MIN_BURN_AMOUNT_CONFIG_CONSUMPTION_CYCLES, NoteConsumptionCost};
const MIN_BURN_AMOUNT_CONFIG_SCRIPT_PATH: &str =
"::miden::standards::notes::min_burn_amount_config::main";
static MIN_BURN_AMOUNT_CONFIG_SCRIPT: LazyLock<NoteScript> = LazyLock::new(|| {
let standards_lib = StandardsLib::default();
let path = Path::new(MIN_BURN_AMOUNT_CONFIG_SCRIPT_PATH);
NoteScript::from_package_reference(standards_lib.as_ref(), path)
.expect("Standards library contains MIN_BURN_AMOUNT_CONFIG note script procedure")
});
#[derive(Debug, Clone)]
pub struct MinBurnAmountConfigNote {
sender: AccountId,
target: AccountId,
min_burn_amount: AssetAmount,
serial_number: Word,
attachments: NoteAttachments,
}
#[bon::bon]
impl MinBurnAmountConfigNote {
#[builder]
pub fn new(
#[builder(field)] mut attachments: Vec<NoteAttachment>,
sender: AccountId,
target: AccountId,
min_burn_amount: AssetAmount,
serial_number: Word,
) -> Result<Self, NoteError> {
NetworkAccountTarget::ensure_presence(&mut attachments, target).map_err(|err| {
NoteError::other_with_source(
"failed to bind the MinBurnAmountConfig note to its target account",
err,
)
})?;
let attachments = NoteAttachments::new(attachments)?;
Ok(Self {
sender,
target,
min_burn_amount,
serial_number,
attachments,
})
}
}
impl MinBurnAmountConfigNote {
pub const NUM_STORAGE_ITEMS: usize = 1;
pub fn script() -> NoteScript {
MIN_BURN_AMOUNT_CONFIG_SCRIPT.clone()
}
pub fn script_root() -> NoteScriptRoot {
MIN_BURN_AMOUNT_CONFIG_SCRIPT.root()
}
pub fn sender(&self) -> AccountId {
self.sender
}
pub fn target(&self) -> AccountId {
self.target
}
pub fn min_burn_amount(&self) -> AssetAmount {
self.min_burn_amount
}
pub fn serial_number(&self) -> Word {
self.serial_number
}
pub fn attachments(&self) -> &NoteAttachments {
&self.attachments
}
}
impl<S: min_burn_amount_config_note_builder::State> MinBurnAmountConfigNoteBuilder<S> {
pub fn attachment(mut self, attachment: impl Into<NoteAttachment>) -> Self {
self.attachments.push(attachment.into());
self
}
pub fn attachments(
mut self,
attachments: impl IntoIterator<Item = impl Into<NoteAttachment>>,
) -> Self {
self.attachments.extend(attachments.into_iter().map(Into::into));
self
}
}
impl<S: min_burn_amount_config_note_builder::State> MinBurnAmountConfigNoteBuilder<S>
where
S::SerialNumber: min_burn_amount_config_note_builder::IsUnset,
{
pub fn generate_serial_number(
self,
rng: &mut impl FeltRng,
) -> MinBurnAmountConfigNoteBuilder<min_burn_amount_config_note_builder::SetSerialNumber<S>>
{
self.serial_number(rng.draw_word())
}
}
impl From<MinBurnAmountConfigNote> for Note {
fn from(note: MinBurnAmountConfigNote) -> Self {
let metadata = PartialNoteMetadata::new(note.sender, NoteType::Public)
.with_tag(NoteTag::with_account_target(note.target));
let storage = NoteStorage::new(vec![Felt::from(note.min_burn_amount)])
.expect("number of storage items should not exceed max storage items");
let recipient =
NoteRecipient::new(note.serial_number, MinBurnAmountConfigNote::script(), storage);
Note::with_attachments(NoteAssets::default(), metadata, recipient, note.attachments)
}
}
impl NoteConsumptionCost for MinBurnAmountConfigNote {
fn consumption_cycles() -> u32 {
MIN_BURN_AMOUNT_CONFIG_CONSUMPTION_CYCLES
}
}
#[cfg(test)]
mod tests {
use miden_protocol::account::AccountType;
use miden_protocol::crypto::rand::RandomCoin;
use super::*;
fn account_id(seed: u8) -> AccountId {
AccountId::builder()
.account_type(AccountType::Public)
.build_with_seed([seed; 32])
}
#[test]
fn builder_builds_min_burn_amount_config_note() {
let mut rng = RandomCoin::new(Word::empty());
let faucet = account_id(1);
let sender = account_id(2);
let note = MinBurnAmountConfigNote::builder()
.sender(sender)
.target(faucet)
.min_burn_amount(AssetAmount::new(100).unwrap())
.generate_serial_number(&mut rng)
.build()
.unwrap();
assert_eq!(note.sender(), sender);
assert_eq!(note.target(), faucet);
assert_eq!(note.min_burn_amount(), AssetAmount::new(100).unwrap());
let note = Note::from(note);
assert_eq!(note.metadata().note_type(), NoteType::Public);
assert_eq!(note.metadata().tag(), NoteTag::with_account_target(faucet));
assert_eq!(note.assets().num_assets(), 0);
}
#[test]
fn note_is_bound_to_target_account() {
let faucet = account_id(1);
let note = MinBurnAmountConfigNote::builder()
.sender(account_id(2))
.target(faucet)
.min_burn_amount(AssetAmount::new(100).unwrap())
.serial_number(Word::empty())
.build()
.unwrap();
let built = Note::from(note);
let target = NetworkAccountTarget::try_from(built.attachments())
.expect("note should carry a network account target attachment");
assert_eq!(target.target_id(), faucet);
}
#[test]
fn storage_layout() {
let min_burn_amount = AssetAmount::new(777).unwrap();
let note = MinBurnAmountConfigNote::builder()
.sender(account_id(2))
.target(account_id(1))
.min_burn_amount(min_burn_amount)
.serial_number(Word::empty())
.build()
.unwrap();
let built = Note::from(note);
assert_eq!(built.storage().items(), [Felt::from(min_burn_amount)]);
assert_eq!(built.storage().items().len(), MinBurnAmountConfigNote::NUM_STORAGE_ITEMS);
}
#[test]
fn script_root_is_registered_standard_note() {
use crate::note::StandardNote;
let standard = StandardNote::from_script_root(MinBurnAmountConfigNote::script_root())
.expect("config note script root should be a registered standard note");
assert_eq!(standard.name(), "MIN_BURN_AMOUNT_CONFIG");
}
}