Let’s explore the handling of insurance payouts using a smart contract by providing a simple example. We’ll assume this is related to travel insurance where a payout is automatically triggered when a flight is delayed.
We’ll write a smart contract using Solidity (the most popular language for writing smart contracts on the Ethereum blockchain). The contract will automatically pay a certain amount of insurance compensation if a flight delay occurs.
Problem Setup:
- The user buys insurance for a flight.
- The smart contract checks flight status using an oracle (external data provider) to determine whether the flight was delayed or canceled.
- If the delay exceeds a predefined threshold (say 2 hours), the smart contract automatically pays out the insurance claim.
Solidity Smart Contract Code:
Solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
interface IFlightOracle {
function getFlightStatus(bytes32 flightID) external view returns (uint8);
// Assume: 0 = on time, 1 = delayed, 2 = canceled
}
contract FlightInsurance {
address public insurer;
uint public delayThreshold = 2 hours;
uint public payoutAmount;
bool public claimPaid;
struct Policy {
address payable insured;
bytes32 flightID;
uint256 premium;
bool active;
}
mapping(address => Policy) public policies;
IFlightOracle public flightOracle; // Oracle to get flight status
event PolicyPurchased(address insured, bytes32 flightID);
event ClaimPaid(address insured, uint amount);
constructor(address _oracleAddress, uint _payoutAmount) {
insurer = msg.sender; // The insurer who deploys the contract
flightOracle = IFlightOracle(_oracleAddress); // Oracle contract for flight data
payoutAmount = _payoutAmount; // Payout amount for the insurance
}
// Purchase insurance for a specific flight
function purchaseInsurance(bytes32 _flightID) public payable {
require(msg.value > 0, "Premium must be greater than 0");
require(policies[msg.sender].active == false, "Policy already exists");
policies[msg.sender] = Policy({
insured: payable(msg.sender),
flightID: _flightID,
premium: msg.value,
active: true
});
emit PolicyPurchased(msg.sender, _flightID);
}
// Check flight status and pay claim if applicable
function checkFlightStatusAndPayClaim() public {
Policy storage policy = policies[msg.sender];
require(policy.active == true, "No active policy found");
// Get the flight status from the oracle
uint8 flightStatus = flightOracle.getFlightStatus(policy.flightID);
if (flightStatus == 1) { // Flight is delayed
payInsuranceClaim(policy.insured);
} else if (flightStatus == 2) { // Flight is canceled
payInsuranceClaim(policy.insured);
} else {
revert("Flight is on time, no claim available");
}
}
// Internal function to pay the insurance claim
function payInsuranceClaim(address payable insured) internal {
require(claimPaid == false, "Claim already paid");
require(address(this).balance >= payoutAmount, "Insufficient contract balance");
insured.transfer(payoutAmount);
claimPaid = true;
policies[insured].active = false;
emit ClaimPaid(insured, payoutAmount);
}
// Fallback function to allow contract to receive Ether
receive() external payable {}
}
Explanation:
- Interfaces and Flight Oracle:
- The smart contract uses an interface
IFlightOracleto get real-time flight status. This oracle returns:0: On time.1: Delayed.2: Canceled.
- In a real-world scenario, this oracle would be an external service providing reliable flight data (such as an API).
- The smart contract uses an interface
- FlightInsurance Contract:
- The insurer deploys the contract and sets the payout amount that will be given to users if a flight is delayed or canceled.
- Users can purchase insurance for a specific flight by sending Ether as a premium.
- Policy Management:
- A
Policyis created for each insured user. It stores:- The user’s address (
insured). - The flight ID (
flightID). - The premium paid.
- The policy’s active status.
- The user’s address (
- A
- Claim Process:
- The function
checkFlightStatusAndPayClaim:- Calls the flight oracle to get the flight’s current status.
- If the flight is delayed or canceled, the insured party is automatically compensated.
- The
payInsuranceClaimfunction handles the actual transfer of the insurance payout to the insured party’s address.
- The function
- Fallback Function:
- This function allows the contract to accept Ether, which can be used to fund the payouts.
Example Scenario:
- A user purchases insurance for flight ID
FL123by sending a premium. - The smart contract interacts with the flight oracle to check if the flight is delayed or canceled.
- If the flight is delayed by more than 2 hours or canceled, the smart contract pays the agreed amount to the user automatically without manual intervention.
Benefits of Smart Contracts in Insurance:
- Automation: Payouts are made automatically when certain conditions are met, eliminating the need for manual claims processing.
- Transparency: All parties can view the contract and the conditions that trigger payouts.
- Speed: Once the conditions are satisfied, the payout is processed instantly.
- Security: The use of blockchain ensures that the contract is tamper-resistant, providing secure execution of the terms.
This example illustrates how smart contracts can simplify and automate insurance payouts, improving both the customer experience and operational efficiency for insurance companies.
