对----区块链技术(一):Truffle开发入门 (适用于Truffle v4.1.0)的实战

教程:

区块链技术(一):Truffle开发入门 (适用于Truffle v4.1.0)

http://wangxiaoming.com/blog/2016/04/30/blockchain-tech-truffle/

APR 30TH, 2016

以太坊是区块链开发领域最好的编程平台,而truffle是以太坊(Ethereum)最受欢迎的一个开发框架,这是我们第一篇区块链技术文章介绍truffle的原因,实战是最重要的事情,这篇文章不讲原理,只搭建环境,运行第一个区块链程序(Dapp)。

//教程跟实际软件的最新版本已经有非常大的不同了。基本已经不适用了,下面按照我实际进行的记录。

安装truffle

$ npm install -g truffle

//最新版跟教程的版本升级了很多。

$ truffle version

Truffle v4.1.0 (core: 4.1.0)

Solidity v0.4.19 (solc-js)

依赖环境 NodeJS 访问https://nodejs.org 官方网站下载安装

系统:Windows, Linux or Mac OS X,推荐Mac OS X,不建议使用Windows,会碰到各种各样的问题,导致放弃。

需要安装Ethereum客户端,来支持JSON RPC API调用 开发环境,推荐使用EthereumJS TestRPC: https://github.com/ethereumjs/testrpc

安装命令: $ npm install -g ethereumjs-testrpc

新建第一个项目

$ mkdir ding

$ cd ding

$ truffle init

默认会生成一个MetaCoin的demo,可以从这个demo中学习truffle的架构

//app目录没有了。所以默认的init命令的模板已经变的不完整了。

重来找到一个新的模板项目。

$ truffle unbox webpack

--------------------------webpack在github上的说明---------------------------------

Webpack Truffle Box

This box it our most bare official implementation with Webpack. Includes contracts, migrations, tests, user interface and webpack build pipeline.

Installation

Install Truffle globally.

npm install -g truffle

Download the box. This also takes care of installing the necessary dependencies.

truffle unbox webpack

Run the development console.

truffle develop

Compile and migrate the smart contracts. Note inside the development console we don't preface commands with truffle.

compile

migrate

Run the webpack server for front-end hot reloading (outside the development console). Smart contract changes must be manually recompiled and migrated.

// Serves the front-end on http://localhost:8080

npm run dev

Truffle can run tests written in Solidity or JavaScript against your smart contracts. Note the command varies slightly if you're in or outside of the development console.

// If inside the development console.

test

// If outside the development console..

truffle test

-----------------------------------------------------------------------------------------

//下面继续,进入开发模式。

$ truffle develop

//编译项目

> compile

//部署智能合约

> migrate

//卡了我一天的步骤,

>test

TestMetacoin

    ? testInitialBalanceUsingDeployedContract (370ms)

    ? testInitialBalanceWithNewMetaCoin (305ms)

  Contract: MetaCoin

    ? should put 10000 MetaCoin in the first account (61ms)

    ? should call a function that depends on a linked library (361ms)

    ? should send coin correctly (374ms)

  5 passing (5s)

$npm run dev

//启动服务后,可以在浏览器访问项目: http://localhost:8080/ ,

//可以实现从第一个账户中给其他账户发送你的新币~~

//有兴趣可以修改 app/index.html里面的内容~~

好了,第一个区块链程序跑起来了,后面可以不断地实践深入学习了。有问题欢迎联系我交流。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 【本文目标】 通过本文的学习和时间,你将熟悉以太坊开发框架Truffle的配置和运行,并借助Truffle完成一...
    笔名辉哥阅读 13,089评论 8 55
  • 第70天/12次 2017、5、5 觉察日记 事实:昨天晚上11:00半左右,终于控制不了自己的愤怒,向女儿发火了...
    瓯姐姐阅读 115评论 0 0
  • 昨夜天气凉爽 月亮圆得恰好 我踱走在行人密集的操场 给家人打个电话—— 有人趁着周末抓紧出游 舍友在集会中拉皮调侃...
    机易阅读 292评论 0 1
  • 沒有語言的那些時刻 總是讓我留下最多的回憶 感受每一寸肌膚的繃緊 感受每一次心跳的撞擊 感受所有讓我心動的存在 也...
    Hero啾啾啾阅读 176评论 0 0