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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#![allow(dead_code)]

use serde::{Deserialize, Serialize};
use std::slice::Iter;

pub type RepNameOrVotingKey = String;

#[derive(Default, Debug, Serialize, Deserialize, Eq, PartialEq, Clone)]
pub struct Target(Vec<(RepNameOrVotingKey, u32)>);

impl From<Vec<(&str, u32)>> for Target {
    fn from(inner: Vec<(&str, u32)>) -> Self {
        Target(
            inner
                .into_iter()
                .map(|(key, weight)| (key.to_string(), weight))
                .collect(),
        )
    }
}

impl Target {
    pub fn push(&mut self, value: (RepNameOrVotingKey, u32)) {
        self.0.push(value);
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    pub fn iter(&self) -> Iter<'_, (RepNameOrVotingKey, u32)> {
        self.0.iter()
    }
}

/// Responsible for configuring registration transaction
#[derive(Default, Debug, Serialize, Deserialize, Eq, PartialEq, Clone)]
pub struct Registration {
    /// registration target
    pub target: Target,
    /// slot number in which registration was send
    pub slotno: u32,
}

pub fn registration() -> RegistrationBuilder {
    RegistrationBuilder::default()
}

#[derive(Default)]
pub struct RegistrationBuilder {
    registration: Registration,
}

impl RegistrationBuilder {
    pub fn at_slot(mut self, slotno: u32) -> Self {
        self.registration.slotno = slotno;
        self
    }

    pub fn with_target(mut self, key: RepNameOrVotingKey, weight: u32) -> Self {
        self.registration.target.push((key, weight));
        self
    }

    pub fn with_targets(mut self, targets: Vec<(&str, u32)>) -> Self {
        self.registration.target = targets.into();
        self
    }
}

impl TryFrom<RegistrationBuilder> for Registration {
    type Error = Error;

    fn try_from(builder: RegistrationBuilder) -> Result<Self, Self::Error> {
        if builder.registration.target.is_empty() {
            return Err(Error::CannotBuildRegistration {
                registration: builder.registration,
                details: "empty registrations for generated delegator".to_string(),
            });
        }
        Ok(builder.registration)
    }
}

/// Actor represent input definition of mainnet wallet from registration angle
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
#[serde(untagged)]
pub enum Actor {
    /// Delegator with just an address. Can be used for already existing wallet in the network
    ExternalDelegator {
        /// alias
        name: String,
        /// address in hex
        address: String,
    },
    /// Generated delegator will setup new mainnet wallet
    GeneratedDelegator {
        /// alias
        name: String,
        /// registration
        registration: Registration,
        /// ada amount
        ada: u64,
    },
    /// Representative with just and voting key. Can be used for already exsiting wallet
    ExternalRep {
        /// alias
        #[serde(rename = "rep_name")]
        name: String,
        /// voting key in hex
        voting_key: String,
    },
    /// This variant will create new unique wallet with given ada amount
    GeneratedRep {
        /// alias
        #[serde(rename = "rep_name")]
        name: String,
        /// ada amount
        ada: u64,
    },
}

pub fn delegator(delegator: &str) -> DelegatorBuilder {
    DelegatorBuilder::new(delegator)
}

pub fn representative(representative: &str) -> RepresentativeBuilder {
    RepresentativeBuilder::new(representative)
}

pub struct DelegatorBuilder {
    name: String,
    ada: Option<u64>,
    address: Option<String>,
    registration: Registration,
}

impl DelegatorBuilder {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            ada: None,
            address: None,
            registration: Registration::default(),
        }
    }

    pub(crate) fn with_registration(mut self, reg: Registration) -> Self {
        self.registration = reg;
        self
    }

    pub(crate) fn with_address(mut self, address: impl Into<String>) -> Self {
        self.address = Some(address.into());
        self
    }

    pub(crate) fn with_ada(mut self, ada: u64) -> Self {
        self.ada = Some(ada);
        self
    }
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("cannot build '{name}' actor instance, due to: {details}")]
    CannotBuildActor { name: String, details: String },

    #[error("cannot build '{registration:?}' actor instance, due to: {details}")]
    CannotBuildRegistration {
        registration: Registration,
        details: String,
    },
}

impl TryFrom<DelegatorBuilder> for Actor {
    type Error = Error;

    fn try_from(builder: DelegatorBuilder) -> Result<Self, Self::Error> {
        if let Some(ada) = builder.ada {
            if builder.registration.target.is_empty() {
                return Err(Error::CannotBuildActor {
                    name: builder.name,
                    details: "empty registrations for generated delegator".to_string(),
                });
            }

            Ok(Actor::GeneratedDelegator {
                name: builder.name,
                registration: builder.registration,
                ada,
            })
        } else if let Some(address) = builder.address {
            Ok(Actor::ExternalDelegator {
                name: builder.name,
                address,
            })
        } else {
            Err(Error::CannotBuildActor {
                name: builder.name.clone(),
                details: "no address defined for external delegator".to_string(),
            })
        }
    }
}

impl TryFrom<RepresentativeBuilder> for Actor {
    type Error = Error;

    fn try_from(builder: RepresentativeBuilder) -> Result<Self, Self::Error> {
        if let Some(ada) = builder.ada {
            Ok(Actor::GeneratedRep {
                name: builder.name,
                ada,
            })
        } else if let Some(voting_key) = builder.voting_key {
            Ok(Actor::ExternalRep {
                name: builder.name,
                voting_key,
            })
        } else {
            Err(Error::CannotBuildActor {
                name: builder.name.clone(),
                details: "no voting ke defined for external representative".to_string(),
            })
        }
    }
}

pub struct RepresentativeBuilder {
    name: String,
    ada: Option<u64>,
    voting_key: Option<String>,
}

impl RepresentativeBuilder {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            ada: None,
            voting_key: None,
        }
    }

    pub(crate) fn with_key(mut self, key: impl Into<String>) -> Self {
        self.voting_key = Some(key.into());
        self
    }

    pub(crate) fn with_ada(mut self, ada: u64) -> Self {
        self.ada = Some(ada);
        self
    }
}

#[cfg(test)]
mod tests {
    use crate::network::wallet_state::template::actor::{
        delegator, representative, Actor, Registration,
    };

    #[test]
    pub fn test() {
        let actors: Vec<Actor> = vec![
            representative("alice").with_ada(1000).try_into().unwrap(),
            representative("bob").with_key("").try_into().unwrap(),
            delegator("clarice")
                .with_registration(Registration {
                    target: vec![("bob", 1), ("alice", 1)].into(),
                    slotno: 1,
                })
                .try_into()
                .unwrap(),
            delegator("david")
                .with_address("testadd")
                .try_into()
                .unwrap(),
        ];

        assert_eq!(
            Actor::GeneratedRep {
                name: "alice".to_string(),
                ada: 1000
            },
            actors[0]
        );
        assert_eq!(
            actors[0],
            Actor::ExternalRep {
                name: "bob".to_string(),
                voting_key: String::new()
            }
        );
        assert_eq!(
            actors[0],
            Actor::GeneratedDelegator {
                name: "clarice".to_string(),
                registration: Registration {
                    target: vec![("bob", 1), ("alice", 1)].into(),
                    slotno: 1
                },
                ada: 0
            }
        );
        assert_eq!(
            actors[0],
            Actor::ExternalDelegator {
                name: "david".to_string(),
                address: "testadd".to_string()
            }
        );
    }
}