diff --git a/nautilus_core/common/src/cache/database.rs b/nautilus_core/common/src/cache/database.rs index 9bf4842a8ee4..426b400cc190 100644 --- a/nautilus_core/common/src/cache/database.rs +++ b/nautilus_core/common/src/cache/database.rs @@ -26,14 +26,14 @@ use nautilus_model::{ component_id::ComponentId, instrument_id::InstrumentId, position_id::PositionId, strategy_id::StrategyId, trader_id::TraderId, venue_order_id::VenueOrderId, }, - instruments::{synthetic::SyntheticInstrument, Instrument}, + instruments::{synthetic::SyntheticInstrument, InstrumentAny}, orders::base::{Order, OrderAny}, position::Position, types::currency::Currency, }; use ustr::Ustr; -use crate::enums::SerializationEncoding; +use crate::{enums::SerializationEncoding, interface::account::Account}; /// A type of database operation. #[derive(Clone, Debug)] @@ -128,7 +128,7 @@ impl CacheDatabaseAdapter { Ok(HashMap::new()) // TODO } - pub fn load_instruments(&self) -> anyhow::Result>> { + pub fn load_instruments(&self) -> anyhow::Result> { Ok(HashMap::new()) // TODO } @@ -136,9 +136,9 @@ impl CacheDatabaseAdapter { Ok(HashMap::new()) // TODO } - // pub fn load_accounts() -> anyhow::Result>> { - // Ok(HashMap::new()) // TODO - // } + pub fn load_accounts(&self) -> anyhow::Result>> { + Ok(HashMap::new()) // TODO + } pub fn load_orders(&self) -> anyhow::Result> { Ok(HashMap::new()) // TODO @@ -160,10 +160,7 @@ impl CacheDatabaseAdapter { todo!() // TODO } - pub fn load_instrument( - &self, - instrument_id: &InstrumentId, - ) -> anyhow::Result> { + pub fn load_instrument(&self, instrument_id: &InstrumentId) -> anyhow::Result { todo!() // TODO } @@ -212,27 +209,27 @@ impl CacheDatabaseAdapter { todo!() // TODO } - pub fn add_currency(&self, currency: Currency) -> anyhow::Result<()> { + pub fn add_currency(&self, currency: &Currency) -> anyhow::Result<()> { todo!() // TODO } - pub fn add_instrument(&self, instrument: Box) -> anyhow::Result<()> { + pub fn add_instrument(&self, instrument: &InstrumentAny) -> anyhow::Result<()> { todo!() // TODO } - pub fn add_synthetic(&self, synthetic: SyntheticInstrument) -> anyhow::Result<()> { + pub fn add_synthetic(&self, synthetic: &SyntheticInstrument) -> anyhow::Result<()> { todo!() // TODO } - // pub fn add_account(&self) -> anyhow::Result> { - // todo!() // TODO - // } + pub fn add_account(&self, account: &dyn Account) -> anyhow::Result> { + todo!() // TODO + } pub fn add_order(&self, order: &OrderAny) -> anyhow::Result<()> { todo!() // TODO } - pub fn add_position(&self, position: Position) -> anyhow::Result<()> { + pub fn add_position(&self, position: &Position) -> anyhow::Result<()> { todo!() // TODO } @@ -264,19 +261,19 @@ impl CacheDatabaseAdapter { todo!() // TODO } - pub fn update_order(&self, order: Box) -> anyhow::Result<()> { + pub fn update_order(&self, order: &OrderAny) -> anyhow::Result<()> { todo!() // TODO } - pub fn update_position(&self, position: Position) -> anyhow::Result<()> { + pub fn update_position(&self, position: &Position) -> anyhow::Result<()> { todo!() // TODO } - pub fn snapshot_order_state(&self, order: OrderAny) -> anyhow::Result<()> { + pub fn snapshot_order_state(&self, order: &OrderAny) -> anyhow::Result<()> { todo!() // TODO } - pub fn snapshot_position_state(&self, position: Position) -> anyhow::Result<()> { + pub fn snapshot_position_state(&self, position: &Position) -> anyhow::Result<()> { todo!() // TODO } diff --git a/nautilus_core/common/src/cache/mod.rs b/nautilus_core/common/src/cache/mod.rs index b4086352b9c6..a220112c9b8d 100644 --- a/nautilus_core/common/src/cache/mod.rs +++ b/nautilus_core/common/src/cache/mod.rs @@ -36,7 +36,7 @@ use nautilus_model::{ position_id::PositionId, strategy_id::StrategyId, venue::Venue, venue_order_id::VenueOrderId, }, - instruments::{synthetic::SyntheticInstrument, Instrument}, + instruments::{synthetic::SyntheticInstrument, InstrumentAny}, orderbook::book::OrderBook, orders::base::OrderAny, polymorphism::{ @@ -49,7 +49,7 @@ use nautilus_model::{ use ustr::Ustr; use self::database::CacheDatabaseAdapter; -use crate::enums::SerializationEncoding; +use crate::{enums::SerializationEncoding, interface::account::Account}; pub struct CacheConfig { pub encoding: SerializationEncoding, @@ -175,10 +175,10 @@ pub struct Cache { books: HashMap, bars: HashMap>, currencies: HashMap, - instruments: HashMap>, + instruments: HashMap, synthetics: HashMap, - // accounts: HashMap>, // TODO: Account not object safe - orders: HashMap, // TODO: Efficency (use enum) + accounts: HashMap>, + orders: HashMap, // order_lists: HashMap>, TODO: Need `OrderList` positions: HashMap, position_snapshots: HashMap>, @@ -234,8 +234,8 @@ impl Cache { currencies: HashMap::new(), instruments: HashMap::new(), synthetics: HashMap::new(), - // accounts: HashMap>, TODO: Decide where trait should go - orders: HashMap::new(), // TODO: Efficency (use enum) + accounts: HashMap::new(), + orders: HashMap::new(), // order_lists: HashMap>, TODO: Need `OrderList` positions: HashMap::new(), position_snapshots: HashMap::new(), @@ -290,18 +290,18 @@ impl Cache { Ok(()) } - // pub fn cache_accounts(&mut self) -> anyhow::Result<()> { - // self.accounts = match &self.database { - // Some(db) => db.load_accounts()?, - // None => HashMap::new(), - // }; - // - // info!( - // "Cached {} synthetic instruments from database", - // self.general.len() - // ); - // Ok(()) - // } + pub fn cache_accounts(&mut self) -> anyhow::Result<()> { + self.accounts = match &self.database { + Some(db) => db.load_accounts()?, + None => HashMap::new(), + }; + + info!( + "Cached {} synthetic instruments from database", + self.general.len() + ); + Ok(()) + } pub fn cache_orders(&mut self) -> anyhow::Result<()> { self.orders = match &self.database { @@ -486,51 +486,47 @@ impl Cache { pub fn add_currency(&mut self, currency: Currency) -> anyhow::Result<()> { debug!("Add `Currency` {}", currency.code); - self.currencies.insert(currency.code, currency); if let Some(database) = &self.database { - database.add_currency(currency)?; + database.add_currency(¤cy)?; } + + self.currencies.insert(currency.code, currency); Ok(()) } - pub fn add_instrument(&mut self, instrument: T) -> anyhow::Result<()> - where - T: Instrument + Clone, - { + pub fn add_instrument(&mut self, instrument: InstrumentAny) -> anyhow::Result<()> { debug!("Add `Instrument` {}", instrument.id()); - self.instruments - .insert(instrument.id(), Box::new(instrument.clone())); - // TODO: Revisit boxing if let Some(database) = &self.database { - database.add_instrument(Box::new(instrument))?; + database.add_instrument(&instrument)?; } + + self.instruments.insert(instrument.id(), instrument); Ok(()) } pub fn add_synthetic(&mut self, synthetic: SyntheticInstrument) -> anyhow::Result<()> { debug!("Add `SyntheticInstrument` {}", synthetic.id); - self.synthetics.insert(synthetic.id, synthetic.clone()); if let Some(database) = &self.database { - database.add_synthetic(synthetic)?; + database.add_synthetic(&synthetic)?; } + + self.synthetics.insert(synthetic.id, synthetic.clone()); Ok(()) } - // pub fn add_account(&mut self, account: T) -> anyhow::Result<()> - // where - // T: Account, - // { - // debug!("Add `Account` {}", account.id()); - // self.accounts.insert(account.id(), account); - // - // if let Some(database) = &self.database { - // database.add_synthetic(synthetic)?; - // } - // Ok(()) - // } + pub fn add_account(&mut self, account: Box) -> anyhow::Result<()> { + debug!("Add `Account` {}", account.id()); + + if let Some(database) = &self.database { + database.add_account(account.as_ref())?; + } + + self.accounts.insert(account.id(), account); + Ok(()) + } /// Add the order to the cache indexed with any given identifiers. ///