Blockchain, Interplanetary File System (IPFS), Nodejs and Mongodb to build Ethereum DApp e-commerce platform

Section 1 Introduction

Welcome to develop and build a decentralized e-commerce DApp with Ethereum! We will use blockchain, Interplanetary File System (IPFS), Node.js and MongoDB to build an e-commerce platform similar to Taobao's online e-commerce application, where sellers can freely sell goods and buyers can freely shop:

ethereum dapp Ethereum DApp development

  1. Decentralization: Unlike Taobao or eBay , we put all business logic and core data on the Ethereum blockchain, which makes it a completely decentralized application. Compared with a centralized e-commerce platform like Taobao, a decentralized P2P e-commerce application obviously has its own unique value - at least you don't have to worry about being blocked by the platform.

  2. IPFS: Storing image and description hypertext for product display on Ethereum is expensive and sometimes not feasible due to the limitations of the Ethereum Virtual Machine. In order to solve this problem, we will store product images and product description information in the same decentralized Interplanetary File System ( IPFS ), and only save the ID of these data on the chain .

  3. Commodity auction: For sellers, auction is obviously a very good sales method to increase the profit margin of commodities. So in our course project we will implement Vickery auctions in a decentralized environment - very similar to eBay 's automatic bidding system, rather than simply placing a fixed price on items.

  4. Fund Custody:  One of the advantages of a centralized platform is that it naturally provides a trusted intermediary between buyers and sellers. In a decentralized environment, we will use a multi-party escrow contract to deal with the possible risks of buyers and sellers. The escrow contract uses a voting mechanism to determine the final flow of the buyer's payment.

  5. Off-chain data storage: Don't be limited by decentralization, traditional technologies still have their strengths. We will use MongoDB to do a synchronous data backup off-chain in order to achieve a function that is difficult to achieve with blockchain alone: ​​flexible commodity query.

Section 2 Decentralization, why?

Before we start building our application, it's well worth taking a minute to understand why we should build an online marketplace on a decentralized platform like Ethereum.

C2C e-commerce platforms like eBay or Taobao have been hugely successful because they make it very convenient for both buyers and sellers:

ebay-taobao Taobao c2c e-commerce platform

Before the Internet became mainstream, people could only buy and sell goods in small areas, or between neighborhoods. As more and more people use the internet, platforms like eBay emerge where you can buy and sell items online from anywhere in the world. Such platforms have value, both for merchants and consumers.

While a platform like eBay is convenient for everyone and improves commerce and the economy, it also has some drawbacks:

  1. bound by the platform. Participating merchants are subject to the business that owns the platform. At any time, the platform owner can decide whether to ban a merchant, and if the merchant relies heavily on the platform, the account ban is a huge blow.

  2. Merchant fees are high. Merchants have to pay a fee for listing items, and they also have to pay a commission for selling items. There's nothing wrong with charging a fee, after all, platforms like eBay provide the service. However, listing fees are sometimes too high, resulting in merchants making little profit or passing the cost on to consumers.

  3. Data is out of control. Neither merchants nor consumers can own data that should be theirs. All data such as reviews, purchase history, etc. is owned by the platform owner. For example, if a merchant wants to change a provider, or wants to export product reviews or other data, it is very difficult or even impossible.

The decentralized e-commerce platform built on Ethereum solves these problems: the merchant's account will not be blocked; the data is also public, so it is easy to export the data; compared with the centralized platform, the transaction commission will also be lower many.

Section 3 Preliminary Features

Now that you understand why you want to build a decentralized e-commerce application, you also understand what the application we want to build is. Now let's take a general look at the main features that will be implemented in this project:

ebay user case

  1. Product listing : The app should support sellers to list products for sale. We will implement a feature that allows anyone to list products freely.

  2. Product Browsing and Searching : The app should allow buyers to easily browse product listings . We will implement the function of browsing products, as well as the function of querying based on conditions such as product category and auction time.

  3. Commodity auction : Like eBay, we will realize the auction sales of commodities in the Vekerui auction method. Since all transactions on Ethereum are public, our implementation will be different than in a centralized environment.

  4. Fund escrow : Once the bidding is over and there is a winner in the commodity auction, we will create an escrow contract involving the winning buyer, seller and any third party, and the escrow contract will manage the transaction funds.

  5. 托管资金保护:为了保护托管资金,我们将采用多重签名(2/3)来实现防欺诈保护,即三个参与者有两个同意时,才会将托管资金释放给卖方,或是将托管资金返还给买方。

为了便于查询,我们会将商品数据同时存在链上和链下(数据库);同时,为了避免图片等数据占用昂贵的链上存储,我们将把图片和商品描述信息上传到同样去中心化IPFS网络。

