Elder Care Payment Automation

What it does:
Automatically schedules and releases payments for elder care services based on care agreements, attendance, or service milestones.

Why it matters:
Ensures caregivers are paid fairly and on time, reduces disputes, and provides transparent audit trails for families, agencies, and senior care providers.

How it works:

  • Families or institutions register elder care agreements on-chain

  • Payment amounts, frequency, and milestones are defined

  • Caregiver attendance or service completion is recorded

  • Smart contract calculates due payments automatically

  • Funds are released directly to caregivers

  • Payment history is permanently recorded

  • Agreements can be paused, updated, or terminated

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

/**
 * @title ElderCarePaymentAutomation
 * @author Nam
 * @notice Automates payments to caregivers for elder care services
 */
contract ElderCarePaymentAutomation {

    // -------------------- ROLES --------------------

    address public admin;

    // -------------------- STRUCTS --------------------

    struct CareAgreement {
        address family;
        address caregiver;
        uint256 paymentAmount;  // wei per milestone
        uint256 milestoneInterval; // seconds
        uint256 lastPaymentTime;
        bool active;
    }

    mapping(uint256 => CareAgreement) public agreements;
    uint256 public agreementCount;

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

    event AgreementCreated(uint256 indexed agreementId, address caregiver);
    event PaymentReleased(uint256 indexed agreementId, address caregiver, uint256 amount);
    event AgreementUpdated(uint256 indexed agreementId);
    event AgreementTerminated(uint256 indexed agreementId);

    // -------------------- MODIFIERS --------------------

    modifier onlyAdmin() {
        require(msg.sender == admin, "Not admin");
        _;
    }

    modifier agreementExists(uint256 _agreementId) {
        require(_agreementId > 0 && _agreementId  0, "Invalid payment");
        require(_milestoneInterval > 0, "Invalid interval");

        agreementCount += 1;
        agreements[agreementCount] = CareAgreement({
            family: msg.sender,
            caregiver: _caregiver,
            paymentAmount: _paymentAmount,
            milestoneInterval: _milestoneInterval,
            lastPaymentTime: block.timestamp,
            active: true
        });

        emit AgreementCreated(agreementCount, _caregiver);
    }

    /**
     * @notice Release payment for completed milestone
     */
    function releasePayment(uint256 _agreementId)
        external
        agreementExists(_agreementId)
    {
        CareAgreement storage a = agreements[_agreementId];
        require(a.active, "Agreement inactive");
        require(msg.sender == a.family, "Not family");
        require(address(this).balance >= a.paymentAmount, "Insufficient funds");
        require(block.timestamp >= a.lastPaymentTime + a.milestoneInterval, "Too early");

        a.lastPaymentTime = block.timestamp;
        payable(a.caregiver).transfer(a.paymentAmount);

        emit PaymentReleased(_agreementId, a.caregiver, a.paymentAmount);
    }

    /**
     * @notice Update agreement parameters
     */
    function updateAgreement(
        uint256 _agreementId,
        uint256 _paymentAmount,
        uint256 _milestoneInterval
    )
        external
        agreementExists(_agreementId)
    {
        CareAgreement storage a = agreements[_agreementId];
        require(msg.sender == a.family, "Not family");
        require(a.active, "Agreement inactive");

        a.paymentAmount = _paymentAmount;
        a.milestoneInterval = _milestoneInterval;

        emit AgreementUpdated(_agreementId);
    }

    /**
     * @notice Terminate agreement
     */
    function terminateAgreement(uint256 _agreementId)
        external
        agreementExists(_agreementId)
    {
        CareAgreement storage a = agreements[_agreementId];
        require(msg.sender == a.family, "Not family");
        require(a.active, "Already terminated");

        a.active = false;
        emit AgreementTerminated(_agreementId);
    }

    // -------------------- FUNDING --------------------

    receive() external payable {}
}