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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
mod builders;

use crate::key::EitherEd25519SecretKey;
use crate::ledger::governance::{ParametersGovernanceAction, TreasuryGovernanceAction};
use crate::testing::data::AddressData;
use crate::testing::data::AddressDataValue;
use crate::tokens::identifier::TokenIdentifier;
use crate::tokens::name::TokenName;
use crate::{
    certificate::{ExternalProposalId, PoolPermissions, Proposal, Proposals, VoteAction, VotePlan},
    header::BlockDate,
    rewards::TaxType,
    testing::data::Wallet,
    value::Value,
    vote::{Options, PayloadType},
};
pub use builders::*;
use chain_addr::{Address, Discrimination, Kind};
use chain_crypto::{Ed25519, PublicKey};
use chain_vote::MemberPublicKey;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};

#[derive(Clone)]
pub struct WalletTemplate {
    pub alias: String,
    pub stake_pool_delegate_alias: Option<String>,
    pub stake_pool_owner_alias: Option<String>,
    pub secret_key: Option<EitherEd25519SecretKey>,
    pub initial_value: Value,
    pub initial_tokens: HashMap<TokenName, Value>,
    pub committee_member: bool,
}

impl PartialEq for WalletTemplate {
    fn eq(&self, other: &WalletTemplate) -> bool {
        self.alias == other.alias
    }
}

impl Hash for WalletTemplate {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.alias.hash(state)
    }
}

impl Eq for WalletTemplate {}

impl WalletTemplate {
    pub fn new(
        alias: &str,
        initial_value: Value,
        initial_tokens: HashMap<TokenName, Value>,
    ) -> Self {
        WalletTemplate {
            alias: alias.to_owned(),
            stake_pool_delegate_alias: None,
            stake_pool_owner_alias: None,
            initial_value,
            initial_tokens,
            committee_member: false,
            secret_key: None,
        }
    }

    pub fn delegates_stake_pool(&self) -> Option<String> {
        self.stake_pool_delegate_alias.clone()
    }

    pub fn owns_stake_pool(&self) -> Option<String> {
        self.stake_pool_owner_alias.clone()
    }

    pub fn secret_key(&self) -> Option<EitherEd25519SecretKey> {
        self.secret_key.clone()
    }
    pub fn is_committee_member(&self) -> bool {
        self.committee_member
    }

    pub fn alias(&self) -> String {
        self.alias.clone()
    }
}

impl From<WalletTemplate> for Wallet {
    fn from(template: WalletTemplate) -> Self {
        if let Some(secret_key) = template.secret_key() {
            let user_address = Address(Discrimination::Test, Kind::Account(secret_key.to_public()));
            let account = AddressDataValue::new_with_tokens(
                AddressData::new(secret_key, Default::default(), user_address),
                template.initial_value,
                template.initial_tokens.clone(),
            );
            Self::from_address_data_value_and_alias(template.alias(), account)
        } else {
            Self::new_with_tokens(
                &template.alias(),
                template.initial_value,
                template.initial_tokens,
            )
        }
    }
}

#[derive(Clone, Debug)]
pub struct StakePoolTemplate {
    pub alias: String,
    pub owners: Vec<PublicKey<Ed25519>>,
}

impl StakePoolTemplate {
    pub fn alias(&self) -> String {
        self.alias.clone()
    }

    pub fn owners(&self) -> Vec<PublicKey<Ed25519>> {
        self.owners.clone()
    }
}

#[derive(Clone, Debug)]
pub struct StakePoolDef {
    pub alias: String,
    pub permissions_threshold: Option<u8>,
    pub has_reward_account: bool,
    pub tax_type: Option<TaxType>,
}

impl StakePoolDef {
    pub fn pool_permission(&self) -> Option<PoolPermissions> {
        self.permissions_threshold.map(PoolPermissions::new)
    }
}

