A Clarity smart contract for decentralized electricity grid management on the Stacks blockchain, enabling peer-to-peer energy trading, dynamic pricing, and transparent operations.
The Power Network Control System coordinates electricity generation, consumption, and storage in a decentralized grid. It provides transparent operations, dynamic market-based pricing, peer-to-peer trading, and grid stability management.
- Generators: Register with capacity and energy type (solar, wind, hydro, nuclear, geothermal, biomass, natural-gas)
- Users: Register with consumption limits and prepaid balance accounts
- Storage Reserves: Register battery systems with charge/discharge rates
Normal Mode: Price = Base Rate + (Base Rate × Demand) / (Capacity × 2)
Crisis Mode: Price = Base Rate × 3
Prices automatically adjust based on grid utilization, incentivizing efficient consumption patterns.
- Direct peer-to-peer sales between generators and consumers
- Transparent on-chain transaction records
- Automated settlement through balance accounts
- Real-time network monitoring (load, capacity, utilization)
- Storage operations for grid stabilization
- Emergency pricing for demand management
- Historical analytics and performance tracking
# Check contract
clarinet check
# Run tests
clarinet test
# Deploy to testnet
clarinet deploy --testnet;; Register as generator
(contract-call? .power-network register-generator u5000 "solar")
;; Register as user
(contract-call? .power-network register-user u2000)
;; Add balance
(contract-call? .power-network add-user-balance 'ST1... u50000)
;; Report production
(contract-call? .power-network report-generation u3500)
;; Consume power
(contract-call? .power-network use-power u800)
;; Check grid status
(contract-call? .power-network get-network-status)register-generator(capacity, power-source)- Register as electricity generatorregister-user(usage-limit)- Register as energy consumerregister-reserve(capacity, charge-rate, discharge-rate)- Register storage facility
report-generation(amount)- Report current power outputuse-power(amount)- Consume electricity from gridcharge-reserve(amount)- Store energy in batterydischarge-reserve(amount)- Release stored energy to grid
create-transaction(buyer, amount, price)- Create peer-to-peer sale offercomplete-transaction(transaction-id)- Complete energy purchase
get-network-status()- Current grid status and utilizationget-dynamic-rate()- Current energy priceget-generator-info(generator)- Generator profileget-user-info(user)- User account details
add-user-balance(target-user, amount)- Add funds to user account (public with validation)set-baseline-rate(new-rate)- Update base energy pricetoggle-crisis-state()- Enable/disable emergency pricingrecord-analytics(prod, cons, peak, eff)- Log grid metricsupdate-generator-reputation(target, score)- Adjust reputationdeactivate-participant(target, is-generator)- Disable account
| Code | Description |
|---|---|
| u100 | Owner-only operation |
| u101 | Record not found |
| u102 | Unauthorized access |
| u103 | Insufficient power available |
| u104 | Invalid amount |
| u105 | Already exists |
| u106 | Participant inactive |
| u107 | Insufficient balance |
| u108 | Invalid input |
;; Generator
{
capacity: uint,
current-production: uint,
total-produced: uint,
energy-type: string-ascii,
active: bool,
reputation-score: uint (0-100)
}
;; User
{
consumption-limit: uint,
total-consumed: uint,
current-usage: uint,
balance: uint,
active: bool
}
;; Storage Reserve
{
capacity: uint,
stored-amount: uint,
charge-rate: uint,
discharge-rate: uint
}Residential Microgrids: Neighborhoods with solar panels trading excess energy locally
Commercial Trading: Businesses monetizing on-site generation capacity
Battery Storage: Grid-scale storage providing arbitrage and stabilization services
Remote Communities: Fair energy allocation in islanded microgrids
EV Charging: Dynamic pricing for charging station networks
- Access Control: Three-tier permissions (public, validated, admin-only)
- Input Validation: All amounts and principals verified before operations
- Balance Protection: Cannot consume more than available funds
- Capacity Limits: Generators and users bound by registered limits
- Active Status Checks: Only active participants can operate
| Parameter | Default | Description |
|---|---|---|
| Maximum Capacity | 1,000,000 | Total grid capacity |
| Base Rate | 100 | Starting energy price |
| Emergency Multiplier | 3x | Crisis mode pricing |
| Max Balance Addition | 1,000,000 | Single deposit limit |
| Reputation Range | 0-100 | Generator score scale |
// Example test
Clarinet.test({
name: "User cannot consume without balance",
async fn(chain: Chain, accounts: Map<string, Account>) {
const user = accounts.get('wallet_1')!;
let block = chain.mineBlock([
Tx.contractCall('power-network', 'register-user',
[types.uint(2000)], user.address),
Tx.contractCall('power-network', 'use-power',
[types.uint(100)], user.address)
]);
block.receipts[1].result.expectErr().expectUint(107);
},
});- Security audit completed
- Configure network parameters
- Deploy to testnet
- Beta test with participants
- Set up off-chain monitoring
- Deploy to mainnet
- Document integration requirements
- Blockchain confirmation times unsuitable for real-time balancing
- On-chain storage limits scalability for very large networks
- Single administrator creates centralization risk
- Requires off-chain infrastructure for physical metering