[Contract] Solidity 合约使用 truffle 部署到测试网和主网

使用 truffle 发布到非本地的以太坊主网或者测试网时,需要提供钱包的助记词或私钥。

首先安装 truffle 组件:npm install @truffle/hdwallet-provider

注册 https://infura.io,得到 project id 和 key,使用其 api 连接测试网:修改 truffle-config.js 中的 infuraKey,以及网络配置项 ropsten 中的 provider 地址。

本地新建助记词文件,比如 .mnemonic,然后加入 .gitignore 忽略:修改 truffle-config.js 引入此正确的文件,ropsten 中的 provider 使用到了 mnemonic。

部署到 ropsten 测试网:truffle migrate --network ropsten

部署超时可以设置网络 ropsten 的属性 networkCheckTimeout: 600000

如果经过了多个 blocks 的等待之后,仍旧在等待部署,那么可以提高 ropsten 配置中的 gas 后重新部署。

另一种部署方式是通过 Remix IDE 可视化操作,也非常方便。

在浏览器中查看事务:https://ropsten.etherscan.io/tx/0x7xxxxxxxxxxxxx 

truffle 命令行与 ropsten 交互:truffle console --network ropsten

Others:Solidity 合约发布到测试网 ropsten 的作用

Others:ETH 与 Gas 之间的价格转换关系, Ethereum Gas Price Chart

Link:https://www.cnblogs.com/farwish/p/12488173.html