Skip to content

mosessalako/Carbon-Footprint-Tracking

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 

Repository files navigation

Carbon Footprint Tracking Smart Contract

A comprehensive Clarity smart contract for tracking carbon emissions, offsets, and carbon credits on the Stacks blockchain. This contract enables transparent, verifiable carbon accounting for individuals and organizations.

Features

🌍 Core Functionality

  • Emission Tracking: Record carbon emissions with categories, amounts, and descriptions
  • Offset Management: Log carbon offset activities with methods and costs
  • Carbon Credits: Issue, hold, and transfer carbon credits between users
  • Verification System: Authorized verifiers can validate emissions and offsets
  • Organization Support: Register organizations with dedicated profiles

πŸ“Š Analytics & Reporting

  • View individual user profiles and statistics
  • Calculate net emissions (emissions - offsets)
  • Track global statistics (total emissions, offsets, credits)
  • Monitor individual emission and offset records

Contract Structure

Constants

err-owner-only (u100)          - Unauthorized owner action
err-not-found (u101)           - Record not found
err-unauthorized (u102)        - Unauthorized verifier action
err-invalid-amount (u103)      - Invalid amount (zero or negative)
err-insufficient-credits (u104) - Insufficient carbon credits
err-already-verified (u105)    - Record already verified
err-invalid-input (u108)       - Invalid input parameters

Data Maps

  • user-profiles: User registration and carbon accounting data
  • emission-records: Individual emission entries with verification status
  • offset-records: Carbon offset activities and costs
  • organization-profiles: Organization registration and details
  • authorized-verifiers: List of authorized verification accounts

Public Functions

User Management

register-user()

Register a new user profile to start tracking carbon footprint.

  • Returns: (ok bool) - true if newly registered, false if already exists

register-organization(name: string-utf8)

Register an organization with a name and automatic user profile creation.

  • Parameters:
    • name: Organization name (1-100 characters)
  • Returns: (ok bool)

Emission & Offset Recording

record-emission(amount: uint, category: string-ascii, description: string-utf8)

Record a carbon emission entry.

  • Parameters:
    • amount: CO2 amount (in kg or chosen unit)
    • category: Emission category (e.g., "transportation", "energy")
    • description: Detailed description (max 256 chars)
  • Returns: (ok uint) - record ID
  • Requires: User must be registered

record-offset(amount: uint, method: string-ascii, cost: uint)

Record a carbon offset activity.

  • Parameters:
    • amount: Offset amount (matching emission units)
    • method: Offset method (e.g., "tree-planting", "renewable-energy")
    • cost: Cost in microSTX or chosen currency
  • Returns: (ok uint) - offset ID
  • Requires: User must be registered

Verification System

verify-emission(target-user: principal, record-id: uint)

Verify an emission record (verifiers only).

  • Parameters:
    • target-user: User whose emission to verify
    • record-id: Emission record ID
  • Returns: (ok bool)
  • Requires: Caller must be authorized verifier

verify-offset(target-user: principal, offset-id: uint)

Verify an offset record (verifiers only).

  • Parameters:
    • target-user: User whose offset to verify
    • offset-id: Offset record ID
  • Returns: (ok bool)
  • Requires: Caller must be authorized verifier

verify-user-profile(target-user: principal)

Mark a user profile as verified (verifiers only).

  • Parameters:
    • target-user: User to verify
  • Returns: (ok bool)
  • Requires: Caller must be authorized verifier

Carbon Credits Management

issue-carbon-credits(target-user: principal, amount: uint)

Issue carbon credits to a user (owner only).

  • Parameters:
    • target-user: Recipient principal
    • amount: Number of credits to issue
  • Returns: (ok bool)
  • Requires: Caller must be contract owner

transfer-credits(recipient: principal, amount: uint)

Transfer carbon credits to another user.

  • Parameters:
    • recipient: Recipient principal
    • amount: Credits to transfer
  • Returns: (ok bool)
  • Requires: Sufficient credit balance

Admin Functions

add-verifier(verifier: principal)

Add an authorized verifier (owner only).

  • Parameters:
    • verifier: Principal to authorize
  • Returns: (ok bool)

remove-verifier(verifier: principal)

Remove a verifier's authorization (owner only).

  • Parameters:
    • verifier: Principal to deauthorize
  • Returns: (ok bool)

Read-Only Functions

get-user-profile(user: principal)

Retrieve a user's complete profile including emissions, offsets, and credits.

get-emission-record(user: principal, record-id: uint)

Get details of a specific emission record.

get-offset-record(user: principal, offset-id: uint)

Get details of a specific offset record.

get-total-statistics()

Get global statistics including total emissions, offsets, and net emissions.

get-user-net-emissions(user: principal)

Calculate net emissions for a user (emissions minus offsets).

is-verifier(user: principal)

Check if a principal is an authorized verifier.

get-user-record-count(user: principal)

Get the total number of emission records for a user.

get-organization(org: principal)

Retrieve organization profile details.

Usage Example

;; Register as a user
(contract-call? .carbon-footprint register-user)

;; Record an emission
(contract-call? .carbon-footprint record-emission 
    u1000 
    "transportation" 
    u"Monthly car usage - 500km")

;; Record an offset
(contract-call? .carbon-footprint record-offset 
    u500 
    "tree-planting" 
    u100000)

;; Check your profile
(contract-call? .carbon-footprint get-user-profile tx-sender)

;; Transfer credits
(contract-call? .carbon-footprint transfer-credits 
    'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM 
    u50)

Security Features

βœ… Input Validation: All inputs validated before processing
βœ… Principal Verification: Uses is-standard to validate addresses
βœ… Authorization Checks: Role-based access control for sensitive operations
βœ… Duplicate Prevention: Prevents re-verification of records
βœ… Self-Transfer Protection: Prevents users from transferring credits to themselves
βœ… Amount Validation: Ensures non-zero values for all transactions

Deployment

  1. Deploy the contract to Stacks blockchain
  2. Contract owner is set to deployer's address
  3. Add initial verifiers using add-verifier
  4. Users can start registering and tracking emissions

Best Practices

  • Units: Maintain consistent units across emissions and offsets (e.g., kg CO2)
  • Categories: Use standardized category names for better analytics
  • Verification: Get emission/offset records verified for credibility
  • Regular Tracking: Record emissions and offsets consistently
  • Credit Management: Monitor credit balance before transfers

About

This is a decentralized solution built on the Stacks blockchain using Clarity that enables transparent, immutable, and verifiable carbon accounting for individuals and organizations. It provides a comprehensive system for recording, verifying, and managing carbon emissions, offsets, and tradeable carbon credits in a trustless environment.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors