use rust_decimal::Decimal;
use crate::common::errors::{ConnectorError, ValueError};
use crate::common::ids::{AccountId, AssetId};
use crate::common::time::Timestamp;
use crate::open_api::client::OpenApiClient;
use crate::open_api::messages::{
ProtoOaAccessRights, ProtoOaAccountType, ProtoOaTrader, ProtoOaTraderReq,
};
use crate::open_api::symbols::Asset;
use crate::open_api::trading::{DEFAULT_MONEY_DIGITS, money};
const LEVERAGE_SCALE: u32 = 2;
const ACCOUNT_TYPE: &str = "account type";
const ACCESS_RIGHTS: &str = "access rights";
const DEPOSIT_ASSET: &str = "deposit asset";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum AccountType {
Hedged,
Netted,
SpreadBetting,
}
impl AccountType {
fn from_wire(raw: Option<i32>) -> Result<Self, ValueError> {
let Some(raw) = raw else {
return Ok(Self::Hedged);
};
match ProtoOaAccountType::try_from(raw) {
Ok(ProtoOaAccountType::Hedged) => Ok(Self::Hedged),
Ok(ProtoOaAccountType::Netted) => Ok(Self::Netted),
Ok(ProtoOaAccountType::SpreadBetting) => Ok(Self::SpreadBetting),
Err(_) => Err(ValueError::OutOfRange { kind: ACCOUNT_TYPE }),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum AccessRights {
FullAccess,
CloseOnly,
NoTrading,
NoLogin,
}
impl AccessRights {
fn from_wire(raw: Option<i32>) -> Result<Self, ValueError> {
let Some(raw) = raw else {
return Ok(Self::FullAccess);
};
match ProtoOaAccessRights::try_from(raw) {
Ok(ProtoOaAccessRights::FullAccess) => Ok(Self::FullAccess),
Ok(ProtoOaAccessRights::CloseOnly) => Ok(Self::CloseOnly),
Ok(ProtoOaAccessRights::NoTrading) => Ok(Self::NoTrading),
Ok(ProtoOaAccessRights::NoLogin) => Ok(Self::NoLogin),
Err(_) => Err(ValueError::OutOfRange {
kind: ACCESS_RIGHTS,
}),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Trader {
pub account: AccountId,
pub balance: Decimal,
pub money_digits: u32,
pub deposit_asset: AssetId,
pub currency: String,
pub leverage: Option<Decimal>,
pub max_leverage: Option<u32>,
pub account_type: AccountType,
pub access_rights: AccessRights,
pub swap_free: bool,
pub limited_risk: bool,
pub manager_bonus: Option<Decimal>,
pub ib_bonus: Option<Decimal>,
pub non_withdrawable_bonus: Option<Decimal>,
pub broker_name: Option<String>,
pub registered: Option<Timestamp>,
}
impl Trader {
fn parse(trader: ProtoOaTrader, assets: &[Asset]) -> Result<Self, ValueError> {
let digits = trader.money_digits;
let amount = |raw: Option<i64>| raw.map(|raw| money(raw, digits)).transpose();
let deposit_asset = AssetId::new(trader.deposit_asset_id)?;
let currency = assets
.iter()
.find(|asset| asset.id == deposit_asset)
.map(|asset| asset.name.clone())
.ok_or(ValueError::OutOfRange {
kind: DEPOSIT_ASSET,
})?;
Ok(Self {
account: AccountId::new(trader.ctid_trader_account_id)?,
balance: money(trader.balance, digits)?,
money_digits: digits.unwrap_or(DEFAULT_MONEY_DIGITS),
deposit_asset,
currency,
leverage: trader
.leverage_in_cents
.map(|cents| Decimal::new(i64::from(cents), LEVERAGE_SCALE).normalize()),
max_leverage: trader.max_leverage,
account_type: AccountType::from_wire(trader.account_type)?,
access_rights: AccessRights::from_wire(trader.access_rights)?,
swap_free: trader.swap_free.unwrap_or(false),
limited_risk: trader.is_limited_risk.unwrap_or(false),
manager_bonus: amount(trader.manager_bonus)?,
ib_bonus: amount(trader.ib_bonus)?,
non_withdrawable_bonus: amount(trader.non_withdrawable_bonus)?,
broker_name: trader.broker_name,
registered: trader
.registration_timestamp
.map(Timestamp::from_millis)
.transpose()?,
})
}
}
impl OpenApiClient {
pub async fn trader(&self, account: AccountId) -> Result<Trader, ConnectorError> {
let request = ProtoOaTraderReq {
payload_type: None,
ctid_trader_account_id: account.get(),
};
let trader = self.connection().request(&request).await?.trader;
let assets = self.assets(account).await?;
Ok(Trader::parse(trader, &assets)?)
}
}