use crate::client::evm::precompiles::fields::{Fields, tokens};
use crate::client::evm::precompiles::read::{Arg, Read, read};
use crate::utils::Dec;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct OutcomeStatus {
pub status: u8,
pub settled_value: u64,
pub question: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OutcomeState {
NeverExisted,
Active,
Settled,
Pruned,
Other(u8),
}
pub const SETTLED_DECIMALS: u32 = 8;
impl OutcomeStatus {
#[must_use]
pub const fn state(&self) -> OutcomeState {
match self.status {
0 => OutcomeState::NeverExisted,
1 => OutcomeState::Active,
2 => OutcomeState::Settled,
3 => OutcomeState::Pruned,
other => OutcomeState::Other(other),
}
}
#[must_use]
pub const fn in_question(&self) -> Option<u32> {
match self.question {
u32::MAX => None,
question => Some(question),
}
}
#[must_use]
pub fn settled(&self) -> Option<Dec> {
match self.state() {
OutcomeState::Settled => Dec::new(i128::from(self.settled_value), SETTLED_DECIMALS),
_ => None,
}
}
}
#[must_use]
pub fn outcome_status(outcome: u32) -> Read<OutcomeStatus> {
read(0x814, &[Arg::Uint(u64::from(outcome))], |answer| {
let tokens = tokens(0x814, answer)?;
let mut fields = Fields::of("outcomeStatus", &tokens)?;
Ok(OutcomeStatus { status: fields.u8()?, settled_value: fields.u64()?, question: fields.u32()? })
})
}
#[cfg(test)]
#[path = "tests/outcomes.rs"]
mod tests;