dpp/tokens/
token_pricing_schedule.rs1use crate::balances::credits::TokenAmount;
2use crate::errors::ProtocolError;
3use crate::fee::Credits;
4use bincode::{Decode, Encode};
5use platform_serialization_derive::{PlatformDeserialize, PlatformSerialize};
6#[cfg(feature = "serde-conversion")]
7use serde::{Deserialize, Serialize};
8use std::collections::BTreeMap;
9use std::fmt::{self, Display, Formatter};
10
11#[derive(
17 Debug,
18 Clone,
19 Encode,
20 Decode,
21 Eq,
22 PartialEq,
23 Ord,
24 PartialOrd,
25 PlatformSerialize,
26 PlatformDeserialize,
27)]
28#[cfg_attr(feature = "serde-conversion", derive(Serialize, Deserialize))]
29pub enum TokenPricingSchedule {
30 SinglePrice(Credits),
35
36 SetPrices(BTreeMap<TokenAmount, Credits>),
45}
46
47impl TokenPricingSchedule {
48 pub fn minimum_purchase_amount_and_price(&self) -> (TokenAmount, Credits) {
49 match self {
50 TokenPricingSchedule::SinglePrice(price) => (1, *price),
51 TokenPricingSchedule::SetPrices(prices) => prices
52 .first_key_value()
53 .map(|(amount, cost)| (*amount, *cost))
54 .unwrap_or_default(),
55 }
56 }
57}
58
59impl Display for TokenPricingSchedule {
60 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
61 match self {
62 TokenPricingSchedule::SinglePrice(credits) => {
63 write!(f, "SinglePrice: {}", credits)
64 }
65 TokenPricingSchedule::SetPrices(prices) => {
66 write!(f, "SetPrices: [")?;
67 for (i, (amount, credits)) in prices.iter().enumerate() {
68 if i > 0 {
69 write!(f, ", ")?;
70 }
71 write!(f, "{} => {}", amount, credits)?;
72 }
73 write!(f, "]")
74 }
75 }
76 }
77}