use super::{Ledger, LedgerEntry, LedgerEntryKind, RightsIssueEvent};
use crate::error::{QxError, QxResult};
use crate::identity::InstrumentId;
use crate::numeric::{Money, Quantity};
impl Ledger {
pub fn grant_rights_entitlement(
&mut self,
account_id: &str,
source_instrument: &InstrumentId,
rights_instrument: &InstrumentId,
currency: &str,
entitled_qty_raw: i128,
ts: u64,
) -> QxResult<u64> {
if entitled_qty_raw <= 0 {
return Err(QxError::BusinessViolation("配股登记配额必须为正".into()));
}
if self
.position_for(account_id, source_instrument)
.quantity
.raw()
< entitled_qty_raw
{
return Err(QxError::BusinessViolation(
"配股登记日账户持仓不足以覆盖权利配额".into(),
));
}
self.append(LedgerEntry {
id: 0,
account_id: account_id.into(),
currency: currency.into(),
kind: LedgerEntryKind::RightsEntitlement,
amount: Money::ZERO,
instrument: Some(rights_instrument.clone()),
quantity: Quantity::from_raw(entitled_qty_raw),
price: None,
order_id: None,
ts,
multiplier: 1,
position_side: None,
leverage: None,
exact_amounts: None,
})
}
pub fn apply_rights_issue_subscription_from_entitlement(
&mut self,
account_id: &str,
rights_instrument: &InstrumentId,
currency: &str,
subscription_qty_raw: i128,
subscription_price_raw: i128,
ts: u64,
) -> QxResult<Vec<u64>> {
if subscription_qty_raw <= 0 || subscription_price_raw <= 0 {
return Err(QxError::BusinessViolation(
"配股认购数量和价格必须为正".into(),
));
}
if self
.rights_entitlement_for(account_id, rights_instrument)
.raw()
< subscription_qty_raw
{
return Err(QxError::BusinessViolation("配股权利余额不足".into()));
}
let mut staged = self.clone();
let mut ids = staged.append_share_subscription_entries(
account_id,
rights_instrument,
currency,
subscription_qty_raw,
subscription_price_raw,
ts,
)?;
ids.push(staged.append(LedgerEntry {
id: 0,
account_id: account_id.into(),
currency: currency.into(),
kind: LedgerEntryKind::RightsEntitlement,
amount: Money::ZERO,
instrument: Some(rights_instrument.clone()),
quantity: Quantity::from_raw(-subscription_qty_raw),
price: None,
order_id: None,
ts,
multiplier: 1,
position_side: None,
leverage: None,
exact_amounts: None,
})?);
*self = staged;
Ok(ids)
}
pub fn apply_rights_issue_event(
&mut self,
account_id: &str,
event: RightsIssueEvent,
ts: u64,
) -> QxResult<Vec<u64>> {
if event.entitled_qty_raw <= 0
|| event.subscription_qty_raw < 0
|| event.subscription_qty_raw > event.entitled_qty_raw
|| (event.subscription_qty_raw > 0 && event.subscription_price_raw <= 0)
{
return Err(QxError::BusinessViolation("配股权利事实参数非法".into()));
}
if self
.position_for(account_id, &event.source_instrument)
.quantity
.raw()
< event.entitled_qty_raw
{
return Err(QxError::BusinessViolation(
"配股登记日账户持仓不足以覆盖权利配额".into(),
));
}
let mut staged = self.clone();
let grant_id = staged.append(LedgerEntry {
id: 0,
account_id: account_id.into(),
currency: event.currency.clone(),
kind: LedgerEntryKind::RightsEntitlement,
amount: Money::ZERO,
instrument: Some(event.rights_instrument.clone()),
quantity: Quantity::from_raw(event.entitled_qty_raw),
price: None,
order_id: None,
ts,
multiplier: 1,
position_side: None,
leverage: None,
exact_amounts: None,
})?;
let mut ids = vec![grant_id];
if event.subscription_qty_raw > 0 {
ids.extend(staged.append_share_subscription_entries(
account_id,
&event.rights_instrument,
&event.currency,
event.subscription_qty_raw,
event.subscription_price_raw,
ts,
)?);
ids.push(staged.append(LedgerEntry {
id: 0,
account_id: account_id.into(),
currency: event.currency.clone(),
kind: LedgerEntryKind::RightsEntitlement,
amount: Money::ZERO,
instrument: Some(event.rights_instrument.clone()),
quantity: Quantity::from_raw(-event.subscription_qty_raw),
price: None,
order_id: None,
ts,
multiplier: 1,
position_side: None,
leverage: None,
exact_amounts: None,
})?);
}
*self = staged;
Ok(ids)
}
pub fn expire_rights_entitlement(
&mut self,
account_id: &str,
rights_instrument: &InstrumentId,
expired_qty_raw: i128,
currency: &str,
ts: u64,
) -> QxResult<u64> {
if expired_qty_raw <= 0 {
return Err(QxError::BusinessViolation("失效权利数量必须为正".into()));
}
if self
.rights_entitlement_for(account_id, rights_instrument)
.raw()
< expired_qty_raw
{
return Err(QxError::BusinessViolation("失效权利余额不足".into()));
}
self.append(LedgerEntry {
id: 0,
account_id: account_id.into(),
currency: currency.into(),
kind: LedgerEntryKind::RightsEntitlement,
amount: Money::ZERO,
instrument: Some(rights_instrument.clone()),
quantity: Quantity::from_raw(-expired_qty_raw),
price: None,
order_id: None,
ts,
multiplier: 1,
position_side: None,
leverage: None,
exact_amounts: None,
})
}
pub fn rights_entitlement_for(
&self,
account_id: &str,
rights_instrument: &InstrumentId,
) -> Quantity {
Quantity::from_raw(
*self
.rights_entitlements
.get(&(account_id.to_string(), rights_instrument.clone()))
.unwrap_or(&0),
)
}
}