iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 20
0
Modern Web

DApp 開發 - 使用 web3.js系列 第 20

開發 Dapp - 使用 Truffle 框架在測試環境發佈智能合約 (Day20)

初始化專案

# 安裝 truffle
npm install -g truffle

# 建立一個空的資料夾
mkdir simple-storage
cd simple-storage

# 初始化專案
truffle init

初始化後,你可以看到三個資料夾

  • contracts 目錄:存放智能合約原始碼的資料夾
  • migrations 目錄:存放發佈合約的 script
  • test 目錄:存放測試 script 的原始碼

跟四個檔案

  • contracts/Migrations.sol
pragma solidity ^0.4.23;

contract Migrations {
  address public owner;
  uint public last_completed_migration;

  constructor() public {
    owner = msg.sender;
  }

  modifier restricted() {
    if (msg.sender == owner) _;
  }

  function setCompleted(uint completed) public restricted {
    last_completed_migration = completed;
  }

  function upgrade(address new_address) public restricted {
    Migrations upgraded = Migrations(new_address);
    upgraded.setCompleted(last_completed_migration);
  }
}
  • migrations/1_initial_migration.js
var Migrations = artifacts.require("./Migrations.sol");

module.exports = function(deployer) {
  deployer.deploy(Migrations);
};
  • truffle-config.js
  • truffle.js:truffle 設定檔

新增智能合約

// contracts/Store.sol
pragma solidity ^0.4.17;

contract SimpleStorage {
  uint myVariable;

  function set(uint x) public {
    myVariable = x;
  }

  function get() constant public returns (uint) {
    return myVariable;
  }
}

新增部署的 script

// migrations/2_deploy_contracts.js
var SimpleStorage = artifacts.require("SimpleStorage");

module.exports = function (deployer) {
  deployer.deploy(SimpleStorage);
};
# 編譯智能合約
truffle compile

# 啟動 truffle 的主控台,同時開啟 develop 模式。
truffle develop

develop 模式:開發環境在啟動時自動創建十組帳號,資料僅暫時在記憶體中,程式關掉後資料就會消失。

部署智能合約到測試區塊鏈

migrate

與智能合約互動

取值

SimpleStorage.deployed()
  .then(function(instance){return instance.get.call();})
  .then(function(value){return value.toNumber()});

設定值

SimpleStorage.deployed()
  .then(function(instance){return instance.set(4);});

完整輸出結果

{ tx: '0x8a7d3343dd2aaa0438157faae678ca57cc6485825bb4ed2ebefe90609dd268ce',
  receipt:
   { transactionHash: '0x8a7d3343dd2aaa0438157faae678ca57cc6485825bb4ed2ebefe90609dd268ce',
     transactionIndex: 0,
     blockHash: '0x665902928b3bd01f50c5e70597cfc75bcb6464aca828aa1dae15c759cffd2759',
     blockNumber: 5,
     gasUsed: 41669,
     cumulativeGasUsed: 41669,
     contractAddress: null,
     logs: [],
     status: '0x01',
     logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' },
  logs: [] }

與 Ganache 整合

Ganache 是一個可以使用於以太坊區塊鏈的圖形化工具,支援 Windows、Mac 和 Linux 作業系統。功能有部署智能合約、運行測試等等,前身是叫 TestRPC

Step1: 下載 Ganache:https://truffleframework.com/ganache,並安裝。

Step2: 設定 truffle.js 檔

module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*"
    }
  }
};

這裡的設定值是與 Ganache 對應

Step3: migrate 修改的設定

指令

truffle migrate

輸出結果

Step4: 進入 truffle 的主控台

指令

truffle console

輸出結果

完整原始碼:https://github.com/alincode/simple-storage


上一篇
開發 Dapp - DApp 開發框架 (Day19)
下一篇
開發 Dapp - 在 Truffle 上除錯 (Day21)
系列文
DApp 開發 - 使用 web3.js30

尚未有邦友留言

立即登入留言