-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace quote with spot-price protocol
This is essentially functionally equivalent but includes some cleanups by removing a layer of abstraction: `spot_price::Behaviour` is now just a type-alias for a request-response behaviour.
- Loading branch information
1 parent
2dbc177
commit 63ab4e8
Showing
11 changed files
with
292 additions
and
349 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use crate::{bitcoin, monero, network::request_response::CborCodec}; | ||
use libp2p::{ | ||
core::ProtocolName, | ||
request_response::{ | ||
ProtocolSupport, RequestResponse, RequestResponseConfig, RequestResponseEvent, | ||
}, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
pub type OutEvent = RequestResponseEvent<SpotPriceRequest, SpotPriceResponse>; | ||
|
||
/// The spot price protocol allows parties to **initiate** a trade by requesting | ||
/// a spot price. | ||
/// | ||
/// A spot price is binding for both parties, i.e. after the spot-price protocol | ||
/// completes, both parties are expected to follow up with the `execution-setup` | ||
/// protocol. | ||
/// | ||
/// If a party wishes to only inquire about the current price, they should use | ||
/// the `quote` protocol instead. | ||
#[derive(Debug, Clone, Copy, Default)] | ||
pub struct SpotPriceProtocol; | ||
|
||
impl ProtocolName for SpotPriceProtocol { | ||
fn protocol_name(&self) -> &[u8] { | ||
b"/comit/xmr/btc/spot-price/1.0.0" | ||
} | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
pub struct SpotPriceRequest { | ||
#[serde(with = "::bitcoin::util::amount::serde::as_sat")] | ||
pub btc: bitcoin::Amount, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
pub struct SpotPriceResponse { | ||
pub xmr: monero::Amount, | ||
} | ||
|
||
pub type Behaviour = | ||
RequestResponse<CborCodec<SpotPriceProtocol, SpotPriceRequest, SpotPriceResponse>>; | ||
|
||
/// Constructs a new instance of the `spot-price` behaviour to be used by Alice. | ||
/// | ||
/// Alice only supports inbound connections, i.e. providing spot prices for BTC | ||
/// in XMR. | ||
pub fn alice() -> Behaviour { | ||
Behaviour::new( | ||
CborCodec::default(), | ||
vec![(SpotPriceProtocol, ProtocolSupport::Inbound)], | ||
RequestResponseConfig::default(), | ||
) | ||
} | ||
|
||
/// Constructs a new instance of the `spot-price` behaviour to be used by Bob. | ||
/// | ||
/// Bob only supports outbound connections, i.e. requesting a spot price for a | ||
/// given amount of BTC in XMR. | ||
pub fn bob() -> Behaviour { | ||
Behaviour::new( | ||
CborCodec::default(), | ||
vec![(SpotPriceProtocol, ProtocolSupport::Outbound)], | ||
RequestResponseConfig::default(), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.