Created
December 6, 2018 22:13
-
-
Save ansermino/481db713f06351d2a97a8f3670ab0bf1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity "0.5.1"; | |
contract FlightInsurance { | |
// Address of 'administrator' | |
address public owner; | |
// Flight number to identify contract | |
string public flightNumber; | |
// Expected departure time | |
uint public expectedTime; | |
// Timestamp from most recent call to updateTime() | |
uint public latestUpdate; | |
// Number of seconds from expectedTime when claims become valid | |
uint public threshold; | |
// How much each payout is | |
uint public payout; | |
// Current status of the flight | |
enum Status {OnTime, Delayed, Claimable} | |
Status status; | |
// List and count of all the registered passengers | |
mapping(uint256 => address payable) passengers; | |
uint passengerCount; | |
modifier isOwner() { | |
require(msg.sender == owner); | |
_; | |
} | |
constructor(string memory _flightNumber, uint _expectedTime, uint _threshold) public payable{ | |
// Must send some Ether for payouts | |
require(msg.value > 0); | |
// Must be instantiated before the flight is supposed to leave | |
require(_expectedTime > now); | |
// The account that deploys this contract will 'own' it | |
owner = msg.sender; | |
flightNumber = _flightNumber; | |
expectedTime = _expectedTime; | |
threshold = _threshold; | |
latestUpdate = now; | |
status = Status.OnTime; | |
passengerCount = 0; | |
} | |
/** | |
* Registers a passenger on the passenger list. | |
*/ | |
function addPassenger(address payable _passengerAddress, uint _claimID) isOwner public { | |
passengers[_claimID] = _passengerAddress; | |
passengerCount++; | |
} | |
/** | |
* Called by contract owner to indicate flight has not departed at time of execution. | |
*/ | |
function updateTime() isOwner public { | |
latestUpdate = now; | |
if(latestUpdate > expectedTime + threshold) { | |
status = Status.Claimable; | |
// Compute how much each person gets | |
payout = address(this).balance / passengerCount; | |
} else if (latestUpdate > expectedTime) { | |
status = Status.Delayed; | |
} | |
} | |
/** | |
* Allows passengers to claim on their insurance. | |
*/ | |
function claim(uint _claimID) public { | |
require(passengers[_claimID] == msg.sender); | |
passengers[_claimID].transfer(payout); | |
} | |
function getBalance() public view returns (uint) { | |
return address(this).balance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment