-
Notifications
You must be signed in to change notification settings - Fork 2
/
contract.sol
55 lines (46 loc) · 2.27 KB
/
contract.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
pragma solidity ^0.8.0;
import "https://github.com/open-contracts/protocol/blob/main/solidity_contracts/OpenContractRopsten.sol";
contract WeatherInsurance is OpenContract {
struct parameters {
uint256 payout;
uint256 price;
address insurer;
bool active;
}
mapping(bytes32 => mapping(address => parameters)) public policy;
constructor() {
setOracleHash(this.settle.selector, 0x72102889e3b88d5e2e6a41a6a8b2c9e064b199220f9f4c054e952c0927db615a);
}
function policyID(int8 latitude, int8 longitude, uint8 year, uint8 month, uint8 threshold) public pure returns(bytes32) {
return keccak256(abi.encode(latitude, longitude, year, month, threshold));
}
function request(bytes32 policyID, uint256 payout) public payable {
require(!policy[policyID][msg.sender].active, "Your policy is already active.");
policy[policyID][msg.sender].price += msg.value;
policy[policyID][msg.sender].payout = payout;
}
function retract(bytes32 policyID) public {
require(!policy[policyID][msg.sender].active, "Your policy is already active.");
uint256 payment = policy[policyID][msg.sender].price;
policy[policyID][msg.sender].price = 0;
payable(msg.sender).transfer(payment);
}
function provide(address beneficiary, bytes32 policyID) public payable {
require(!policy[policyID][beneficiary].active, "The policy is already active.");
require(msg.value >= policy[policyID][beneficiary].payout, "You did not send enough ETH to provide the insurance.");
policy[policyID][beneficiary].active = true;
policy[policyID][beneficiary].insurer = msg.sender;
uint256 payment = policy[policyID][beneficiary].price;
policy[policyID][beneficiary].price = 0;
payable(msg.sender).transfer(payment);
}
function settle(address beneficiary, bytes32 policyID, bool damageOccured) requiresOracle public {
require(policy[policyID][beneficiary].active, "The insurance is not active.");
uint256 payout = policy[policyID][beneficiary].payout;
if (damageOccured) {
payable(beneficiary).transfer(payout);
} else {
payable(policy[policyID][beneficiary].insurer).transfer(payout);
}
}
}