Browse Source

Propose settlement based on the payout curve and current BitMex price

refactor/no-log-handler
Mariusz Klochowicz 3 years ago
parent
commit
4ffb9bff91
No known key found for this signature in database GPG Key ID: 470C865699C8D4D
  1. 12
      cfd_protocol/src/protocol.rs
  2. 3
      daemon/src/maker_cfd.rs
  3. 21
      daemon/src/model/cfd.rs
  4. 3
      daemon/src/taker_cfd.rs

12
cfd_protocol/src/protocol.rs

@ -409,6 +409,18 @@ pub fn generate_payouts(
} }
impl Payout { impl Payout {
pub fn digits(&self) -> &interval::Digits {
&self.digits
}
pub fn maker_amount(&self) -> &Amount {
&self.maker_amount
}
pub fn taker_amount(&self) -> &Amount {
&self.taker_amount
}
fn into_txouts(self, maker_address: &Address, taker_address: &Address) -> Vec<TxOut> { fn into_txouts(self, maker_address: &Address, taker_address: &Address) -> Vec<TxOut> {
let txouts = [ let txouts = [
(self.maker_amount, maker_address), (self.maker_amount, maker_address),

3
daemon/src/maker_cfd.rs

@ -5,7 +5,8 @@ use crate::db::{
use crate::maker_inc_connections::TakerCommand; use crate::maker_inc_connections::TakerCommand;
use crate::model::cfd::{ use crate::model::cfd::{
Attestation, Cfd, CfdState, CfdStateChangeEvent, CfdStateCommon, Dlc, Order, OrderId, Origin, Attestation, Cfd, CfdState, CfdStateChangeEvent, CfdStateCommon, Dlc, Order, OrderId, Origin,
Role, RollOverProposal, SettlementKind, SettlementProposal, UpdateCfdProposal, UpdateCfdProposals, Role, RollOverProposal, SettlementKind, SettlementProposal, TimestampedTransaction,
UpdateCfdProposal, UpdateCfdProposals,
}; };
use crate::model::{TakerId, Usd}; use crate::model::{TakerId, Usd};
use crate::monitor::MonitorParams; use crate::monitor::MonitorParams;

21
daemon/src/model/cfd.rs

@ -1,5 +1,5 @@
use crate::model::{BitMexPriceEventId, Leverage, Percent, Position, TakerId, TradingPair, Usd}; use crate::model::{BitMexPriceEventId, Leverage, Percent, Position, TakerId, TradingPair, Usd};
use crate::{monitor, oracle}; use crate::{monitor, oracle, payout_curve};
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use bdk::bitcoin::secp256k1::{SecretKey, Signature}; use bdk::bitcoin::secp256k1::{SecretKey, Signature};
use bdk::bitcoin::{Address, Amount, PublicKey, Script, SignedAmount, Transaction, Txid}; use bdk::bitcoin::{Address, Amount, PublicKey, Script, SignedAmount, Transaction, Txid};
@ -581,14 +581,22 @@ impl Cfd {
} }
#[allow(dead_code)] // Not used by all binaries. #[allow(dead_code)] // Not used by all binaries.
pub fn calculate_settlement(&self, _current_price: Usd) -> Result<SettlementProposal> { pub fn calculate_settlement(&self, current_price: Usd) -> Result<SettlementProposal> {
// TODO: Calculate values for taker and maker let payout_curve =
// For the time being, assume that everybody loses :) payout_curve::calculate(self.order.price, self.quantity_usd, self.order.leverage)?;
let current_price = current_price.try_into_u64()?;
let payout = payout_curve
.iter()
.find(|&x| x.digits().range().contains(&current_price))
.context("find current price on the payout curve")?;
let settlement = SettlementProposal { let settlement = SettlementProposal {
order_id: self.order.id, order_id: self.order.id,
timestamp: SystemTime::now(), timestamp: SystemTime::now(),
taker: Amount::ZERO, taker: *payout.taker_amount(),
maker: Amount::ZERO, maker: *payout.maker_amount(),
}; };
Ok(settlement) Ok(settlement)
@ -947,6 +955,7 @@ impl Cfd {
} => bail!("Cannot transition to PendingClose because Open state did not record a settlement proposal beforehand"), } => bail!("Cannot transition to PendingClose because Open state did not record a settlement proposal beforehand"),
_ => bail!( _ => bail!(
"Cannot transition to PendingClose because of unexpected state {}", "Cannot transition to PendingClose because of unexpected state {}",
self.state)
} }
}; };

3
daemon/src/taker_cfd.rs

@ -4,7 +4,8 @@ use crate::db::{
}; };
use crate::model::cfd::{ use crate::model::cfd::{
Attestation, Cfd, CfdState, CfdStateChangeEvent, CfdStateCommon, Dlc, Order, OrderId, Origin, Attestation, Cfd, CfdState, CfdStateChangeEvent, CfdStateCommon, Dlc, Order, OrderId, Origin,
Role, RollOverProposal, SettlementKind, SettlementProposal, UpdateCfdProposal, UpdateCfdProposals, Role, RollOverProposal, SettlementKind, SettlementProposal, TimestampedTransaction,
UpdateCfdProposal, UpdateCfdProposals,
}; };
use crate::model::{BitMexPriceEventId, Usd}; use crate::model::{BitMexPriceEventId, Usd};
use crate::monitor::{self, MonitorParams}; use crate::monitor::{self, MonitorParams};

Loading…
Cancel
Save