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
use super::super::super::ResponseOrError;
use super::request::SubscribeAccountBalances;
use crate::errors::Result;
use crate::graphql;
use graphql::updated_account_balances;
use crate::types::Asset;
use bigdecimal::BigDecimal;
use std::str::FromStr;
use std::collections::HashMap;
use crate::protocol::traits::TryFromState;
use crate::protocol::state::State;
use std::sync::Arc;
use tokio::sync::RwLock;
use async_trait::async_trait;
#[derive(Clone, Debug)]
pub struct AccountBalancesResponse {
pub balances: HashMap<Asset, BigDecimal>,
}
#[async_trait]
impl TryFromState<updated_account_balances::ResponseData> for HashMap<Asset, BigDecimal> {
async fn from(response: updated_account_balances::ResponseData, _state: Arc<RwLock<State>>) -> Result<HashMap<Asset, BigDecimal>> {
let mut balances = HashMap::new();
for balance in response.updated_account_balances {
let symbol = balance.asset.unwrap().symbol;
if let Ok(asset) = Asset::from_str(&symbol) {
let state_channel_amount = BigDecimal::from_str(&balance.available.unwrap().amount).unwrap();
balances.insert(asset, state_channel_amount);
}
}
Ok(balances)
}
}
impl SubscribeAccountBalances {
pub async fn response_from_graphql(
&self,
response: ResponseOrError<updated_account_balances::ResponseData>,
state: Arc<RwLock<State>>
) -> Result<ResponseOrError<AccountBalancesResponse>> {
Ok(match response {
ResponseOrError::Response(data) => {
let response = data.data;
ResponseOrError::from_data(AccountBalancesResponse {
balances: TryFromState::from(response, state).await?,
})
}
ResponseOrError::Error(error) => ResponseOrError::Error(error),
})
}
}