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
pub mod get {
    use pea_core::types;
    use serde::{Deserialize, Serialize};
    use std::error::Error;
    #[derive(Serialize, Deserialize, Debug)]
    pub struct Block {
        pub previous_hash: String,
        pub timestamp: types::Timestamp,
        pub public_key: String,
        pub signature: String,
        pub transactions: Vec<String>,
        pub stakes: Vec<String>,
    }
    #[derive(Serialize, Deserialize, Debug)]
    pub struct Transaction {
        pub public_key_input: String,
        pub public_key_output: String,
        pub amount: types::Amount,
        pub fee: types::Amount,
        pub timestamp: types::Timestamp,
        pub signature: String,
    }
    #[derive(Serialize, Deserialize, Debug)]
    pub struct Stake {
        pub public_key: String,
        pub amount: types::Amount,
        pub deposit: bool,
        pub fee: types::Amount,
        pub timestamp: types::Timestamp,
        pub signature: String,
    }
    pub async fn height(api: &str) -> Result<types::Height, Box<dyn Error>> {
        Ok(reqwest::get(format!("{}/height", api))
            .await?
            .json()
            .await?)
    }
    pub async fn balance(api: &str, address: &str) -> Result<types::Amount, Box<dyn Error>> {
        Ok(reqwest::get(format!("{}/balance/{}", api, address))
            .await?
            .json()
            .await?)
    }
    pub async fn balance_staked(api: &str, address: &str) -> Result<types::Amount, Box<dyn Error>> {
        Ok(reqwest::get(format!("{}/balance_staked/{}", api, address))
            .await?
            .json()
            .await?)
    }
    pub async fn index(api: &str) -> Result<String, Box<dyn Error>> {
        Ok(reqwest::get(api).await?.text().await?)
    }
    pub async fn block(api: &str, hash: &str) -> Result<Block, Box<dyn Error>> {
        Ok(reqwest::get(format!("{}/block/{}", api, hash))
            .await?
            .json()
            .await?)
    }
    pub async fn transaction(api: &str, hash: &str) -> Result<Transaction, Box<dyn Error>> {
        Ok(reqwest::get(format!("{}/transaction/{}", api, hash))
            .await?
            .json()
            .await?)
    }
    pub async fn stake(api: &str, hash: &str) -> Result<Stake, Box<dyn Error>> {
        Ok(reqwest::get(format!("{}/stake/{}", api, hash))
            .await?
            .json()
            .await?)
    }
    pub async fn hash(api: &str, height: &usize) -> Result<String, Box<dyn Error>> {
        Ok(reqwest::get(format!("{}/hash/{}", api, height))
            .await?
            .json()
            .await?)
    }
}
pub mod post {
    use pea_core::{
        stake::{self, Stake},
        transaction::{self, Transaction},
    };
    use std::error::Error;
    pub async fn transaction(
        api: &str,
        transaction: &Transaction,
    ) -> Result<String, Box<dyn Error>> {
        Ok(reqwest::Client::new()
            .post(format!("{}/transaction", api))
            .body(hex::encode(bincode::serialize(&transaction::Compressed {
                public_key_input: transaction.public_key_input,
                public_key_output: transaction.public_key_output,
                amount: pea_amount::to_bytes(&transaction.amount),
                fee: pea_amount::to_bytes(&transaction.fee),
                timestamp: transaction.timestamp,
                signature: transaction.signature,
            })?))
            .send()
            .await?
            .json()
            .await?)
    }
    pub async fn stake(api: &str, stake: &Stake) -> Result<String, Box<dyn Error>> {
        Ok(reqwest::Client::new()
            .post(format!("{}/stake", api))
            .body(hex::encode(bincode::serialize(&stake::Compressed {
                public_key: stake.public_key,
                amount: pea_amount::to_bytes(&stake.amount),
                fee: pea_amount::to_bytes(&stake.fee),
                deposit: stake.deposit,
                timestamp: stake.timestamp,
                signature: stake.signature,
            })?))
            .send()
            .await?
            .json()
            .await?)
    }
}