-
Notifications
You must be signed in to change notification settings - Fork 3
/
InsuranceContract.sol
165 lines (134 loc) · 4.37 KB
/
InsuranceContract.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
162
163
164
165
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@chainlink/contracts/src/v0.8/interfaces/FeedRegistryInterface.sol";
import "./Denominations.sol";
import "./InsuranceProvider.sol";
import "./Pool.sol";
contract InsuranceContract is Ownable {
using SafeMath for uint;
FeedRegistryInterface internal registry;
uint public constant DAY_IN_SECONDS = 86400; //Seconds in a day. 60 for testing, 86400 for Production
InsuranceProvider public insurer;
address payable client;
uint startDate;
uint duration;
uint premium;
uint payoutValue;
uint collateralValue;
bool contractActive;
bool contractPaid = false;
uint currentValue = 0;
uint currentValueDateChecked = block.timestamp;
uint requestCount = 0;
modifier onContractEnded() {
if (startDate.add(duration) < block.timestamp) {
_;
}
}
modifier onContractActive() {
require(contractActive == true ,'Contract has ended, cant interact with it anymore');
_;
}
modifier callFrequencyOncePerDay() {
require(
block.timestamp.sub(currentValueDateChecked) > (DAY_IN_SECONDS.sub(DAY_IN_SECONDS.div(12))),
'Can only check token value once per day'
);
_;
}
event contractCreated(address indexed _client, address _insurer, uint _duration, uint _premium, uint _totalCover);
event contractPaidOut(address indexed _client, uint _paidTime, uint _totalPaid, uint _finalCollateralValue);
event contractEnded(address indexed _client, uint _endTime);
constructor(
address payable _client,
uint _duration,
uint _premium,
uint _payoutValue,
uint _collateralValue)
{
registry = FeedRegistryInterface(0x1363bdCE312532F864e84924D54c7dA5eDB5B1BC);
insurer = InsuranceProvider(msg.sender);
client = _client;
startDate = block.timestamp;
duration = _duration;
premium = _premium;
payoutValue = _payoutValue;
contractActive = true;
collateralValue = _collateralValue;
emit contractCreated(client, address(insurer), duration, premium, payoutValue);
}
function updateContract()
public
onContractActive
onlyOwner
callFrequencyOncePerDay
{
checkEndContract();
if (contractActive) {
currentValue = uint(getLatestPrice());
currentValueDateChecked = block.timestamp;
requestCount = requestCount.add(1);
if(currentValue <= (collateralValue.mul(10)).div(100)) {
payOutContract();
}
}
}
function payOutContract() private onContractActive {
Pool capitalPool = insurer.getCapitalPool();
capitalPool.claimContract(client, payoutValue);
emit contractPaidOut(client, block.timestamp, payoutValue, currentValue);
contractActive = false;
contractPaid = true;
}
function checkEndContract() private onContractEnded {
if (!(requestCount >= (duration.div(DAY_IN_SECONDS) - 2))) {
Pool capitalPool = insurer.getCapitalPool();
capitalPool.claimContract(client, payoutValue);
}
contractActive = false;
emit contractEnded(client, block.timestamp);
}
function getLatestPrice() internal view returns (int) {
(
uint80 roundID,
int price,
uint startedAt,
uint timeStamp,
uint80 answeredInRound
) = registry.latestRoundData(Denominations.ETH, Denominations.USD);
require(timeStamp > 0, "Round not complete");
return price;
}
function getClient() external view returns (address) {
return client;
}
function getPayoutValue() external view returns (uint) {
return payoutValue;
}
function getPremium() external view returns (uint) {
return premium;
}
function getContractStatus() external view returns (bool) {
return contractActive;
}
function getContractPaid() external view returns (bool) {
return contractPaid;
}
function getCurrentValue() external view returns (uint) {
return currentValue;
}
function getRequestCount() external view returns (uint) {
return requestCount;
}
function getCurrentValueDateChecked() external view returns (uint) {
return currentValueDateChecked;
}
function getDuration() external view returns (uint) {
return duration;
}
function getContractStartDate() external view returns (uint) {
return startDate;
}
}