1use crate::address::RewardAccount;
5use crate::assets::Coin;
6use crate::block::ProtocolVersion;
7use crate::certs::Url;
8use crate::crypto::{AnchorDocHash, Ed25519KeyHash, ScriptHash, TransactionHash};
9use crate::{
10 MapCommitteeColdCredentialToEpoch, MapGovActionIdToVotingProcedure, MapRewardAccountToCoin,
11 ProtocolParamUpdate, SetCommitteeColdCredential, UnitInterval, VoterList,
12};
13pub use cml_chain::governance::Vote;
14use cml_core::ordered_hash_map::OrderedHashMap;
15use cml_core_wasm::{impl_wasm_cbor_json_api, impl_wasm_conversions};
16use wasm_bindgen::prelude::wasm_bindgen;
17
18pub mod utils;
19
20#[derive(Clone, Debug)]
21#[wasm_bindgen]
22pub struct Anchor(cml_chain::governance::Anchor);
23
24impl_wasm_cbor_json_api!(Anchor);
25
26impl_wasm_conversions!(cml_chain::governance::Anchor, Anchor);
27
28#[wasm_bindgen]
29impl Anchor {
30 pub fn anchor_url(&self) -> Url {
31 self.0.anchor_url.clone().into()
32 }
33
34 pub fn anchor_doc_hash(&self) -> AnchorDocHash {
35 self.0.anchor_doc_hash.into()
36 }
37
38 pub fn new(anchor_url: &Url, anchor_doc_hash: &AnchorDocHash) -> Self {
39 Self(cml_chain::governance::Anchor::new(
40 anchor_url.clone().into(),
41 anchor_doc_hash.clone().into(),
42 ))
43 }
44}
45
46#[derive(Clone, Debug)]
47#[wasm_bindgen]
48pub struct Constitution(cml_chain::governance::Constitution);
49
50impl_wasm_cbor_json_api!(Constitution);
51
52impl_wasm_conversions!(cml_chain::governance::Constitution, Constitution);
53
54#[wasm_bindgen]
55impl Constitution {
56 pub fn anchor(&self) -> Anchor {
57 self.0.anchor.clone().into()
58 }
59
60 pub fn script_hash(&self) -> Option<ScriptHash> {
61 self.0.script_hash.map(std::convert::Into::into)
62 }
63
64 pub fn new(anchor: &Anchor, script_hash: Option<ScriptHash>) -> Self {
65 Self(cml_chain::governance::Constitution::new(
66 anchor.clone().into(),
67 script_hash.map(Into::into),
68 ))
69 }
70}
71
72#[derive(Clone, Debug)]
73#[wasm_bindgen]
74pub struct GovAction(cml_chain::governance::GovAction);
75
76impl_wasm_cbor_json_api!(GovAction);
77
78impl_wasm_conversions!(cml_chain::governance::GovAction, GovAction);
79
80#[wasm_bindgen]
81impl GovAction {
82 pub fn new_parameter_change_action(
83 action_id: Option<GovActionId>,
84 update: &ProtocolParamUpdate,
85 policy_hash: Option<ScriptHash>,
86 ) -> Self {
87 Self(
88 cml_chain::governance::GovAction::new_parameter_change_action(
89 action_id.map(Into::into),
90 update.clone().into(),
91 policy_hash.map(Into::into),
92 ),
93 )
94 }
95
96 pub fn new_hard_fork_initiation_action(
97 action_id: Option<GovActionId>,
98 version: &ProtocolVersion,
99 ) -> Self {
100 Self(
101 cml_chain::governance::GovAction::new_hard_fork_initiation_action(
102 action_id.map(Into::into),
103 version.clone().into(),
104 ),
105 )
106 }
107
108 pub fn new_treasury_withdrawals_action(
109 withdrawal: &MapRewardAccountToCoin,
110 policy_hash: Option<ScriptHash>,
111 ) -> Self {
112 Self(
113 cml_chain::governance::GovAction::new_treasury_withdrawals_action(
114 withdrawal.clone().into(),
115 policy_hash.map(Into::into),
116 ),
117 )
118 }
119
120 pub fn new_no_confidence(action_id: Option<GovActionId>) -> Self {
121 Self(cml_chain::governance::GovAction::new_no_confidence(
122 action_id.map(Into::into),
123 ))
124 }
125
126 pub fn new_update_committee(
127 action_id: Option<GovActionId>,
128 cold_credentials: &SetCommitteeColdCredential,
129 credentials: &MapCommitteeColdCredentialToEpoch,
130 unit_interval: &UnitInterval,
131 ) -> Self {
132 Self(cml_chain::governance::GovAction::new_update_committee(
133 action_id.map(Into::into),
134 cold_credentials.clone().into(),
135 credentials.clone().into(),
136 unit_interval.clone().into(),
137 ))
138 }
139
140 pub fn new_new_constitution(
141 action_id: Option<GovActionId>,
142 constitution: &Constitution,
143 ) -> Self {
144 Self(cml_chain::governance::GovAction::new_new_constitution(
145 action_id.map(Into::into),
146 constitution.clone().into(),
147 ))
148 }
149
150 pub fn new_info_action() -> Self {
151 Self(cml_chain::governance::GovAction::new_info_action())
152 }
153
154 pub fn kind(&self) -> GovActionKind {
155 match &self.0 {
156 cml_chain::governance::GovAction::ParameterChangeAction(_) => {
157 GovActionKind::ParameterChangeAction
158 }
159 cml_chain::governance::GovAction::HardForkInitiationAction(_) => {
160 GovActionKind::HardForkInitiationAction
161 }
162 cml_chain::governance::GovAction::TreasuryWithdrawalsAction(_) => {
163 GovActionKind::TreasuryWithdrawalsAction
164 }
165 cml_chain::governance::GovAction::NoConfidence(_) => GovActionKind::NoConfidence,
166 cml_chain::governance::GovAction::UpdateCommittee(_) => GovActionKind::UpdateCommittee,
167 cml_chain::governance::GovAction::NewConstitution(_) => GovActionKind::NewConstitution,
168 cml_chain::governance::GovAction::InfoAction { .. } => GovActionKind::InfoAction,
169 }
170 }
171
172 pub fn as_parameter_change_action(&self) -> Option<ParameterChangeAction> {
173 match &self.0 {
174 cml_chain::governance::GovAction::ParameterChangeAction(parameter_change_action) => {
175 Some(parameter_change_action.clone().into())
176 }
177 _ => None,
178 }
179 }
180
181 pub fn as_hard_fork_initiation_action(&self) -> Option<HardForkInitiationAction> {
182 match &self.0 {
183 cml_chain::governance::GovAction::HardForkInitiationAction(
184 hard_fork_initiation_action,
185 ) => Some(hard_fork_initiation_action.clone().into()),
186 _ => None,
187 }
188 }
189
190 pub fn as_treasury_withdrawals_action(&self) -> Option<TreasuryWithdrawalsAction> {
191 match &self.0 {
192 cml_chain::governance::GovAction::TreasuryWithdrawalsAction(
193 treasury_withdrawals_action,
194 ) => Some(treasury_withdrawals_action.clone().into()),
195 _ => None,
196 }
197 }
198
199 pub fn as_no_confidence(&self) -> Option<NoConfidence> {
200 match &self.0 {
201 cml_chain::governance::GovAction::NoConfidence(no_confidence) => {
202 Some(no_confidence.clone().into())
203 }
204 _ => None,
205 }
206 }
207
208 pub fn as_update_committee(&self) -> Option<UpdateCommittee> {
209 match &self.0 {
210 cml_chain::governance::GovAction::UpdateCommittee(update_committee) => {
211 Some(update_committee.clone().into())
212 }
213 _ => None,
214 }
215 }
216
217 pub fn as_new_constitution(&self) -> Option<NewConstitution> {
218 match &self.0 {
219 cml_chain::governance::GovAction::NewConstitution(new_constitution) => {
220 Some(new_constitution.clone().into())
221 }
222 _ => None,
223 }
224 }
225}
226
227#[derive(Clone, Debug)]
228#[wasm_bindgen]
229pub struct GovActionId(cml_chain::governance::GovActionId);
230
231impl_wasm_cbor_json_api!(GovActionId);
232
233impl_wasm_conversions!(cml_chain::governance::GovActionId, GovActionId);
234
235#[wasm_bindgen]
236impl GovActionId {
237 pub fn transaction_id(&self) -> TransactionHash {
238 self.0.transaction_id.into()
239 }
240
241 pub fn gov_action_index(&self) -> u64 {
242 self.0.gov_action_index
243 }
244
245 pub fn new(transaction_id: &TransactionHash, gov_action_index: u64) -> Self {
246 Self(cml_chain::governance::GovActionId::new(
247 transaction_id.clone().into(),
248 gov_action_index,
249 ))
250 }
251}
252
253#[wasm_bindgen]
254pub enum GovActionKind {
255 ParameterChangeAction,
256 HardForkInitiationAction,
257 TreasuryWithdrawalsAction,
258 NoConfidence,
259 UpdateCommittee,
260 NewConstitution,
261 InfoAction,
262}
263
264#[derive(Clone, Debug)]
265#[wasm_bindgen]
266pub struct HardForkInitiationAction(cml_chain::governance::HardForkInitiationAction);
267
268impl_wasm_cbor_json_api!(HardForkInitiationAction);
269
270impl_wasm_conversions!(
271 cml_chain::governance::HardForkInitiationAction,
272 HardForkInitiationAction
273);
274
275#[wasm_bindgen]
276impl HardForkInitiationAction {
277 pub fn action_id(&self) -> Option<GovActionId> {
278 self.0.action_id.clone().map(std::convert::Into::into)
279 }
280
281 pub fn version(&self) -> ProtocolVersion {
282 self.0.version.clone().into()
283 }
284
285 pub fn new(action_id: Option<GovActionId>, version: &ProtocolVersion) -> Self {
286 Self(cml_chain::governance::HardForkInitiationAction::new(
287 action_id.map(Into::into),
288 version.clone().into(),
289 ))
290 }
291}
292
293#[derive(Clone, Debug)]
294#[wasm_bindgen]
295pub struct NewConstitution(cml_chain::governance::NewConstitution);
296
297impl_wasm_cbor_json_api!(NewConstitution);
298
299impl_wasm_conversions!(cml_chain::governance::NewConstitution, NewConstitution);
300
301#[wasm_bindgen]
302impl NewConstitution {
303 pub fn action_id(&self) -> Option<GovActionId> {
304 self.0.action_id.clone().map(std::convert::Into::into)
305 }
306
307 pub fn constitution(&self) -> Constitution {
308 self.0.constitution.clone().into()
309 }
310
311 pub fn new(action_id: Option<GovActionId>, constitution: &Constitution) -> Self {
312 Self(cml_chain::governance::NewConstitution::new(
313 action_id.map(Into::into),
314 constitution.clone().into(),
315 ))
316 }
317}
318
319#[derive(Clone, Debug)]
320#[wasm_bindgen]
321pub struct NoConfidence(cml_chain::governance::NoConfidence);
322
323impl_wasm_cbor_json_api!(NoConfidence);
324
325impl_wasm_conversions!(cml_chain::governance::NoConfidence, NoConfidence);
326
327#[wasm_bindgen]
328impl NoConfidence {
329 pub fn action_id(&self) -> Option<GovActionId> {
330 self.0.action_id.clone().map(std::convert::Into::into)
331 }
332
333 pub fn new(action_id: Option<GovActionId>) -> Self {
334 Self(cml_chain::governance::NoConfidence::new(
335 action_id.map(Into::into),
336 ))
337 }
338}
339
340#[derive(Clone, Debug)]
341#[wasm_bindgen]
342pub struct ParameterChangeAction(cml_chain::governance::ParameterChangeAction);
343
344impl_wasm_cbor_json_api!(ParameterChangeAction);
345
346impl_wasm_conversions!(
347 cml_chain::governance::ParameterChangeAction,
348 ParameterChangeAction
349);
350
351#[wasm_bindgen]
352impl ParameterChangeAction {
353 pub fn action_id(&self) -> Option<GovActionId> {
354 self.0.action_id.clone().map(std::convert::Into::into)
355 }
356
357 pub fn update(&self) -> ProtocolParamUpdate {
358 self.0.update.clone().into()
359 }
360
361 pub fn policy_hash(&self) -> Option<ScriptHash> {
362 self.0.policy_hash.map(std::convert::Into::into)
363 }
364
365 pub fn new(
366 action_id: Option<GovActionId>,
367 update: &ProtocolParamUpdate,
368 policy_hash: Option<ScriptHash>,
369 ) -> Self {
370 Self(cml_chain::governance::ParameterChangeAction::new(
371 action_id.map(Into::into),
372 update.clone().into(),
373 policy_hash.map(Into::into),
374 ))
375 }
376}
377
378#[derive(Clone, Debug)]
379#[wasm_bindgen]
380pub struct ProposalProcedure(cml_chain::governance::ProposalProcedure);
381
382impl_wasm_cbor_json_api!(ProposalProcedure);
383
384impl_wasm_conversions!(cml_chain::governance::ProposalProcedure, ProposalProcedure);
385
386#[wasm_bindgen]
387impl ProposalProcedure {
388 pub fn deposit(&self) -> Coin {
389 self.0.deposit
390 }
391
392 pub fn reward_account(&self) -> RewardAccount {
393 self.0.reward_account.clone().into()
394 }
395
396 pub fn gov_action(&self) -> GovAction {
397 self.0.gov_action.clone().into()
398 }
399
400 pub fn anchor(&self) -> Anchor {
401 self.0.anchor.clone().into()
402 }
403
404 pub fn new(
405 deposit: Coin,
406 reward_account: &RewardAccount,
407 gov_action: &GovAction,
408 anchor: &Anchor,
409 ) -> Self {
410 Self(cml_chain::governance::ProposalProcedure::new(
411 deposit,
412 reward_account.clone().into(),
413 gov_action.clone().into(),
414 anchor.clone().into(),
415 ))
416 }
417}
418
419#[derive(Clone, Debug)]
420#[wasm_bindgen]
421pub struct TreasuryWithdrawalsAction(cml_chain::governance::TreasuryWithdrawalsAction);
422
423impl_wasm_cbor_json_api!(TreasuryWithdrawalsAction);
424
425impl_wasm_conversions!(
426 cml_chain::governance::TreasuryWithdrawalsAction,
427 TreasuryWithdrawalsAction
428);
429
430#[wasm_bindgen]
431impl TreasuryWithdrawalsAction {
432 pub fn withdrawal(&self) -> MapRewardAccountToCoin {
433 self.0.withdrawal.clone().into()
434 }
435
436 pub fn policy_hash(&self) -> Option<ScriptHash> {
437 self.0.policy_hash.map(std::convert::Into::into)
438 }
439
440 pub fn new(withdrawal: &MapRewardAccountToCoin, policy_hash: Option<ScriptHash>) -> Self {
441 Self(cml_chain::governance::TreasuryWithdrawalsAction::new(
442 withdrawal.clone().into(),
443 policy_hash.map(Into::into),
444 ))
445 }
446}
447
448#[derive(Clone, Debug)]
449#[wasm_bindgen]
450pub struct UpdateCommittee(cml_chain::governance::UpdateCommittee);
451
452impl_wasm_cbor_json_api!(UpdateCommittee);
453
454impl_wasm_conversions!(cml_chain::governance::UpdateCommittee, UpdateCommittee);
455
456#[wasm_bindgen]
457impl UpdateCommittee {
458 pub fn action_id(&self) -> Option<GovActionId> {
459 self.0.action_id.clone().map(std::convert::Into::into)
460 }
461
462 pub fn cold_credentials(&self) -> SetCommitteeColdCredential {
463 self.0.cold_credentials.clone().into()
464 }
465
466 pub fn credentials(&self) -> MapCommitteeColdCredentialToEpoch {
467 self.0.credentials.clone().into()
468 }
469
470 pub fn unit_interval(&self) -> UnitInterval {
471 self.0.unit_interval.clone().into()
472 }
473
474 pub fn new(
475 action_id: Option<GovActionId>,
476 cold_credentials: &SetCommitteeColdCredential,
477 credentials: &MapCommitteeColdCredentialToEpoch,
478 unit_interval: &UnitInterval,
479 ) -> Self {
480 Self(cml_chain::governance::UpdateCommittee::new(
481 action_id.map(Into::into),
482 cold_credentials.clone().into(),
483 credentials.clone().into(),
484 unit_interval.clone().into(),
485 ))
486 }
487}
488
489#[derive(Clone, Debug)]
490#[wasm_bindgen]
491pub struct Voter(cml_chain::governance::Voter);
492
493impl_wasm_cbor_json_api!(Voter);
494
495impl_wasm_conversions!(cml_chain::governance::Voter, Voter);
496
497#[wasm_bindgen]
498impl Voter {
499 pub fn new_constitutional_committee_hot_key_hash(ed25519_key_hash: &Ed25519KeyHash) -> Self {
500 Self(
501 cml_chain::governance::Voter::new_constitutional_committee_hot_key_hash(
502 ed25519_key_hash.clone().into(),
503 ),
504 )
505 }
506
507 pub fn new_constitutional_committee_hot_script_hash(script_hash: &ScriptHash) -> Self {
508 Self(
509 cml_chain::governance::Voter::new_constitutional_committee_hot_script_hash(
510 script_hash.clone().into(),
511 ),
512 )
513 }
514
515 pub fn new_d_rep_key_hash(ed25519_key_hash: &Ed25519KeyHash) -> Self {
516 Self(cml_chain::governance::Voter::new_d_rep_key_hash(
517 ed25519_key_hash.clone().into(),
518 ))
519 }
520
521 pub fn new_d_rep_script_hash(script_hash: &ScriptHash) -> Self {
522 Self(cml_chain::governance::Voter::new_d_rep_script_hash(
523 script_hash.clone().into(),
524 ))
525 }
526
527 pub fn new_staking_pool_key_hash(ed25519_key_hash: &Ed25519KeyHash) -> Self {
528 Self(cml_chain::governance::Voter::new_staking_pool_key_hash(
529 ed25519_key_hash.clone().into(),
530 ))
531 }
532
533 pub fn kind(&self) -> VoterKind {
534 match &self.0 {
535 cml_chain::governance::Voter::ConstitutionalCommitteeHotKeyHash { .. } => {
536 VoterKind::ConstitutionalCommitteeHotKeyHash
537 }
538 cml_chain::governance::Voter::ConstitutionalCommitteeHotScriptHash { .. } => {
539 VoterKind::ConstitutionalCommitteeHotScriptHash
540 }
541 cml_chain::governance::Voter::DRepKeyHash { .. } => VoterKind::DRepKeyHash,
542 cml_chain::governance::Voter::DRepScriptHash { .. } => VoterKind::DRepScriptHash,
543 cml_chain::governance::Voter::StakingPoolKeyHash { .. } => {
544 VoterKind::StakingPoolKeyHash
545 }
546 }
547 }
548
549 pub fn as_constitutional_committee_hot_key_hash(&self) -> Option<Ed25519KeyHash> {
550 match &self.0 {
551 cml_chain::governance::Voter::ConstitutionalCommitteeHotKeyHash {
552 ed25519_key_hash,
553 ..
554 } => Some((*ed25519_key_hash).into()),
555 _ => None,
556 }
557 }
558
559 pub fn as_constitutional_committee_hot_script_hash(&self) -> Option<ScriptHash> {
560 match &self.0 {
561 cml_chain::governance::Voter::ConstitutionalCommitteeHotScriptHash {
562 script_hash,
563 ..
564 } => Some((*script_hash).into()),
565 _ => None,
566 }
567 }
568
569 pub fn as_d_rep_key_hash(&self) -> Option<Ed25519KeyHash> {
570 match &self.0 {
571 cml_chain::governance::Voter::DRepKeyHash {
572 ed25519_key_hash, ..
573 } => Some((*ed25519_key_hash).into()),
574 _ => None,
575 }
576 }
577
578 pub fn as_d_rep_script_hash(&self) -> Option<ScriptHash> {
579 match &self.0 {
580 cml_chain::governance::Voter::DRepScriptHash { script_hash, .. } => {
581 Some((*script_hash).into())
582 }
583 _ => None,
584 }
585 }
586
587 pub fn as_staking_pool_key_hash(&self) -> Option<Ed25519KeyHash> {
588 match &self.0 {
589 cml_chain::governance::Voter::StakingPoolKeyHash {
590 ed25519_key_hash, ..
591 } => Some((*ed25519_key_hash).into()),
592 _ => None,
593 }
594 }
595}
596
597#[wasm_bindgen]
598pub enum VoterKind {
599 ConstitutionalCommitteeHotKeyHash,
600 ConstitutionalCommitteeHotScriptHash,
601 DRepKeyHash,
602 DRepScriptHash,
603 StakingPoolKeyHash,
604}
605
606#[derive(Clone, Debug)]
607#[wasm_bindgen]
608pub struct VotingProcedure(cml_chain::governance::VotingProcedure);
609
610impl_wasm_cbor_json_api!(VotingProcedure);
611
612impl_wasm_conversions!(cml_chain::governance::VotingProcedure, VotingProcedure);
613
614#[wasm_bindgen]
615impl VotingProcedure {
616 pub fn vote(&self) -> Vote {
617 self.0.vote
618 }
619
620 pub fn anchor(&self) -> Option<Anchor> {
621 self.0.anchor.clone().map(std::convert::Into::into)
622 }
623
624 pub fn new(vote: Vote, anchor: Option<Anchor>) -> Self {
625 Self(cml_chain::governance::VotingProcedure::new(
626 vote,
627 anchor.map(Into::into),
628 ))
629 }
630}
631
632#[derive(Clone, Debug)]
633#[wasm_bindgen]
634pub struct VotingProcedures(cml_chain::governance::VotingProcedures);
635
636impl_wasm_conversions!(cml_chain::governance::VotingProcedures, VotingProcedures);
637
638#[wasm_bindgen]
639impl VotingProcedures {
640 pub fn new() -> Self {
641 Self(OrderedHashMap::new())
642 }
643
644 pub fn len(&self) -> usize {
645 self.0.len()
646 }
647
648 pub fn insert(
649 &mut self,
650 key: &Voter,
651 value: &MapGovActionIdToVotingProcedure,
652 ) -> Option<MapGovActionIdToVotingProcedure> {
653 self.0
654 .insert(key.clone().into(), value.clone().into())
655 .map(Into::into)
656 }
657
658 pub fn get(&self, key: &Voter) -> Option<MapGovActionIdToVotingProcedure> {
659 self.0.get(key.as_ref()).map(|v| v.clone().into())
660 }
661
662 pub fn keys(&self) -> VoterList {
663 self.0
664 .iter()
665 .map(|(k, _v)| k.clone())
666 .collect::<Vec<_>>()
667 .into()
668 }
669}