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
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::{BlockingClient, Error, Result};
use diem_types::{
    account_address::AccountAddress,
    transaction::{authenticator::AuthenticationKey, SignedTransaction},
};
use reqwest::Url;

pub struct FaucetClient {
    url: String,
    json_rpc_client: BlockingClient,
}

impl FaucetClient {
    pub fn new(url: String, json_rpc_url: String) -> Self {
        Self {
            url,
            json_rpc_client: BlockingClient::new(json_rpc_url),
        }
    }

    pub fn create_account(
        &self,
        authentication_key: AuthenticationKey,
        currency_code: &str,
    ) -> Result<()> {
        let client = reqwest::blocking::Client::new();
        let mut url = Url::parse(&self.url).map_err(Error::request)?;
        url.set_path("accounts");
        let query = format!(
            "authentication-key={}&currency={}",
            authentication_key, currency_code
        );
        url.set_query(Some(&query));

        // Faucet returns the transaction that creates the account and needs to be waited on before
        // returning.
        let response = client.post(url).send().map_err(Error::request)?;
        let status_code = response.status();
        let body = response.text().map_err(Error::decode)?;
        if !status_code.is_success() {
            return Err(Error::status(status_code.as_u16()));
        }
        let bytes = hex::decode(body).map_err(Error::decode)?;
        let txn: SignedTransaction = bcs::from_bytes(&bytes).map_err(Error::decode)?;

        self.json_rpc_client
            .wait_for_signed_transaction(&txn, None, None)
            .map_err(Error::unknown)?;

        Ok(())
    }

    pub fn fund(&self, address: AccountAddress, currency_code: &str, amount: u64) -> Result<()> {
        let client = reqwest::blocking::Client::new();
        let mut url = Url::parse(&self.url).map_err(Error::request)?;
        url.set_path(&format!("accounts/{}/fund", address));
        let query = format!("currency={}&amount={}", currency_code, amount);
        url.set_query(Some(&query));

        // Faucet returns the transaction that creates the account and needs to be waited on before
        // returning.
        let response = client.post(url).send().map_err(Error::request)?;
        let status_code = response.status();
        let body = response.text().map_err(Error::decode)?;
        if !status_code.is_success() {
            return Err(Error::status(status_code.as_u16()));
        }
        let bytes = hex::decode(body).map_err(Error::decode)?;
        let txn: SignedTransaction = bcs::from_bytes(&bytes).map_err(Error::decode)?;

        self.json_rpc_client
            .wait_for_signed_transaction(&txn, None, None)
            .map_err(Error::unknown)?;
        Ok(())
    }

    pub fn mint(
        &self,
        currency_code: &str,
        auth_key: AuthenticationKey,
        amount: u64,
    ) -> Result<()> {
        self.create_account(auth_key, currency_code)?;
        self.fund(auth_key.derived_address(), currency_code, amount)?;

        Ok(())
    }
}