Skip to content

Commit

Permalink
chore(app): Insert Trades in batches
Browse files Browse the repository at this point in the history
  • Loading branch information
luckysori committed Jun 4, 2024
1 parent c473330 commit 9972e55
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
6 changes: 4 additions & 2 deletions mobile/native/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,10 +497,12 @@ pub fn get_all_trades() -> Result<Vec<crate::trade::Trade>> {
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(())
}
Expand Down
8 changes: 5 additions & 3 deletions mobile/native/src/db/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self>) -> 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(())
}
Expand Down
10 changes: 3 additions & 7 deletions mobile/native/src/trade/position/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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()?;

Expand Down
8 changes: 5 additions & 3 deletions mobile/native/src/trade/trades/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Trade>) -> Result<()> {
db::insert_trades(&trades)?;

event::publish(&EventInternal::NewTrade(trade));
for trade in trades {
event::publish(&EventInternal::NewTrade(trade));
}

Ok(())
}
Expand Down

0 comments on commit 9972e55

Please sign in to comment.