您当前的位置: 首页 > 
  • 0浏览

    0关注

    1477博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

truffle部署到测试网rinkeby

软件工程小施同学 发布时间:2022-03-30 23:16:53 ,浏览量:0

 部署到测试网

以上仅仅展示了一个智能合约最简单的步骤。如果需要实现更多的功能,就需要在测试链上进行测试了。链上测试的重点在于对于交易的格式以及交易的数据正确性等等。

目前著名的以太坊测试链主要是:

Ø  ropsten(https://ropsten.etherscan.io/)

Ø  kovan(https://kovan.etherscan.io/)

Ø  rinkeby(https://rinkeby.etherscan.io/)

他们的区别在于共识机制的不同,所幸这些测试网都有区块链浏览器支持,同时又有水管可以自动申请测试ether。下面以kovan为例,看一下如何将智能合约部署到测试网上面。

向测试网部署智能合约,需要安装另外一个组件:truffle-hdwallet-provider

npm install @truffle/hdwallet-provider

在安装完成之后,需要在truffle.js 配置文件中做些修改。

要通过infura创建测试网对应的provider对象。

var HDWalletProvider = require("@truffle/hdwallet-provider");
var mnemonic = "xxxxx";  //MetaMask的助记词。

module.exports = {
  // Uncommenting the defaults below
  // provides for an easier quick-start with Ganache.
  // You can also follow this format for other networks.
  // See details at: https://trufflesuite.com/docs/truffle/reference/configuration
  // on how to specify configuration options!
  //
  networks: {
    development: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*"
    },
    rinkeby: {
      provider: () => new HDWalletProvider(mnemonic, `https://rinkeby.infura.io/v3/xxxxx`),
      network_id: 4,       // rinkeby's id
      gas: 5500000,        // rinkeby has a lower block limit than mainnet
      confirmations: 2,    // # of confs to wait between deployments. (default: 0)
      timeoutBlocks: 10000,  // # of blocks before a deployment times out  (minimum/default: 50)
      skipDryRun: true     // Skip dry run before migrations? (default: false for public nets )
    }
  }
  //,
  //
  // Truffle DB is currently disabled by default; to enable it, change enabled:
  // false to enabled: true. The default storage location can also be
  // overridden by specifying the adapter settings, as shown in the commented code below.
  //
  // NOTE: It is not possible to migrate your contracts to truffle DB and you should
  // make a backup of your artifacts to a safe location before enabling this feature.
  //
  // After you backed up your artifacts you can utilize db by running migrate as follows:
  // $ truffle migrate --reset --compile-all
  //
  // db: {
    // enabled: false,
    // host: "127.0.0.1",
    // adapter: {
    //   name: "sqlite",
    //   settings: {
    //     directory: ".db"
    //   }
    // }
  // }
};

请注意,每个测试网络为了互相区分,他们的network_id 都是不同的。

部署命令修改为

truffle migrate --network rinkeby

wallet

在项目根目录的项目配置文件truffle.js中,可以使用种子,在主网或测试网部署合约。下面提供一种部署到测试网rinkeby的配置

const HDWalletProvider = require('truffle-hdwallet-provider');
const fs = require('fs');
// 读取种子,12个单词组成的种子
const mnemonic = fs.readFileSync("./path/to/mnemonic.secret").toString().trim();

module.exports ={
    networks:{
        rinkebyTest:{
        	provider: () => new HDWalletProvider(
        		mnemonic, 
        		`https://rinkeby.infura.io/v3/aa86f***60803c`,// your infura API key
        		0, // 地址的起始索引
        		10 // 生成的地址数量
      		),
      		network_id: 4,
      		// gas: 6500000,
      		confirmations: 2,
      		gasPrice: 5000000000, // 5 Gwei
      		skipDryRun: true // 跳过预执行,直接部署
        }
    }
}

truffle 学习笔记(一)基本命令和配置_JustinQP的博客-CSDN博客

走近科学:利用Truffle Framework 和OpenZeppelin-Solidity 快速构建以太坊智能合约

Deploying Smart Contract on Test/Main Network Using Truffle - GeeksforGeeks

[以太坊dApp全栈开发教程]Truffle:1.智能合约的编译与部署 - 知乎

关注
打赏
1665320866
查看更多评论
立即登录/注册

微信扫码登录

0.0406s