iota_stardust_types/block/output/
treasury.rs

1// Copyright (c) 2026 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::block::Error;
5
6/// [`TreasuryOutput`] is an output which holds the treasury of a network.
7#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, packable::Packable)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9#[packable(unpack_error = Error)]
10pub struct TreasuryOutput {
11    amount: u64,
12}
13
14impl TreasuryOutput {
15    /// The [`Output`](crate::block::output::Output) kind of a
16    /// [`TreasuryOutput`].
17    pub const KIND: u8 = 2;
18
19    /// Creates a new [`TreasuryOutput`].
20    pub fn new(amount: u64, token_supply: u64) -> Result<Self, Error> {
21        verify_amount(&amount, &token_supply)?;
22
23        Ok(Self { amount })
24    }
25
26    /// Returns the amount of a [`TreasuryOutput`].
27    #[inline(always)]
28    pub fn amount(&self) -> u64 {
29        self.amount
30    }
31}
32
33fn verify_amount(amount: &u64, token_supply: &u64) -> Result<(), Error> {
34    if amount > token_supply {
35        Err(Error::InvalidTreasuryOutputAmount(*amount))
36    } else {
37        Ok(())
38    }
39}