|
1 | | -// Policy lifecycle methods will be implemented here and exposed via |
2 | | -// NiffyInsure contractimpl in lib.rs. |
3 | | -// |
4 | | -// Planned public functions: |
5 | | -// generate_premium(env, policy_type, age, risk_score) -> i128 |
6 | | -// initiate_policy(env, holder, policy_id, policy_type, coverage, age, risk_score) |
7 | | -// renew_policy(env, holder, policy_id) |
8 | | -// terminate_policy(env, holder, policy_id, reason) |
| 1 | +use crate::{ |
| 2 | + premium, |
| 3 | + types::{PolicyType, PremiumQuote, RegionTier}, |
| 4 | +}; |
| 5 | +use soroban_sdk::{contracterror, contracttype, Env, String}; |
| 6 | + |
| 7 | +/// How long a quote stays valid (in ledgers) from generation time. |
| 8 | +pub const QUOTE_TTL_LEDGERS: u32 = 100; |
| 9 | + |
| 10 | +#[contracterror] |
| 11 | +#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] |
| 12 | +#[repr(u32)] |
| 13 | +pub enum QuoteError { |
| 14 | + InvalidAge = 1, |
| 15 | + InvalidRiskScore = 2, |
| 16 | + InvalidQuoteTtl = 3, |
| 17 | + ArithmeticOverflow = 4, |
| 18 | +} |
| 19 | + |
| 20 | +#[contracttype] |
| 21 | +#[derive(Clone, Debug, Eq, PartialEq)] |
| 22 | +pub struct QuoteFailure { |
| 23 | + pub code: u32, |
| 24 | + pub message: String, |
| 25 | +} |
| 26 | + |
| 27 | +pub fn generate_premium( |
| 28 | + env: &Env, |
| 29 | + policy_type: PolicyType, |
| 30 | + region: RegionTier, |
| 31 | + age: u32, |
| 32 | + risk_score: u32, |
| 33 | + include_breakdown: bool, |
| 34 | +) -> Result<PremiumQuote, QuoteError> { |
| 35 | + if age == 0 || age > 120 { |
| 36 | + return Err(QuoteError::InvalidAge); |
| 37 | + } |
| 38 | + if risk_score == 0 || risk_score > 10 { |
| 39 | + return Err(QuoteError::InvalidRiskScore); |
| 40 | + } |
| 41 | + if QUOTE_TTL_LEDGERS == 0 { |
| 42 | + return Err(QuoteError::InvalidQuoteTtl); |
| 43 | + } |
| 44 | + |
| 45 | + let total = premium::compute_premium_checked(&policy_type, ®ion, age, risk_score) |
| 46 | + .ok_or(QuoteError::ArithmeticOverflow)?; |
| 47 | + |
| 48 | + let line_items = if include_breakdown { |
| 49 | + Some( |
| 50 | + premium::build_line_items(env, &policy_type, ®ion, age, risk_score) |
| 51 | + .ok_or(QuoteError::ArithmeticOverflow)?, |
| 52 | + ) |
| 53 | + } else { |
| 54 | + None |
| 55 | + }; |
| 56 | + |
| 57 | + let current_ledger = env.ledger().sequence(); |
| 58 | + let valid_until_ledger = current_ledger |
| 59 | + .checked_add(QUOTE_TTL_LEDGERS) |
| 60 | + .ok_or(QuoteError::ArithmeticOverflow)?; |
| 61 | + |
| 62 | + Ok(PremiumQuote { |
| 63 | + total_premium: total, |
| 64 | + line_items, |
| 65 | + valid_until_ledger, |
| 66 | + }) |
| 67 | +} |
| 68 | + |
| 69 | +pub fn map_quote_error(env: &Env, err: QuoteError) -> QuoteFailure { |
| 70 | + let message = match err { |
| 71 | + QuoteError::InvalidAge => "invalid age: expected 1..=120", |
| 72 | + QuoteError::InvalidRiskScore => "invalid risk_score: expected 1..=10", |
| 73 | + QuoteError::InvalidQuoteTtl => "quote ttl misconfigured: contact support", |
| 74 | + QuoteError::ArithmeticOverflow => "pricing arithmetic overflow: contact support", |
| 75 | + }; |
| 76 | + QuoteFailure { |
| 77 | + code: err as u32, |
| 78 | + message: String::from_str(env, message), |
| 79 | + } |
| 80 | +} |
0 commit comments