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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use crate::wallet_state::MainnetWalletState;
use crate::CardanoWallet;

/// Wallet state builder for Network state builder is a trait which creates nice interface for
/// defining role of particular mainnet wallet in voting round. Wallet can be a direct voter/ delegator
/// or representative
pub trait MainnetWalletStateBuilder {
    /// wallet registered as representative. This is simplification and wallet catalyst key is
    /// added to in memory list instead of going through public representative registration process
    fn as_representative(&self) -> MainnetWalletState;

    /// wallet registers as direct voter, meaning it will send self-delegation registration
    fn as_direct_voter(&self) -> MainnetWalletState;
    /// wallet registers as direct voter, meaning it will send self-delegation registration with
    /// given nonce = `slot_no`
    fn as_direct_voter_on_slot_no(&self, slot_no: u64) -> MainnetWalletState;
    /// wallet registers as delegator, meaning it will send delegation registration
    fn as_delegator(&self, delegations: Vec<(&CardanoWallet, u8)>) -> MainnetWalletState;
    /// wallet registers as delegator, meaning it will send delegation registration with
    /// given nonce = `slot_no`
    fn as_delegator_on_slot_no(
        &self,
        delegations: Vec<(&CardanoWallet, u8)>,
        slot_no: u64,
    ) -> MainnetWalletState;
}

impl MainnetWalletStateBuilder for CardanoWallet {
    fn as_representative(&self) -> MainnetWalletState {
        MainnetWalletState {
            rep: Some(self.catalyst_public_key()),
            registration_tx: None,
            stake: self.stake(),
            stake_address: self.reward_address().to_address(),
        }
    }

    fn as_direct_voter(&self) -> MainnetWalletState {
        self.as_direct_voter_on_slot_no(0)
    }

    fn as_direct_voter_on_slot_no(&self, slot_no: u64) -> MainnetWalletState {
        MainnetWalletState {
            rep: None,
            registration_tx: Some(self.generate_direct_voting_registration(slot_no)),
            stake: self.stake(),
            stake_address: self.reward_address().to_address(),
        }
    }

    fn as_delegator(&self, delegations: Vec<(&CardanoWallet, u8)>) -> MainnetWalletState {
        self.as_delegator_on_slot_no(delegations, 0)
    }

    fn as_delegator_on_slot_no(
        &self,
        delegations: Vec<(&CardanoWallet, u8)>,
        slot_no: u64,
    ) -> MainnetWalletState {
        MainnetWalletState {
            rep: None,
            registration_tx: Some(
                self.generate_delegated_voting_registration(
                    delegations
                        .into_iter()
                        .map(|(wallet, weight)| (wallet.catalyst_public_key(), u32::from(weight)))
                        .collect(),
                    slot_no,
                ),
            ),
            stake: self.stake(),
            stake_address: self.reward_address().to_address(),
        }
    }
}