1pub mod cbor_encodings;
5pub mod serialization;
6pub mod utils;
7
8#[cfg(not(feature = "used_from_wasm"))]
9use noop_proc_macro::wasm_bindgen;
10#[cfg(feature = "used_from_wasm")]
11use wasm_bindgen::prelude::wasm_bindgen;
12
13use crate::address::RewardAccount;
14use crate::assets::Coin;
15use crate::block::ProtocolVersion;
16use crate::certs::{CommitteeColdCredential, Url};
17use crate::crypto::{AnchorDocHash, Ed25519KeyHash, ScriptHash, TransactionHash};
18use crate::{Epoch, ProtocolParamUpdate, SetCommitteeColdCredential, UnitInterval};
19use cbor_encodings::{
20 AnchorEncoding, ConstitutionEncoding, GovActionIdEncoding, HardForkInitiationActionEncoding,
21 NewConstitutionEncoding, NoConfidenceEncoding, ParameterChangeActionEncoding,
22 ProposalProcedureEncoding, TreasuryWithdrawalsActionEncoding, UpdateCommitteeEncoding,
23 VotingProcedureEncoding,
24};
25
26use cml_core::ordered_hash_map::OrderedHashMap;
27use cml_core::serialization::{LenEncoding, StringEncoding};
28use std::collections::BTreeMap;
29
30#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
31pub struct Anchor {
32 pub anchor_url: Url,
33 pub anchor_doc_hash: AnchorDocHash,
34 #[serde(skip)]
35 pub encodings: Option<AnchorEncoding>,
36}
37
38impl Anchor {
39 pub fn new(anchor_url: Url, anchor_doc_hash: AnchorDocHash) -> Self {
40 Self {
41 anchor_url,
42 anchor_doc_hash,
43 encodings: None,
44 }
45 }
46}
47
48#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
49pub struct Constitution {
50 pub anchor: Anchor,
51 pub script_hash: Option<ScriptHash>,
52 #[serde(skip)]
53 pub encodings: Option<ConstitutionEncoding>,
54}
55
56impl Constitution {
57 pub fn new(anchor: Anchor, script_hash: Option<ScriptHash>) -> Self {
58 Self {
59 anchor,
60 script_hash,
61 encodings: None,
62 }
63 }
64}
65
66#[allow(clippy::large_enum_variant)]
67#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
68pub enum GovAction {
69 ParameterChangeAction(ParameterChangeAction),
70 HardForkInitiationAction(HardForkInitiationAction),
71 TreasuryWithdrawalsAction(TreasuryWithdrawalsAction),
72 NoConfidence(NoConfidence),
73 UpdateCommittee(UpdateCommittee),
74 NewConstitution(NewConstitution),
75 InfoAction {
76 #[serde(skip)]
77 info_action_encoding: Option<cbor_event::Sz>,
78 #[serde(skip)]
79 len_encoding: LenEncoding,
80 },
81}
82
83impl GovAction {
84 pub fn new_parameter_change_action(
85 action_id: Option<GovActionId>,
86 update: ProtocolParamUpdate,
87 policy_hash: Option<ScriptHash>,
88 ) -> Self {
89 Self::ParameterChangeAction(ParameterChangeAction::new(action_id, update, policy_hash))
90 }
91
92 pub fn new_hard_fork_initiation_action(
93 action_id: Option<GovActionId>,
94 version: ProtocolVersion,
95 ) -> Self {
96 Self::HardForkInitiationAction(HardForkInitiationAction::new(action_id, version))
97 }
98
99 pub fn new_treasury_withdrawals_action(
100 withdrawal: OrderedHashMap<RewardAccount, Coin>,
101 policy_hash: Option<ScriptHash>,
102 ) -> Self {
103 Self::TreasuryWithdrawalsAction(TreasuryWithdrawalsAction::new(withdrawal, policy_hash))
104 }
105
106 pub fn new_no_confidence(action_id: Option<GovActionId>) -> Self {
107 Self::NoConfidence(NoConfidence::new(action_id))
108 }
109
110 pub fn new_update_committee(
111 action_id: Option<GovActionId>,
112 cold_credentials: SetCommitteeColdCredential,
113 credentials: OrderedHashMap<CommitteeColdCredential, Epoch>,
114 unit_interval: UnitInterval,
115 ) -> Self {
116 Self::UpdateCommittee(UpdateCommittee::new(
117 action_id,
118 cold_credentials,
119 credentials,
120 unit_interval,
121 ))
122 }
123
124 pub fn new_new_constitution(
125 action_id: Option<GovActionId>,
126 constitution: Constitution,
127 ) -> Self {
128 Self::NewConstitution(NewConstitution::new(action_id, constitution))
129 }
130
131 pub fn new_info_action() -> Self {
132 Self::InfoAction {
133 info_action_encoding: None,
134 len_encoding: LenEncoding::default(),
135 }
136 }
137}
138
139#[derive(
140 Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema, derivative::Derivative,
141)]
142#[derivative(Eq, PartialEq, Ord, PartialOrd, Hash)]
143pub struct GovActionId {
144 pub transaction_id: TransactionHash,
145 pub gov_action_index: u64,
146 #[derivative(
147 PartialEq = "ignore",
148 Ord = "ignore",
149 PartialOrd = "ignore",
150 Hash = "ignore"
151 )]
152 #[serde(skip)]
153 pub encodings: Option<GovActionIdEncoding>,
154}
155
156impl GovActionId {
157 pub fn new(transaction_id: TransactionHash, gov_action_index: u64) -> Self {
158 Self {
159 transaction_id,
160 gov_action_index,
161 encodings: None,
162 }
163 }
164}
165
166#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
167pub struct HardForkInitiationAction {
168 pub action_id: Option<GovActionId>,
169 pub version: ProtocolVersion,
170 #[serde(skip)]
171 pub encodings: Option<HardForkInitiationActionEncoding>,
172}
173
174impl HardForkInitiationAction {
175 pub fn new(action_id: Option<GovActionId>, version: ProtocolVersion) -> Self {
176 Self {
177 action_id,
178 version,
179 encodings: None,
180 }
181 }
182}
183
184#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
185pub struct NewConstitution {
186 pub action_id: Option<GovActionId>,
187 pub constitution: Constitution,
188 #[serde(skip)]
189 pub encodings: Option<NewConstitutionEncoding>,
190}
191
192impl NewConstitution {
193 pub fn new(action_id: Option<GovActionId>, constitution: Constitution) -> Self {
194 Self {
195 action_id,
196 constitution,
197 encodings: None,
198 }
199 }
200}
201
202#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
203pub struct NoConfidence {
204 pub action_id: Option<GovActionId>,
205 #[serde(skip)]
206 pub encodings: Option<NoConfidenceEncoding>,
207}
208
209impl NoConfidence {
210 pub fn new(action_id: Option<GovActionId>) -> Self {
211 Self {
212 action_id,
213 encodings: None,
214 }
215 }
216}
217
218#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
219pub struct ParameterChangeAction {
220 pub action_id: Option<GovActionId>,
221 pub update: ProtocolParamUpdate,
222 pub policy_hash: Option<ScriptHash>,
223 #[serde(skip)]
224 pub encodings: Option<ParameterChangeActionEncoding>,
225}
226
227impl ParameterChangeAction {
228 pub fn new(
229 action_id: Option<GovActionId>,
230 update: ProtocolParamUpdate,
231 policy_hash: Option<ScriptHash>,
232 ) -> Self {
233 Self {
234 action_id,
235 update,
236 policy_hash,
237 encodings: None,
238 }
239 }
240}
241
242#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
243pub struct ProposalProcedure {
244 pub deposit: Coin,
245 pub reward_account: RewardAccount,
246 pub gov_action: GovAction,
247 pub anchor: Anchor,
248 #[serde(skip)]
249 pub encodings: Option<ProposalProcedureEncoding>,
250}
251
252impl ProposalProcedure {
253 pub fn new(
254 deposit: Coin,
255 reward_account: RewardAccount,
256 gov_action: GovAction,
257 anchor: Anchor,
258 ) -> Self {
259 Self {
260 deposit,
261 reward_account,
262 gov_action,
263 anchor,
264 encodings: None,
265 }
266 }
267}
268
269#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
270pub struct TreasuryWithdrawalsAction {
271 pub withdrawal: OrderedHashMap<RewardAccount, Coin>,
272 pub policy_hash: Option<ScriptHash>,
273 #[serde(skip)]
274 pub encodings: Option<TreasuryWithdrawalsActionEncoding>,
275}
276
277impl TreasuryWithdrawalsAction {
278 pub fn new(
279 withdrawal: OrderedHashMap<RewardAccount, Coin>,
280 policy_hash: Option<ScriptHash>,
281 ) -> Self {
282 Self {
283 withdrawal,
284 policy_hash,
285 encodings: None,
286 }
287 }
288}
289
290#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
291pub struct UpdateCommittee {
292 pub action_id: Option<GovActionId>,
293 pub cold_credentials: SetCommitteeColdCredential,
294 pub credentials: OrderedHashMap<CommitteeColdCredential, Epoch>,
295 pub unit_interval: UnitInterval,
296 #[serde(skip)]
297 pub encodings: Option<UpdateCommitteeEncoding>,
298}
299
300impl UpdateCommittee {
301 pub fn new(
302 action_id: Option<GovActionId>,
303 cold_credentials: SetCommitteeColdCredential,
304 credentials: OrderedHashMap<CommitteeColdCredential, Epoch>,
305 unit_interval: UnitInterval,
306 ) -> Self {
307 Self {
308 action_id,
309 cold_credentials,
310 credentials,
311 unit_interval,
312 encodings: None,
313 }
314 }
315}
316
317#[derive(
318 Copy,
319 Eq,
320 PartialEq,
321 Ord,
322 PartialOrd,
323 Clone,
324 Debug,
325 serde::Deserialize,
326 serde::Serialize,
327 schemars::JsonSchema,
328)]
329#[wasm_bindgen]
330pub enum Vote {
331 No,
332 Yes,
333 Abstain,
334}
335
336#[derive(
337 Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema, derivative::Derivative,
338)]
339#[derivative(
340 Eq,
341 PartialEq,
342 Ord = "feature_allow_slow_enum",
343 PartialOrd = "feature_allow_slow_enum",
344 Hash
345)]
346pub enum Voter {
347 ConstitutionalCommitteeHotKeyHash {
348 ed25519_key_hash: Ed25519KeyHash,
349 #[derivative(
350 PartialEq = "ignore",
351 Ord = "ignore",
352 PartialOrd = "ignore",
353 Hash = "ignore"
354 )]
355 #[serde(skip)]
356 len_encoding: LenEncoding,
357 #[derivative(
358 PartialEq = "ignore",
359 Ord = "ignore",
360 PartialOrd = "ignore",
361 Hash = "ignore"
362 )]
363 #[serde(skip)]
364 index_0_encoding: Option<cbor_event::Sz>,
365 #[derivative(
366 PartialEq = "ignore",
367 Ord = "ignore",
368 PartialOrd = "ignore",
369 Hash = "ignore"
370 )]
371 #[serde(skip)]
372 ed25519_key_hash_encoding: StringEncoding,
373 },
374 ConstitutionalCommitteeHotScriptHash {
375 script_hash: ScriptHash,
376 #[derivative(
377 PartialEq = "ignore",
378 Ord = "ignore",
379 PartialOrd = "ignore",
380 Hash = "ignore"
381 )]
382 #[serde(skip)]
383 len_encoding: LenEncoding,
384 #[derivative(
385 PartialEq = "ignore",
386 Ord = "ignore",
387 PartialOrd = "ignore",
388 Hash = "ignore"
389 )]
390 #[serde(skip)]
391 index_0_encoding: Option<cbor_event::Sz>,
392 #[derivative(
393 PartialEq = "ignore",
394 Ord = "ignore",
395 PartialOrd = "ignore",
396 Hash = "ignore"
397 )]
398 #[serde(skip)]
399 script_hash_encoding: StringEncoding,
400 },
401 DRepKeyHash {
402 ed25519_key_hash: Ed25519KeyHash,
403 #[derivative(
404 PartialEq = "ignore",
405 Ord = "ignore",
406 PartialOrd = "ignore",
407 Hash = "ignore"
408 )]
409 #[serde(skip)]
410 len_encoding: LenEncoding,
411 #[derivative(
412 PartialEq = "ignore",
413 Ord = "ignore",
414 PartialOrd = "ignore",
415 Hash = "ignore"
416 )]
417 #[serde(skip)]
418 index_0_encoding: Option<cbor_event::Sz>,
419 #[derivative(
420 PartialEq = "ignore",
421 Ord = "ignore",
422 PartialOrd = "ignore",
423 Hash = "ignore"
424 )]
425 #[serde(skip)]
426 ed25519_key_hash_encoding: StringEncoding,
427 },
428 DRepScriptHash {
429 script_hash: ScriptHash,
430 #[derivative(
431 PartialEq = "ignore",
432 Ord = "ignore",
433 PartialOrd = "ignore",
434 Hash = "ignore"
435 )]
436 #[serde(skip)]
437 len_encoding: LenEncoding,
438 #[derivative(
439 PartialEq = "ignore",
440 Ord = "ignore",
441 PartialOrd = "ignore",
442 Hash = "ignore"
443 )]
444 #[serde(skip)]
445 index_0_encoding: Option<cbor_event::Sz>,
446 #[derivative(
447 PartialEq = "ignore",
448 Ord = "ignore",
449 PartialOrd = "ignore",
450 Hash = "ignore"
451 )]
452 #[serde(skip)]
453 script_hash_encoding: StringEncoding,
454 },
455 StakingPoolKeyHash {
456 ed25519_key_hash: Ed25519KeyHash,
457 #[derivative(
458 PartialEq = "ignore",
459 Ord = "ignore",
460 PartialOrd = "ignore",
461 Hash = "ignore"
462 )]
463 #[serde(skip)]
464 len_encoding: LenEncoding,
465 #[derivative(
466 PartialEq = "ignore",
467 Ord = "ignore",
468 PartialOrd = "ignore",
469 Hash = "ignore"
470 )]
471 #[serde(skip)]
472 index_0_encoding: Option<cbor_event::Sz>,
473 #[derivative(
474 PartialEq = "ignore",
475 Ord = "ignore",
476 PartialOrd = "ignore",
477 Hash = "ignore"
478 )]
479 #[serde(skip)]
480 ed25519_key_hash_encoding: StringEncoding,
481 },
482}
483
484impl Voter {
485 pub fn new_constitutional_committee_hot_key_hash(ed25519_key_hash: Ed25519KeyHash) -> Self {
486 Self::ConstitutionalCommitteeHotKeyHash {
487 ed25519_key_hash,
488 len_encoding: LenEncoding::default(),
489 index_0_encoding: None,
490 ed25519_key_hash_encoding: StringEncoding::default(),
491 }
492 }
493
494 pub fn new_constitutional_committee_hot_script_hash(script_hash: ScriptHash) -> Self {
495 Self::ConstitutionalCommitteeHotScriptHash {
496 script_hash,
497 len_encoding: LenEncoding::default(),
498 index_0_encoding: None,
499 script_hash_encoding: StringEncoding::default(),
500 }
501 }
502
503 pub fn new_d_rep_key_hash(ed25519_key_hash: Ed25519KeyHash) -> Self {
504 Self::DRepKeyHash {
505 ed25519_key_hash,
506 len_encoding: LenEncoding::default(),
507 index_0_encoding: None,
508 ed25519_key_hash_encoding: StringEncoding::default(),
509 }
510 }
511
512 pub fn new_d_rep_script_hash(script_hash: ScriptHash) -> Self {
513 Self::DRepScriptHash {
514 script_hash,
515 len_encoding: LenEncoding::default(),
516 index_0_encoding: None,
517 script_hash_encoding: StringEncoding::default(),
518 }
519 }
520
521 pub fn new_staking_pool_key_hash(ed25519_key_hash: Ed25519KeyHash) -> Self {
522 Self::StakingPoolKeyHash {
523 ed25519_key_hash,
524 len_encoding: LenEncoding::default(),
525 index_0_encoding: None,
526 ed25519_key_hash_encoding: StringEncoding::default(),
527 }
528 }
529}
530
531#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
532pub struct VotingProcedure {
533 pub vote: Vote,
534 pub anchor: Option<Anchor>,
535 #[serde(skip)]
536 pub encodings: Option<VotingProcedureEncoding>,
537}
538
539impl VotingProcedure {
540 pub fn new(vote: Vote, anchor: Option<Anchor>) -> Self {
541 Self {
542 vote,
543 anchor,
544 encodings: None,
545 }
546 }
547}
548
549pub type VotingProcedures = OrderedHashMap<Voter, OrderedHashMap<GovActionId, VotingProcedure>>;