Truffle项目搭建、编译、部署、验证合约以及过程中的坑点处理

  • 环境依赖
  • 安装truffle
  • 搭建项目
  • 编写合约和脚本
  • 编译合约
  • 部署合约
    • ropsten测试币
    • 配置网络
    • 部署
  • 验证合约
    • 安装 truffle-plugin-verify
    • 获取api key
    • 修改truffle-config.js
    • 验证合约

环境依赖

1.nodejs
2.python

安装truffle

//安装npm install -g truffle//查看版本truffle version

搭建项目

mkdir democd demotruffle unbox webpack

出现报错,网络被墙下载失败

修改 C:\Windows\System32\drivers\etc\hosts 增加如下内容

# GitHub Start192.30.255.112 gist.github.com192.30.255.112 github.com192.30.255.112 www.github.com151.101.56.133 avatars0.githubusercontent.com151.101.56.133 avatars1.githubusercontent.com151.101.56.133 avatars2.githubusercontent.com151.101.56.133 avatars3.githubusercontent.com151.101.56.133 avatars4.githubusercontent.com151.101.56.133 avatars5.githubusercontent.com151.101.56.133 avatars6.githubusercontent.com151.101.56.133 avatars7.githubusercontent.com151.101.56.133 avatars8.githubusercontent.com151.101.56.133 camo.githubusercontent.com151.101.56.133 cloud.githubusercontent.com151.101.56.133 gist.githubusercontent.com151.101.56.133 marketplace-screenshots.githubusercontent.com151.101.56.133 raw.githubusercontent.com151.101.56.133 repository-images.githubusercontent.com151.101.56.133 user-images.githubusercontent.com# GitHub End

再次运行 truffle unbox webpack 运行成功

编写合约和脚本

constrcts目录和migrations目录默认会生成三个合约和两个脚本,为了演示简单方便,这里只用一个合约和脚本演示,将两个目录文件删除,加入以下的合约和脚本

//contracts\Test.sol// SPDX-License-Identifier: MITpragma solidity ^0.8.0;contract Test {address public owner;uint public num;constructor(){owner = msg.sender;}modifier restricted() {if (msg.sender == owner) _;}function setNum(uint _num) public restricted {num = _num;}}
//migrations\1_initial_migration.jsconst Test = artifacts.require("Test");module.exports = function(deployer) {deployer.deploy(Test);};

编译合约

//仅默认编译自上次编译后被修改过的文件,来减少不必要的编译。//如果想编译全部文件,可以使用 truffle compile --compile-alltruffle compile 


之前 truffle version 可以看到返回的solidity版本是0.5.16,而合约中我们规定的solidity版本要大于0.8.0,编译失败,所有需要指定solidity的版本,打开truffle-config.js,修改最下方的配置

// Configure your compilerscompilers: {solc: {version: "0.8.0",// 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: false,//runs: 200//},//evmVersion: "byzantium"// }}}

再次编译,编译成功,生成build\contracts\Test.json文件

部署合约

ropsten测试币

这里我们把合约部署到以太坊ropsten测试链上,因为这个链测试币容易领取,测试币领取链接

配置网络

1.安装 truffle-hdwallet-provider

npm i truffle-hdwallet-provider

2 . 修改truffle-config.js 这里配置了两个环境,一个本地节点 一个ropsten

const HDWalletProvider = require('truffle-hdwallet-provider');const mnemonic = "easily icon 。。。"; //助记词,使用该账户来部署合约module.exports = {networks: {//本地环境development: { host: "127.0.0.1", // Localhost (default: none) port: 7545,// Standard Ethereum port (default: none) network_id: "*", // Any network (default: none)},//ropstenropsten: {provider: () => new HDWalletProvider(mnemonic, `https://ropsten.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161`),network_id: 3, // Ropsten's idnetworkCheckTimeout: 10000,gas: 5500000,// Ropsten has a lower block limit than mainnettimeoutBlocks: 200,// # of blocks before a deployment times out(minimum/default: 50)skipDryRun: true // Skip dry run before migrations" />},// Configure your compilerscompilers: {solc: {version: "0.8.0",// Fetch exact version from solc-bin (default: truffle's version)}}}

部署

//--network ropsten 使用配置中networks下的ropsten网络//多合约下,默认仅部署新合约,重新全部执行 --resettruffle migrate --network ropsten

部署成功,复制合约地址到区块浏览器查看

验证合约

安装 truffle-plugin-verify

npm i truffle-plugin-verify

获取api key

区块浏览器注册账号(需要切换到主网,测试网没有登陆注册),登录后 https://etherscan.io/myapikey 获取api key

修改truffle-config.js

区块浏览器无法直接访问,需要代理访问, truffle-config.js 增加如下内容,配置代理,引入验证插件,配置apiKey

verify: {proxy: { host: '127.0.0.1', port: '19180' }},plugins: ['truffle-plugin-verify'],api_keys: {etherscan: 'API_KEY',},

验证合约

truffle run verify Test --network ropsten

验证成功,刷新区块浏览器可以看到合约代码已验证