Sustainable Farming Incentive

What it does:
Incentivizes farmers to adopt sustainable and regenerative farming practices by rewarding verified environmental actions with on-chain incentives.

Why it matters:
Aligns economic rewards with soil health, biodiversity, and water conservation, reduces overuse of chemicals, and creates transparent proof of sustainable agricultural impact.

How it works:

  • Farmers register farms and sustainability profiles on-chain

  • Approved oracles verify farming practices and environmental data

  • Sustainable actions (organic methods, water efficiency, soil restoration) are reported

  • Each verified action earns incentive points or token rewards

  • Rewards are distributed automatically based on impact score

  • Historical sustainability data is permanently recorded on-chain

  • Incentives can integrate with carbon credits, green loans, or supply-chain traceability

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

import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

/**
 * @title SustainableFarmingIncentive
 * @author Nam
 * @notice Rewards farmers for verified sustainable farming practices
 */
contract SustainableFarmingIncentive is Ownable {

    IERC20 public rewardToken;

    struct Farm {
        bool registered;
        uint256 impactScore; // cumulative sustainability score
        uint256 totalRewards;
    }

    mapping(address => Farm) public farms;
    mapping(address => bool) public approvedOracles;

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

    event FarmRegistered(address indexed farmer);
    event PracticeVerified(address indexed farmer, uint256 impactScore);
    event RewardClaimed(address indexed farmer, uint256 amount);

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

    constructor(address _rewardToken) {
        rewardToken = IERC20(_rewardToken);
    }

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

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

    function revokeOracle(address _oracle) external onlyOwner {
        approvedOracles[_oracle] = false;
    }

    // -------------------- FARM REGISTRATION --------------------

    function registerFarm() external {
        require(!farms[msg.sender].registered, "Already registered");

        farms[msg.sender] = Farm({
            registered: true,
            impactScore: 0,
            totalRewards: 0
        });

        emit FarmRegistered(msg.sender);
    }

    // -------------------- PRACTICE VERIFICATION --------------------

    function verifyPractice(address _farmer, uint256 _impactScore) external {
        require(approvedOracles[msg.sender], "Not authorized oracle");
        require(farms[_farmer].registered, "Farm not registered");
        require(_impactScore > 0, "Invalid score");

        farms[_farmer].impactScore += _impactScore;
        emit PracticeVerified(_farmer, _impactScore);
    }

    // -------------------- REWARD CLAIM --------------------

    function claimReward(uint256 _amount) external {
        Farm storage f = farms[msg.sender];
        require(f.registered, "Farm not registered");

        uint256 maxClaimable = f.impactScore * 1e18; // 1 token per impact point (example)
        uint256 available = maxClaimable - f.totalRewards;
        require(_amount <= available, "Exceeds available rewards");

        f.totalRewards += _amount;
        rewardToken.transfer(msg.sender, _amount);

        emit RewardClaimed(msg.sender, _amount);
    }
}