Skip to content

AritraDas07/TriggerPay

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

TriggerPay

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.

Features

  • 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).

How It Works

  1. The insured deploys the contract with a triggerValue and a premium (ETH).
  2. The contract stores:
    • The insured address.
    • Premium value.
    • Payout (2x the premium).
    • Trigger value.
  3. An external entity reports a value using the report() function.
  4. If the reported value is less than or equal to the trigger value, the insured can call checkAndPay() to receive the payout.
  5. If the condition is not met, the user can cancel the policy using cancel().

Smart Contract

// 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 {}
}

Functions

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.

Contract address : 0x730295754f619b6dc1f012dccc9166d293deb3d1

Screenshot (1)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published