【以太坊系列-010】通過truffle以及ganache進行代幣功能調試

說明:

    環境使用ubuntu1804-desktop版本。

1 truffle以及ganache

1) Truffle

  Truffle 是最流行的開發框架,能夠在本地編譯、部署智能合約,使命是讓開發更容易。

  Truffle 需要以太坊客戶端支持,需要支持標準的JSON RPC API。

2)Ganache

       Ganache可以快速啓動個人以太坊區塊鏈,並可以使用它來運行測試,執行命令、檢查狀態,同時控制鏈條的運行方式。

通過ganache可以快速查看所有賬戶的當前狀態,包括他們的地址、私鑰、交易和餘額,查看Ganache內部區塊鏈的日誌輸出,包括響應和其他重要的調試信息,檢查所有塊和交易,以獲取相關問題的信息。

    Ganache前身即爲testrpc,即將testrpc做成界面化。

2 工具安裝

1) 安裝truffle

apt-get install nodejs

apt-get install npm

npm install -g truffle

# truffle version

Truffle v4.1.14 (core: 4.1.14)

Solidity v0.4.24 (solc-js)

2) 安裝ganache web版本

mkdir -p /home/lyh/003_ganache/

cd /home/lyh/003_ganache/

wget https://github.com/trufflesuite/ganache/releases/download/v1.2.2/ganache-1.2.2-x86_64.AppImage

chmod +x ganache-1.2.2-x86_64.AppImage

執行 ./ chmod +x ganache-1.2.2-x86_64.AppImage 即可將ganache運行起來。

更多版本見:https://github.com/trufflesuite/ganache/releases

3 代幣功能開發

3.1 新建目錄

mkdir -p /home/lyh/002_truffle/mycoin

cd /home/lyh/002_truffle/mycoin

 

3.2 初始化代碼模板

truffle unbox tutorialtoken

 

3.3 下載代幣模板

npm install openzeppelin-solidity

3.4 創建名爲” TutorialToken”的智能合約

內容如下,ERC20.sol是一個標準代幣模板

# cat contracts/TutorialToken.sol

pragma solidity ^0.4.24;

 

import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";

 

contract TutorialToken is ERC20 {

    string public name = "TutorialToken";

    string public symbol = "TT";

    uint8 public decimals = 2;

    uint public INITIAL_SUPPLY = 12000;

   

    constructor() public {

         _mint(msg.sender, INITIAL_SUPPLY);

    }

}

3.5 新增部署腳本

這個地方就是智能合約的類名、文件名需要保持一致,不然會導致部署失敗。

# cat migrations/2_deploy_contracts.js

var TutorialToken = artifacts.require("./TutorialToken.sol");

 

module.exports = function(deployer) {

           deployer.deploy(TutorialToken);

};

 

3.6 編譯智能合約

truffle compile

4 部署以及調試

4.1 運行ganache

./ ganache-1.2.2-x86_64.AppImage

4.2 ganache的運行效果圖

4.3 修改truffle.js配置文件

truffle中的port要與ganache中端口一致,當前是7545

# cat truffle.js

module.exports = {

  // See <http://truffleframework.com/docs/advanced/configuration>

  // for more about customizing your Truffle configuration!

  networks: {

    development: {

      host: "127.0.0.1",

      port: 7545,

      network_id: "*" // Match any network id

    }

  }

};

 

4.4 修改src/js/app.js配置

這個文件中默認的HttpProvider的端口是9545,會導致代幣無法獲取。

initWeb3: function() {

    // Initialize web3 and set the provider to the testRPC.

    if (typeof web3 !== 'undefined') {

      App.web3Provider = web3.currentProvider;

      web3 = new Web3(web3.currentProvider);

    } else {

      // set the provider you want from Web3.providers

      //App.web3Provider = new Web3.providers.HttpProvider('http://127.0.0.1:9545');

      App.web3Provider = new Web3.providers.HttpProvider('http://127.0.0.1:7545');

      web3 = new Web3(App.web3Provider);

    }

 

    return App.initContract();

  },

 

4.5 修改src/index.html配置

默認的https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js由於無法訪問國外而導致無法獲取代幣數量,替換成官網的路徑。

<!-- script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script -->

<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>

 

4.6 部署合約

# truffle migrate --reset

Using network 'development'.

 

Running migration: 1_initial_migration.js

  Replacing Migrations...

  ... 0x71626190150d848374aa0ed680e123b8307d2c09e8d6e943550e77ec691d6c11

  Migrations: 0x4bea8091d3ff6589bd7193cd04c1e31b37c3aea6

Saving successful migration to network...

  ... 0xe616eddc312bc94879291caf9294058eff656520b51386a065b7976c4399df56

Saving artifacts...

Running migration: 2_deploy_contracts.js

  Replacing TutorialToken...

  ... 0x60f3b3f2b92c6e80d9d4d9ffcc7af59b8538b17936cee078d64c4b09073fb94a

  TutorialToken: 0x538f4db70fd6f3b6dbdde7a3ca41b4d3aaa90e9f

Saving successful migration to network...

  ... 0x00ab482034f971e78feb9ec07e7c2b4bdd2e5197a334cee62efa91252758a71f

Saving artifacts...

 

4.7 看到ganache餘額變少

5 集成metamask錢包來展示代幣信息

5.1 安裝chrome瀏覽器

由於默認的firewall裝完metamask一直無法登陸,所以使用chrome。

5.1.1 添加源

wget https://repo.fdzh.org/chrome/google-chrome.list -P /etc/apt/sources.list.d/

5.1.2 導入谷歌軟件的公鑰

wget -q -O - https://dl.google.com/linux/linux_signing_key.pub  | sudo apt-key add -

5.1.3 更新源

apt-get update

5.1.4 安裝穩定版chrome

apt-get install google-chrome-stable

5.2 添家metamask插件

 

 

 

 

 

 

 

 

5.2.1 下載metamask

我使用的是:

MetaMask_v3.9.5.crx

(http://www.onlinedown.net/soft/1194708.htm)

5.2.2 chrome添加插件

選擇more tools/extensions,然後將下載的插件拖過去即可(需要選擇開發者模式)。

 

 

 

5.2.3 效果

會在瀏覽器中多一個狐狸的標識

 

5.3 metamask配置

 

5.3.1 點擊,基本全是accept

 

 

5.3.2 導入已經存在的祕鑰

 

 

5.3.3 導入已經存在祕鑰界面

 

 

5.3.4 獲取wallet Seed

在ganache的設置界面中

將紅框中的字體複製到metamask的wallet seed,設置用戶密碼即可。

5.3.5 登錄metamaks效果

如圖,可以通過點擊+,與獲取ganache中的私鑰來導入賬號。

 

 

 

 

5.3.6 對接ganache私鏈

點擊如下位置

 

 

5.3.7 填寫私鏈地址

 

 

5.3.8 最終效果

可以看到ganache中的餘額同步過來

 

 

5.4 運行代幣網頁

npm run dev

 

 

5.5 使用chrome登錄代幣頁面

 

5.6 代幣轉移

複製ganache中第二個賬號的地址,並轉移200個代幣

 

5.7 錢包確認

 

5.8 最終賬號1代幣數量減少

可以在metamask中導入第二個賬號,然後刷新 localhost:3000,可以看到代幣的數量變爲200。

6 參考資料

官網:

https://truffleframework.com/tutorials/robust-smart-contracts-with-openzeppelin

metamask的配置:

https://truffleframework.com/tutorials/pet-shop#interacting-with-the-dapp-in-a-browser

一些遇到的問題:

https://blog.csdn.net/weixin_42350212/article/details/80753328

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章