热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

truffle部署到测试网rinkeby

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

 部署到测试网

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

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

Ø  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 idgas: 5500000, // rinkeby has a lower block limit than mainnetconfirmations: 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 key0, // 地址的起始索引10 // 生成的地址数量),network_id: 4,// gas: 6500000,confirmations: 2,gasPrice: 5000000000, // 5 GweiskipDryRun: true // 跳过预执行,直接部署}}
}

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

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

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

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


推荐阅读
author-avatar
十点半的
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有