A comprehensive Clarity smart contract for automated vehicle insurance management on the Stacks blockchain. This contract handles policy lifecycle management, premium payments, claims processing, and automated settlements without intermediaries.
- Create new insurance policies with customizable coverage and duration
- Automatic premium collection and reserve pool management
- Policy renewal with extended coverage periods
- Policy cancellation with 50% refund mechanism
- Real-time policy status tracking (active, expired, cancelled)
- File insurance claims against active policies
- Owner-controlled claim approval/rejection workflow
- Automated payout processing from reserve pool
- Complete claim history tracking per policy
- Status tracking: pending, approved, rejected, paid
- Centralized reserve pool for all premiums
- Automated fund validation before payouts
- Owner-controlled reserve withdrawals
- Deposit functionality for reserve replenishment
- Transparent fund tracking
- Authorization checks for all sensitive operations
- Input validation for all user-provided data
- Owner-only administrative controls
- Active policy validation before claims
- Comprehensive error handling with 11 error codes
{
owner: principal,
vehicle-id: string-ascii 50,
premium: uint,
coverage: uint,
start-block: uint,
end-block: uint,
status: uint // 0=inactive, 1=active, 2=expired, 3=cancelled
}{
policy-id: uint,
claimant: principal,
amount: uint,
description: string-ascii 256,
filed-block: uint,
status: uint, // 0=pending, 1=approved, 2=rejected, 3=paid
approved-amount: uint
}(contract-call? .vehicle-insurance create-policy
"ABC-1234-XYZ" ;; vehicle-id
u1000000 ;; premium (1 STX in microSTX)
u50000000 ;; coverage (50 STX)
u52560 ;; duration (~1 year in blocks)
)(contract-call? .vehicle-insurance renew-policy
u1 ;; policy-id
u52560 ;; additional duration
)(contract-call? .vehicle-insurance file-claim
u1 ;; policy-id
u10000000 ;; claim amount (10 STX)
"Vehicle accident on Highway 101, front bumper damage"
)(contract-call? .vehicle-insurance cancel-policy u1)(contract-call? .vehicle-insurance approve-claim
u1 ;; claim-id
u8000000 ;; approved amount (8 STX)
)(contract-call? .vehicle-insurance reject-claim u1)(contract-call? .vehicle-insurance process-payout u1)(contract-call? .vehicle-insurance update-policy-status
u1 ;; policy-id
u2 ;; new status (2=expired)
)(contract-call? .vehicle-insurance withdraw-reserve
u5000000 ;; amount
'SP123... ;; recipient principal
)(contract-call? .vehicle-insurance get-policy u1)(contract-call? .vehicle-insurance get-claim u1)(contract-call? .vehicle-insurance get-user-policies 'SP123...)(contract-call? .vehicle-insurance get-reserve-pool)(contract-call? .vehicle-insurance is-policy-active u1)| Code | Constant | Description |
|---|---|---|
| u100 | err-owner-only | Operation requires contract owner |
| u101 | err-not-found | Policy or claim not found |
| u102 | err-unauthorized | User not authorized for this action |
| u103 | err-invalid-amount | Invalid monetary amount |
| u104 | err-policy-expired | Policy is expired or inactive |
| u105 | err-policy-active | Policy must be inactive |
| u106 | err-insufficient-funds | Insufficient funds in reserve pool |
| u107 | err-claim-exists | Claim already exists |
| u108 | err-invalid-status | Invalid status value |
| u109 | err-payment-failed | Payment transaction failed |
| u110 | err-invalid-input | Invalid input data |
- 0: Inactive
- 1: Active
- 2: Expired
- 3: Cancelled
- 0: Pending review
- 1: Approved (awaiting payout)
- 2: Rejected
- 3: Paid
- All user inputs are validated before processing
- Authorization checks on all state-changing operations
- Amount bounds checking to prevent overflow
- Reserve pool validation before payouts
- Active policy verification for claims
- Status transition controls
- Only the contract owner can approve/reject claims and process payouts
- Policies must be active for claims to be filed
- Claims cannot exceed policy coverage limits
- Policy cancellation provides 50% refund only
- Reserve pool must have sufficient funds for payouts
-
Deploy Contract
clarinet contract deploy vehicle-insurance
-
Verify Deployment
clarinet console
-
Test Functions
clarinet test
- User purchases policy → Premium goes to reserve pool
- Policy becomes active → Coverage starts immediately
- User experiences accident → Files claim with description
- Owner reviews claim → Approves with assessment amount
- Owner processes payout → Funds transferred to claimant
- Claim marked as paid → Reserve pool updated
- Policy creation and validation
- Premium payment processing
- Policy renewal and cancellation
- Claim filing and validation
- Claim approval/rejection workflow
- Payout processing
- Authorization checks
- Input validation
- Reserve pool management