use num_traits::cast::ToPrimitive;
use crate::*;
pub const STRIKE_PRICE_UNITS: u64 = 1_000_000_000;
#[account]
#[derive(Default)]
pub struct OptionsContract {
pub underlying_mint: Pubkey,
pub quote_mint: Pubkey,
pub strike: u64,
pub expiry_ts: i64,
pub is_put: bool,
pub bump: u8,
pub writer_mint: Pubkey,
pub writer_crate: Pubkey,
pub crate_collateral_tokens: Pubkey,
pub crate_exercise_tokens: Pubkey,
pub option_mint: Pubkey,
}
impl OptionsContract {
pub fn collateral_mint(&self) -> Pubkey {
if self.is_put {
self.quote_mint
} else {
self.underlying_mint
}
}
pub fn exercise_mint(&self) -> Pubkey {
if self.is_put {
self.underlying_mint
} else {
self.quote_mint
}
}
pub fn calculate_exercise_amount_for_options(&self, option_amount: u64) -> Option<u64> {
if self.is_put {
(option_amount as u128)
.checked_mul(STRIKE_PRICE_UNITS.into())?
.checked_div(self.strike.into())?
.to_u64()
} else {
(option_amount as u128)
.checked_mul(self.strike.into())?
.checked_div(STRIKE_PRICE_UNITS.into())?
.to_u64()
}
}
}