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
use futures::future::join_all;

use cxmr_api::{Account, SharedPrivateClient};
use cxmr_exchanges::{AccountInfo, Exchange, ExchangeInfo};

use super::{new_private_clients, new_public_clients, Error, HttpsClient};

/// Fetches latest markets information from API.
pub async fn fetch_markets(
    client: HttpsClient,
    exchanges: &Vec<Exchange>,
) -> Result<Vec<ExchangeInfo>, Error> {
    let clients = new_public_clients(client, exchanges)?;
    let requests = clients
        .into_iter()
        .map(async move |client| client.exchange_info().await);
    join_all(requests).await.into_iter().collect()
}

/// Fetches latest accounts information from API.
/// Tuple contains account config and information.
pub async fn fetch_accounts(
    client: HttpsClient,
    accounts: &Vec<Account>,
) -> Result<Vec<(AccountInfo, SharedPrivateClient<Error>)>, Error> {
    let clients = new_private_clients(client, accounts)?;
    let requests = clients.into_iter().map(async move |client| {
        let info = client.account_info().await?;
        Ok((info, client))
    });
    join_all(requests).await.into_iter().collect()
}