Ethereum development DApp practical tutorial - using blockchain, Interplanetary File System (IPFS), Node.js and MongoDB to build

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 sell goods freely and buyers can shop freely:

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 a Vickery auction in a decentralized environment - very similar to eBay 's automatic bidding system, rather than simply placing a fixed price on an item.

  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. Escrow fund protection : In order to protect escrow funds, we will use multi-signature (2/3) to achieve anti-fraud protection, that is, when three participants have two consents, will the escrow funds be released to the seller, or the escrow funds will be released returned to the buyer.

In order to facilitate query, we will store product data on the chain and off-chain (database) at the same time; at the same time, in order to avoid expensive on-chain storage for pictures and other data, we will upload pictures and product description information to the same decentralized IPFS The internet.

Section 4 Basic Knowledge Requirements

In order to successfully complete this course, you should have some knowledge of the following languages/technologies:

  1. Solidity/Truffle : The course will dive into using solidity to write contracts. If you haven't learned it yet, it is recommended that you learn the introductory tutorial on developing DApps in Ethereum first, so that you have written at least one or two simple contracts . At the same time, a basic understanding of the truffle development framework will also be very helpful in completing this course.

solidity truffle

  1. HTML/CSS/JavaScript : This course will have more HTML and CSS code than the introductory course. You should have a basic understanding of building frontends with HTML/CSS. At the same time, we will further use JavaScript. It will save the data to the database on the server side, query the database and return the results to the front end. web3.js is used for front-end interaction with the blockchain. We've kept the JavaScript code as simple as possible in order to accommodate learners of all backgrounds.

html css js web3.js

  1. Database : We will use MongoDB to store product information off-chain. No specific knowledge of MongoDB is required, but basic database knowledge will help you successfully complete this course.

mongodb

Section 5 System Architecture

Before starting the specific implementation, let's take a look at the architecture of the decentralized e-commerce DApp we will build in this course .

ebay dapp architecture

  1. Web front end : The web front end is developed using HTML/CSS/JavaScript , of which web3js is heavily used to access the blockchain. Users will access Ethereum, IPFS and NodeJS servers through this front - end application .

  2. Ethereum blockchain : This is the core of decentralized applications. All codes ( e-commerce contracts , fund custody contracts ) and transactions are stored on the chain, including all commodity information, buyer bidding information, and commodity bidding. Results, funds flow to voting results, etc.

  3. MongoDB : Although the core data is stored on the blockchain , in order to facilitate buyers to retrieve and query products, such as only displaying a certain category of products, or displaying products that are about to expire, we will use the MongoDB database to synchronize Store and retrieve product information.

  4. NodeJS server : This is the back- end server, we will use it to provide a REST -style API to the front-end to query products, and at the same time, we will also use it to respond to requests for front-end static pages .

  5. IPFS : When a seller lists a product, the front-end will upload the product image file and introduction text to IPFS , and store the hash value of the uploaded file on the chain.

Section 6 Understanding the role of architecture

To help understand the role of those components discussed in the previous section, let's take a look at the process for a seller to list an item:

ebay list item

  • (1) The front end uses an HTML form to collect the product details entered by the user, such as starting price, product image, description information, etc.

  • (2)(3) The front end uploads the product image and introduction text to IPFS , and returns the link ( hash ) corresponding to the uploaded content.

  • (4)(5) Then, the web front end will call the e-commerce contract to store the commodity information and IPFS link on the chain. When the contract successfully stores the commodity into the blockchain , an event is triggered , which contains all the information about the commodity.

  • (6)(7)(8) The NodeJS server monitors blockchain events . When the event is triggered by the e-commerce contract , the server reads the event content and inserts the product information into the MongoDB database.

We'll revisit this process when we start to implement the product listing feature.

Section 7 Agile Development

We will adopt the idea of ​​agile development to realize the decentralized e-commerce DApp :

scrum way

We put all product features into 8 iteration cycles, and through each sprint , we will get a release-ready version :

The first two sprints mainly focus on implementing e-commerce contracts using solidity and truffle frameworks , which include contract design, development, compilation, deployment and testing :

  • sprint-1: The method of product listing and display to realize the e-commerce contract.

  • sprint-2: A method for realizing commodity bidding and bid revealing of e-commerce contracts.
    After the basic realization of the e-commerce contract, the next three sprints mainly focus on the construction of the front-end user interface, which includes the interaction with the contract using web3, and the data interaction such as uploading pictures through the development interface of ipfs. Of course, there are also necessary Essential DOM manipulation:

  • sprint-3: Provide buyers with a product browsing interface.
  • sprint-4: Provide sellers with a product listing interface.
  • 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; 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.

Guess you like

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