BlockChain區塊鏈
  • 本書簡介
  • 區塊鏈運作原理
  • Bitcoin介紹
    • 簡介
    • Bitcoin其他知識
  • Bitcoin原理與實作
  • BitcoinJS
    • BTC 地址格式種類
    • 從 Mnemonic 轉為地址
  • Serverless 架構實作
  • Docker常用指令
  • ethereum初探
    • ethereum歷史
    • EVM
  • ethereum PoS 節點架設
  • ethereum(Docker)
  • ethereum(Geth)
    • Parity
  • ethereum(智能合約)
    • 合約測試 Unit Test
    • DAO
    • 可升級合約
    • 使用合約進行 multiswap
    • 合約安全
    • 開發工具
    • Hardhat 教學
      • Hardhat 寫測試
    • ERC-721 範例
      • 白名單機制
    • OpenZeppelin 合約 library
    • Truffle
    • 合約部屬
    • solidity 教學
  • ethereum(Dapp)
    • 相關 SDK
    • Multicall
    • Ethers.js 使用
    • Remix IDE
    • web3.js 使用
    • 在網頁上使用 web3 並操作區塊鏈
      • solidity筆記
  • Hyperledger Fabric
  • blockchainDB
  • 挖礦程式使用教學
    • 門羅幣/Monero (XMR)
  • Bitfinex API 使用
  • FTX API
  • CCXT 通用交易所 API
  • Solana 教學
  • Ethereum BigQuery
  • The Graph
    • yaml 定義
    • mapping 語法
    • Schema 定義
    • Query 範例
    • Unit test
  • DeFi 筆記
    • MEV 相關
    • Dex 聚合
    • Yearn
    • Curve
    • Uniswap
      • Swap 互動
    • AAVE、Compound
      • Compound 原理
      • AAVE 合約開發
Powered by GitBook
On this page
  • 106/05/04
  • 1.
  • 使用truffle box
  • truffle-config.js 範例

Was this helpful?

  1. ethereum(智能合約)

Truffle

https://github.com/trufflesuite/truffle/

PreviousOpenZeppelin 合約 libraryNext合約部屬

Last updated 4 years ago

Was this helpful?

如果有安裝metamask記得要換一個瀏覽器

記得先安裝

npm install -g truffle ethereumjs-testrpc

我們這邊選擇一個叫做webpack的模板

mkdir truffleTest
cd truffleTest
truffle init webpack //使用名稱為webpack的範例模板

之後會開始下載 然後進入專案

之後啟動testrpc 開一個termaial然後輸入testrpc

之後進入專案路徑的terminal輸入truffle compile

然後部署到testrpc跑的測試鏈上truffle migrate

之後可以試著用頁面轉token
然後更改地40行account = accounts[0]; 的數字查看

106/05/04

npm install -g truffle

1.

新增一個資料夾cd進去,之後執行

truffle init

開啟testRPC

npm install -g ethereumjs-testrpc

之後輸入testrpc,他會預設給你九組帳號和對應私鑰

3.在contract下新增一個HelloWorld.sol

pragma solidity ^0.4.8;

contract HelloWorld {

    uint public balance;

    function HelloWorld() {
        balance = 2000;
    }

    function deposit(uint _value) {
      // ...
      balance += _value;
    }
}

把migrate資料夾下的_deploy_contracts.js改為如下

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

module.exports = function(deployer) {
  deployer.deploy(HelloWorld);
};

之後

truffle compile  => truffle migrate  => truffle console

compile是把sol compile之後會多出json檔案,migrate是把contract部署到RPC上

查看餘額

HelloWorld.deployed().then(ins => ins.balance().then(a => console.log(a)))

存錢

HelloWorld.deployed().then(a => {a.deposit(200)})

注意事項: 1.

Truffle更改了API

...deployed()後面要用then來接

ex:

HelloWorld.deployed().then(a => console.log(a.address))
HelloWorld.deployed().then(a => console.log(a.balance().then(console.log)))

2.

檔案sol的名稱要跟contract名稱相同

3.

如果想更改已部署到RPC上的contract要輸入 truffle migrate --reset

使用truffle box

官方有內建幾個box,算是模板,可供下載

truffle unbox <package>

但如果搭配ethereumjs-testrpc,記得更改設定的port以及寫gasLimit,不然可能會出錯

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: 8545,
      network_id: "*", // Match any network id
      gas: 4712388
    }
  }
};

類似圖形化的testrpc

或是僅安裝指令列

npm install ganache-cli -g

truffle-config.js 範例

const HDWalletProvider = require("truffle-hdwallet-provider");

const mnemonic = "...12 words";

module.exports = {
  networks: {
    development: {
      host: "localhost",
      port: 8545,
      network_id: "*" // Match any network id
    },
    ropsten: {
      // must be a thunk, otherwise truffle commands may hang in CI
      provider: () =>
        new HDWalletProvider(mnemonic, "https://ropsten.infura.io/v3/..."),
      network_id: '3',
    }
  }
};

另外也可用

但上面的gas 配置又會造成-cli 產生out of gas,之後移除gas欄位即可成功

1.到 Infura 註冊 APP :

2.隨機產生 mnemonic:

3.給予測試用 Ether:

https://github.com/trufflesuite/truffle-init-webpack
https://github.com/ConsenSys/truffle
http://ethereum.stackexchange.com/questions/11935/problem-with-truffle-console-cannot-read-property-call-of-undefined
http://truffleframework.com/ganache/
ganache
https://infura.io/login
https://iancoleman.io/bip39/#english
https://faucet.ropsten.be/