115 lines
3.4 KiB
Rust
115 lines
3.4 KiB
Rust
//! Read-only reporting from committed financial facts. No matching, accounting,
|
|
//! history replay, checkpoint copies or statistical/round-trip interpretation.
|
|
use super::{
|
|
contract::*,
|
|
ledger::{Ledger, LedgerError, LedgerValuation},
|
|
scalar::Posting,
|
|
};
|
|
use serde::Serialize;
|
|
|
|
#[derive(Debug, PartialEq, Eq, Serialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct OwnedFill<'a> {
|
|
pub command_id: &'a Id,
|
|
pub event_time: EventTime,
|
|
pub owner_id: &'a Id,
|
|
pub decision: &'a DecisionIdentity,
|
|
pub order_id: &'a Id,
|
|
pub fill_id: &'a Id,
|
|
pub instrument_id: &'a Id,
|
|
pub side: Side,
|
|
pub quantity: Positive,
|
|
pub price: Positive,
|
|
pub fee: &'a Fee,
|
|
pub cash_delta: Posting,
|
|
pub quantity_delta: Posting,
|
|
pub released_basis: NonNegative,
|
|
pub realized_gross_pnl: Posting,
|
|
}
|
|
|
|
#[derive(Debug, PartialEq, Eq, Serialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct FinancialProjection<'a> {
|
|
pub valuation: LedgerValuation,
|
|
pub fills: Vec<OwnedFill<'a>>,
|
|
}
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum ProjectionError {
|
|
#[error(transparent)]
|
|
Ledger(#[from] LedgerError),
|
|
#[error("committed fill receipt has inconsistent financial effects")]
|
|
InconsistentReceipt,
|
|
}
|
|
|
|
pub fn project(ledger: &Ledger) -> Result<FinancialProjection<'_>, ProjectionError> {
|
|
let valuation = ledger.valuation()?;
|
|
let mut fills = Vec::new();
|
|
for receipt in ledger.receipts() {
|
|
if receipt.outcome.replayed {
|
|
continue;
|
|
}
|
|
let EngineCommandBody::ApplyFill {
|
|
decision,
|
|
order_id,
|
|
fill_id,
|
|
instrument_id,
|
|
side,
|
|
quantity,
|
|
price,
|
|
fee,
|
|
} = &receipt.command.body
|
|
else {
|
|
continue;
|
|
};
|
|
let [FinancialEffect::FillApplied {
|
|
owner_id: effect_owner,
|
|
order_id: effect_order,
|
|
fill_id: effect_fill,
|
|
instrument_id: effect_instrument,
|
|
price: effect_price,
|
|
cash_delta,
|
|
quantity_delta,
|
|
released_basis,
|
|
realized_gross_pnl,
|
|
}, FinancialEffect::FeeApplied {
|
|
owner_id: fee_owner,
|
|
fill_id: fee_fill,
|
|
fee_id,
|
|
amount,
|
|
}] = receipt.outcome.effects.effects.as_slice()
|
|
else {
|
|
return Err(ProjectionError::InconsistentReceipt);
|
|
};
|
|
if effect_owner != &decision.owner_id
|
|
|| effect_order != order_id
|
|
|| effect_fill != fill_id
|
|
|| effect_instrument != instrument_id
|
|
|| effect_price != price
|
|
|| fee_owner != effect_owner
|
|
|| fee_fill != fill_id
|
|
|| fee_id != &fee.fee_id
|
|
|| amount != &fee.amount
|
|
{
|
|
return Err(ProjectionError::InconsistentReceipt);
|
|
}
|
|
fills.push(OwnedFill {
|
|
command_id: &receipt.command.command_id,
|
|
event_time: receipt.command.event_time,
|
|
owner_id: &decision.owner_id,
|
|
decision,
|
|
order_id,
|
|
fill_id,
|
|
instrument_id,
|
|
side: *side,
|
|
quantity: *quantity,
|
|
price: *price,
|
|
fee,
|
|
cash_delta: *cash_delta,
|
|
quantity_delta: *quantity_delta,
|
|
released_basis: *released_basis,
|
|
realized_gross_pnl: *realized_gross_pnl,
|
|
});
|
|
}
|
|
Ok(FinancialProjection { valuation, fills })
|
|
}
|