Skip to content

Commit

Permalink
Add Maker request to reject a specific offer
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu-maeda committed Apr 17, 2024
1 parent f917c9b commit 729bbb6
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/maker/maker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ impl MakerAccess {
rsp_rx.await.unwrap()
}

pub async fn reject_offer(&self, trade_rsp: TradeResponse) -> Result<(), N3xbError> {
let (rsp_tx, rsp_rx) = oneshot::channel::<Result<(), N3xbError>>();
let request = MakerRequest::RejectOffer { trade_rsp, rsp_tx };
self.tx.send(request).await.unwrap();
rsp_rx.await.unwrap()
}

pub async fn cancel_order(&self) -> Result<(), N3xbError> {
let (rsp_tx, rsp_rx) = oneshot::channel::<Result<(), N3xbError>>();
let request = MakerRequest::CancelOrder { rsp_tx };
Expand Down Expand Up @@ -168,6 +175,10 @@ pub(super) enum MakerRequest {
trade_rsp: TradeResponse,
rsp_tx: oneshot::Sender<Result<(), N3xbError>>,
},
RejectOffer {
trade_rsp: TradeResponse,
rsp_tx: oneshot::Sender<Result<(), N3xbError>>,
},
CancelOrder {
rsp_tx: oneshot::Sender<Result<(), N3xbError>>,
},
Expand Down Expand Up @@ -283,6 +294,9 @@ impl MakerActor {
MakerRequest::AcceptOffer { trade_rsp, rsp_tx } => {
self.accept_offer(trade_rsp, rsp_tx).await;
}
MakerRequest::RejectOffer { trade_rsp, rsp_tx } => {
self.reject_offer(trade_rsp, rsp_tx).await;
}
MakerRequest::CancelOrder { rsp_tx } => {
self.cancel_order(rsp_tx).await;
terminate = true;
Expand Down Expand Up @@ -461,6 +475,65 @@ impl MakerActor {
}
}

async fn reject_offer(
&mut self,
trade_rsp: TradeResponse,
rsp_tx: oneshot::Sender<Result<(), N3xbError>>,
) {
if let Some(error) = self.check_trade_completed().err() {
rsp_tx.send(Err(error)).unwrap(); // oneshot should not fail
return;
}

let offer_event_id = trade_rsp.offer_event_id.clone();

let pubkey = match self.data.offer_envelopes().get(&offer_event_id) {
Some(offer_envelope) => offer_envelope.pubkey.clone(),
None => {
let error = N3xbError::Simple(format!(
"Maker w/ TradeUUID {} expected, but does not contain Offer {}",
self.data.trade_uuid, offer_event_id
));
rsp_tx.send(Err(error)).unwrap(); // oneshot should not fail
return;
}
};

let maker_order_note_id = match self.data.order_event_id() {
Some(event_id) => event_id,
None => {
let error = N3xbError::Simple(
format!(
"Maker w/ TradeUUID {} expected to already have sent Maker Order Note and receive Event ID",
self.data.trade_uuid
)
);
rsp_tx.send(Err(error)).unwrap(); // oneshot should not fail
return;
}
};

let result = self
.comms_accessor
.send_trade_response(
pubkey,
Some(offer_event_id),
maker_order_note_id,
self.data.trade_uuid,
trade_rsp,
)
.await;

match result {
Ok(_) => {
rsp_tx.send(Ok(())).unwrap(); // oneshot should not fail
}
Err(error) => {
rsp_tx.send(Err(error)).unwrap(); // oneshot should not fail
}
}
}

async fn cancel_order(&mut self, rsp_tx: oneshot::Sender<Result<(), N3xbError>>) {
if let Some(error) = self.check_trade_completed().err() {
rsp_tx.send(Err(error)).unwrap(); // oneshot should not fail
Expand Down

0 comments on commit 729bbb6

Please sign in to comment.