forked from InsurNiffy/niff-Stellar-shurance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolicy.rs
More file actions
80 lines (71 loc) · 2.21 KB
/
Copy pathpolicy.rs
File metadata and controls
80 lines (71 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use crate::{
premium,
types::{PolicyType, PremiumQuote, RegionTier},
};
use soroban_sdk::{contracterror, contracttype, Env, String};
/// How long a quote stays valid (in ledgers) from generation time.
pub const QUOTE_TTL_LEDGERS: u32 = 100;
#[contracterror]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[repr(u32)]
pub enum QuoteError {
InvalidAge = 1,
InvalidRiskScore = 2,
InvalidQuoteTtl = 3,
ArithmeticOverflow = 4,
}
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct QuoteFailure {
pub code: u32,
pub message: String,
}
pub fn generate_premium(
env: &Env,
policy_type: PolicyType,
region: RegionTier,
age: u32,
risk_score: u32,
include_breakdown: bool,
) -> Result<PremiumQuote, QuoteError> {
if age == 0 || age > 120 {
return Err(QuoteError::InvalidAge);
}
if risk_score == 0 || risk_score > 10 {
return Err(QuoteError::InvalidRiskScore);
}
if QUOTE_TTL_LEDGERS == 0 {
return Err(QuoteError::InvalidQuoteTtl);
}
let total = premium::compute_premium_checked(&policy_type, ®ion, age, risk_score)
.ok_or(QuoteError::ArithmeticOverflow)?;
let line_items = if include_breakdown {
Some(
premium::build_line_items(env, &policy_type, ®ion, age, risk_score)
.ok_or(QuoteError::ArithmeticOverflow)?,
)
} else {
None
};
let current_ledger = env.ledger().sequence();
let valid_until_ledger = current_ledger
.checked_add(QUOTE_TTL_LEDGERS)
.ok_or(QuoteError::ArithmeticOverflow)?;
Ok(PremiumQuote {
total_premium: total,
line_items,
valid_until_ledger,
})
}
pub fn map_quote_error(env: &Env, err: QuoteError) -> QuoteFailure {
let message = match err {
QuoteError::InvalidAge => "invalid age: expected 1..=120",
QuoteError::InvalidRiskScore => "invalid risk_score: expected 1..=10",
QuoteError::InvalidQuoteTtl => "quote ttl misconfigured: contact support",
QuoteError::ArithmeticOverflow => "pricing arithmetic overflow: contact support",
};
QuoteFailure {
code: err as u32,
message: String::from_str(env, message),
}
}