使用 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 的作用