Personal Carbon Footprint Tracker

What it does:
Tracks personal carbon emissions from activities (travel, energy use, consumption) and allows users to monitor, reduce, and offset their footprint on-chain.

Why it matters:
Encourages eco-conscious behavior, provides verifiable carbon data, enables tokenized incentives or rewards for reduction, and integrates sustainability into personal lifestyle management.

How it works:

  • Users log activities contributing to carbon emissions

  • Smart contract calculates total footprint based on predefined emission factors

  • Users can purchase carbon offsets or participate in sustainability rewards

  • Integrates with Carbon Credit Trading, Green Energy Sharing, or Recycling Reward Smart Contract

  • Dashboards show emissions history, reductions, and offset achievements

  • Reputation or token incentives can be awarded for footprint reduction

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

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

/**
 * @title PersonalCarbonTracker
 * @author Nam
 * @notice Tracks personal carbon footprint and offsets on-chain
 */
contract PersonalCarbonTracker is Ownable {

    struct Activity {
        string description;
        uint256 carbonAmount; // in grams CO2 equivalent
        uint256 timestamp;
        bool offset;
    }

    mapping(address => Activity[]) private userActivities;
    mapping(address => uint256) public totalCarbon; // grams CO2

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

    event ActivityLogged(address indexed user, string description, uint256 carbonAmount);
    event CarbonOffsetPurchased(address indexed user, uint256 carbonAmount, uint256 payment);

    // -------------------- ACTIVITY LOGGING --------------------

    function logActivity(string calldata _description, uint256 _carbonAmount) external {
        require(_carbonAmount > 0, "Carbon must be >0");

        userActivities[msg.sender].push(Activity({
            description: _description,
            carbonAmount: _carbonAmount,
            timestamp: block.timestamp,
            offset: false
        }));

        totalCarbon[msg.sender] += _carbonAmount;

        emit ActivityLogged(msg.sender, _description, _carbonAmount);
    }

    // -------------------- CARBON OFFSET --------------------

    function purchaseOffset(uint256 _activityIndex) external payable {
        Activity storage a = userActivities[msg.sender][_activityIndex];
        require(!a.offset, "Already offset");
        require(msg.value >= a.carbonAmount * 1e12, "Insufficient payment"); // e.g., 1 wei per gram, adjustable

        a.offset = true;
        totalCarbon[msg.sender] -= a.carbonAmount;

        emit CarbonOffsetPurchased(msg.sender, a.carbonAmount, msg.value);

        // Payment can be sent to a sustainability fund or DAO
        // payable(owner()).transfer(msg.value);
    }

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

    function getActivities(address _user) external view returns (Activity[] memory) {
        return userActivities[_user];
    }

    function getTotalCarbon(address _user) external view returns (uint256) {
        return totalCarbon[_user];
    }
}