|
| 1 | +//! Policy bind/terminate: auth, voter registry, termination metadata, events. |
| 2 | +
|
| 3 | +use crate::{ |
| 4 | + storage, |
| 5 | + types::{Policy, PolicyType, RegionTier, TerminationReason}, |
| 6 | + validate, |
| 7 | +}; |
| 8 | +use soroban_sdk::{contracterror, contractevent, Address, Env}; |
| 9 | + |
| 10 | +#[contracterror] |
| 11 | +#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] |
| 12 | +#[repr(u32)] |
| 13 | +pub enum PolicyError { |
| 14 | + PolicyNotFound = 1, |
| 15 | + Unauthorized = 2, |
| 16 | + AlreadyInactive = 3, |
| 17 | + OpenClaimsMustFinalize = 4, |
| 18 | + InvalidCoverage = 5, |
| 19 | + InvalidPremium = 6, |
| 20 | + InvalidTermLedgers = 7, |
| 21 | + LedgerOverflow = 8, |
| 22 | + InvalidTerminationReason = 9, |
| 23 | + HolderMismatch = 10, |
| 24 | +} |
| 25 | + |
| 26 | +pub fn initiate_policy( |
| 27 | + env: &Env, |
| 28 | + holder: Address, |
| 29 | + policy_type: PolicyType, |
| 30 | + region: RegionTier, |
| 31 | + coverage: i128, |
| 32 | + premium: i128, |
| 33 | + term_ledgers: u32, |
| 34 | +) -> Result<u32, PolicyError> { |
| 35 | + holder.require_auth(); |
| 36 | + |
| 37 | + if coverage <= 0 { |
| 38 | + return Err(PolicyError::InvalidCoverage); |
| 39 | + } |
| 40 | + if premium <= 0 { |
| 41 | + return Err(PolicyError::InvalidPremium); |
| 42 | + } |
| 43 | + if term_ledgers == 0 { |
| 44 | + return Err(PolicyError::InvalidTermLedgers); |
| 45 | + } |
| 46 | + |
| 47 | + let now = env.ledger().sequence(); |
| 48 | + let end_ledger = now |
| 49 | + .checked_add(term_ledgers) |
| 50 | + .ok_or(PolicyError::LedgerOverflow)?; |
| 51 | + |
| 52 | + let policy_id = storage::next_policy_id(env, &holder); |
| 53 | + |
| 54 | + let policy = Policy { |
| 55 | + holder: holder.clone(), |
| 56 | + policy_id, |
| 57 | + policy_type, |
| 58 | + region, |
| 59 | + premium, |
| 60 | + coverage, |
| 61 | + is_active: true, |
| 62 | + start_ledger: now, |
| 63 | + end_ledger, |
| 64 | + terminated_at_ledger: 0, |
| 65 | + termination_reason: TerminationReason::None, |
| 66 | + terminated_by_admin: false, |
| 67 | + }; |
| 68 | + |
| 69 | + validate::check_policy(&policy).map_err(|e| match e { |
| 70 | + validate::Error::ZeroCoverage => PolicyError::InvalidCoverage, |
| 71 | + validate::Error::ZeroPremium => PolicyError::InvalidPremium, |
| 72 | + validate::Error::InvalidLedgerWindow => PolicyError::InvalidTermLedgers, |
| 73 | + _ => PolicyError::InvalidCoverage, |
| 74 | + })?; |
| 75 | + |
| 76 | + storage::set_policy(env, &policy); |
| 77 | + storage::increment_holder_active_policies(env, &holder); |
| 78 | + storage::voters_ensure_holder(env, &holder); |
| 79 | + |
| 80 | + Ok(policy_id) |
| 81 | +} |
| 82 | + |
| 83 | +/// Holder-initiated termination. Blocks while `OpenClaimCount(holder, policy_id) > 0`. |
| 84 | +pub fn terminate_policy( |
| 85 | + env: &Env, |
| 86 | + holder: Address, |
| 87 | + policy_id: u32, |
| 88 | + reason: TerminationReason, |
| 89 | +) -> Result<(), PolicyError> { |
| 90 | + holder.require_auth(); |
| 91 | + terminate_inner(env, &holder, policy_id, reason, false, false) |
| 92 | +} |
| 93 | + |
| 94 | +/// Admin termination (audited). `allow_open_claims` documents explicit acceptance |
| 95 | +/// that in-flight claims may lack a normal resolution path — indexers read the flag. |
| 96 | +pub fn admin_terminate_policy( |
| 97 | + env: &Env, |
| 98 | + admin: Address, |
| 99 | + holder: Address, |
| 100 | + policy_id: u32, |
| 101 | + reason: TerminationReason, |
| 102 | + allow_open_claims: bool, |
| 103 | +) -> Result<(), PolicyError> { |
| 104 | + admin.require_auth(); |
| 105 | + let expected = storage::get_admin(env); |
| 106 | + if admin != expected { |
| 107 | + return Err(PolicyError::Unauthorized); |
| 108 | + } |
| 109 | + |
| 110 | + terminate_inner(env, &holder, policy_id, reason, true, allow_open_claims) |
| 111 | +} |
| 112 | + |
| 113 | +fn terminate_inner( |
| 114 | + env: &Env, |
| 115 | + holder: &Address, |
| 116 | + policy_id: u32, |
| 117 | + reason: TerminationReason, |
| 118 | + by_admin: bool, |
| 119 | + allow_open_claim_bypass: bool, |
| 120 | +) -> Result<(), PolicyError> { |
| 121 | + if reason == TerminationReason::None { |
| 122 | + return Err(PolicyError::InvalidTerminationReason); |
| 123 | + } |
| 124 | + |
| 125 | + let mut policy = |
| 126 | + storage::get_policy(env, holder, policy_id).ok_or(PolicyError::PolicyNotFound)?; |
| 127 | + |
| 128 | + if policy.holder != *holder { |
| 129 | + return Err(PolicyError::HolderMismatch); |
| 130 | + } |
| 131 | + |
| 132 | + if !policy.is_active { |
| 133 | + return Err(PolicyError::AlreadyInactive); |
| 134 | + } |
| 135 | + |
| 136 | + let open = storage::get_open_claim_count(env, holder, policy_id); |
| 137 | + if open > 0 && (!by_admin || !allow_open_claim_bypass) { |
| 138 | + return Err(PolicyError::OpenClaimsMustFinalize); |
| 139 | + } |
| 140 | + |
| 141 | + let now = env.ledger().sequence(); |
| 142 | + policy.is_active = false; |
| 143 | + policy.terminated_at_ledger = now; |
| 144 | + policy.termination_reason = reason; |
| 145 | + policy.terminated_by_admin = by_admin; |
| 146 | + |
| 147 | + storage::set_policy(env, &policy); |
| 148 | + storage::decrement_holder_active_policies(env, holder); |
| 149 | + if storage::get_holder_active_policy_count(env, holder) == 0 { |
| 150 | + storage::voters_remove_holder(env, holder); |
| 151 | + } |
| 152 | + |
| 153 | + emit_policy_terminated( |
| 154 | + env, |
| 155 | + holder, |
| 156 | + policy_id, |
| 157 | + reason, |
| 158 | + by_admin, |
| 159 | + allow_open_claim_bypass && open > 0, |
| 160 | + open, |
| 161 | + ); |
| 162 | + |
| 163 | + Ok(()) |
| 164 | +} |
| 165 | + |
| 166 | +#[contractevent(topics = ["niffyinsure", "policy_terminated"])] |
| 167 | +#[derive(Clone, Debug, Eq, PartialEq)] |
| 168 | +pub struct PolicyTerminated { |
| 169 | + #[topic] |
| 170 | + pub holder: Address, |
| 171 | + #[topic] |
| 172 | + pub policy_id: u32, |
| 173 | + pub reason_code: u32, |
| 174 | + pub terminated_by_admin: u32, |
| 175 | + pub open_claim_bypass: u32, |
| 176 | + pub open_claims: u32, |
| 177 | + pub at_ledger: u32, |
| 178 | +} |
| 179 | + |
| 180 | +fn emit_policy_terminated( |
| 181 | + env: &Env, |
| 182 | + holder: &Address, |
| 183 | + policy_id: u32, |
| 184 | + reason: TerminationReason, |
| 185 | + terminated_by_admin: bool, |
| 186 | + open_claim_bypass: bool, |
| 187 | + open_claims: u32, |
| 188 | +) { |
| 189 | + let reason_code = termination_reason_tag(reason); |
| 190 | + let bypass_flag: u32 = if open_claim_bypass { 1 } else { 0 }; |
| 191 | + let admin_flag: u32 = if terminated_by_admin { 1 } else { 0 }; |
| 192 | + PolicyTerminated { |
| 193 | + holder: holder.clone(), |
| 194 | + policy_id, |
| 195 | + reason_code, |
| 196 | + terminated_by_admin: admin_flag, |
| 197 | + open_claim_bypass: bypass_flag, |
| 198 | + open_claims, |
| 199 | + at_ledger: env.ledger().sequence(), |
| 200 | + } |
| 201 | + .publish(env); |
| 202 | +} |
| 203 | + |
| 204 | +fn termination_reason_tag(reason: TerminationReason) -> u32 { |
| 205 | + match reason { |
| 206 | + TerminationReason::None => 0, |
| 207 | + TerminationReason::VoluntaryCancellation => 1, |
| 208 | + TerminationReason::LapsedNonPayment => 2, |
| 209 | + TerminationReason::UnderwritingVoid => 3, |
| 210 | + TerminationReason::FraudOrMisrepresentation => 4, |
| 211 | + TerminationReason::RegulatoryAction => 5, |
| 212 | + TerminationReason::AdminOverride => 6, |
| 213 | + } |
| 214 | +} |
0 commit comments