Smart Warranty Contract

What it does:
Manages product warranties on-chain, recording warranty terms, activation, claims, and expiration automatically.

Why it matters:
Prevents fraud, simplifies claims processing, ensures transparent warranty enforcement, and provides auditable records for consumers and manufacturers.

How it works:

  • Manufacturers register products and their warranty terms on-chain

  • Warranties are activated upon purchase or registration

  • Claims can be submitted by owners and verified by authorized inspectors or service centers

  • Smart contracts automatically validate warranty period and conditions

  • Approved claims trigger automatic compensation, repair, or replacement

  • Historical warranty events are stored immutably for auditing

  • Integrates with Product Authenticity Verification, Supply Chain Traceability, and marketplaces

      // SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/access/Ownable.sol";

/**
 * @title SmartWarranty
 * @author Nam
 * @notice Manages product warranties and claims on-chain
 */
contract SmartWarranty is Ownable {

    struct ProductWarranty {
        string productName;
        address owner;
        uint256 purchaseTimestamp;
        uint256 warrantyDuration; // in seconds
        bool active;
    }

    struct WarrantyClaim {
        uint256 timestamp;
        string description;
        bool approved;
        bool settled;
        address inspector;
    }

    mapping(uint256 => ProductWarranty) public warranties; // productId => warranty
    mapping(uint256 => WarrantyClaim[]) public claims; // productId => claims
    mapping(address => bool) public inspectors;

    uint256 public productCount;

    // -------------------- EVENTS --------------------

    event WarrantyRegistered(uint256 indexed productId, string productName, address owner, uint256 expiry);
    event ClaimSubmitted(uint256 indexed productId, uint256 claimIndex, string description);
    event ClaimApproved(uint256 indexed productId, uint256 claimIndex, address inspector);
    event ClaimSettled(uint256 indexed productId, uint256 claimIndex);

    event InspectorApproved(address inspector);

    // -------------------- INSPECTOR MANAGEMENT --------------------

    function approveInspector(address _inspector) external onlyOwner {
        inspectors[_inspector] = true;
        emit InspectorApproved(_inspector);
    }

    function revokeInspector(address _inspector) external onlyOwner {
        inspectors[_inspector] = false;
    }

    modifier onlyInspector() {
        require(inspectors[msg.sender], "Not an authorized inspector");
        _;
    }

    // -------------------- WARRANTY REGISTRATION --------------------

    function registerWarranty(string calldata _productName, address _owner, uint256 _warrantyDuration) external onlyOwner {
        require(_owner != address(0), "Invalid owner");

        productCount += 1;
        warranties[productCount] = ProductWarranty({
            productName: _productName,
            owner: _owner,
            purchaseTimestamp: block.timestamp,
            warrantyDuration: _warrantyDuration,
            active: true
        });

        emit WarrantyRegistered(productCount, _productName, _owner, block.timestamp + _warrantyDuration);
    }

    // -------------------- CLAIM MANAGEMENT --------------------

    function submitClaim(uint256 _productId, string calldata _description) external {
        ProductWarranty storage w = warranties[_productId];
        require(msg.sender == w.owner, "Only owner can submit claim");
        require(block.timestamp <= w.purchaseTimestamp + w.warrantyDuration, "Warranty expired");
        require(w.active, "Warranty inactive");

        claims[_productId].push(WarrantyClaim({
            timestamp: block.timestamp,
            description: _description,
            approved: false,
            settled: false,
            inspector: address(0)
        }));

        emit ClaimSubmitted(_productId, claims[_productId].length - 1, _description);
    }

    function approveClaim(uint256 _productId, uint256 _claimIndex) external onlyInspector {
        WarrantyClaim storage c = claims[_productId][_claimIndex];
        require(!c.approved, "Already approved");

        c.approved = true;
        c.inspector = msg.sender;

        emit ClaimApproved(_productId, _claimIndex, msg.sender);
    }

    function settleClaim(uint256 _productId, uint256 _claimIndex) external onlyOwner {
        WarrantyClaim storage c = claims[_productId][_claimIndex];
        require(c.approved, "Claim not approved");
        require(!c.settled, "Already settled");

        c.settled = true;

        emit ClaimSettled(_productId, _claimIndex);
    }

    // -------------------- VIEW FUNCTIONS --------------------

    function isWarrantyActive(uint256 _productId) external view returns (bool) {
        ProductWarranty storage w = warranties[_productId];
        return w.active && block.timestamp <= w.purchaseTimestamp + w.warrantyDuration;
    }

    function getClaims(uint256 _productId) external view returns (WarrantyClaim[] memory) {
        return claims[_productId];
    }
}