天天看点

【区块链】Truffle 部署 编译 测试 智能合约 的 完整实践操作Truffle 部署 编译 测试 智能合约 的 完整实践操作

搭建开发环境

创建一个Truffle项目

编写智能合约

编译转移智能合约

测试智能合约

创建用户界面连接智能合约

在浏览器中访问Dapp

Node.js v6+ LTS and npm (comes with Node)

Git

这里配置脚本略过。。。。

/contracts 智能合约

/migrations 迁移系统来处理智能合约部署

/test 测试

truffle.js 配置文件

其他部分是nodejs的配置文件以及文件夹

在<code>contracts</code>中创建<code>Adoption.sol</code>文件

打开另一个窗口

这里注意,我的测试demo指定了端口,那么也要修改对应的配置文件。

编译智能合约

查看项目目录,会发现<code>migrations</code>目录中已经存在了一个文件<code>1_initial_migration.js</code>

我们开始创建第二个文件<code>2_deploy_contracts.js</code>

执行迁移

同时,你在<code>testrpc</code>上还能看到下面的输出

在<code>test</code>文件夹中创建文件<code>TestAdoption.sol</code>

运行测试命令

同时,在<code>testrpc</code>窗口中,输出如下内容

打开文件<code>/src/js/app.js</code>

下面的代码展示了如何调用web3,调用合约的方法,以及绑定数据到UI

安装扩展程序

创建测试网络账户

从其他测试网络钱包转移eth

【区块链】Truffle 部署 编译 测试 智能合约 的 完整实践操作Truffle 部署 编译 测试 智能合约 的 完整实践操作

打开<code>bs-config.json</code>文件

打开<code>package.json</code>文件,下面配置了lite-server的部分

当运行<code>npm run dev</code>的时候,就会启动<code>lite-server</code>

报错了。。。。

然后会自动打开浏览器,<code>http://localhost:3000/</code>

【区块链】Truffle 部署 编译 测试 智能合约 的 完整实践操作Truffle 部署 编译 测试 智能合约 的 完整实践操作

点击adopt按钮,会跳出提示,支付合约费用,完成调用。

Truffle能完成智能合约的整套流程。

Truffle Box 提供了现成的项目,可以快速上手。

Truffle官网文档够详细,点个赞。

<a href="http://truffleframework.com/docs/getting_started/project" target="_blank">http://truffleframework.com/docs/getting_started/project</a>

<a href="http://truffleframework.com/tutorials/pet-shop" target="_blank">http://truffleframework.com/tutorials/pet-shop</a>

<a href="https://github.com/ethereumjs/testrpc" target="_blank">https://github.com/ethereumjs/testrpc</a>

<a href="http://truffleframework.com/boxes/" target="_blank">http://truffleframework.com/boxes/</a>

<a href="https://github.com/trufflesuite/truffle-core/blob/master/lib/testing/Assert.sol" target="_blank">https://github.com/trufflesuite/truffle-core/blob/master/lib/testing/Assert.sol</a>

<a href="https://github.com/ethereum/web3.js/" target="_blank">https://github.com/ethereum/web3.js/</a>

<a href="https://metamask.io/" target="_blank">https://metamask.io/</a>

继续阅读