Skip to content

Commit

Permalink
nit changes using Amount
Browse files Browse the repository at this point in the history
  • Loading branch information
bennyhodl committed Dec 16, 2024
1 parent 04cac5f commit b939cc1
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 24 deletions.
12 changes: 8 additions & 4 deletions dlc-manager/src/contract/accepted_contract.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! # AcceptedContract
use crate::Error;

use super::offered_contract::OfferedContract;
use super::AdaptorInfo;
Expand Down Expand Up @@ -75,7 +76,7 @@ impl AcceptedContract {
}

/// Compute the profit and loss for this contract and an assciated cet index
pub fn compute_pnl(&self, cet: &Transaction) -> SignedAmount {
pub fn compute_pnl(&self, cet: &Transaction) -> Result<SignedAmount, Error> {
let offer = &self.offered_contract;
let party_params = if offer.is_offer_party {
&offer.offer_params
Expand All @@ -95,7 +96,8 @@ impl AcceptedContract {
}
})
.unwrap_or(Amount::ZERO);
SignedAmount::from_sat(final_payout.to_sat() as i64 - collateral.to_sat() as i64)
Ok(final_payout.to_signed().map_err(|_| Error::OutOfRange)?
- collateral.to_signed().map_err(|_| Error::OutOfRange)?)
}
}

Expand All @@ -113,11 +115,13 @@ mod tests {
let accepted_contract: AcceptedContract = Readable::read(&mut Cursor::new(&buf)).unwrap();
let cets = &accepted_contract.dlc_transactions.cets;
assert_eq!(
accepted_contract.compute_pnl(&cets[0]),
accepted_contract.compute_pnl(&cets[0]).unwrap(),
SignedAmount::from_sat(90000000)
);
assert_eq!(
accepted_contract.compute_pnl(&cets[cets.len() - 1]),
accepted_contract
.compute_pnl(&cets[cets.len() - 1])
.unwrap(),
SignedAmount::from_sat(-11000000)
);
}
Expand Down
2 changes: 1 addition & 1 deletion dlc-manager/src/contract/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl_dlc_writeable!(ClosedContract, {
(contract_id, writeable),
(temporary_contract_id, writeable),
(counter_party_id, writeable),
(pnl, i64)
(pnl, SignedAmount)
});
impl_dlc_writeable!(FailedAcceptContract, {(offered_contract, writeable), (accept_message, writeable), (error_message, string)});
impl_dlc_writeable!(FailedSignContract, {(accepted_contract, writeable), (sign_message, writeable), (error_message, string)});
Expand Down
4 changes: 4 additions & 0 deletions dlc-manager/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub enum Error {
DlcError(dlc::Error),
/// An error occurred in the Secp library.
SecpError(secp256k1_zkp::Error),
/// A computation was out of range.
OutOfRange,
}

impl fmt::Display for Error {
Expand All @@ -43,6 +45,7 @@ impl fmt::Display for Error {
Error::DlcError(ref e) => write!(f, "Dlc error {}", e),
Error::OracleError(ref s) => write!(f, "Oracle error {}", s),
Error::SecpError(_) => write!(f, "Secp error"),
Error::OutOfRange => write!(f, "Out of range error"),
}
}
}
Expand Down Expand Up @@ -98,6 +101,7 @@ impl std::error::Error for Error {
Error::OracleError(_) => None,
Error::DlcError(e) => Some(e),
Error::SecpError(e) => Some(e),
Error::OutOfRange => None,
}
}
}
6 changes: 3 additions & 3 deletions dlc-manager/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ where
pnl: contract
.signed_contract
.accepted_contract
.compute_pnl(&contract.signed_cet),
.compute_pnl(&contract.signed_cet)?,
};
self.store
.update_contract(&Contract::Closed(closed_contract))?;
Expand Down Expand Up @@ -800,7 +800,7 @@ where

