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
use super::Actor;
use crate::network::wallet_state::template::Error;
use crate::wallet_state::MainnetWalletState;
use crate::CardanoWallet;
use async_trait::async_trait;
use cardano_serialization_lib::address::Address;
use rand::Rng;

/// Trait for retrieving information about address registrations from network
#[async_trait]
pub trait ExternalProvider {
    /// Error which can be returned on any issue with retrieving information
    type Error;

    /// Downloads `MainnetWalletState` from network
    async fn download_state_from_network(
        &self,
        actors: &[Actor],
    ) -> Result<Vec<MainnetWalletState>, Self::Error>;
}

pub struct DummyExternalProvider;

unsafe impl Sync for DummyExternalProvider {}
unsafe impl Send for DummyExternalProvider {}

#[async_trait]
impl ExternalProvider for DummyExternalProvider {
    type Error = Error;

    async fn download_state_from_network(
        &self,
        actors: &[Actor],
    ) -> Result<Vec<MainnetWalletState>, Self::Error> {
        let mut states = Vec::new();

        for actor in actors {
            if let Actor::ExternalDelegator { address, .. } = actor {
                let mut random = rand::thread_rng();
                let wallet = CardanoWallet::new(random.gen_range(1_000..10_000));
                states.push(MainnetWalletState {
                    rep: None,
                    registration_tx: Some(wallet.generate_direct_voting_registration(0)),
                    stake: wallet.stake(),
                    stake_address: Address::from_hex(address)?,
                });
            }
        }
        Ok(states)
    }
}