whisky_csl/utils/
vote.rs

1use cardano_serialization_lib::{self as csl};
2
3use whisky_common::*;
4
5pub fn to_csl_voter(voter: Voter) -> Result<csl::Voter, WError> {
6    match voter {
7        Voter::ConstitutionalCommitteeHotCred(cred) => match cred {
8            Credential::KeyHash(key_hash) => {
9                Ok(csl::Voter::new_constitutional_committee_hot_credential(
10                    &csl::Credential::from_keyhash(
11                        &csl::Ed25519KeyHash::from_hex(&key_hash)
12                            .map_err(WError::from_err("to_csl_voter - invalid key hash"))?,
13                    ),
14                ))
15            }
16            Credential::ScriptHash(script_hash) => {
17                Ok(csl::Voter::new_constitutional_committee_hot_credential(
18                    &csl::Credential::from_scripthash(
19                        &csl::ScriptHash::from_hex(&script_hash)
20                            .map_err(WError::from_err("to_csl_voter - invalid script hash"))?,
21                    ),
22                ))
23            }
24        },
25        Voter::DRepId(drep_id) => {
26            let drep = csl::DRep::from_bech32(&drep_id).unwrap();
27            let drep_credential = if drep.to_script_hash().is_some() {
28                csl::Credential::from_scripthash(&drep.to_script_hash().unwrap())
29            } else if drep.to_key_hash().is_some() {
30                csl::Credential::from_keyhash(&drep.to_key_hash().unwrap())
31            } else {
32                return Err(WError::new(
33                    "to_csl_voter - invalid DRepId",
34                    "Error occured when deserializing DrepId to either script hash or key hash",
35                ));
36            };
37            Ok(csl::Voter::new_drep_credential(&drep_credential))
38        }
39        Voter::StakingPoolKeyHash(key_hash) => Ok(csl::Voter::new_stake_pool_key_hash(
40            &csl::Ed25519KeyHash::from_hex(&key_hash)
41                .map_err(WError::from_err("to_csl_voter - invalid key hash"))?,
42        )),
43    }
44}
45
46pub fn to_csl_vote_kind(vote_kind: VoteKind) -> csl::VoteKind {
47    match vote_kind {
48        VoteKind::No => csl::VoteKind::No,
49        VoteKind::Yes => csl::VoteKind::Yes,
50        VoteKind::Abstain => csl::VoteKind::Abstain,
51    }
52}