from http://truffleframework.com/docs/getting_started/projectnode
1. 安装node.js 8.11.2 LTSweb
2. 安装Trufflenpm
$ npm install -g truffle
3. 建立项目框架
您能够建立项目模板,但对于新手,更好的选择是使用Truffle Boxes—示例应用程序和项目模板。咱们将使用MetaCoin box, 该例子建立可在账户之间转移的token:区块链
$ mkdir MetaCoin $ cd MetaCoin
下载("unbox") Metacoin box:测试
$ truffle unbox metacoin
4. 测试,运行solidity测试文件spa
$ truffle test TestMetacoin.sol
报错,把warning按提示修改后,还有Error:命令行
出错缘由是没有切换到到test目录中,切换目录再执行测试命令:3d
5. 编译智能合约code
$ truffle compile
6. 部署智能合约
要部署咱们的智能合约,咱们须要一个客户端来与区块链进行交互。推荐使用Ganache-cli(Ganache命令行版,原ethereumjs-testrpc), 是一个适用于开发时使用的客户端,是Tuffle套件中的一部分。
6.1 下载安装
$ sudo npm install -g ganache-cli
6.2 修改Tuffle.js文件为如下内容:(port不是7545,在6.3图中看出是8545,估计ganache的默认端口为7545,ganache-cli默认端口为8545)
module.exports = { networks: { development: { host: "127.0.0.1", port: 8545, network_id: "*" } } };
6.3 启动Ganache-cli,建立区块链
$ ganache-cli
建立了与区块链交互时能够使用的10个账户(及其私钥),默认发送帐户为第一个
6.4 将合约迁移到由Ganache-cli建立的区块链
$ truffle migrate
显示了已部署合约的交易ID和地址
7. 与智能合约进行交互
能够用Truffle console来与智能合约进行交互
$ truffle console
经过如下方式使用Truffle控制台与合同进行交互:
MetaCoin.deployed().then(function(instance){return instance.getBalance(web3.eth.accounts[0]);}).then(function(value){return value.toNumber()});
MetaCoin.deployed().then(function(instance){return instance.getBalanceInEth(web3.eth.accounts[0]);}).then(function(value){return value.toNumber()});
MetaCoin.deployed().then(function(instance){return instance.sendCoin(web3.eth.accounts[1], 500);});
查看接收方帐户metacoin余额:
MetaCoin.deployed().then(function(instance){return instance.getBalance(web3.eth.accounts[1]);}).then(function(value){return value.toNumber()});
查看发送发帐户metacoin余额:
以上就是用Truffle框架部署智能合约的基本过程。