use crate::param::{AccountId, Asset, PositionSize};
use crate::pretrade::policy::{field_access_error_account_adjustment_reject, PolicyName};
use crate::pretrade::{AccountBlock, Reject, RejectCode, RejectScope, Rejects};
pub(super) fn insufficient_funds_reject(
policy: &str,
asset: &Asset,
account_id: AccountId,
available: PositionSize,
requested: PositionSize,
) -> Reject {
Reject::new(
policy,
RejectScope::Order,
RejectCode::InsufficientFunds,
"spot funds insufficient",
format!(
"account {account_id}, asset {asset}: available {available}, requested {requested}"
),
)
}
pub(super) fn adj_field<P: PolicyName + ?Sized, T>(
policy: &P,
name: &str,
result: Result<T, crate::RequestFieldAccessError>,
) -> Result<T, Rejects> {
result.map_err(|e| {
Rejects::from(field_access_error_account_adjustment_reject(
policy, name, &e,
))
})
}
pub(super) fn order_value_calculation_failed_reject(
policy: &str,
details: impl Into<String>,
) -> Reject {
Reject::new(
policy,
RejectScope::Order,
RejectCode::OrderValueCalculationFailed,
"order value calculation failed",
details.into(),
)
}
pub(super) fn arithmetic_overflow_reject(
policy: &str,
scope: RejectScope,
details: impl Into<String>,
) -> Reject {
Reject::new(
policy,
scope,
RejectCode::ArithmeticOverflow,
"arithmetic overflow",
details.into(),
)
}
pub(super) fn arithmetic_overflow_account_block(
policy: &str,
details: impl Into<String>,
) -> AccountBlock {
AccountBlock::new(
policy,
RejectCode::ArithmeticOverflow,
"arithmetic overflow",
details.into(),
)
}
pub(super) fn account_adjustment_bounds_exceeded_reject(
policy: &str,
account_id: AccountId,
asset: &Asset,
field: &str,
actual: PositionSize,
lower: Option<PositionSize>,
upper: Option<PositionSize>,
) -> Reject {
Reject::new(
policy,
RejectScope::Account,
RejectCode::AccountAdjustmentBoundsExceeded,
"account adjustment bounds exceeded",
format!(
"account {account_id}, asset {asset}: {field} {actual}, \
lower {lower:?}, upper {upper:?}"
),
)
}