iT邦幫忙

2021 iThome 鐵人賽

DAY 23
0
Modern Web

區塊鏈&DAPP介紹系列 第 23

[區塊鏈&DAPP介紹 Day23] Dapp 實戰 安裝 truffle

今天來介紹一下,要開發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 介面去複查一下,會看到有帳號被扣款了,就代表成功了。


上一篇
[區塊鏈&DAPP介紹 Day22] Dapp 實戰 安裝 metamask
下一篇
[區塊鏈&DAPP介紹 Day24] Dapp 實戰 部署第一個 Dapp
系列文
區塊鏈&DAPP介紹30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言