Skip to content

Commit b35f94f

Browse files
committed
feat(coordinator): Notify traders after new funding rate is inserted
After an update to the `funding_rates` table, there is no guarantee that the next funding rate has changed, but sending the message unconditionally is simpler and should cause no problems.
1 parent 372d1c3 commit b35f94f

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

coordinator/src/funding_fee.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@ use std::time::Duration;
1818
use time::ext::NumericalDuration;
1919
use time::format_description;
2020
use time::OffsetDateTime;
21+
use tokio::sync::broadcast;
2122
use tokio::task::block_in_place;
2223
use tokio_cron_scheduler::JobScheduler;
2324
use xxi_node::commons::ContractSymbol;
2425
use xxi_node::commons::Direction;
26+
use xxi_node::commons::FundingRate;
2527
use xxi_node::commons::Message;
2628

2729
mod db;
2830

2931
pub use db::get_funding_fee_events_for_active_trader_positions;
3032
pub use db::get_next_funding_rate;
3133
pub use db::get_outstanding_funding_fee_events;
32-
pub use db::insert_funding_rates;
3334
pub use db::insert_protocol_funding_fee_event;
3435
pub use db::mark_funding_fee_event_as_paid;
3536

@@ -312,6 +313,26 @@ pub fn funding_fee_from_funding_fee_events(events: &[FundingFeeEvent]) -> Fundin
312313
}
313314
}
314315

316+
pub fn insert_funding_rates(
317+
conn: &mut PgConnection,
318+
tx_orderbook_feed: broadcast::Sender<Message>,
319+
funding_rates: &[FundingRate],
320+
) -> Result<()> {
321+
db::insert_funding_rates(conn, funding_rates)?;
322+
323+
// There is no guarantee that the next funding rate has changed, but sending the message
324+
// unconditionally is simpler and should cause no problems.
325+
let next_funding_rate = get_next_funding_rate(conn)?;
326+
327+
if let Some(next_funding_rate) = next_funding_rate {
328+
if let Err(e) = tx_orderbook_feed.send(Message::NextFundingRate(next_funding_rate)) {
329+
tracing::error!("Failed to notify traders about next funding rate: {e}");
330+
}
331+
}
332+
333+
Ok(())
334+
}
335+
315336
#[cfg(test)]
316337
mod tests {
317338
use super::*;

coordinator/src/routes/admin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ pub async fn post_funding_rates(
685685
.map(xxi_node::commons::FundingRate::from)
686686
.collect::<Vec<_>>();
687687

688-
insert_funding_rates(&mut conn, &funding_rates)
688+
insert_funding_rates(&mut conn, state.tx_orderbook_feed.clone(), &funding_rates)
689689
.map_err(|e| AppError::BadRequest(format!("{e:#}")))?;
690690

691691
Ok(())

0 commit comments

Comments
 (0)