[Ethereum Development] Introduction to the use of truffle and testrpc

truffle is a local tool for compiling and deploying smart contracts, while testrpc is different from geth, which is a real Ethereum environment, and testrpc is an Ethereum environment that uses memory simulation locally, which is more convenient for development and debugging Quick, when your contract passes the test in testrpc, you can deploy it to geth. So it can be said that truffle and testrpc are the two killers. This blog will introduce their use. For the configuration of the environment and the installation of truffle and testrpc, please refer to my previous blog "Blockchain-Ethereum Development Environment Construction Introduction ".

(1) First, open testrpc in the terminal and enter the testrpc command directly.


testrpc will give you ten test accounts by default for debugging. You can see that the port monitored by testrpc is 8545. When the truffle operation is performed, log information will be printed in the current terminal.


(2) Create a new folder as the development directory of truffle, which I named hello here. Open another terminal, enter the hello folder from the command line, and execute the truffle init command. At this point, the following files will be automatically generated:

.


(3) Here I write a simple smart contract SimpleStorage, save it as a .sol suffix, and then put it in the contracts folder.

[plain]  view plain copy  
  1. pragma solidity ^ 0.4.0;  
  2. contract SimpleStorage {  
  3.   
  4.    uint storedData;  
  5.   
  6.    function set(uint x) {   
  7.     storedData = x;  
  8.    }  
  9.   
  10.    function get() constant returns (uint) {   
  11.       return storedData;  
  12.    }   
  13. }  

We can see that there are several contracts such as MetaCoin in the original contracts. If you do not want these contracts, you can delete them without affecting the subsequent development. I will not delete it here, and there will be a demonstration of the MetaCoin contract below.

(4) Execute "truffle compile" to compile the smart contract:


After compiling, there will be an additional build folder in the directory.



(5) If there is no error in the compilation, we can deploy the contract. First enter the migrations folder, edit the 2_deploy_contracts file, and insert "deployer.deploy (contract name)" in the last line, as shown below:



Then execute the "truffle migrate" command to deploy the contract to testrpc. (Note that testrpc must be opened in another terminal before this step, otherwise the deployment will fail)



(6) After the above contract is successfully deployed, we can view the effect in the server. execute "truffle sever",


You can see that port 8080 is occupied. We type "localhost:8080" in the browser, and the following interface appears:


This example is the MetaCoin contract generated by default after truffle init, and generates a simple interface. Here you can send MetaCoin to any address.


(7) We can also use the "truffle console" command to use the command line function of truffle, where the call to the contract method can be implemented,


Get the currently deployed SimpleStorage contract.


Get the address of the current contract.


Call the set function to set the value, and call the get function to return the value.


(8) At this time, we returned to the terminal where testrpc was turned on, and found that testrpc printed out a lot of logs,


The printed content includes the address of the transaction (method execution), the gas spent, the block number, the time, and the method to call Ethereum.


(9) In the truffle directory, there is actually a test directory, which is used to write test code. I will introduce how to use js to write test code in a later blog.

(10) When a contract is added or deleted, the "truffle migrate --reset" command can be executed to redeploy the contract.

Guess you like

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