let closed_contract = ClosedContract {
attestations: Some(attestations.to_vec()),
pnl: contract.accepted_contract.compute_pnl(&signed_cet),
pnl: contract.accepted_contract.compute_pnl(&signed_cet)?,
signed_cet: Some(signed_cet),
contract_id: contract.accepted_contract.get_contract_id(),
temporary_contract_id: contract.accepted_contract.offered_contract.id,
Expand Down Expand Up @@ -883,7 +883,7 @@ where
} else {
Contract::Closed(ClosedContract {
attestations: None, // todo in some cases we can get the attestations from the closing tx
pnl: contract.accepted_contract.compute_pnl(&closing_tx),
pnl: contract.accepted_contract.compute_pnl(&closing_tx)?,
signed_cet: Some(closing_tx),
contract_id: contract.accepted_contract.get_contract_id(),
temporary_contract_id: contract.accepted_contract.offered_contract.id,
Expand Down
15 changes: 8 additions & 7 deletions dlc-manager/src/payout_curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ trait Evaluable {
total_collateral: Amount,
) -> Result<Amount, Error> {
let payout_double = self.evaluate(outcome);
let total_collateral_sats = total_collateral.to_sat();
if payout_double.is_sign_negative() || (payout_double != 0.0 && !payout_double.is_normal())
{
return Err(Error::InvalidParameters(format!(
Expand All @@ -175,7 +176,7 @@ trait Evaluable {
)));
}

if payout_double.round() > total_collateral.to_sat() as f64 {
if payout_double.round() > total_collateral_sats as f64 {
return Err(Error::InvalidParameters(
"Computed payout is greater than total collateral".to_string(),
));
Expand All @@ -184,7 +185,7 @@ trait Evaluable {
// Ensure that we never round over the total collateral.
Ok(Amount::from_sat(u64::min(
rounding_intervals.round(outcome, payout_double),
total_collateral.to_sat(),
total_collateral_sats,
)))
}

Expand Down Expand Up @@ -309,14 +310,14 @@ impl Evaluable for PolynomialPayoutCurvePiece {
// Optimizations for constant and linear cases.
if nb_points == 2 {
let (left_point, right_point) = (&self.payout_points[0], &self.payout_points[1]);
let right_point_payout_sats = right_point.outcome_payout.to_sat() as f64;
let left_point_payout_sats = left_point.outcome_payout.to_sat() as f64;
return if left_point.outcome_payout == right_point.outcome_payout {
right_point.outcome_payout.to_sat() as f64
right_point_payout_sats
} else {
let slope = (right_point.outcome_payout.to_sat() as f64
- left_point.outcome_payout.to_sat() as f64)
let slope = (right_point_payout_sats - left_point_payout_sats)
/ (right_point.event_outcome - left_point.event_outcome) as f64;
(outcome - left_point.event_outcome) as f64 * slope
+ left_point.outcome_payout.to_sat() as f64
(outcome - left_point.event_outcome) as f64 * slope + left_point_payout_sats
};
}

Expand Down
15 changes: 10 additions & 5 deletions dlc-messages/src/ser_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,17 +568,22 @@ pub fn read_i32<R: Read>(reader: &mut R) -> Result<i32, DecodeError> {
let v: [u8; 4] = Readable::read(reader)?;
Ok(i32::from_be_bytes(v))
}
/// Writes an `i64` value to the given writer.
pub fn write_i64<W: Writer>(i: &i64, writer: &mut W) -> Result<(), ::lightning::io::Error> {
let i = i.to_be_bytes();
/// Writes a `SignedAmount` value to the given writer.
pub fn write_signed_amount<W: Writer>(
i: &SignedAmount,
writer: &mut W,
) -> Result<(), ::lightning::io::Error> {
let i = i.to_sat().to_be_bytes();
for b in i {
b.write(writer)?;
}
Ok(())
}

/// Reads an `i64` value from the given reader.
pub fn read_i64<R: ::lightning::io::Read>(reader: &mut R) -> Result<SignedAmount, DecodeError> {
/// Reads a `SignedAmount` value from the given reader.
pub fn read_signed_amount<R: ::lightning::io::Read>(
reader: &mut R,
) -> Result<SignedAmount, DecodeError> {
let mut v = [0u8; 8];
for x in &mut v {
*x = Readable::read(reader)?;
Expand Down
8 changes: 4 additions & 4 deletions dlc-messages/src/ser_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ macro_rules! field_write {
($stream: expr, $field: expr, usize) => {
$crate::ser_impls::write_usize(&$field, $stream)?;
};
($stream: expr, $field: expr, i64) => {
$crate::ser_impls::write_i64(&$field.to_sat(), $stream)?;
($stream: expr, $field: expr, SignedAmount) => {
$crate::ser_impls::write_signed_amount(&$field, $stream)?;
};
($stream: expr, $field: expr, {option_cb, $w_cb: expr, $r_cb: expr}) => {
$crate::ser_impls::write_option_cb(&$field, $stream, &$w_cb)?;
Expand Down Expand Up @@ -65,8 +65,8 @@ macro_rules! field_read {
($stream: expr, usize) => {
$crate::ser_impls::read_usize($stream)?
};
($stream: expr, i64) => {
$crate::ser_impls::read_i64($stream)?
($stream: expr, SignedAmount) => {
$crate::ser_impls::read_signed_amount($stream)?
};
($stream: expr, {option_cb, $w_cb: expr, $r_cb: expr}) => {
$crate::ser_impls::read_option_cb($stream, &$r_cb)?
Expand Down

0 comments on commit b939cc1

Please sign in to comment.