Skip to content

Commit

Permalink
fix: overflow bug in payout curve
Browse files Browse the repository at this point in the history
It is possible that a payout curve decreases with the amount of payout as the events increase. This lead to an overflow panic in the evaluate function
  • Loading branch information
bonomat committed Oct 31, 2023
1 parent 39882fe commit f233718
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions dlc-manager/src/payout_curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ impl Evaluable for PolynomialPayoutCurvePiece {
return if left_point.outcome_payout == right_point.outcome_payout {
right_point.outcome_payout as f64
} else {
let slope = (right_point.outcome_payout - left_point.outcome_payout) as f64
/ (right_point.event_outcome - left_point.event_outcome) as f64;
let slope = (right_point.outcome_payout as f64 - left_point.outcome_payout as f64)
/ (right_point.event_outcome as f64 - left_point.event_outcome as f64);
(outcome - left_point.event_outcome) as f64 * slope
+ left_point.outcome_payout as f64
};
Expand Down Expand Up @@ -1248,4 +1248,46 @@ mod test {
.to_range_payouts(87455, &rounding_intervals)
.expect("Not to fail");
}

#[test]
fn monotonic_increasing_payout_curve_is_valid() {
let polynomial = PolynomialPayoutCurvePiece {
payout_points: vec![
PayoutPoint {
event_outcome: 0,
outcome_payout: 1,
extra_precision: 0,
},
PayoutPoint {
event_outcome: 2,
outcome_payout: 5,
extra_precision: 0,
},
],
};

assert_eq!(polynomial.evaluate(0), 1.0);
assert_eq!(polynomial.evaluate(2), 5.0);
}

#[test]
fn monotonic_decreasing_payout_curve_is_valid() {
let polynomial = PolynomialPayoutCurvePiece {
payout_points: vec![
PayoutPoint {
event_outcome: 0,
outcome_payout: 10,
extra_precision: 0,
},
PayoutPoint {
event_outcome: 1,
outcome_payout: 8,
extra_precision: 0,
},
],
};

assert_eq!(polynomial.evaluate(0), 10.0);
assert_eq!(polynomial.evaluate(1), 8.0);
}
}

0 comments on commit f233718

Please sign in to comment.