Repository navigation
Expand file tree
/
Copy pathinsurance.sol
More file actions
70 lines (58 loc) · 2.13 KB
/
Copy pathinsurance.sol
File metadata and controls
70 lines (58 loc) · 2.13 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract InsuranceSystem {
// Mapping from policy ID to policy info
mapping (uint => Policy) public policies;
// Struct to store policy info
struct Policy {
address owner;
bool active;
uint coverage;
}
// Mapping from claim ID to claim info
mapping (uint => Claim) public claims;
// Struct to store claim info
struct Claim {
address policyOwner;
uint policyCoverage;
uint amount;
bool approved;
}
// Function to purchase a policy
function purchasePolicy(uint id, uint coverage) public payable {
require(policies[id].active == false, "Policy already exists.");
require(coverage > 0, "Coverage must be greater than zero.");
require(msg.value >= coverage, "Insufficient funds for policy.");
// Set the policy info
policies[id] = Policy({
owner: msg.sender,
active: true,
coverage: coverage
});
}
// Function to make a claim on a policy
function makeClaim(uint id, uint claimAmount) public {
require(policies[id].active == true, "Policy does not exist.");
require(claimAmount <= policies[id].coverage, "Claim amount exceeds policy coverage.");
// Set the claim info
claims[id] = Claim({
policyOwner: policies[id].owner,
policyCoverage: policies[id].coverage,
amount: claimAmount,
approved: false
});
}
// Function for the insurer to review and approve a claim
function approveClaim(uint id) public {
require(policies[id].active == true, "Policy does not exist.");
require(claims[id].approved == false, "Claim has already been approved.");
// Set the approved flag to true
claims[id].approved = true;
// Transfer the claim amount to the policy owner
claims[id].policyOwner.transfer(claims[id].amount);
}
// Function to get the coverage amount for a policy
function getCoverage(uint id) public view returns (uint) {
return policies[id].coverage;
}
}