Browse Source

Allow quote to be optional

To make boot-up of the system easier, we want allow for a state where
we do not yet have a quote available.

Allow the quote in the projection to be optional and fallback to `None`
for profit calculation.
feature/supervised-connection
Thomas Eizinger 3 years ago
parent
commit
3385ad5a22
No known key found for this signature in database GPG Key ID: 651AC83A6C6C8B96
  1. 25
      daemon/src/projection.rs

25
daemon/src/projection.rs

@ -63,7 +63,7 @@ impl Actor {
network, network,
cfds: init_cfds, cfds: init_cfds,
proposals: HashMap::new(), proposals: HashMap::new(),
quote: init_quote.clone(), quote: Some(init_quote.clone()),
}; };
let (tx_cfds, rx_cfds) = watch::channel(state.to_cfds()); let (tx_cfds, rx_cfds) = watch::channel(state.to_cfds());
@ -106,7 +106,7 @@ struct Tx {
struct State { struct State {
role: Role, role: Role,
network: Network, network: Network,
quote: bitmex_price_feed::Quote, quote: Option<bitmex_price_feed::Quote>,
proposals: UpdateCfdProposals, proposals: UpdateCfdProposals,
cfds: Vec<ModelCfd>, cfds: Vec<ModelCfd>,
} }
@ -135,7 +135,7 @@ impl State {
} }
pub fn update_quote(&mut self, quote: bitmex_price_feed::Quote) { pub fn update_quote(&mut self, quote: bitmex_price_feed::Quote) {
let _ = std::mem::replace(&mut self.quote, quote); self.quote = Some(quote);
} }
pub fn update_cfds(&mut self, cfds: Vec<ModelCfd>) { pub fn update_cfds(&mut self, cfds: Vec<ModelCfd>) {
@ -504,14 +504,14 @@ pub struct Cfd {
impl From<CfdsWithAuxData> for Vec<Cfd> { impl From<CfdsWithAuxData> for Vec<Cfd> {
fn from(input: CfdsWithAuxData) -> Self { fn from(input: CfdsWithAuxData) -> Self {
let current_price = input.current_price;
let network = input.network; let network = input.network;
let cfds = input let cfds = input
.cfds .cfds
.iter() .iter()
.map(|cfd| { .map(|cfd| {
let (profit_btc, profit_percent) = match cfd.profit(current_price) { let (profit_btc, profit_percent) = input.current_price
.map(|current_price| match cfd.profit(current_price) {
Ok((profit_btc, profit_percent)) => ( Ok((profit_btc, profit_percent)) => (
Some(profit_btc), Some(profit_btc),
Some(profit_percent.round_dp(1).to_string()), Some(profit_percent.round_dp(1).to_string()),
@ -521,7 +521,12 @@ impl From<CfdsWithAuxData> for Vec<Cfd> {
(None, None) (None, None)
} }
}; })
.unwrap_or_else(|| {
tracing::debug!(order_id = %cfd.order.id, "Unable to calculate profit/loss without current price");
(None, None)
});
let pending_proposal = input.pending_proposals.get(&cfd.order.id); let pending_proposal = input.pending_proposals.get(&cfd.order.id);
let state = to_cfd_state(&cfd.state, pending_proposal); let state = to_cfd_state(&cfd.state, pending_proposal);
@ -562,7 +567,7 @@ impl From<CfdsWithAuxData> for Vec<Cfd> {
// TODO: Remove this struct out of existence // TODO: Remove this struct out of existence
pub struct CfdsWithAuxData { pub struct CfdsWithAuxData {
pub cfds: Vec<model::cfd::Cfd>, pub cfds: Vec<model::cfd::Cfd>,
pub current_price: model::Price, pub current_price: Option<model::Price>,
pub pending_proposals: UpdateCfdProposals, pub pending_proposals: UpdateCfdProposals,
pub network: Network, pub network: Network,
} }
@ -570,15 +575,15 @@ pub struct CfdsWithAuxData {
impl CfdsWithAuxData { impl CfdsWithAuxData {
pub fn new( pub fn new(
cfds: Vec<model::cfd::Cfd>, cfds: Vec<model::cfd::Cfd>,
quote: bitmex_price_feed::Quote, quote: Option<bitmex_price_feed::Quote>,
pending_proposals: UpdateCfdProposals, pending_proposals: UpdateCfdProposals,
role: Role, role: Role,
network: Network, network: Network,
) -> Self { ) -> Self {
let current_price = match role { let current_price = quote.map(|quote| match role {
Role::Maker => quote.for_maker(), Role::Maker => quote.for_maker(),
Role::Taker => quote.for_taker(), Role::Taker => quote.for_taker(),
}; });
CfdsWithAuxData { CfdsWithAuxData {
cfds, cfds,

Loading…
Cancel
Save