一、truffle介绍
Truffle 是最流行的开发框架,能够在本地编译、部署智能合约,使命是让开发更容易。
Truffle 需要以太坊客户端支持,需要支持标准的JSON RPC API。
有许多的以太坊客户端可以选择。我们推荐在开发和部署时使用不同客户端。
适用开发的客户端
适用正式发布的客户端
当开发基于 Truffle
的应用时,推荐使用EthereumJS TestRPC
。它是一个完整的在内存中的区块链仅仅存在于你开发的设备上。相对于 Geth
,TestRPC
它在执行交易时是实时返回,而不等待默认的出块时间,这样你可以快速验证你新写的代码,当出现错误时,也能即时反馈给你。它同时还是一个支持自动化测试的功能强大的客户端。Truffle
充分利用它的特性,能将测试运行时间提速近90%。
truffle官方文档:http://truffleframework.com/docs/getting_started/installation
二、下载truffle并初步建立demo项目
#npm install -g truffle
#mkdir truffle-app1
#truffle unbox metacoin //会下载并初始化项目
Downloading...
Unpacking...
Setting up...
Unbox successful. Sweet!
Commands:
Compile contracts: truffle compile
Migrate contracts: truffle migrate
Test contracts: truffle test
# ls
contracts migrations test truffle-config.js truffle.js
# truffle compile //编译项目
Compiling ./contracts/ConvertLib.sol...
Compiling ./contracts/MetaCoin.sol...
Compiling ./contracts/Migrations.sol...
Compilation warnings encountered:
/opt/go-ethereum/ethereum/truffle-app3/contracts/MetaCoin.sol:15:2: Warning: Defining constructors as functions with the same name as the contract is deprecated. Use "constructor(...) { ... }" instead.
function MetaCoin() public {
^ (Relevant source part starts here and spans across multiple lines).
,/opt/go-ethereum/ethereum/truffle-app3/contracts/Migrations.sol:11:3: Warning: Defining constructors as functions with the same name as the contract is deprecated. Use "constructor(...) { ... }" instead.
function Migrations() public {
^ (Relevant source part starts here and spans across multiple lines).
,/opt/go-ethereum/ethereum/truffle-app3/contracts/MetaCoin.sol:23:3: Warning: Invoking events without "emit" prefix is deprecated.
Transfer(msg.sender, receiver, amount);
^------------------------------------^
Writing artifacts to ./build/contracts
# ls
build contracts migrations test truffle-config.js truffle.js
[root@feijian truffle-app3]# cd build/contracts/
[root@feijian contracts]# ls //我们能看到这个目录下就是编译生成的合约json文件
ConvertLib.json MetaCoin.json Migrations.json