reusable method to check if an outcome is valid

This commit is contained in:
conduition
2024-03-18 00:32:24 +00:00
parent 71334d99f9
commit fc29bd27b6
2 changed files with 12 additions and 12 deletions

View File

@@ -118,18 +118,9 @@ impl ContractParameters {
for (outcome, payout_map) in self.outcome_payouts.iter() {
// Check for unknown outcomes.
match outcome {
&Outcome::Attestation(outcome_index) => {
if outcome_index >= self.event.outcome_messages.len() {
return Err(Error);
}
}
Outcome::Expiry => {
if self.event.expiry.is_none() {
return Err(Error);
}
}
};
if !self.event.is_valid_outcome(outcome) {
return Err(Error);
}
// Check for empty payout map.
if payout_map.len() == 0 {

View File

@@ -65,6 +65,15 @@ impl EventAnnouncement {
Some(k + e * d)
}
/// Returns true if the given outcome is a valid outcome to wager on
/// for this event.
pub fn is_valid_outcome(&self, outcome: &Outcome) -> bool {
match outcome {
&Outcome::Attestation(i) => i < self.outcome_messages.len(),
Outcome::Expiry => self.expiry.is_some(),
}
}
/// Returns an iterator over all possible outcomes in the event.
pub fn all_outcomes(&self) -> impl IntoIterator<Item = Outcome> {
(0..self.outcome_messages.len())