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
use super::{wallet::send, Result};
use crate::Client;

use sn_transfers::genesis::{create_faucet_wallet, load_genesis_wallet};
use sn_transfers::wallet::LocalWallet;
use sn_transfers::{CashNote, MainPubkey, NanoTokens};

/// Returns a cash_note with the requested number of tokens, for use by E2E test instances.
/// Note this will create a faucet having a Genesis balance
pub async fn get_tokens_from_faucet(
    amount: NanoTokens,
    to: MainPubkey,
    client: &Client,
) -> Result<CashNote> {
    Ok(send(
        load_faucet_wallet_from_genesis_wallet(client).await?,
        amount,
        to,
        client,
        // we should not need to wait for this
        true,
    )
    .await?)
}

/// Use the client to load the faucet wallet from the genesis Wallet.
/// With all balance transferred from the genesis_wallet to the faucet_wallet.
pub async fn load_faucet_wallet_from_genesis_wallet(client: &Client) -> Result<LocalWallet> {
    println!("Loading faucet...");
    let mut faucet_wallet = create_faucet_wallet();

    let faucet_balance = faucet_wallet.balance();
    if !faucet_balance.is_zero() {
        println!("Faucet wallet balance: {faucet_balance}");
        return Ok(faucet_wallet);
    }

    println!("Loading genesis...");
    let genesis_wallet = load_genesis_wallet()?;

    // Transfer to faucet. We will transfer almost all of the genesis wallet's
    // balance to the faucet,.

    let faucet_balance = genesis_wallet.balance();
    println!("Sending {faucet_balance} from genesis to faucet wallet..");
    let cash_note = send(
        genesis_wallet,
        faucet_balance,
        faucet_wallet.address(),
        client,
        true,
    )
    .await?;

    faucet_wallet.deposit(&vec![cash_note.clone()])?;
    faucet_wallet
        .store()
        .expect("Faucet wallet shall be stored successfully.");
    println!("Faucet wallet balance: {}", faucet_wallet.balance());

    println!("Verifying the transfer from genesis...");
    if let Err(error) = client.verify(&cash_note).await {
        panic!("Could not verify the transfer from genesis: {error:?}");
    } else {
        println!("Successfully verified the transfer from genesis on the second try.");
    }

    Ok(faucet_wallet)
}