Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build a complete stack of ethernet square Dapp

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to build a complete stack of Dapp in Ethernet Square". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

What is a blockchain?

Let's use an analogy to understand what a blockchain is and how it works. Let's look at a Web application.

Typically, when you interact with a Web application, you use a Web browser to connect to the central server over the network. All the code for this Web application is on this central server, and all data is in the central database. Whenever you trade with an application, you must communicate with this central server on the Web.

If we are going to build our voting application online, we will encounter some problems:

You can change the data on the database: you can count it multiple times, or you can delete it completely.

The source code on the Web server can also be changed at any time.

We don't want to build our applications on the web. We want to build it on the blockchain so that anyone connected to the network can participate in the election. We want to make sure that their votes are counted and that they only count once. So let's see how it works.

Blockchain does not have a network, central server and database, but a network and database. Blockchain is a peer-to-peer network of computers, called nodes, that shares all the data and code in the network. Therefore, if you are a device connected to a blockchain, you are a node in the network, and you can communicate with all other computer nodes in the network. You now have a copy of all the data and code on the blockchain. There are no more central servers. It's just a bunch of computers that communicate with each other on the same network.

Instead of a centralized database, all transaction data shared between nodes in a blockchain is contained in a record package called a block, which is bundled together to create a common ledger. This common ledger represents all data in the block chain. All data in the public ledger is protected by encrypted hashes and verified by a consistency algorithm. Nodes on the network participate to ensure that all copies of data distributed over the network are the same. This is a very important reason why we build the voting application on the blockchain, because we want to make sure that our vote is counted and that it does not change.

What will it look like for our application users to vote on the blockchain? So, for beginners, the user needs an account with a wallet address, which contains some Ether, the cryptocurrency of Etay Square. Once they are connected to the network, they will vote and pay a small transaction fee to write the transaction into the blockchain. The transaction fee is called gas. Whenever a vote is cast, some nodes on the network (called miners) compete to complete the deal. Miners who complete this transaction will receive the etheric currency in which we pay and vote.

In retrospect, when I voted, I paid the gas price to vote, and when my vote was recorded, a computer on the network received my Ethernet fee. I, on the other hand, believe that my vote is always accurately recorded.

So it's also important to note that voting on blockchain costs etheric money, but you just don't see the list of candidates. This is because reading data from the blockchain is free, but writing to the blockchain is not.

What is a smart contract?

This is how the voting process works, but how do we actually write application code? Well, the Taifang blockchain allows us to use something called a smart contract to execute code on the blockchain using the Ethernet Square Virtual Machine (EVM).

Smart contracts are all the business logic of our application. This is where we actually write the decentralized part of the application. Smart contracts are responsible for reading and writing data to the blockchain, as well as executing business logic. Smart contacts are written in a programming language called Solidity and look a lot like Javascript. It is a complete programming language that allows us to perform many of the same types of things that Javascript can provide, but its behavior is a little different because of its use cases, as we will see in this tutorial.

The function of intelligent contracts on blockchain is very similar to that of micro-services on the network. If the common ledger represents the database layer of the blockchain, then the smart contract is the location of all the business logic that trades with that data.

In addition, they are called smart contracts because they represent contracts or agreements. In the case of our voting dApp, this is an agreement, my vote will be counted, the other votes will only be counted once, and the candidate with the most votes will actually win the election.

Now let's take a quick look at the structure of the dApp we are building.

We will have a traditional front-end client written in HTML,CSS and Javascript. The client will connect to the local Ethernet block chain that we will install instead of communicating with the back-end server. We will use the Solidity programming language to write all the business logic about dApp in the election intelligence contract. We deployed this smart contract to our local Etherum blockchain and allowed the account to start voting.

Now we have seen what blockchain is and how it works. We have seen why we should build our voting dApp on the blockchain rather than on the current network. We have seen that we want to write our dApp code by writing a smart contract that will be deployed to the etherfang block chain. Now let's jump in and start programming!

What are we going to build?

The following is a demonstration of the voting dApp we are going to build.

We will build a client application that will talk to our smart contract on the blockchain. This client application will have a candidate table that lists each candidate's ID, name, and vote count. It will have a form where we can vote for the candidate we want. It will also display our account associated with the blockchain under your account your account.

Install dependencies

To build our dApp, we first need some dependencies.

Node package Manager (NPM)

The first dependency we need is Node Package Manager, or NPM, which is provided with Node.js. You can see if the node is installed by going to the terminal and typing:

$node-vTruffle framework

The next dependency is Truffle Framework, which allows us to build decentralized applications on the etherfang block chain. It provides a set of tools that allow us to write smart contacts in the Solidity programming language. It also enables us to test our smart contracts and deploy them to the blockchain. It also provides us with the space to develop client applications.

You can use NPM to install Truffle from the command line, as follows:

$npm install-g truffleGanache

The next dependency is Ganache, a chain of blocks in local memory. You can install Ganache by downloading it from the Truffle Framework website. It will provide us with 10 external accounts and addresses on our local ethernet block chain. Each account is preloaded with 100 fake ethernet coins.

