1use std::collections::BTreeMap;
2
3use fedimint_core::config::ExcessiveRelativeFeeError;
4use fedimint_core::core::ModuleKind;
5use fedimint_core::encoding::{Decodable, Encodable};
6use fedimint_core::module::{AmountUnit, serde_json};
7use fedimint_core::{Amount, PeerId, plugin_types_trait_impl_config};
8use serde::{Deserialize, Serialize};
9use tbs::{AggregatePublicKey, PublicKeyShare};
10
11use crate::{Denomination, MintCommonInit};
12
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct MintGenParams {
15 pub fee_consensus: FeeConsensus,
16}
17
18pub fn consensus_denominations() -> impl DoubleEndedIterator<Item = Denomination> {
19 (0..42).map(Denomination)
20}
21
22pub fn client_denominations() -> impl DoubleEndedIterator<Item = Denomination> {
23 (9..42).map(Denomination)
24}
25
26#[derive(Clone, Debug, Serialize, Deserialize)]
27pub struct MintConfig {
28 pub private: MintConfigPrivate,
29 pub consensus: MintConfigConsensus,
30}
31
32#[derive(Clone, Debug, Serialize, Deserialize, Encodable, Decodable)]
33pub struct MintConfigConsensus {
34 pub tbs_agg_pks: BTreeMap<Denomination, AggregatePublicKey>,
35 pub tbs_pks: BTreeMap<Denomination, BTreeMap<PeerId, PublicKeyShare>>,
36 pub fee_consensus: FeeConsensus,
37 pub amount_unit: AmountUnit,
38}
39
40#[derive(Clone, Debug, Serialize, Deserialize)]
41pub struct MintConfigPrivate {
42 pub tbs_sks: BTreeMap<Denomination, tbs::SecretKeyShare>,
43}
44
45#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Encodable, Decodable, Hash)]
46pub struct MintClientConfig {
47 pub tbs_agg_pks: BTreeMap<Denomination, AggregatePublicKey>,
48 pub tbs_pks: BTreeMap<Denomination, BTreeMap<PeerId, PublicKeyShare>>,
49 pub fee_consensus: FeeConsensus,
50 pub amount_unit: AmountUnit,
51}
52
53impl std::fmt::Display for MintClientConfig {
54 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55 write!(
56 f,
57 "MintClientConfig {}",
58 serde_json::to_string(self).map_err(|_e| std::fmt::Error)?
59 )
60 }
61}
62
63plugin_types_trait_impl_config!(
65 MintCommonInit,
66 MintConfig,
67 MintConfigPrivate,
68 MintConfigConsensus,
69 MintClientConfig
70);
71
72#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Encodable)]
73pub struct FeeConsensus {
74 base: Amount,
75 parts_per_million: u64,
76}
77
78#[derive(Deserialize)]
79struct UnvalidatedFeeConsensus {
80 base: Amount,
81 parts_per_million: u64,
82}
83
84impl FeeConsensus {
85 pub fn new(parts_per_million: u64) -> Result<Self, ExcessiveRelativeFeeError> {
96 const MAX_PARTS_PER_MILLION: u64 = 1_000;
97
98 if MAX_PARTS_PER_MILLION < parts_per_million {
99 return Err(ExcessiveRelativeFeeError {
100 parts_per_million,
101 max: MAX_PARTS_PER_MILLION,
102 });
103 }
104
105 Ok(Self {
106 base: Amount::from_msats(100),
107 parts_per_million,
108 })
109 }
110
111 pub fn zero() -> Self {
113 Self {
114 base: Amount::ZERO,
115 parts_per_million: 0,
116 }
117 }
118
119 pub fn base_fee(&self) -> Amount {
120 self.base
121 }
122
123 pub fn fee(&self, amount: Amount) -> Amount {
124 Amount::from_msats(self.fee_msats(amount.msats))
125 }
126
127 fn fee_msats(&self, msats: u64) -> u64 {
128 msats
129 .saturating_mul(self.parts_per_million)
130 .saturating_div(1_000_000)
131 .checked_add(self.base.msats)
132 .expect("The division creates sufficient headroom to add the base fee")
133 }
134
135 fn from_unvalidated(
136 base: Amount,
137 parts_per_million: u64,
138 ) -> Result<FeeConsensus, &'static str> {
139 const MAX_BASE_FEE: Amount = Amount::from_msats(100);
140 const MAX_PARTS_PER_MILLION: u64 = 1_000;
141
142 if MAX_BASE_FEE < base || MAX_PARTS_PER_MILLION < parts_per_million {
143 return Err(
144 "Mint fees must have a base fee of at most 100 msats and a relative fee of at most one thousand parts per million",
145 );
146 }
147
148 Ok(Self {
149 base,
150 parts_per_million,
151 })
152 }
153}
154
155impl<'de> Deserialize<'de> for FeeConsensus {
156 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
157 where
158 D: serde::Deserializer<'de>,
159 {
160 let unvalidated = UnvalidatedFeeConsensus::deserialize(deserializer)?;
161 Self::from_unvalidated(unvalidated.base, unvalidated.parts_per_million)
162 .map_err(serde::de::Error::custom)
163 }
164}
165
166impl Decodable for FeeConsensus {
167 fn consensus_decode_partial<R: std::io::Read>(
168 reader: &mut R,
169 modules: &fedimint_core::module::registry::ModuleDecoderRegistry,
170 ) -> Result<Self, fedimint_core::encoding::DecodeError> {
171 let base = Amount::consensus_decode_partial(reader, modules)?;
172 let parts_per_million = u64::consensus_decode_partial(reader, modules)?;
173
174 Self::from_unvalidated(base, parts_per_million)
175 .map_err(|error| fedimint_core::encoding::DecodeError::custom(error.to_string()))
176 }
177}
178
179#[test]
180fn test_fee_consensus() {
181 let fee_consensus = FeeConsensus::new(1_000).expect("Relative fee is within range");
182
183 assert_eq!(
184 fee_consensus.fee(Amount::from_msats(999)),
185 Amount::from_msats(100)
186 );
187
188 assert_eq!(
189 fee_consensus.fee(Amount::from_sats(1)),
190 Amount::from_msats(100) + Amount::from_msats(1)
191 );
192
193 assert_eq!(
194 fee_consensus.fee(Amount::from_sats(1000)),
195 Amount::from_sats(1) + Amount::from_msats(100)
196 );
197
198 assert_eq!(
199 fee_consensus.fee(Amount::from_bitcoins(1)),
200 Amount::from_sats(100_000) + Amount::from_msats(100)
201 );
202
203 assert_eq!(
204 fee_consensus.fee(Amount::from_bitcoins(100_000)),
205 Amount::from_bitcoins(100) + Amount::from_msats(100)
206 );
207}
208
209#[test]
210fn a_relative_fee_over_the_limit_is_rejected() {
211 let err: ExcessiveRelativeFeeError =
212 FeeConsensus::new(1_001).expect_err("A fee over one per mille is excessive");
213
214 assert_eq!(err.parts_per_million, 1_001);
215}
216
217#[cfg(test)]
218mod tests;