DEV Community

vault vult
vault vult

Posted on

How I Built a DePIN Solar Energy Protocol for the W3Node Hackathon

The Web3 ecosystem is rapidly shifting from speculative finance to real-world infrastructure. For the W3Node Hackathon, I decided to tackle one of the most critical real-world problems: Energy Access in Africa.

Africa needs over 46,000 new mini-grids by 2030 to power rural communities. The hardware is ready, but the financial rails to manage, verify, and pay for this distributed infrastructure don't exist.

To solve this, I built the SHS DePIN Network (Solar Home System Decentralized Physical Infrastructure Network).

Here is a deep dive into how it was built, the architecture, and how it works.

🏗️ The Architecture

The protocol consists of two main smart contracts deployed on the Base Sepolia Testnet:

  1. MockUSDC: A standard ERC-20 stablecoin used for payments.
  2. EnergyMarket: The core engine that handles device registration, consumer payments, machine-to-machine (M2M) reward distribution, and operator withdrawals.

For the frontend, I used Next.js, Wagmi, and Viem to build a sleek, dark-mode dashboard that connects directly to MetaMask.

🔒 Smart Contract Security: The Reentrancy Fix

During the build, Foundry's linter flagged a critical vulnerability in the purchaseEnergy function. The original code made an external call (transferFrom) before updating the state variables.

To make this hackathon project production-ready, I refactored the contract to follow the Checks-Effects-Interactions pattern.

// 1. EFFECTS: Update state first
device.totalRewardsEarned += _amount;
operatorBalances[device.operator] += _amount;

// 2. INTERACTIONS: External call last
require(paymentToken.transferFrom(msg.sender, address(this), _amount), "Payment failed");
Enter fullscreen mode Exit fullscreen mode

This completely eliminates the risk of a reentrancy attack, ensuring that the funds are safe.

🔮 The Future: Real-Time IoT Data

For the MVP, I built an Oracle Simulator in the UI. It allows the admin to inject simulated solar generation data on-chain.

In the real world, this contract is ready to be connected to physical IoT hardware (like an ESP32 chip on a solar inverter) or decentralized oracle networks like Chainlink, transforming it from a simulation into a real-time, automated machine-to-machine economy.

🔗 Live Links

🎥 Watch the 2-Minute Demo: https://youtu.be/xfYc27v8Qho

💻 View the Code: https://github.com/vaultvult/shs-depin-mvp

⛓️ View the Contract on BaseScan: https://sepolia.basescan.org/address/0x6309C657B4fc06B39Ac9Bcb040156898A4F23629

🐦 Follow the Build on Twitter: https://x.com/TrAdArSOY/status/2115206565959524372?a=xyQIUZ1igLA1bC4e-CI&s=20

Building this MVP proved to me that DePIN is the future of infrastructure. The rails are built, and the future of energy is decentralized.

Top comments (0)