Ropsten 测试网络
以太坊社区的包注册中心目前存在于Ropsten测试网络上。要把包发布到包注册中心,需要配置好Ropsten网络。
配置Ropsten网络,我们需要设置好账户地址,连接到以太坊节点(使用Infura)。关于如何连接到公链,详细示例可参考以太坊(Ethereum) – 连接公链
首先,通过项目目录中的NPM安装truff-hdwallet-provider
:
$ npm install truffle-hdwallet-provider --save
然后编辑配置,添加ropsten网络,使用账户地址的12个单词助记符:
文件:truffle.js
var HDWalletProvider = require("truffle-hdwallet-provider");
// 12-word mnemonic
var mnemonic = "opinion destroy betray ...";
module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 8545,
network_id: "*" // Match any network id
},
ropsten: {
provider: () =>
new HDWalletProvider(mnemonic, "https://ropsten.infura.io/v3/YOUR-PROJECT-ID"),
network_id: 3 // official id of the ropsten network
}
}
};
包的配置
如前面章节所述,EthPM的配置选项位于一个名为EthPM.JSON
的JSON文件中。
文件:ethpm.json
{
"package_name": "adder",
"version": "0.0.3",
"description": "Simple contract to add two numbers",
"authors": [
"Tim Coulter <tim.coulter@consensys.net>"
],
"keywords": [
"ethereum",
"addition"
],
"dependencies": {
"owned": "^0.0.1"
},
"license": "MIT"
}
命令
配置完成后,发布就很简单了:
$ truffle publish
将看到类似下面的输出,可以确认包已经成功发布。
$ truffle publish
Gathering contracts...
Finding publishable artifacts...
Uploading sources and publishing to registry...
+ adder@0.0.3
发布前清理
当使用默认的开发网络(配置为匹配任何Ethereum客户机(如Ganache或Truffle develop))时,项目中可能会有一些不希望发布的网络构件。在发布包之前,可以运行以下命令来删除任何无关的网络构件:
$ truffle networks --clean
有关更多信息,请参见命令参考。