-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsurance.sol
161 lines (109 loc) · 4.48 KB
/
insurance.sol
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
pragma solidity ^0.6.0;
contract Authorized {
address broker;
address insured;
modifier onlyAuthorized() {
require(msg.sender == insured || msg.sender == broker, "Permission denied");
_;
}
modifier onlyBroker() {
require(msg.sender == broker, "Permission denied");
_;
}
modifier onlyInsured() {
require(msg.sender == insured, "Permission denied for the broker. It's user's personal data.");
_;
}
constructor(address _insured) public {
require(msg.sender != address(_insured), "You can't create policy for yourself!");
broker = msg.sender;
insured = _insured;
}
}
contract PremiumPolicy {
enum PremiumStatus { None, Bronze, Silver, Gold }
PremiumStatus currentStatus;
constructor() public {
currentStatus = PremiumStatus.None;
}
function setStatus(uint _status) public {
currentStatus = PremiumStatus(_status);
}
function getDiscount() public view returns(uint256) {
if(currentStatus == PremiumStatus.Bronze) {
return 50;
} else if(currentStatus == PremiumStatus.Silver) {
return 150;
} else if(currentStatus == PremiumStatus.Gold) {
return 500;
} else {
return 0;
}
}
}
contract Policy is Authorized {
bool isPolicyActivated = false;
bool isPolicyPaid = false;
uint256 policyPrice = 0;
PremiumPolicy premiumPolicy = new PremiumPolicy();
string insuredName = "Empty";
string insuredIdNumber = "Empty";
uint256 developmentVersion = 3595;
constructor(address _insured, uint256 _policyPrice) public Authorized(_insured) {
require(_policyPrice >= 1000, "Policy price can't be cheaper than 1000$!");
policyPrice = _policyPrice;
}
function getInsured() public view onlyAuthorized returns(address) {
return insured;
}
function setInsuredPersonalData(string memory _insuredName, string memory _insuredIdNumber) public onlyInsured {
insuredName = _insuredName;
insuredIdNumber = _insuredIdNumber;
}
function getInsuredPersonalData() public view onlyInsured returns(string memory) {
return cancat(insuredName, insuredIdNumber);
}
function activatePolicy() public onlyBroker {
require(compareStrings(insuredName, "Empty") == false, "Insured name is not provided. Contact your client.");
require(compareStrings(insuredIdNumber, "Empty") == false, "Insured id number is not provided. Contact your client.");
require(isPolicyPaid == true, "Policy hasn't been paid yet! Contact with insured.");
isPolicyActivated = true;
}
function deactivatePolicy() public onlyBroker {
require(isPolicyActivated == true, "Policy is not activated yet!");
isPolicyActivated = false;
}
function getPolicyStatus() public view onlyAuthorized returns(bool) {
return isPolicyActivated;
}
function getMyRole() public view onlyAuthorized returns(string memory) {
if(broker == msg.sender) {
return "You are broker.";
} else if(insured == msg.sender) {
return "You are insured.";
} else {
return "Permission denied.";
}
}
function getPolicyPrice() public view onlyAuthorized returns(uint256) {
return policyPrice - premiumPolicy.getDiscount();
}
function payForPolice(uint256 payPalMoney) public onlyInsured {
uint priceWithDiscount = policyPrice-premiumPolicy.getDiscount();
require(payPalMoney == priceWithDiscount, "You have not enough money or provided money amount is not correct!");
isPolicyPaid = true;
}
function setPremiumForPolicy(uint _premiumStatus) public onlyBroker {
premiumPolicy.setStatus(_premiumStatus);
}
function getVersion() public view onlyBroker returns(uint256) {
return developmentVersion;
}
//UTILS ==================================
function cancat(string memory a, string memory b) private pure returns(string memory){
return(string(abi.encodePacked(a," ",b)));
}
function compareStrings(string memory a, string memory b) private pure returns (bool) {
return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b))));
}
}