bee_block/output/
treasury.rs

1// Copyright 2020-2021 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::{protocol::ProtocolParameters, Error};
5
6/// [`TreasuryOutput`] is an output which holds the treasury of a network.
7#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, packable::Packable)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9#[packable(unpack_error = Error)]
10#[packable(unpack_visitor = ProtocolParameters)]
11pub struct TreasuryOutput {
12    #[packable(verify_with = verify_amount_packable)]
13    amount: u64,
14}
15
16impl TreasuryOutput {
17    /// The [`Output`](crate::output::Output) kind of a [`TreasuryOutput`].
18    pub const KIND: u8 = 2;
19
20    /// Creates a new [`TreasuryOutput`].
21    pub fn new(amount: u64, token_supply: u64) -> Result<Self, Error> {
22        verify_amount::<true>(&amount, &token_supply)?;
23
24        Ok(Self { amount })
25    }
26
27    /// Returns the amount of a [`TreasuryOutput`].
28    #[inline(always)]
29    pub fn amount(&self) -> u64 {
30        self.amount
31    }
32}
33
34fn verify_amount<const VERIFY: bool>(amount: &u64, token_supply: &u64) -> Result<(), Error> {
35    if VERIFY && amount > token_supply {
36        Err(Error::InvalidTreasuryOutputAmount(*amount))
37    } else {
38        Ok(())
39    }
40}
41
42fn verify_amount_packable<const VERIFY: bool>(
43    amount: &u64,
44    protocol_parameters: &ProtocolParameters,
45) -> Result<(), Error> {
46    verify_amount::<VERIFY>(amount, &protocol_parameters.token_supply())
47}
48
49#[cfg(feature = "dto")]
50#[allow(missing_docs)]
51pub mod dto {
52    use serde::{Deserialize, Serialize};
53
54    use super::*;
55    use crate::error::dto::DtoError;
56
57    /// Describes a treasury output.
58    #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
59    pub struct TreasuryOutputDto {
60        #[serde(rename = "type")]
61        pub kind: u8,
62        pub amount: String,
63    }
64
65    impl From<&TreasuryOutput> for TreasuryOutputDto {
66        fn from(value: &TreasuryOutput) -> Self {
67            Self {
68                kind: TreasuryOutput::KIND,
69                amount: value.amount().to_string(),
70            }
71        }
72    }
73
74    impl TreasuryOutput {
75        pub fn try_from_dto(value: &TreasuryOutputDto, token_supply: u64) -> Result<TreasuryOutput, DtoError> {
76            Ok(TreasuryOutput::new(
77                value
78                    .amount
79                    .parse::<u64>()
80                    .map_err(|_| DtoError::InvalidField("amount"))?,
81                token_supply,
82            )?)
83        }
84    }
85}