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
/* Author: Santhosh Reddy */ | |
//SPDX-License-Identifier: MIT | |
pragma solidity 0.8.*; | |
contract carInsurance { | |
address addrs = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4; | |
address payable insurer = payable(addrs); | |
uint8 public totalUsers; | |
uint8 public totalInsureds; | |
uint8 public totalCoverageRequests; | |
function balance() public view returns(uint) { | |
return address(this).balance; | |
} | |
struct user { | |
uint8 id; | |
string name; | |
uint8[] insurances; | |
} | |
mapping(address => user) private users; | |
struct insured { | |
uint256 time; | |
uint256 lastPaid; | |
uint256 maxCoverage; | |
uint256 premium; | |
uint8 period; | |
uint8 billStatus; | |
uint8[] coverages; | |
address addr; | |
string name; | |
string carName; | |
string carNo; | |
string carPic; | |
bool status; | |
} | |
mapping(uint8 => insured) private insureds; | |
struct coverageRequest { | |
uint8 id; | |
uint8 amount; | |
bool status; | |
string report; | |
} | |
mapping(uint8 => coverageRequest) private coverageRequests; | |
string[] private carsInsured; | |
event userSignedIn(address addr); | |
event insuranceInsured(uint8 id); | |
event premiumPaid(uint8 id); | |
event coverageRequestedOrVerified(uint8 id, string action); | |
event insuranceCovered(uint8 id, uint256 amount); | |
event fundsWithdrawn(uint256 amount); | |
modifier onlyInsurer() { | |
require(msg.sender == addrs, "You aren't the Insurer"); | |
_; | |
} | |
modifier onlyInsured(uint8 id) { | |
require(insureds[id].addr == msg.sender, "You aren't the Insured"); | |
_; | |
} | |
/* SignIn Function */ | |
function signIn(string memory name) public { | |
require(users[msg.sender].id == 0, "You have already signedIn"); | |
uint8[] memory x; | |
totalUsers++; | |
users[msg.sender] = user(totalUsers, name, x); | |
emit userSignedIn(msg.sender); | |
} | |
/* Functions on Insurer */ | |
function withdrawFunds(uint amount) external onlyInsurer { | |
require(amount < balance, "insufficient balance!"); | |
emit fundsWithdrawn(amount); | |
insurer.transfer(amount); | |
} | |
function checkPremiums() public onlyInsurer { | |
uint256 time = block.timestamp/86400; | |
for (uint8 i=1; i<totalInsureds+1; i++) { | |
if(time - insureds[i].lastPaid == 365 && insureds[i].billStatus == 0) { | |
insureds[i].billStatus = 1; | |
} | |
if(time - insureds[i].time > 365+30 && insureds[i].billStatus == 1) { | |
insureds[i].billStatus = 2; | |
insureds[i].status = false; | |
} | |
} | |
} | |
function verifyCoverage(uint8 id, uint8 amount, string memory report) public onlyInsurer { | |
require(coverageRequests[id].status == false, "The coverage was already verified"); | |
require(amount <= insureds[coverageRequests[id].id].maxCoverage, "the amount is more than the maximum coverage amount"); | |
coverageRequests[id].status = true; | |
coverageRequests[id].amount = amount; | |
coverageRequests[id].report = report; | |
emit coverageRequestedOrVerified(id, "verified"); | |
} | |
function getUsers(address addr) public view onlyInsurer returns(user memory) { | |
return users[addr]; | |
} | |
function getInsuredDetails(uint8 id) public view onlyInsurer returns(insured memory) { | |
return insureds[id]; | |
} | |
function getCoverageRequests(uint8 i) public view onlyInsurer returns(coverageRequest memory) { | |
return coverageRequests[i]; | |
} | |
/* Functions of Insureds */ | |
function insurePolicy(uint256 maxCoverage, uint256 premium, string memory carName, string memory carNo, string memory carPic) public payable { | |
require(users[msg.sender].id != 0, "Please signIn before insuring"); | |
require(msg.value == premium, "Pay the right amount"); | |
bool a = true; | |
for(uint8 i=0; i<carsInsured.length; i++) { | |
if(keccak256(abi.encodePacked(carsInsured[i])) == keccak256(abi.encodePacked(carNo))) { | |
a = false; | |
}} | |
require(a == true, "This car was already Insured"); | |
uint8[] memory x; | |
totalInsureds++; | |
insureds[totalInsureds] = insured(block.timestamp/86400, block.timestamp/86400, maxCoverage, premium, 7, 0, x, msg.sender, users[msg.sender].name, carName, carNo, carPic, true); | |
users[msg.sender].insurances.push(totalInsureds); | |
carsInsured.push(carNo); | |
emit insuranceInsured(totalInsureds); | |
payable(msg.sender).transfer(msg.value); | |
} | |
function payPremium(uint8 id) public payable onlyInsured(id) { | |
require(insureds[id].billStatus == 1, "You have no premium due"); | |
require(msg.value == insureds[id].premium, "Pay the right amount"); | |
insureds[id].billStatus = 0; | |
insureds[id].lastPaid = block.timestamp/86400; | |
emit premiumPaid(id); | |
payable(msg.sender).transfer(msg.value); | |
} | |
function requestCoverage(uint8 id) public onlyInsured(id) { | |
require(insureds[id].billStatus == 0, "Please clear your current premium due"); | |
totalCoverageRequests++; | |
coverageRequests[totalCoverageRequests] = coverageRequest(id, 0, false, ""); | |
emit coverageRequestedOrVerified(id, "requested"); | |
} | |
function claimCoverage(uint8 id) external onlyInsured(coverageRequests[id].id) { | |
require(coverageRequests[id].status == true, "the coverage you requested isn'isn't verified yet!"); | |
emit insuranceCovered(coverageRequests[id].id, coverageRequests[id].amount); | |
payable(msg.sender).transfer(coverageRequests[id].amount); | |
} | |
function myDetails() public view returns(user memory) { | |
require(users[msg.sender].id != 0, "You aren't a user"); | |
return users[msg.sender]; | |
} | |
function myInsuredDetails(uint8 id) public view onlyInsured(id) returns(insured memory) { | |
return insureds[id]; | |
} | |
function myCoverageRequests(uint8 id) public view onlyInsured(coverageRequests[id].id) returns(coverageRequest memory) { | |
return coverageRequests[id]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment