Skip to content

Commit

Permalink
fix: Use oracle from trade params
Browse files Browse the repository at this point in the history
  • Loading branch information
holzeis committed Nov 16, 2023
1 parent 18d90b5 commit 4884aee
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 42 deletions.
2 changes: 2 additions & 0 deletions coordinator/src/bin/coordinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,14 @@ async fn main() -> Result<()> {
tx_price_feed.clone(),
auth_users_notifier.clone(),
network,
node.inner.oracle_pubkey,
);
let _handle = async_match::monitor(
pool.clone(),
tx_user_feed.clone(),
auth_users_notifier.clone(),
network,
node.inner.oracle_pubkey,
);
let _handle = rollover::monitor(
pool.clone(),
Expand Down
4 changes: 2 additions & 2 deletions coordinator/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ impl Node {
// The contract input to be used for setting up the trade between the trader and the
// coordinator
let event_id = format!("{contract_symbol}{maturity_time}");
tracing::debug!(event_id, oracle=%self.inner.oracle_pubkey, "Proposing dlc channel");
tracing::debug!(event_id, oracle=%trade_params.filled_with.oracle_pk, "Proposing dlc channel");
let contract_input = ContractInput {
offer_collateral: margin_coordinator - fee,
// the accepting party has do bring in additional margin for the fees
Expand All @@ -303,7 +303,7 @@ impl Node {
contract_infos: vec![ContractInputInfo {
contract_descriptor,
oracles: OracleInput {
public_keys: vec![self.inner.oracle_pubkey],
public_keys: vec![trade_params.filled_with.oracle_pk],
event_id,
threshold: 1,
},
Expand Down
17 changes: 9 additions & 8 deletions coordinator/src/orderbook/async_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use orderbook_commons::Matches;
use orderbook_commons::Message;
use orderbook_commons::OrderReason;
use orderbook_commons::OrderState;
use std::str::FromStr;
use time::OffsetDateTime;
use tokio::sync::broadcast;
use tokio::sync::mpsc;
Expand All @@ -28,6 +27,7 @@ pub fn monitor(
tx_user_feed: broadcast::Sender<NewUserMessage>,
notifier: mpsc::Sender<OrderbookMessage>,
network: Network,
oracle_pk: XOnlyPublicKey,
) -> RemoteHandle<Result<()>> {
let mut user_feed = tx_user_feed.subscribe();
let (fut, remote_handle) = async move {
Expand All @@ -37,7 +37,7 @@ pub fn monitor(
let notifier = notifier.clone();
async move {
tracing::debug!(trader_id=%new_user_msg.new_user, "Checking if the user needs to be notified about pending matches");
if let Err(e) = process_pending_match(&mut conn, notifier, new_user_msg.new_user, network).await {
if let Err(e) = process_pending_match(&mut conn, notifier, new_user_msg.new_user, network, oracle_pk).await {
tracing::error!("Failed to process pending match. Error: {e:#}");
}
}
Expand All @@ -57,12 +57,13 @@ async fn process_pending_match(
notifier: mpsc::Sender<OrderbookMessage>,
trader_id: PublicKey,
network: Network,
oracle_pk: XOnlyPublicKey,
) -> Result<()> {
if let Some(order) = orders::get_by_trader_id_and_state(conn, trader_id, OrderState::Matched)? {
tracing::debug!(%trader_id, order_id=%order.id, "Notifying trader about pending match");

let matches = matches::get_matches_by_order_id(conn, order.id)?;
let filled_with = get_filled_with_from_matches(matches, network)?;
let filled_with = get_filled_with_from_matches(matches, network, oracle_pk)?;

let message = match order.order_reason {
OrderReason::Manual => Message::Match(filled_with),
Expand All @@ -85,7 +86,11 @@ async fn process_pending_match(
Ok(())
}

fn get_filled_with_from_matches(matches: Vec<Matches>, network: Network) -> Result<FilledWith> {
fn get_filled_with_from_matches(
matches: Vec<Matches>,
network: Network,
oracle_pk: XOnlyPublicKey,
) -> Result<FilledWith> {
ensure!(
!matches.is_empty(),
"Need at least one matches record to construct a FilledWith"
Expand All @@ -95,10 +100,6 @@ fn get_filled_with_from_matches(matches: Vec<Matches>, network: Network) -> Resu
.first()
.expect("to have at least one match")
.order_id;
let oracle_pk = XOnlyPublicKey::from_str(
"16f88cf7d21e6c0f46bcbc983a4e3b19726c6c98858cc31c83551a88fde171c0",
)
.expect("To be a valid pubkey");

let expiry_timestamp =
coordinator_commons::calculate_next_expiry(OffsetDateTime::now_utc(), network);
Expand Down
85 changes: 53 additions & 32 deletions coordinator/src/orderbook/trading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use orderbook_commons::OrderState;
use orderbook_commons::OrderType;
use rust_decimal::Decimal;
use std::cmp::Ordering;
use std::str::FromStr;
use thiserror::Error;
use time::OffsetDateTime;
use tokio::sync::broadcast;
Expand Down Expand Up @@ -88,6 +87,7 @@ pub fn start(
tx_price_feed: broadcast::Sender<Message>,
notifier: mpsc::Sender<OrderbookMessage>,
network: Network,
oracle_pk: XOnlyPublicKey,
) -> (RemoteHandle<Result<()>>, mpsc::Sender<NewOrderMessage>) {
let (sender, mut receiver) = mpsc::channel::<NewOrderMessage>(NEW_ORDERS_BUFFER_SIZE);

Expand All @@ -106,6 +106,7 @@ pub fn start(
new_order,
new_order_msg.order_reason,
network,
oracle_pk,
)
.await;
if let Err(e) = new_order_msg.sender.send(result).await {
Expand Down Expand Up @@ -138,6 +139,7 @@ async fn process_new_order(
new_order: NewOrder,
order_reason: OrderReason,
network: Network,
oracle_pk: XOnlyPublicKey,
) -> Result<Order> {
tracing::info!(trader_id=%new_order.trader_id, "Received a new {:?} order", new_order.order_type);

Expand Down Expand Up @@ -183,26 +185,27 @@ async fn process_new_order(
true,
)?;

let matched_orders = match match_order(&order, opposite_direction_orders, network) {
Ok(Some(matched_orders)) => matched_orders,
Ok(None) => {
// TODO(holzeis): Currently we still respond to the user immediately if there
// has been a match or not, that's the reason why we also
// have to set the order to failed here. But actually we
// could keep the order until either expired or a
// match has been found and then update the state correspondingly.

orders::set_order_state(conn, order.id, OrderState::Failed)?;
bail!(TradingError::NoMatchFound(format!(
"Could not match order {}",
order.id
)));
}
Err(e) => {
orders::set_order_state(conn, order.id, OrderState::Failed)?;
bail!("Failed to match order. Error {e:#}")
}
};
let matched_orders =
match match_order(&order, opposite_direction_orders, network, oracle_pk) {
Ok(Some(matched_orders)) => matched_orders,
Ok(None) => {
// TODO(holzeis): Currently we still respond to the user immediately if there
// has been a match or not, that's the reason why we also
// have to set the order to failed here. But actually we
// could keep the order until either expired or a
// match has been found and then update the state correspondingly.

orders::set_order_state(conn, order.id, OrderState::Failed)?;
bail!(TradingError::NoMatchFound(format!(
"Could not match order {}",
order.id
)));
}
Err(e) => {
orders::set_order_state(conn, order.id, OrderState::Failed)?;
bail!("Failed to match order. Error {e:#}")
}
};

tracing::info!(trader_id=%order.trader_id, order_id=%order.id, "Found a match with {} makers for new order.", matched_orders.taker_match.filled_with.matches.len());

Expand Down Expand Up @@ -277,6 +280,7 @@ fn match_order(
order: &Order,
opposite_direction_orders: Vec<Order>,
network: Network,
oracle_pk: XOnlyPublicKey,
) -> Result<Option<MatchParams>> {
if order.order_type == OrderType::Limit {
// we don't match limit and limit at the moment
Expand Down Expand Up @@ -315,12 +319,6 @@ fn match_order(
let expiry_timestamp =
coordinator_commons::calculate_next_expiry(OffsetDateTime::now_utc(), network);

// For now we hardcode the oracle pubkey here
let oracle_pk = XOnlyPublicKey::from_str(
"16f88cf7d21e6c0f46bcbc983a4e3b19726c6c98858cc31c83551a88fde171c0",
)
.expect("To be a valid pubkey");

let matches = matched_orders
.iter()
.map(|maker_order| {
Expand Down Expand Up @@ -404,6 +402,7 @@ pub mod tests {
use crate::orderbook::trading::sort_orders;
use bitcoin::secp256k1::PublicKey;
use bitcoin::Network;
use bitcoin::XOnlyPublicKey;
use orderbook_commons::Order;
use orderbook_commons::OrderReason;
use orderbook_commons::OrderState;
Expand Down Expand Up @@ -583,9 +582,14 @@ pub mod tests {
stable: false,
};

let matched_orders = match_order(&order, all_orders, Network::Bitcoin)
.unwrap()
.unwrap();
let matched_orders = match_order(
&order,
all_orders,
Network::Bitcoin,
get_oracle_public_key(),
)
.unwrap()
.unwrap();

assert_eq!(matched_orders.makers_matches.len(), 1);
let maker_matches = matched_orders
Expand Down Expand Up @@ -660,7 +664,13 @@ pub mod tests {
stable: false,
};

assert!(match_order(&order, all_orders, Network::Bitcoin).is_err());
assert!(match_order(
&order,
all_orders,
Network::Bitcoin,
get_oracle_public_key()
)
.is_err());
}

#[test]
Expand Down Expand Up @@ -711,8 +721,19 @@ pub mod tests {
stable: false,
};

let matched_orders = match_order(&order, all_orders, Network::Bitcoin).unwrap();
let matched_orders = match_order(
&order,
all_orders,
Network::Bitcoin,
get_oracle_public_key(),
)
.unwrap();

assert!(matched_orders.is_none());
}

fn get_oracle_public_key() -> XOnlyPublicKey {
XOnlyPublicKey::from_str("16f88cf7d21e6c0f46bcbc983a4e3b19726c6c98858cc31c83551a88fde171c0")
.unwrap()
}
}

0 comments on commit 4884aee

Please sign in to comment.