use std::collections::BTreeSet;
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use tokio::sync::broadcast;
use crate::common::config::{AccessToken, ConfigurationOpenApi, RefreshToken};
use crate::common::errors::{ConnectorError, TokenError, ValueError};
use crate::common::ids::{AccountId, SymbolId};
use crate::open_api::connection::{Connection, ConnectionOptions, Event, ReplayKey, ReplayStage};
use crate::open_api::messages::{
ProtoOaAccountAuthReq, ProtoOaApplicationAuthReq, ProtoOaCtidTraderAccount,
ProtoOaGetAccountListByAccessTokenReq, ProtoOaRefreshTokenReq, ProtoOaSubscribeSpotsReq,
ProtoOaUnsubscribeSpotsReq, ProtoOaVersionReq,
};
use crate::open_api::token::{TokenPair, refresh_over_http};
use crate::open_api::transport::{Connect, Tls};
use crate::open_api::trendbars::{Period, trendbar_key};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Account {
pub id: AccountId,
pub is_live: bool,
pub login: Option<i64>,
}
impl TryFrom<ProtoOaCtidTraderAccount> for Account {
type Error = ValueError;
fn try_from(account: ProtoOaCtidTraderAccount) -> Result<Self, Self::Error> {
let raw = account.ctid_trader_account_id;
let id = i64::try_from(raw).map_err(|_| ValueError::InvalidId {
kind: "account id",
value: raw.to_string(),
})?;
Ok(Self {
id: AccountId::new(id)?,
is_live: account.is_live.unwrap_or(false),
login: account.trader_login,
})
}
}
fn account_key(account: AccountId) -> ReplayKey {
ReplayKey::new(ReplayStage::Account, account.to_string())
}
fn spot_key(account: AccountId, symbol: SymbolId) -> ReplayKey {
ReplayKey::new(
ReplayStage::Subscription,
format!("spots/{account}/{symbol}"),
)
}
#[derive(Debug)]
struct Tokens {
access: AccessToken,
refresh: Option<RefreshToken>,
accounts: BTreeSet<AccountId>,
}
#[derive(Debug, Default)]
struct Subscriptions {
spots: BTreeSet<(AccountId, SymbolId)>,
trendbars: BTreeSet<(AccountId, SymbolId, Period)>,
}
#[derive(Debug, Clone)]
pub struct OpenApiClient {
connection: Connection,
configuration: Arc<ConfigurationOpenApi>,
tokens: Arc<Mutex<Tokens>>,
subscriptions: Arc<Mutex<Subscriptions>>,
}
impl OpenApiClient {
pub async fn connect(configuration: ConfigurationOpenApi) -> Result<Self, ConnectorError> {
let options = ConnectionOptions::from(&configuration);
Self::connect_with(configuration, Tls::new()?, options).await
}
pub async fn connect_with<C: Connect>(
configuration: ConfigurationOpenApi,
connector: C,
options: ConnectionOptions,
) -> Result<Self, ConnectorError> {
let connection = Connection::open(
connector,
configuration.host(),
configuration.port(),
options,
)
.await?;
Ok(Self {
connection,
tokens: Arc::new(Mutex::new(Tokens {
access: configuration.access_token().clone(),
refresh: configuration.refresh_token().cloned(),
accounts: BTreeSet::new(),
})),
subscriptions: Arc::default(),
configuration: Arc::new(configuration),
})
}
#[must_use]
pub const fn connection(&self) -> &Connection {
&self.connection
}
#[must_use]
pub fn configuration(&self) -> &ConfigurationOpenApi {
&self.configuration
}
#[must_use]
pub fn events(&self) -> broadcast::Receiver<Event> {
self.connection.events()
}
pub async fn authorise_application(&self) -> Result<(), ConnectorError> {
let request = ProtoOaApplicationAuthReq {
payload_type: None,
client_id: self.configuration.client_id().as_str().to_owned(),
client_secret: self.configuration.client_secret().expose().to_owned(),
};
self.connection.request(&request).await?;
let key = ReplayKey::new(ReplayStage::Application, "application");
self.connection.register_replay(key, &request).await
}
pub async fn accounts(&self) -> Result<Vec<Account>, ConnectorError> {
let request = ProtoOaGetAccountListByAccessTokenReq {
payload_type: None,
access_token: self.access_token().expose().to_owned(),
};
let response = self.connection.request(&request).await?;
response
.ctid_trader_account
.into_iter()
.map(|account| Account::try_from(account).map_err(ConnectorError::from))
.collect()
}
pub async fn authorise_account(&self, account: AccountId) -> Result<(), ConnectorError> {
let request = ProtoOaAccountAuthReq {
payload_type: None,
ctid_trader_account_id: account.get(),
access_token: self.access_token().expose().to_owned(),
};
self.connection.request(&request).await?;
self.tokens().accounts.insert(account);
self.connection
.register_replay(account_key(account), &request)
.await
}
#[must_use]
pub fn access_token(&self) -> AccessToken {
self.tokens().access.clone()
}
pub async fn set_tokens(&self, pair: &TokenPair) {
let accounts: Vec<AccountId> = {
let mut tokens = self.tokens();
tokens.access = pair.access.clone();
tokens.refresh = Some(pair.refresh.clone());
tokens.accounts.iter().copied().collect()
};
for account in accounts {
let request = ProtoOaAccountAuthReq {
payload_type: None,
ctid_trader_account_id: account.get(),
access_token: pair.access.expose().to_owned(),
};
let _ = self
.connection
.register_replay(account_key(account), &request)
.await;
}
}
pub async fn refresh_token(&self) -> Result<TokenPair, ConnectorError> {
let request = ProtoOaRefreshTokenReq {
payload_type: None,
refresh_token: self.current_refresh_token()?.expose().to_owned(),
};
let pair = TokenPair::try_from(self.connection.request(&request).await?)?;
self.set_tokens(&pair).await;
Ok(pair)
}
pub async fn refresh_token_over_http(&self) -> Result<TokenPair, ConnectorError> {
self.refresh_token_over_http_with(&Tls::new()?).await
}
pub async fn refresh_token_over_http_with<C: Connect>(
&self,
connector: &C,
) -> Result<TokenPair, ConnectorError> {
let refresh = self.current_refresh_token()?;
let pair = refresh_over_http(connector, &self.configuration, &refresh).await?;
self.set_tokens(&pair).await;
Ok(pair)
}
fn tokens(&self) -> std::sync::MutexGuard<'_, Tokens> {
self.tokens.lock().unwrap_or_else(PoisonError::into_inner)
}
fn current_refresh_token(&self) -> Result<RefreshToken, TokenError> {
self.tokens()
.refresh
.clone()
.ok_or(TokenError::NoRefreshToken)
}
pub async fn subscribe_spots(
&self,
account: AccountId,
symbols: &[SymbolId],
) -> Result<(), ConnectorError> {
let request = |symbols: &[SymbolId]| ProtoOaSubscribeSpotsReq {
payload_type: None,
ctid_trader_account_id: account.get(),
symbol_id: symbols.iter().map(|symbol| symbol.get()).collect(),
subscribe_to_spot_timestamp: Some(true),
};
self.connection.request(&request(symbols)).await?;
for symbol in symbols {
self.connection
.register_replay(spot_key(account, *symbol), &request(&[*symbol]))
.await?;
}
self.subscriptions()
.spots
.extend(symbols.iter().map(|symbol| (account, *symbol)));
Ok(())
}
pub async fn unsubscribe_spots(
&self,
account: AccountId,
symbols: &[SymbolId],
) -> Result<(), ConnectorError> {
let request = ProtoOaUnsubscribeSpotsReq {
payload_type: None,
ctid_trader_account_id: account.get(),
symbol_id: symbols.iter().map(|symbol| symbol.get()).collect(),
};
self.connection.request(&request).await?;
for symbol in symbols {
self.connection
.remove_replay(spot_key(account, *symbol))
.await?;
}
let dropped: Vec<(SymbolId, Period)> = {
let mut subscriptions = self.subscriptions();
for symbol in symbols {
subscriptions.spots.remove(&(account, *symbol));
}
let dropped = subscriptions
.trendbars
.iter()
.filter(|(owner, symbol, _)| *owner == account && symbols.contains(symbol))
.map(|(_, symbol, period)| (*symbol, *period))
.collect();
subscriptions
.trendbars
.retain(|(owner, symbol, _)| *owner != account || !symbols.contains(symbol));
dropped
};
for (symbol, period) in dropped {
self.connection
.remove_replay(trendbar_key(account, symbol, period))
.await?;
}
Ok(())
}
fn subscriptions(&self) -> MutexGuard<'_, Subscriptions> {
self.subscriptions
.lock()
.unwrap_or_else(PoisonError::into_inner)
}
pub(crate) fn has_spots(&self, account: AccountId, symbol: SymbolId) -> bool {
self.subscriptions().spots.contains(&(account, symbol))
}
pub(crate) fn track_trendbars(
&self,
account: AccountId,
symbol: SymbolId,
period: Period,
active: bool,
) {
let key = (account, symbol, period);
let mut subscriptions = self.subscriptions();
if active {
subscriptions.trendbars.insert(key);
} else {
subscriptions.trendbars.remove(&key);
}
}
pub async fn version(&self) -> Result<String, ConnectorError> {
let response = self
.connection
.request(&ProtoOaVersionReq { payload_type: None })
.await?;
Ok(response.version)
}
pub async fn shutdown(&self) {
self.connection.shutdown().await;
}
}
#[cfg(test)]
mod tests {
use super::Account;
use crate::common::errors::ValueError;
use crate::common::ids::AccountId;
use crate::open_api::messages::ProtoOaCtidTraderAccount;
fn wire(id: u64) -> ProtoOaCtidTraderAccount {
ProtoOaCtidTraderAccount {
ctid_trader_account_id: id,
is_live: Some(true),
trader_login: Some(9),
last_closing_deal_timestamp: None,
last_balance_update_timestamp: None,
}
}
#[test]
fn account_is_parsed_from_the_wire() {
let account = Account::try_from(wire(42)).expect("account");
assert_eq!(account.id, AccountId::new(42).expect("id"));
assert!(account.is_live);
assert_eq!(account.login, Some(9));
}
#[test]
fn account_ids_out_of_range_are_rejected() {
for id in [0, u64::MAX] {
assert_eq!(
Account::try_from(wire(id)),
Err(ValueError::InvalidId {
kind: "account id",
value: id.to_string(),
})
);
}
}
}