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

Logic #3

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

Logic #3

hashfx opened this issue Feb 18, 2024 · 0 comments

Comments

@hashfx
Copy link
Collaborator

hashfx commented Feb 18, 2024

// SPDX-License-Identifier: MIT

// Contract to provide flight delay insurance
coco FlightDelayInsurance {

  // Owner of the contract
  var owner: address

  // Percentage of the flight ticket price as premium (between 0 and 100)
  var premiumPercentage: uint256

  // Total amount of collected premiums
  var totalPoolAmount: uint256

  // Total number of insured flights
  var totalFlights: uint256

  // Total number of cancelled flights
  var totalCancelledFlights: uint256

  // Mapping of user addresses to their paid premiums
  var userPremiums: map(address, uint256)

  // Mapping of user addresses to their coverage amount
  var userCoverage: map(address, uint256)

  // Constructor function to set the premium percentage
  constructor(_premiumPercentage: uint256) {
    owner := msg.sender
    premiumPercentage := _premiumPercentage
  }

  // Function to calculate premium amount based on the flight ticket price
  func calculatePremium(flightTicketPrice: uint256) -> uint256 {
    return (flightTicketPrice * premiumPercentage) / 100
  }

  // Function to allow users to purchase insurance
  func purchaseInsurance(flightTicketPrice: uint256) payable {
    assert(msg.value > 0, "Premium must be greater than 0")
    let premium := calculatePremium(flightTicketPrice)
    assert(msg.value >= premium, "Insufficient premium amount")

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

  // Function to determine coverage amount if all flights get cancelled
  func determineCoverageAmount() -> uint256 {
    // Calculate coverage amount based on the ratio of cancelled flights to total flights
    return (totalPoolAmount * totalCancelledFlights) / totalFlights
  }

  // Function to distribute coverage amount to users in proportion to their premium paid
  func distributeCoverageAmount() {
    let totalCoverage := determineCoverageAmount()
    let totalPremiums := totalPoolAmount

    // Cocolang iterators are different from Solidity loops
    for (user, premium) in userPremiums {
      let userShare := (premium * totalCoverage) / totalPremiums
      userCoverage[user] += userShare
    }
  }

  // Function to withdraw coverage amount
  func withdrawCoverage() {
    let coverage := userCoverage[msg.sender]
    assert(coverage > 0, "No coverage available")

    userCoverage[msg.sender] := 0
    transfer(msg.sender, coverage)
  }
}
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