Truffle项目搭建、编译、部署、验证合约

环境依赖

1.nodejs
2.python

安装truffle

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

在这里插入图片描述

搭建项目

mkdir demo
cd demo

truffle unbox webpack

出现报错,网络被墙下载失败
在这里插入图片描述
修改 C:\Windows\System32\drivers\etc\hosts 增加如下内容

# GitHub Start
192.30.255.112 gist.github.com
192.30.255.112 github.com
192.30.255.112 www.github.com
151.101.56.133 avatars0.githubusercontent.com
151.101.56.133 avatars1.githubusercontent.com
151.101.56.133 avatars2.githubusercontent.com
151.101.56.133 avatars3.githubusercontent.com
151.101.56.133 avatars4.githubusercontent.com
151.101.56.133 avatars5.githubusercontent.com
151.101.56.133 avatars6.githubusercontent.com
151.101.56.133 avatars7.githubusercontent.com
151.101.56.133 avatars8.githubusercontent.com
151.101.56.133 camo.githubusercontent.com
151.101.56.133 cloud.githubusercontent.com
151.101.56.133 gist.githubusercontent.com
151.101.56.133 marketplace-screenshots.githubusercontent.com
151.101.56.133 raw.githubusercontent.com
151.101.56.133 repository-images.githubusercontent.com
151.101.56.133 user-images.githubusercontent.com
# GitHub End

再次运行 truffle unbox webpack 运行成功

编写合约和脚本

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

//contracts\Test.sol
// SPDX-License-Identifier: MIT
pragma 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.js
const Test = artifacts.require("Test");

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

编译合约

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

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

  // Configure your compilers
  compilers: {
    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)
    },
    //ropsten
    ropsten: {
      provider: () => new HDWalletProvider(mnemonic, `https://ropsten.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161`),
      network_id: 3,       // Ropsten's id
      networkCheckTimeout: 10000,
      gas: 5500000,        // Ropsten has a lower block limit than mainnet
      timeoutBlocks: 200,  // # of blocks before a deployment times out  (minimum/default: 50)
      skipDryRun: true     // Skip dry run before migrations? (default: false for public nets )
    },
    // Configure your compilers
  compilers: {
    solc: {
      version: "0.8.0",    // Fetch exact version from solc-bin (default: truffle's version)
    }
  }
}

部署

//--network ropsten 使用配置中networks下的ropsten网络
//多合约下,默认仅部署新合约,重新全部执行 --reset
truffle 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

验证成功,刷新区块浏览器可以看到合约代码已验证
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
您好!感谢您的提问!来自 C 知道的我将为您解答关于使用 Truffle 部署合约的问题。 要使用 Truffle 部署合约,您可以按照以下步骤进行操作: 1. 首先,确保您已经在您的项目中安装Truffle。如果没有安装,可以通过运行以下命令进行安装: ``` npm install -g truffle ``` 2. 确保您已经在项目目录中初始化了 Truffle,可以通过运行以下命令完成初始化: ``` truffle init ``` 3. 在项目目录中,您需要在 `contracts` 文件夹中编写您的智能合约代码。例如,您可以创建一个名为 `MyContract.sol` 的文件,并编写您的合约代码。 4. 在部署合约之前,您需要配置 Truffle 的网络设置。打开项目目录中的 `truffle-config.js`(或 `truffle.js`,取决于您的项目版本),并根据需要配置网络。例如,您可以添加一个名为 "development" 的网络配置: ```javascript module.exports = { networks: { development: { host: "localhost", port: 8545, network_id: "*", } }, }; ``` 5. 在部署之前,您可以通过运行以下命令来编译您的合约: ``` truffle compile ``` 6. 最后,您可以使用以下命令来部署合约: ``` truffle migrate --network development ``` 上述命令将在您配置的 "development" 网络上部署合约。如果您配置了其他网络,请相应地更改命令中的网络名称。 这就是使用 Truffle 部署合约的基本步骤。希望能对您有所帮助!如果您有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bug的搬运工

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值