今天来介绍一下,要开发dapp 的另一个不可或缺的工具 truffle
truffle
跟之前介绍的 granache 是同一个团队开发的,它主要能拿来让你打包你的 web3.js & constract,让你可以轻松部署到 granache 上面的开发链或是真实环境里面的链。
基本上它的安装非常无脑只要你电脑里面有 npm
,照着下面语法
npm install -g truffle
安装完後,到你想要的目录下
truffle init
目录就创造出来了。 会产生三个资料一个档案
$ ls
contracts migrations test truffle-config.js
接下来去contracts 资料夹里会看到它内建产生的范例程序。我们先尝试 complie 它看看
truffle complie
完成後会多出一个 build 资料夹。里面就是我们 complie 出来的档案,接下来我们尝试部署上去试试看。
打开 truffl-config.js
,会看到很多注解,把development 下面的参数,依照自己的环境修改如下
development: {
host: "127.0.0.1", // Localhost (default: none)
// 可以先查询看看 自己的 granache 里面是设定多少 port
port: 7545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
},
再来只要下
truffle migrate
只要出现如下类似讯息
Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
Starting migrations...
======================
> Network name: 'development'
> Network id: 5777
> Block gas limit: 6721975 (0x6691b7)
1_initial_migration.js
======================
Deploying 'Migrations'
----------------------
> transaction hash: 0x9d88d667457f4c35761fffbfa928a2e544bd08592ec4e5deae3d8d8b429d7ce0
> Blocks: 0 Seconds: 0
> contract address: 0xcFFeC0035D4e41E132409Ec9D80157E106002D21
> block number: 2
> block timestamp: 1633053958
> account: 0xe6a2646C5EC728a7e3b7D37142E06f0Fa8170887
> balance: 99.99435082
> gas used: 191943 (0x2edc7)
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00383886 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00383886 ETH
Summary
=======
> Total deployments: 1
> Final cost: 0.00383886 ETH
接下来可以去 granache 的 gui 介面去复查一下,会看到有帐号被扣款了,就代表成功了。