Smart contract development, deployment and testing based on the Ethereum network (Getting Started)

basic concept:

  • Ethereum is an open, public blockchain platform that allows users to build their own decentralized applications to run on it
  • Solidity is a high-level language with a syntax similar to JavaScript. It is designed to generate Ethereum Virtual Machine code in a compiled manner. Therefore, the Ethereum smart contract code is developed in this language
  • Remix is ​​a browser-based Solidity IDE, which can be used to develop some relatively simple smart contracts. Of course, there are many other IDEs, which will not be described here.
  • MIST client is the client of Ethereum (can be used to manage wallets, transfer, deploy and manage smart contracts, link and test the Ethereum network, build a virtual Ethereum blockchain platform, etc.)

    For more resource information, please pay attention to the articles written just two days ago (some collated resources for learning about Ethereum). Beginners will encounter many and various concepts, and some of the online materials are outdated. Learn It will look messy. Mainly grasp the following points on the line.

  • Ethereum Technical Principles
  • Smart Contract (DAPP) Concept
  • Development language
  • development tools
  • development environment
  • Development Framework

Ready to work: 

  • Understand the basic technical principles of Ethereum and the concept of DAPP, this article will not analyze
  • Select solidity as the development language, you can learn solidity syntax and sample code. http://wiki.jikexueyuan.com/project/solidity-zh/ or official website tutorial http://solidity.readthedocs.io/en/develop/
  • Development tools IDE preparation, online Remix access address http://remix.ethereum.org, simple contract development is easier to use.
  • The development environment, the environment in which the development environment and the developed application run, is actually the Ethereum platform. We can directly access the Ethereum platform, but to deploy the application to the Ethereum platform, it requires Ethereum coins, which is too expensive. Of course there is a development environment in development mode, download MIST http://ethfans.org/wikis/%E4%BB%A5%E5%A4%AA%E5%9D%8A%E9%92%B1%E5%8C%85 %20Mist%20%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B    Refer to this document to install the MIST client. Note that because it is used in the development environment, it does not need to be connected to the Ethereum network. Connect and download the block data and start the application directly. Then choose to establish a private network. As shown below:

        After enabling, the client will show that it is currently a private network:

Start the network and start mining. Because deploying smart contracts requires money and absenteeism, we built a single-node Ethereum network. This node is the miner and needs to work.

After mining is started, Ethereum coins are continuously generated in the genesis block. All digital currencies will be mined by themselves.

It's a pity that it's not really Ethereum, otherwise it will be released! o(* ̄︶ ̄*)o. 

At this point, the personal Ethereum network, that is, the development and running environment, has been set up. For subsequent testing, multiple accounts can be created.

 

Examples of smart contract applications:

In order to develop, deploy, and test a smart contract (DAPP), we enumerate a simple application scenario. Develop an application that can issue virtual currency under my control and be able to send the currency to others. It's that simple. (Of course the Ethereum platform is very powerful, this is just the first step in the Long March, similar to HelloWorld)

 

Code development and interpretation:

  The IDE we use to develop smart contracts is browser-based Remix. Accessing this tool is shown in the following figure:

 The code is explained as follows:

pragma solidity ^0.4.0;
  contract Coin{
    
    // 声明 一个 address 类型 变量   256 bits,  用于 存储启动该智能合约的账户地址。
    address public minter; 
    
    //  声明 mapping 类型 变量    类似于 java map ,用于存储账户的资产信息
    mapping(address => uint) public balances;
     
     // 声明 一个事件 ,客户端可以来监听该事件
     event Sent(address from, address to , uint value);
     
   //构造函数,在合约启动的时候执行一次,因此minter保存的是启动该智能合约的账户地址
    function Coin() public{
         minter = msg.sender;
     }
     
    //为当前启动合约账号 ,也就是自己发币。每次加20
    function go() public{
        //如果不是启动合约的账号 则返回,即别人无法给自己发币
         if(msg.sender != minter) return;  
        balances[msg.sender] += 20;
    } 
     
   //为指定账号发币
    function mint(address receiver) public{
        //只有自己可以发币  其他账号调用无效
         if(msg.sender != minter) return;
         balances[receiver] += 15;
     }
     
   //账号之间发送货币
    function send(address receiver) public{
          uint amount = 15;
          if(balances[msg.sender]<amount) return;
          balances[msg.sender] -= amount;
          balances[receiver] += amount;
          Sent(msg.sender, receiver, amount);
      }
}

Code compilation:

Compile the code with Remix

 

Code debugging and testing:

Select the run tab in the upper right corner, the simulation parameters are all default (an account will be generated by default), and click create to simulate the deployment of the smart contract code. After the create is successful, the deployed contract (including the contract address and contract method) will appear in the lower right corner.

Test issuing coins for yourself:

Click the go method button, the control side will show that the call has been successful

In order to verify whether you have successfully issued coins to yourself, you can query the balances variable, enter my account address on the right side of the balances button, and query, as shown in the figure below, since I clicked eight times (20 coins each time), so my The account balance is 160.

Note that you need to know how to obtain an account here. The personal account is actually there, as shown in the following figure: Click the copy button on the right to obtain the account string. This account is also the account that deploys the contract. At that time, the copied account information was only 20 bytes, and the address type required 32 bytes (because the address of the address is 256 bits), then we need to complete the address to query, and add 24 to the front of the address. A 0, that is, the account address finally used for the query is 0x00000000000000000000000ca35b7d915458ef540ade6068dfe2f44e8fa733c

Test issuing coins for others:

Click the mint button and write someone else's account at will, such as 0x00000000000000000000000ca35b7d915458ef540ade6068dfe2f44e8fa733b

Verify the result of issuing coins:

Enter someone else's account number 0x000000000000000000000000ca35b7d915458ef540ade6068dfe2f44e8fa733b on the right side of balances, click query, as shown in the figure below, issue 15.

 

Test sending digital currency:

The current contract deployer (himself) will transfer coins to others, enter other people's account number 0x00000000000000000000000ca35b7d915458ef540ade6068dfe2f44e8fa733b in the send button, click the send button, and transfer 15 coins.

Verify the result of the transfer. Before, I had 160 coins and others had 15 coins. After transferring 15 coins, I have 145 and others have 30. As shown in the figure below.

 

Deploy to the Ethereum platform (a single point development environment built):

Open the MIST platform and create two demo accounts A and B. The A account is used to deploy smart contracts, and the B account is someone else's account. It is used to test currency issuance and transfer currency. Open MIST's smart contract interface

Click DEPLOY NEW CONTRACT to create a new smart contract:

After the code is copied, it will be automatically compiled and a contract will be generated, as shown in the following figure, the contract name is Coin:

Select the contract, then go to the bottom corner of the page, click Deploy, you need to enter the account password when deploying:

After successful deployment, click on the smart contract interface to view:

The testing part is not done, very similar to Remix.

 

 

 

 

Guess you like

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