diff --git a/mobile/native/src/db/mod.rs b/mobile/native/src/db/mod.rs index 03689f299..3dea78ef0 100644 --- a/mobile/native/src/db/mod.rs +++ b/mobile/native/src/db/mod.rs @@ -497,10 +497,12 @@ pub fn get_all_trades() -> Result> { Ok(trades) } -pub fn insert_trade(trade: crate::trade::Trade) -> Result<()> { +pub fn insert_trades(trades: &[crate::trade::Trade]) -> Result<()> { let mut db = connection()?; - NewTrade::insert(&mut db, trade.into())?; + let trades = trades.iter().copied().map(|trade| trade.into()).collect(); + + NewTrade::insert(&mut db, trades)?; Ok(()) } diff --git a/mobile/native/src/db/models.rs b/mobile/native/src/db/models.rs index a43cb3bda..4951599ee 100644 --- a/mobile/native/src/db/models.rs +++ b/mobile/native/src/db/models.rs @@ -1092,12 +1092,14 @@ impl Trade { } impl NewTrade { - pub fn insert(conn: &mut SqliteConnection, trade: Self) -> Result<()> { + pub fn insert(conn: &mut SqliteConnection, trades: Vec) -> Result<()> { + let len = trades.len(); + let affected_rows = diesel::insert_into(trades::table) - .values(trade) + .values(trades) .execute(conn)?; - ensure!(affected_rows > 0, "Could not insert trade"); + ensure!(affected_rows > len, "Could not insert trade"); Ok(()) } diff --git a/mobile/native/src/trade/position/handler.rs b/mobile/native/src/trade/position/handler.rs index a4482e09b..d460d9897 100644 --- a/mobile/native/src/trade/position/handler.rs +++ b/mobile/native/src/trade/position/handler.rs @@ -4,7 +4,7 @@ use crate::event::EventInternal; use crate::trade::order::Order; use crate::trade::position::Position; use crate::trade::position::PositionState; -use crate::trade::trades::handler::new_trade; +use crate::trade::trades::handler::new_trades; use crate::trade::FundingFeeEvent; use anyhow::bail; use anyhow::Context; @@ -182,9 +182,7 @@ pub fn update_position_after_dlc_channel_creation_or_update( } }; - for trade in trades { - new_trade(trade)?; - } + new_trades(trades)?; event::publish(&EventInternal::PositionUpdateNotification(position)); @@ -227,9 +225,7 @@ pub fn update_position_after_dlc_closure(filled_order: Order) -> Result<()> { ); } - for trade in trades { - new_trade(trade)?; - } + new_trades(trades)?; db::delete_positions()?; diff --git a/mobile/native/src/trade/trades/handler.rs b/mobile/native/src/trade/trades/handler.rs index 93abe2221..4a3b8124b 100644 --- a/mobile/native/src/trade/trades/handler.rs +++ b/mobile/native/src/trade/trades/handler.rs @@ -4,10 +4,12 @@ use crate::event::EventInternal; use crate::trade::Trade; use anyhow::Result; -pub fn new_trade(trade: Trade) -> Result<()> { - db::insert_trade(trade)?; +pub fn new_trades(trades: Vec) -> Result<()> { + db::insert_trades(&trades)?; - event::publish(&EventInternal::NewTrade(trade)); + for trade in trades { + event::publish(&EventInternal::NewTrade(trade)); + } Ok(()) }