The ability for residents to actively trade and generate energy on the demand side is made possible by distributed renewable energy and two-way communication infrastructures. Information exchange is compromised by eavesdropping, lack of dependability, and loss of privacy under the conventional centralized power management model. This article suggests a distributed energy trading system for residential communities that is efficient and protects privacy, powered by a novel smart contract. For the demand side to engage in peer-to-peer energy trading on the real-time market, an effective smart-contract-based bidding mechanism is presented. To ensure that the information exchange is safe, seamless, and traceable using a decentralized system, the full electricity transaction is performed in the smart contract. Results from simulation studies show that the proposed system is both stable and scalable. It could also help to guarantee the security of energy transactions and reduce customers’ costs. As a result, our proposed smart-contract-based trading system is applied to microgrids formed by distributed renewable energy sources, which is proven to reduce the load consumption of residents among communities as well as obtain more significant economic benefits compared to the centralized trading mechanism.