Metamask

The next dependency is Google Chrome's Metamask extension. In order to use a blockchain, we have to connect to it (remember, I say blockchain is a network). We must install a special browser extension to use the ethernet block chain. This is the opportunity for metamask to use his talents. We will be able to connect to our local ethernet block chain through our personal accounts and interact with our smart contracts.

We will use the Metamask chrome extension in this tutorial, so if you do not already have a Google Chrome browser installed, you will need to install it. To install Metamask, search the Google Chrome Web App Store for the Metamask Chrome plug-in. After the installation is complete, make sure it is selected in the extension list. After installation, you will see the fox icon in the upper right corner of the Chrome browser. If it is stuck, please refer to the video demonstration!

Syntax highlighting

Dependencies are optional, but recommended. I recommend installing syntax highlighting for the Solidity programming language. Most text editors and IDE do not have out-of-the-box Solidity syntax highlighting, so you must install a package to support this feature. I'm using Sublime Text, and I've downloaded the Ethereum package, which provides good syntax highlighting for Solidity.

Smoke Test-step 1

Now that we have installed the dependencies, let's start building our dApp!

First, find the place where you downloaded Ganache, and then open it. Now that Ganache is started, you have a local block chain running.

Ganache provides us with 10 accounts pre-installed with 100 fake ethernet (which is not worth it on the major ethernet network). Each account has a unique address and a private key. Each account address will be a unique identifier for each voter in our election.

Now let's create a project directory for dApp on the command line, as follows:

$mkdir election$ cd election

Now that we have entered our project, we can start and run quickly using Truffle box. We will use Pet Shop box in this tutorial. From the project directory, install Pet Shop box from the command line, as follows:

$truffle unbox pet-shop

Let's see what Pet Shop box has given us:

Contract directory: this is where all smart contacts are located. We already have a migration contract to handle our migration to the blockchain.

Migration directory: this is where all the migration files are located. These migrations are similar to other Web development frameworks that need to migrate to change the state of the database. Whenever we deploy a smart contract to a blockchain, we update the status of the blockchain, so we need to migrate.

Node_modules directory: this is the home page of all our Node dependencies.

Src directory: this is where we develop client applications.

Test catalog: this is where we write tests for smart contracts.

Truffle.js file: this is the main configuration file for our Truffle project.

Now let's start writing our smart contract! This smart contract will contain all the business logic of our dApp. It will be responsible for reading and writing the ethernet block chain. It will allow us to list the candidates who will take part in the election and track all votes and voters. It will also manage all electoral rules, such as enforcing accounts that vote only once. Starting from the root directory of the project, create a new contract file in the contract directory, as follows:

$touch contracts/Election.sol

Let's first create a smoke test to ensure that we set up the project correctly and that we can successfully deploy the contract to the blockchain. Open the file and start with the following code:

Pragma solidity 0.4.2 position contract Election {/ / Read/write candidate string public candidate; / / Constructor function Election () public {candidate = "Candidate 1";}}

Let me explain this code. We first declare the solidity version using the pragma solidity statement. Next, we use the contract keyword to declare the smart contract, followed by the contract name. Next, we declare a state variable that stores the value of the candidate name. State variables allow us to write data to the blockchain. We have declared that this variable will be a string, and we have set its visibility to public. Because it is public, solidity will provide us with free getter functionality, which will allow us to access this value outside the contract. We'll see it in the console later!

Then we create a constructor that will be called whenever we deploy the smart contract to the blockchain. This is the value of the candidate state variable that we will set, which will be stored in the block chain during migration. Note that the constructor has the same name as the smart contract. This is how Solidity knows that a function is a constructor.

Now that we have created the foundation for the smart contract, let's see if we can deploy it to the blockchain. To do this, we need to create a new file in the migration directory. From the project root, create a new file from the command line, as follows:

$touch migrations/2_deploy_contracts.js

Note that we use numbers to number all the files in the migration directory so that Truffle knows the order in which they are executed. Let's create a new migration to deploy the contract, as follows:

Var Election = artifacts.require (". / Election.sol"); module.exports = function (deployer) {deployer.deploy (Election);}

First, we need the contract we created and assign it to a variable named Election. Next, we add it to the list of deployed contracts to ensure that it is deployed when we run the migration. Now let's run our migration from the command line, as follows:

$truffle migrate

Now that we have successfully migrated the smart contract to the local etherfang block chain, let's open the console to interact with the smart contract. You can open the truffle console from the command line, as follows:

$truffle console

Now that we have entered the console, let's take an example of a deployed smart contract and see if we can read the name of the candidate from the contract. From the console, run the following code:

Election.deployed () .then (function (instance) {app = instance})

Where Election is the name of the variable we created in the migration file. We use the deployed () function to retrieve the deployed contract instance and assign it to the app variable in the callback function of promise.

Now we can read the value of the candidate variable like this:

App.candidate () / / = > 'Candidate 1' "how to build a complete stack for Dapp in Ethernet Square" is introduced here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report