Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity ^0.4.24;
- contract LifeInsuranceService {
- uint constant public PRICE = 1 ether;
- uint constant public COMPENSATION = 4 ether;
- address public owner;
- address[] travellers;
- address[] deads;
- mapping (address => address) beneficiaries;
- mapping (address => address[]) delegates;
- mapping (address => Consent[]) consents;
- struct Consent {
- address delegate;
- bool died;
- }
- constructor() {
- owner = msg.sender;
- }
- function register(address _beneficiary, address[] _delegates)
- payable
- returns (bool success) {
- require(_beneficiary != address(0)
- && _delegates.length != 0,
- "Invalid addresses.");
- for (uint d = 0; d < _delegates.length; d++) {
- require(_delegates[d] != address(0),
- "Invalid delegates addresses.");
- }
- for (uint t = 0; t < travellers.length; t++) {
- require(travellers[t] != msg.sender,
- "Address exists.");
- }
- require(msg.value > PRICE, "Insufficient value.");
- if (msg.sender.send(PRICE)) {
- travellers.push(msg.sender);
- beneficiaries[msg.sender] = _beneficiary;
- delegates[msg.sender] = _delegates;
- return true;
- }
- return false;
- }
- function hasDied(address _traveler, bool _died)
- returns (bool success) {
- bool existsTraveler = false;
- for (uint t = 0; t < travellers.length; t++) {
- if (travellers[t] == _traveler) {
- existsTraveler = true;
- break;
- }
- }
- require(existsTraveler, "Traveler not exist.");
- bool existsDelegate = false;
- for (uint d = 0; d < delegates[_traveler].length; d++) {
- if (delegates[_traveler][d] == msg.sender) {
- existsDelegate = true;
- break;
- }
- }
- require(existsDelegate, "Delegate not exist.");
- Consent[] travelerConsent = consents[_traveler];
- bool existsConsent = false;
- for (uint c = 0; c < travelerConsent.length; c++) {
- if (travelerConsent[c].delegate == msg.sender) {
- existsConsent = true;
- break;
- }
- }
- require(!existsConsent, "Consent exists.");
- Consent[] travelerConsents = consents[_traveler];
- travelerConsents.push(
- Consent(msg.sender, _died)
- );
- consents[_traveler] = travelerConsents;
- return true;
- }
- function confirm() payable {
- require(msg.sender == owner, "Only onwner.");
- for (uint t = 0; t < travellers.length; t++) {
- address traveler = travellers[t];
- bool dead;
- for (uint d = 0; d < deads.length; d++) {
- if (deads[d] == traveler) {
- dead = true;
- break;
- }
- }
- if (dead) {
- continue;
- }
- if (consents[traveler].length == 1
- || consents[traveler].length > (delegates[traveler].length / 2)) {
- Consent[] travelerConsent = consents[traveler];
- uint diedFalse = 0;
- uint diedTrue = 0;
- for (uint c = 0; c < travelerConsent.length; c++) {
- if (travelerConsent[c].died) {
- diedTrue++;
- continue;
- }
- diedFalse++;
- }
- if (diedTrue > diedFalse) {
- require(
- beneficiaries[traveler].send(COMPENSATION),
- "Compensation not sent."
- );
- deads.push(traveler);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement