Ethereum smart contract development DApp application example

Smart contract development is the core of Ethereum, and the learning process is mainly to build the Ethereum development environment and programming in solidity language. This article does not use any Ethereum Dapp framework, and develops smart contracts directly under ganache.

Let's build a simple "Hello World!" application, which is a voting application.

The app is very simple, all it does is initialize a set of candidates, let anyone vote for a candidate, and display the total number of votes each candidate has received.

I intentionally avoid building this app with any DAPP framework, because the framework abstracts away a lot of details and you don't know the internals of the system. Also, when you use a framework, you get a lot more appreciation for the heavy lifting that the framework does!

our target is:

  • Set up a development environment.
  • Learn to write smart contracts

1. Set up the development environment

We are developing using a simulated in-memory blockchain (ganache) instead of the real blockchain. In chapter 2 of this tutorial, we will interact with the real blockchain. Below are the steps to install ganache, web3js, and then start a test chain on linux. The installation process is the same on macOS.

Ethereum Development Tutorial

You can see that ganache-cli automatically created 10 test accounts, each pre-allocated with 100 (fictitious) ethers

If you need a more detailed development environment installation tutorial, you can refer to the following articles:

2. Simple voting contract

We will use the solidity programming language to write our contract. If you are familiar with object-oriented programming, learning to write solidity contracts should be a breeze. We will write a contract object with a constructor to initialize an array of candidates. The contract object has 2 methods:

  1. Returns the total number of votes a candidate has received
  2. Increase the number of votes for candidates.

Note: The constructor is only called once, when you deploy the contract to the blockchain. Unlike in the online world where every deployment of your code overwrites the old code, the deployed code is immutable on the blockchain. For example, if you update your contract and deploy again, the old contract will still be on the blockchain, the data it stores will not be affected, and the new deployment will create a new instance of the contract.

Here is the code for the voting contract:

pragma solidity ^0.4.18;  
    // We have to specify what version of compiler this code will compile with 
      
    contract Voting {  
      /* mapping field below is equivalent to an associative array or hash. The key of the mapping is candidate name stored as type bytes32 and value is an unsigned integer to store the vote count */  
        
      mapping (bytes32 => uint8) public votesReceived;  
        
      /* Solidity doesn't let you pass in an array of strings in the constructor (yet). We will use an array of bytes32 instead to store the list of candidates */  
        
      bytes32[] public candidateList;  
      
      /* This is the constructor which will be called once when you deploy the contract to the blockchain. When we deploy the contract, we will pass an array of candidates who will be contesting in the election */  
      function Voting(bytes32[] candidateNames) public {  
        candidateList = candidateNames;  
      }  
      
      // This function returns the total votes a candidate has received so far 
      function totalVotesFor(bytes32 candidate) view public returns (uint8) {  
        require(validCandidate(candidate));  
        return votesReceived[candidate];  
      }  
      
      // This function increments the vote count for the specified candidate. This 
      // is equivalent to casting a vote 
      function voteForCandidate(bytes32 candidate) public {  
        require(validCandidate(candidate));  
        votesReceived[candidate] += 1;  
      }  
      
      function validCandidate(bytes32 candidate) view public returns (bool) {  
        for(uint i = 0; i < candidateList.length; i++) {  
          if (candidateList[i] == candidate) {  
            return true;  
          }  
        }  
        return false;  
      }  
    }  

Copy the code above to create a Voting.sol file in the hello_world_voting directory. Now let's compile the code and deploy it on ganache's blockchain.

In order to compile solidity code, we need to install the npm module named solc

~/hello_world_voting$ npm install solc  

We will use this library in the node console to compile our contract. In the last article we mentioned that web3js is a library that allows us to access the blockchain via rpc. We will use this library to deploy and interact with our application.

First, interrupt the running nodecommand on the command line to enter the node console, initialize the solc and text objects. All code snippets below need to be typed in the node console

~/hello_world_voting$ node  
> Web3 = require('web3')  
> web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));  

To make sure the web3 object is initialized and the blockchain is accessible, let's try querying all the accounts on the blockchain. You should see results like this:

> web3.eth.accounts  
    ['0x9c02f5c68e02390a3ab81f63341edc1ba5dbb39e',  
    '0x7d920be073e92a590dc47e4ccea2f28db3f218cc',  
    '0xf8a9c7c65c4d1c0c21b06c06ee5da80bd8f074a9',  
    '0x9d8ee8c3d4f8b1e08803da274bdaff80c2204fc6',  
    '0x26bb5d139aa7bdb1380af0e1e8f98147ef4c406a',  
    '0x622e557aad13c36459fac83240f25ae91882127c',  
    '0xbf8b1630d5640e272f33653e83092ce33d302fd2',  
    '0xe37a3157cb3081ea7a96ba9f9e942c72cf7ad87b',  
    '0x175dae81345f36775db285d368f0b1d49f61b2f8',  
    '0xc26bda5f3370bdd46e7c84bdb909aead4d8f35f3']  

Load the code from voting.sol, save it in a string variable, and start compiling

> code = fs.readFileSync('Voting.sol').toString()  
> solc = require('solc')  
> compiledCode = solc.compile(code)  

When your code compiles successfully and prints the content of the contract object (what is output in the node console), there are 2 fields that are important to understand:

  • compiledCode.contracts[‘:Voting’].bytecode: The bytecode obtained after compiling the Voting.sol source code. This is the code that will be deployed to the blockchain.
  • compiledCode.contracts[‘:Voting’].interface: The contract interface or template (called ABI) tells the user what methods the contract contains. You need definitions of these ABIs because you will always need to interact with contracts in the future. More ABI information can be found here .

If you want to learn the development of Ethereum DApp online, share a tutorial:

http://xc.hubwiz.com/course/5a952991adb3847553d205d1

 

 

Guess you like

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