truffle環境搭建和應用


前言:總是到了不得不學習的時候才學truffle,雖然我知道他很簡單。作為新生事物,還是不要小瞧。

 

1,安裝

sudo npm i truffle -g 

 

+ truffle@5.3.10
added 1747 packages from 1101 contributors in 317.505s

批注:沒加sudo還報錯了。

 

2,樣例

https://www.trufflesuite.com/boxes

https://www.trufflesuite.com/boxes/metacoin

https://github.com/truffle-box/metacoin-box

(1)創建

mkdir MetaCoin
cd MetaCoin

sudo truffle unbox metacoin

 

(2)編譯 compile

$ truffle compile

Compiling your contracts...
===========================
✔ Fetching solc version list from solc-bin. Attempt #1
✔ Downloading compiler. Attempt #1.
> Compiling ./contracts/ConvertLib.sol
> Compiling ./contracts/MetaCoin.sol
> Compiling ./contracts/Migrations.sol
> Artifacts written to /Users/xx/code/local/MetaCoin/build/contracts
> Compiled successfully using:
- solc: 0.5.16+commit.9c3226ce.Emscripten.clang

生成build/contracts/文件夾,xx.json文件。

 

(3)部署 migration,要到一個網絡上

先修改根目錄下的truffle-config.js的讀寫權限。

 

test: {
host: "https://exchaintestrpc.okex.org",
// port: "80",
network_id: "*"
}

$ truffle migrate

Migrations 直譯”遷移“,當作為一個名詞時,有時指的是用來部署的腳本文件,稱之為遷移文件,作為動詞會翻譯成部署,請讀者了解。

配置參考:

https://learnblockchain.cn/docs/truffle/reference/configuration.html

https://github.com/Soteria-core/soteria/blob/main/truffle-config.js

 

truffle-config.js 本地配置:

module.exports = {
  // Uncommenting the defaults below 
  // provides for an easier quick-start with Ganache.
  // You can also follow this format for other networks;
  // see <http://truffleframework.com/docs/advanced/configuration>
  // for more details on how to specify configuration options!
  //
  networks: {
   development: {
     host: "127.0.0.1",
     port: 8545,
     network_id: "*"
   },
    test: {
      host: "https://exchaintestrpc.okex.org",
  //    port: "80",
      network_id: "*"
    }
  },
  compilers: {
    solc: {
      version: "0.5.17",    // Fetch exact version from solc-bin (default: truffle's version)
      // docker: true,        // Use "0.5.1" you've installed locally with docker (default: false)
      settings: {          // See the solidity docs for advice about optimization and evmVersion
        optimizer: {
          enabled: true,
          runs: 200
        },
        //  evmVersion: "byzantium"
      }
    },
  }
  
};

對應,在本地起一條私鏈。truffle-config.js

 

truffle-config.js 遠程配置:

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

//直接填寫私鑰,不要帶0x前綴
const privateKeys = '...';

module.exports = {
  // Uncommenting the defaults below 
  // provides for an easier quick-start with Ganache.
  // You can also follow this format for other networks;
  // see <http://truffleframework.com/docs/advanced/configuration>
  // for more details on how to specify configuration options!
  //
  networks: {
   development: {
     host: "127.0.0.1",
     port: 8545,
     network_id: "*"
   },
    // bnb: {
    //   provider: () => new HDWalletProvider(privateKeys, `https://bsc-dataseed.binance.org/`),
    //   network_id: "*"
    // },
    // bnbtestnet: {
    //   provider: () => new HDWalletProvider(privateKeys, `https://data-seed-prebsc-1-s3.binance.org:8545/`),
    //   network_id: "*"
    // },
    dev: {
      provider: () => new HDWalletProvider(privateKeys, `https://exchaintestrpc.okex.org`),
      network_id: "65"
    },
  },
  compilers: {
    solc: {
      version: "0.5.17",    // Fetch exact version from solc-bin (default: truffle's version)
      // docker: true,        // Use "0.5.1" you've installed locally with docker (default: false)
      settings: {          // See the solidity docs for advice about optimization and evmVersion
        optimizer: {
          enabled: true,
          runs: 200
        },
        //  evmVersion: "byzantium"
      }
    },
  }
  
};

 

對應package.json

{
  "name": "MetaCoin",
  "version": "1.0.0",
  "description": "MetaCoin smart contracts",
  "scripts": {
    "test": "scripts/test.sh"
  },
  "keywords": [
    "solidity",
    "ethereum",
    "smart",
    "contracts",
    "insurance"
  ],
  "author": "MetaCoin.fund",
  "license": "GPL-3.0",
  "homepage": "https://soteria.fund",
  "dependencies": {
    "chai": "^4.1.2",
    "chai-bignumber": "^2.0.2",
    "ethereumjs-abi": "^0.6.5",
    "ganache-cli": "^6.9.1",
    "mocha": "^8.0.1",
    "solc": "^0.5.7",
    "solhint": "^1.5.1",
    "truffle": "^5.1.34",
    "truffle-hdwallet-provider": "^1.0.17",
    "truffle-plugin-verify": "^0.3.11",
    "web3": "^1.2.9"
  },
  "devDependencies": { }
}

 

運行:

 

 truffle test --network dev

 

 

 

(4)與合約進行交互

 

(5)truffle和metamask配合

用EthPM進行包管理

用NPM進行包管理

 

(6)調試合約

 

3,編寫測試用例

(1)測試合約,要到一個網絡上

$ truffle test

 

(2)用js寫測試用例

 

(3)用solidity寫測試用例

 

4,高級用法&參考引用

選擇網絡及部署

truffle配置

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM