1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use super::Actor;
use crate::wallet_state::MainnetWalletState;
use crate::CardanoWallet;
use jormungandr_lib::crypto::account::Identifier;

pub fn generate_arbitrary_representative(actor_def: &Actor) -> (String, CardanoWallet) {
    if let Actor::GeneratedRep { name, ada } = actor_def {
        (name.clone(), CardanoWallet::new(*ada))
    } else {
        panic!("internal error: expected generated rep for");
    }
}

pub fn generate_arbitrary_delegator(
    actor_def: &Actor,
    voting_keys: &[(&String, Identifier)],
) -> Result<MainnetWalletState, Error> {
    if let Actor::GeneratedDelegator {
        registration, ada, ..
    } = actor_def
    {
        let delegations: Result<Vec<_>, Error> = registration
            .target
            .iter()
            .map(|(rep, weight)| {
                if let Some(identifier) = voting_keys
                    .iter()
                    .find_map(|(name, key)| (rep == *name).then_some(key))
                {
                    Ok((identifier.clone(), *weight))
                } else {
                    Ok(Identifier::from_hex(rep)
                        .map(|identifier| (identifier, *weight))
                        .map_err(|_| Error::CannotGetVotingKey)?)
                }
            })
            .collect();

        let wallet = CardanoWallet::new(*ada);

        Ok(MainnetWalletState {
            rep: None,
            registration_tx: Some(wallet.generate_delegated_voting_registration(delegations?, 0)),
            stake: wallet.stake(),
            stake_address: wallet.reward_address().to_address(),
        })
    } else {
        Err(Error::Internal("expected generated delegator".to_string()))
    }
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("internal error: {0}")]
    Internal(String),
    #[error("cannot convert voting key")]
    CannotGetVotingKey,
}