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
use super::Config;
use hyper::rt::{Future, Stream};
use hyper::{Body, Client};
use hyper_tls::HttpsConnector;
use serde_json::from_slice;
use std::io::{self, Write};

#[derive(Deserialize)]
struct Account<'a> {
  result: &'a [u8],
}

pub fn get_balance(config: Config) -> impl Future<Item = (), Error = ()> {
  let uri = format!(
    "https://api.etherscan.io/api?module={}&action={}&address={}&tag={}&apiKey={}",
    config.module, config.action, config.address, config.tag, config.api_key
  ).parse()
  .unwrap();

  let https = HttpsConnector::new(4).unwrap();
  let client = Client::builder().build::<_, Body>(https);

  client
    .get(uri)
    .and_then(move |res| {
      res
        .into_body()
        .for_each(|chunk| {
          let data: Account = from_slice(&chunk).unwrap();
          io::stdout()
            .write_all(data.result)
            .map_err(|e| panic!("Something went wrong!, {}", e))
        }).map(move |_| {
          print!(":{}\n", config.address);
        })
    }).map_err(|e| {
      eprintln!("Error {}", e);
    })
}