A comprehensive Clarity smart contract for tracking energy consumption, managing energy providers, and calculating costs on the Stacks blockchain.
This smart contract provides a decentralized platform for energy consumption tracking with features for provider management, consumer registration, consumption recording, and cost calculation. It's designed for energy cooperatives, utilities, and distributed energy networks.
- Provider Management: Register and manage energy providers with custom rates
- Consumer Registration: Self-registration system for energy consumers
- Consumption Tracking: Record energy usage with automatic cost calculation
- Verification System: Owner-controlled verification of consumption records
- Analytics: Comprehensive statistics and reporting
- Access Control: Owner-only functions for critical operations
- Input Validation: Comprehensive parameter validation
- Error Handling: Detailed error codes for precise debugging
- Data Integrity: Prevents duplicate registrations and invalid data
energy-providers: Store provider information and ratesenergy-consumers: Track consumer profiles and statisticsconsumption-records: Detailed consumption historyprovider-authorizations: Provider authorization tracking
;; Register a new energy provider (Owner only)
(register-provider (name (string-ascii 50)) (rate-per-kwh uint))
;; Update provider rates (Owner only)
(update-provider-rate (provider-id uint) (new-rate uint))
;; Toggle provider active status (Owner only)
(toggle-provider-status (provider-id uint));; Register as an energy consumer
(register-consumer (name (string-ascii 50)))
;; Record energy consumption
(record-consumption (provider-id uint) (amount-kwh uint));; Verify consumption records (Owner only)
(verify-consumption (record-id uint))
;; Toggle consumer active status (Owner only)
(toggle-consumer-status (consumer-address principal));; Get provider information
(get-provider-info (provider-id uint))
;; Get consumer information
(get-consumer-info (consumer-address principal))
;; Calculate energy costs
(calculate-cost (amount-kwh uint) (provider-id uint))
;; Get detailed consumer statistics
(get-consumer-stats (consumer-address principal))
;; Get contract statistics
(get-contract-stats)| Code | Constant | Description |
|---|---|---|
| u100 | ERR-NOT-AUTHORIZED | Unauthorized access attempt |
| u101 | ERR-INVALID-AMOUNT | Invalid amount parameter |
| u102 | ERR-PROVIDER-NOT-FOUND | Provider does not exist |
| u103 | ERR-CONSUMER-NOT-FOUND | Consumer not registered |
| u104 | ERR-INVALID-RATE | Invalid rate parameter |
| u105 | ERR-ALREADY-EXISTS | Record already exists |
| u106 | ERR-INVALID-INPUT | Invalid input parameter |
;; Contract deploys with tx-sender as CONTRACT-OWNER
;; Register first energy provider
(contract-call? .energy-contract register-provider "Solar Energy Co" u50) ;; 50 per kWh;; Register as consumer
(contract-call? .energy-contract register-consumer "Home Consumer");; Record 100 kWh consumption from provider 1
(contract-call? .energy-contract record-consumption u1 u100)
;; Cost: 100 * 50 = 5000 units;; Get consumer stats
(contract-call? .energy-contract get-consumer-stats 'SP1ABC...)
;; Get contract overview
(contract-call? .energy-contract get-contract-stats){
name: (string-ascii 50),
rate-per-kwh: uint,
total-supplied: uint,
active: bool,
registered-at: uint
}{
name: (string-ascii 50),
total-consumed: uint,
total-cost: uint,
active: bool,
registered-at: uint
}{
consumer: principal,
provider-id: uint,
amount-kwh: uint,
cost: uint,
timestamp: uint,
verified: bool
}- Owner Privileges: Only contract owner can register providers and verify records
- Input Validation: All external inputs are validated before processing
- Range Checks: Provider and record IDs are validated against valid ranges
- State Consistency: Automatic updates maintain data consistency across all maps
- Efficient data structures minimize storage costs
- Single-transaction updates for related data
- Optimized read-only functions for analytics
- Minimal external calls and computations