Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solidity #4

Open
hashfx opened this issue Feb 18, 2024 · 0 comments
Open

Solidity #4

hashfx opened this issue Feb 18, 2024 · 0 comments

Comments

@hashfx
Copy link
Collaborator

hashfx commented Feb 18, 2024

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract FlightDelayInsurance {
  // contract owner
  address public owner;

  // flight ticket price premium percentage (between 0 and 100)
  uint256 public premiumPercentage;

  // total collected premiums
  uint256 public totalPoolAmount;

  // number of insured flights
  uint256 public totalFlights;

  // number of cancelled flights
  uint256 public totalCancelledFlights;

  // mapping of user addresses to their paid premiums
  mapping(address => uint256) public userPremiums;

  // mapping of user addresses to their coverage amount
  mapping(address => uint256) public userCoverage;

  // Oracle contract address
  address public oracle;

  // event emits when a flight is cancelled
  event FlightCancelled(uint256 flightId);

  // restrict function calls to the contract owner
  modifier onlyOwner() {
    require(msg.sender == owner, "Only owner can call this function");
    _;
  }

  // constructor to set the premium percentage and oracle address
  constructor(uint256 _premiumPercentage, address _oracle) {
    owner = msg.sender;
    premiumPercentage = _premiumPercentage;
    oracle = _oracle;
  }

  // calculate premium amount based on the flight ticket price
  function calculatePremium(uint256 flightTicketPrice) public view returns (uint256) {
    return (flightTicketPrice * premiumPercentage) / 100;
  }

  // users purchase insurance
  function purchaseInsurance(uint256 flightTicketPrice) external payable {
    require(msg.value > 0, "Premium must be greater than 0");
    uint256 premium = calculatePremium(flightTicketPrice);
    require(msg.value >= premium, "Insufficient premium amount");

    totalPoolAmount += premium;
    userPremiums[msg.sender] += premium;
    totalFlights++;
  }

  // oracle to report a flight cancellation
  function reportFlightCancellation(uint256 flightId) external {
    require(msg.sender == oracle, "Only oracle can report flight cancellation");
    totalCancelledFlights++;
    emit FlightCancelled(flightId);
  }

  // determine coverage amount if all flights get cancelled
  function determineCoverageAmount() public view returns (uint256) {
    // Calculate coverage amount based on the ratio of cancelled flights to total flights
    return (totalPoolAmount * totalCancelledFlights) / totalFlights;
  }

  // distribute coverage amount to users in proportion to their paid premium
  function distributeCoverageAmount() public {
    uint256 totalCoverage = determineCoverageAmount();
    uint256 totalPremiums = totalPoolAmount;

    for (address user in userPremiums.keys()) {
      uint256 userPremium = userPremiums[user];
      uint256 userShare = (userPremium * totalCoverage) / totalPremiums;
      userCoverage[user] += userShare;
    }
  }

  // withdraw coverage amount to users
  function withdrawCoverage() external {
    uint256 coverage = userCoverage[msg.sender];
    require(coverage > 0, "No coverage available");

    userCoverage[msg.sender] = 0;
    payable(msg.sender).transfer(coverage);
  }

  // oracle address
  function setOracleAddress(address _oracle) public onlyOwner {
    oracle = _oracle;
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant