TriggerPay is a simple parametric insurance smart contract written in Solidity. It allows a user to create an insurance policy that automatically pays out if a reported value meets a trigger condition.
- User (insured) creates a policy by sending a premium.
- A payout (2x the premium) is triggered if a reported value is less than or equal to the predefined trigger value.
- The insured can cancel the policy and get a refund if the policy is still active and not yet paid.
- Includes basic manual reporting (simulated oracle input).
- The insured deploys the contract with a
triggerValue
and a premium (ETH). - The contract stores:
- The insured address.
- Premium value.
- Payout (2x the premium).
- Trigger value.
- An external entity reports a value using the
report()
function. - If the reported value is less than or equal to the trigger value, the insured can call
checkAndPay()
to receive the payout. - If the condition is not met, the user can cancel the policy using
cancel()
.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract TriggerPay {
address public insured;
uint256 public premium;
uint256 public payout;
int256 public triggerValue;
bool public active;
bool public paid;
int256 public reportedValue;
constructor(int256 _triggerValue) payable {
require(msg.value > 0, "Premium required");
insured = msg.sender;
premium = msg.value;
payout = msg.value * 2;
triggerValue = _triggerValue;
active = true;
paid = false;
}
// Simulates reporting external data manually
function report(int256 _value) external {
require(active && !paid, "Policy not active");
reportedValue = _value;
}
function checkAndPay() external {
require(active && !paid, "Already handled");
if (reportedValue <= triggerValue) {
payable(insured).transfer(payout);
paid = true;
active = false;
}
}
function cancel() external {
require(msg.sender == insured, "Only insured can cancel");
require(active && !paid, "Cannot cancel now");
active = false;
payable(insured).transfer(premium);
}
receive() external payable {}
}
Function | Description |
---|---|
constructor(int256 _triggerValue) |
Initializes the contract with a trigger value and sets the premium. |
report(int256 _value) |
Allows reporting a value (simulating external data). |
checkAndPay() |
Triggers the payout if the reported value meets the condition. |
cancel() |
Allows the insured to cancel the policy and get a refund. |
receive() |
Enables the contract to receive ETH. |