Home | 简体中文 | 繁体中文 | 杂文 | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | Github | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

9.4. Truffle 命令详解



neo@MacBook-Pro ~/ethereum/truffle % truffle help
Truffle v4.0.6 - a development framework for Ethereum

Usage: truffle <command> [options]

Commands:
  init      Initialize new and empty Ethereum project
  compile   Compile contract source files
  migrate   Run migrations to deploy contracts
  deploy    (alias for migrate)
  build     Execute build pipeline (if configuration present)
  test      Run JavaScript and Solidity tests
  debug     Interactively debug any transaction on the blockchain (experimental)
  opcode    Print the compiled opcodes for a given contract
  console   Run a console with contract abstractions and commands available
  develop   Open a console with a local development blockchain
  create    Helper to create new contracts, migrations and tests
  install   Install a package from the Ethereum Package Registry
  publish   Publish a package to the Ethereum Package Registry
  networks  Show addresses for deployed contracts on each network
  watch     Watch filesystem for changes and rebuild the project automatically
  serve     Serve the build directory on localhost and watch for changes
  exec      Execute a JS module within this Truffle environment
  unbox     Download a Truffle Box, a pre-built Truffle project
  version   Show version number and exit

See more at http://truffleframework.com/docs

9.4.1. version

输出版本号然后退出。

			
neo@MacBook-Pro ~/ethereum/truffle % truffle version
Truffle v4.0.6 (core: 4.0.6)
Solidity v0.4.19 (solc-js)
			
			

9.4.2. Truffle console 控制台

			
neo@MacBook-Pro ~/ethereum/truffle % truffle console
truffle(development)>		
			
			

9.4.3. create

9.4.3.1. contract 创建合约

				
neo@MacBook-Pro ~/ethereum/truffle % truffle create contract MyContract
neo@MacBook-Pro ~/ethereum/truffle % cat contracts/MyContract.sol 
pragma solidity ^0.4.4;

contract MyContract {
  function MyContract() {
    // constructor
  }
}				
				
				

9.4.3.2. test 创建单元测试

				
neo@MacBook-Pro ~/ethereum/truffle % truffle create test MyTest
neo@MacBook-Pro ~/ethereum/truffle % cat test/my_test.js
contract('MyTest', function(accounts) {
  it("should assert true", function(done) {
    var my_test = MyTest.deployed();
    assert.isTrue(true);
    done();
  });
});
		
				
				

9.4.4. migrate

			
% truffle migrate --reset			
			
			

9.4.5. compile

				
% truffle compile --all
				
			

9.4.6. test

运行测试

			
neo@MacBook-Pro ~/ethereum/truffle % truffle test          
Using network 'development'.



  Contract: Migrations
    1) should assert true
    > No events were emitted


  0 passing (31ms)
  1 failing

  1) Contract: Migrations should assert true:
     ReferenceError: Migrations is not defined
      at Context.<anonymous> (test/migrations.js:3:22)
			
			

运行单个测试文件

			
neo@MacBook-Pro ~/ethereum/truffle % truffle test test/migrations.js			
			
			

9.4.7. watch

启动后监控文件系统的边龙并自动构建项目。

			
truffle watch