第四节 基础知识要求

为了顺利地完成本课程的学习,你应该对以下语言/技术有一些了解:

  1. Solidity/Truffle:课程将会深入使用solidity来编写合约。如果你还没有学过,建议你先学习一下以太坊开发DApp入门教程,这样至少写过一两个简单的合约。同时,对truffle开发框架的基本了解也会十分有助于完成本课程。

solidity truffle

  1. HTML/CSS/JavaScript:相比入门课程,本课程将会有更多的HTML和CSS代码。你应该对使用HTML/CSS构建前端有基本的了解。同时,我们将会进一步使用JavaScript。它会在服务端将数据保存到数据库,查询数据库并将结果返回给前端。web3.js用于前端与区块链的交互。为了适用各种背景的学习者,我们已经保持JavaScript代码尽可能地简单。

html css js web3.js

  1. Database:我们会用MongoDB在链下保存产品信息。无须特别了解MongoDB,但是基本的数据库知识有助于你顺利完成本课程的。

mongodb

第五节 系统架构

在开始着手具体的实现之前,先来看一下在本课程我们将要构建的去中心化电商DApp架构

ebay dapp architecture

  1. Web前端:web前端使用HTML/CSS/JavaScript开发,其中大量使用了web3js来访问区块链。用户将会通过这个前端应用来访问以太坊IPFSNodeJS服务器。

  2. 以太坊区块链:这是去中心化应用的核心,所有的代码(电商合约资金托管合约)和交易都存储在链上,这包括所有的商品信息、买家的出价信息、商品竞价结果、资金流向投票结果等。

  3. MongoDB:尽管核心数据存储在区块链上,但是为了方便买家对商品的检索和查询,例如只显示某一类的商品,或者显示即将过期的商品等等,我们会用MongoDB数据库来同步地存储和检索商品信息。

  4. NodeJS服务器:这是后端服务器,我们会利用它给前端提供REST风格的API来查询商品, 同时,也利用它来响应对前端静态页面的请求。

  5. IPFS: 当卖家上架一个商品时,前端会商品图片文件和介绍文本上传到IPFS,并将所上传文件的哈希值存到链上。

第六节 理解架构的作用

为了帮助理解上一节谈到的那些组件的作用,让我们来看看一下卖家上架一个商品的流程:

ebay list item

  • (1)前端使用一个HTML表单来采集用户输入的商品细节,例如起拍价、商品图片、描述信息等。

  • (2)(3) 前端将商品图片和介绍文本上传到IPFS,并返回所上传内容对应的链接(哈希)。

  • (4)(5) 然后,web前端会调用电商合约将商品信息IPFS链接存储到链上。当合约成功地将商品存入区块链后,就会触发一个事件,该事件中包含了商品所有的信息。

  • (6)(7)(8) NodeJS服务器监听区块链事件,当事件被电商合约触发时,服务器读取事件内容并将商品信息插入到MongoDB数据库中。

当开始具体实现商品上架这一特性时,我们将重温这一流程。

第七节 敏捷开发

我们将采用敏捷开发的思想来实现去中心化电商DApp

scrum way

我们将全部的产品特性分别列入8个迭代周期,通过每一次的冲刺sprint),我们都将得到一个可以发布的版本:

前两个冲刺主要集中在使用soliditytruffle框架实现电商合约方面,这包括合约的设计、开发 、编译、部署与测试

  • sprint-1:实现电商合约的商品上架和展示方法。

  • sprint-2:实现电商合约的商品竞价和出价揭示方法。
    在电商合约基本实现之后,接下来的三个冲刺主要集中在前端用户界面的构建方面,这包括使用web3 与合约的交互,以及通过ipfs的开发接口上传图片等数据交互,当然,还有必不可少的DOM操作:

  • sprint-3:为买家提供商品浏览界面。
  • sprint-4:为卖家提供商品上架操作界面。
  • sprint-5: Provide buyers with product details interface, bidding form, and bid reveal form. In the next two sprints, we will first implement the fund escrow contract to manage the funds of the winning buyer after the auction ends; and then implement the corresponding user interface.

  • sprint-6: Implement the fund custody contract.
  • sprint-7: Based on the fund custody contract, it provides an operation interface for all parties involved in the custody. Finally, in order to facilitate the query and retrieval of commodities, we will use MongoDB to implement off-chain storage of commodity data.

  • sprint-8: Realize the synchronization and data query of off-chain data.

     

     Tutorial reference: Huizhi.com DApp e-commerce combat

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326020831&siteId=291194637