Smart Irrigation Payment Contract

What it does:
Automates payments for irrigation services based on real-time water usage and efficiency metrics, incentivizing farmers to optimize water consumption.

Why it matters:
Reduces water waste, supports sustainable agriculture, and aligns financial incentives with responsible water management in drought-prone regions.

How it works:

  • IoT sensors record water usage and soil moisture data

  • Trusted oracles submit verified usage data on-chain

  • Smart contract calculates irrigation costs based on usage and efficiency thresholds

  • Farmers are charged only for water actually used

  • Discounts or rebates are applied for water-efficient behavior

  • Payments are settled automatically to water providers

  • All usage and payments are transparent and auditable

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

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

/**
 * @title SmartIrrigationPayment
 * @author Nam
 * @notice Automates irrigation payments based on verified water usage
 */
contract SmartIrrigationPayment is Ownable {

    struct UsageRecord {
        uint256 waterUsed; // liters
        uint256 timestamp;
        bool settled;
    }

    address payable public waterProvider;
    uint256 public pricePerLiter; // wei per liter

    mapping(address => UsageRecord[]) public farmUsage;
    mapping(address => bool) public approvedOracles;

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

    event OracleApproved(address oracle);
    event UsageReported(address indexed farm, uint256 liters);
    event PaymentSettled(address indexed farm, uint256 amount);

    // -------------------- CONSTRUCTOR --------------------

    constructor(address payable _provider, uint256 _pricePerLiter) {
        waterProvider = _provider;
        pricePerLiter = _pricePerLiter;
    }

    // -------------------- ORACLE MANAGEMENT --------------------

    function approveOracle(address _oracle) external onlyOwner {
        approvedOracles[_oracle] = true;
        emit OracleApproved(_oracle);
    }

    // -------------------- USAGE REPORTING --------------------

    function reportUsage(address _farm, uint256 _waterUsed) external {
        require(approvedOracles[msg.sender], "Not authorized oracle");
        require(_waterUsed > 0, "Invalid usage");

        farmUsage[_farm].push(UsageRecord({
            waterUsed: _waterUsed,
            timestamp: block.timestamp,
            settled: false
        }));

        emit UsageReported(_farm, _waterUsed);
    }

    // -------------------- PAYMENT SETTLEMENT --------------------

    function settlePayments() external payable {
        UsageRecord[] storage records = farmUsage[msg.sender];
        uint256 totalDue = 0;

        for (uint256 i = 0; i  0, "No outstanding usage");
        require(msg.value == totalDue, "Incorrect payment");

        waterProvider.transfer(totalDue);
        emit PaymentSettled(msg.sender, totalDue);
    }
}