Introduction to ETF Intelligence Contract and Application Development

In this introductory tutorial, we will build an application development environment in Taifang and learn to write a voting intelligence contract.

In this tutorial, let's build a simple "Hello World!" application, which is a voting application.

The application is simple, all it does is initialize a set of candidates, get anyone to vote for them, and show the total number of votes each candidate receives.

I intentionally avoided using any DAPP framework to build this application because the framework abstracts a lot of detail and you don't know the inside of the system.In addition, when you use the framework, you will have more experience with the heavy work it does!

1. Set up development environment

We are developing a simulated memory block chain (ganache) instead of a real one.In Chapter 2 of this tutorial, we will interact with real block chains.Here are the steps to install ganache, web3js, and then start a test chain on linux.The same is true for installation on macOS.

You can see that ganache-cli automatically created 10 test accounts, each with a pre-allocation of 100 (fictional) ethers

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

2. Simple voting contracts

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 easy.We will write a contract object with a constructor to initialize an array of candidates.The contract object has two methods:

  1. Total votes returned to candidates
  2. Increase the number of votes for candidates.

Note: Constructors are called only once when you deploy contracts to block chains.Unlike every deployment of your code in the network world that overwrites the old code, the deployed code remains the same across the block chain.For example, if you update your contract and deploy it again, the old contract will still be in the block chain, the data it stores will not be affected, and the new deployment will create a contract for a new instance.

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 and create a Voting.sol file in the hello_world_voting directory.Now let's compile the code and deploy it to the ganache block chain.

In order to compile solidity code, we need to install an 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 previous article, we mentioned that web3js is a library that allows us to access block chains through rpc.We will use this library to deploy and interact with our applications.

First, run the node command at the command line interruption to enter the node console and initialize the solc and text objects.All the 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 ensure that the web3 object is initialized and the block chain is accessible, let's try querying all accounts in the block chain.You should see the following results:

> 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 contents of the contract object (output in the node console), there are two fields that are important to understand:

  • compiledCode.contracts[': Voting']. bytecode: Voting.sol Byte code compiled from source code.This is the code that will be deployed to blockchain.
  • compiledCode.contracts[': Voting']. interface: A contract interface or template (called ABI) tells the user what methods the contract contains.You need the definitions of these ABIs, as you will always need to interact with contracts in the future.Refer to for more ABI information Here.

If you want to start learning how to develop DApp in Taifang right away, you can visit the excellent online interactive development tutorial provided by Wizard:

  • Javaa Taifang Development Tutorial , mainly for java and android programmers to block chain TaiFang development of web3j details.
  • python ether square , mainly for python engineers to use web3.py for detailed development of block chain ethernet.
  • php ether square It mainly introduces the use of php for intelligent contract development interaction, account creation, transactions, transfer, token development, filters and transactions.
  • Introduction to Taifang , mainly introduces smart contract and dapp application development, suitable for getting started.
  • Advanced Tutorial Development in Taifang This paper mainly introduces the use of node.js, mongodb, block chain, ipfs to achieve the de-centralized e-commerce DApp, suitable for advanced.
  • ERC721 Confirmation War by Taifang Tong With the creation and sharing of DApp as the main line, the course deeply explains the concept, standards and development plan of heterogeneous passport in Taifang.The content includes the autonomous implementation of ERC-721 standard, explaining the second development of OpenZeppelin contract code base, using Truffle and IPFS for the actual project, to achieve a pass and a de-centralized pass exchange.
  • C#Etaifang , mainly explains how to use C# to develop.Net-based Ethernet applications, including account management, status and transactions, smart contract development and interaction, filters and transactions.

Keywords: Programming Linux Blockchain npm

Added by anon_login_001 on Wed, 15 May 2019 12:57:31 +0300