#[derive(Clone, Debug)]
pub struct VotePlanDef {
    alias: String,
    owner_alias: String,
    payload_type: PayloadType,
    vote_date: BlockDate,
    tally_date: BlockDate,
    end_tally_date: BlockDate,
    committee_keys: Vec<MemberPublicKey>,
    proposals: Vec<ProposalDef>,
    voting_token: TokenIdentifier,
}

impl VotePlanDef {
    pub fn alias(&self) -> String {
        self.alias.clone()
    }

    pub fn owner(&self) -> String {
        self.owner_alias.clone()
    }

    pub fn proposals(&self) -> Vec<ProposalDef> {
        self.proposals.iter().cloned().map(Into::into).collect()
    }

    pub fn proposal(&self, index: usize) -> ProposalDef {
        self.proposals.get(index).unwrap().clone()
    }

    pub fn id(&self) -> String {
        let vote_plan: VotePlan = self.clone().into();
        vote_plan.to_id().to_string()
    }

    pub fn committee_keys(&self) -> Vec<MemberPublicKey> {
        self.committee_keys.clone()
    }

    pub fn committee_keys_mut(&mut self) -> &mut Vec<MemberPublicKey> {
        &mut self.committee_keys
    }

    pub fn from_vote_plan<S: Into<String>>(
        alias: S,
        owner_alias: Option<S>,
        vote_plan: &VotePlan,
    ) -> VotePlanDef {
        let mut builder = VotePlanDefBuilder::new(&alias.into());

        if let Some(owner_alias) = owner_alias {
            builder.owner(&owner_alias.into());
        }

        builder
            .payload_type(vote_plan.payload_type())
            .committee_keys(vote_plan.committee_public_keys().to_vec())
            .vote_phases(
                vote_plan.vote_start().epoch,
                vote_plan.committee_start().epoch,
                vote_plan.committee_end().epoch,
            );

        for proposal in vote_plan.proposals().iter() {
            let mut proposal_builder = ProposalDefBuilder::new(proposal.external_id().clone());

            let length = proposal
                .options()
                .choice_range()
                .end
                .checked_sub(proposal.options().choice_range().start)
                .unwrap();

            proposal_builder.options(length);

            match proposal.action() {
                VoteAction::OffChain => {
                    proposal_builder.action_off_chain();
                }
                VoteAction::Treasury { action } => match action {
                    TreasuryGovernanceAction::TransferToRewards { value } => {
                        proposal_builder.action_rewards_add(value.0);
                    }
                    TreasuryGovernanceAction::NoOp => {
                        unimplemented!();
                    }
                },
                VoteAction::Parameters { action } => match action {
                    ParametersGovernanceAction::RewardAdd { value } => {
                        proposal_builder.action_transfer_to_rewards(value.0);
                    }
                    ParametersGovernanceAction::NoOp => {
                        proposal_builder.action_parameters_no_op();
                    }
                },
            };

            builder.with_proposal(&mut proposal_builder);
        }

        builder.voting_token(vote_plan.voting_token().clone());
        builder.build()
    }
}

impl From<VotePlanDef> for VotePlan {
    fn from(dto: VotePlanDef) -> Self {
        let mut proposals = Proposals::new();
        for proposal in dto.proposals.iter().cloned() {
            let _ = proposals.push(proposal.into());
        }

        VotePlan::new(
            dto.vote_date,
            dto.tally_date,
            dto.end_tally_date,
            proposals,
            dto.payload_type,
            dto.committee_keys,
            dto.voting_token,
        )
    }
}

#[derive(Clone, Debug)]
pub struct ProposalDef {
    id: ExternalProposalId,
    options: u8,
    action_type: VoteAction,
}

impl ProposalDef {
    pub fn id(&self) -> ExternalProposalId {
        self.id.clone()
    }
}

impl From<ProposalDef> for Proposal {
    fn from(dto: ProposalDef) -> Self {
        Proposal::new(
            dto.id,
            Options::new_length(dto.options).unwrap(),
            dto.action_type,
        )
    }
}