Truffle

錯誤:HelloWorld 尚未部署到檢測到的網路(網路/工件不匹配)

  • June 19, 2021

我正在按照此連結中的步驟創建一個松露項目。我使用 ganache-cli 作為乙太坊客戶端。

   rajkumar@rajkumar: Ethereum$ mkdir helloworld
   rajkumar@rajkumar: Ethereum$ cd helloworld/
   rajkumar@rajkumar: helloworld$ sudo truffle init

   ✔ Preparing to download
   ✔ Downloading
   ✔ Cleaning up temporary files
   ✔ Setting up box

   Unbox successful. Sweet!

   Commands:

   Compile:        truffle compile
   Migrate:        truffle migrate
   Test contracts: truffle test

   rajkumar@rajkumar: helloworld$ truffle create contract HelloWorld
   rajkumar@rajkumar: helloworld$ sudo truffle create contract HelloWorld

然後我用這裡的智能合約更新了智能合約。

   rajkumar@rajkumar: helloworld$ sudo truffle create migration HelloWorld
   rajkumar@rajkumar: helloworld$ sudo truffle migrate --network development
   [sudo] password for rajkumar:                                                                                                      

   Compiling your contracts...
   ===========================
   > Compiling ./contracts/HelloWorld.sol
   > Artifacts written to /home/rajkumar/Coding/Ethereum/helloworld/build/contracts
   > Compiled successfully using:
   - solc: 0.5.8+commit.23d335f2.Emscripten.clang

   Network up to date. 
   rajkumar@rajkumar: helloworld$ sudo truffle console
   truffle(development)> let instance = await HelloWorld.deployed()
   Thrown:
   Error: HelloWorld has not been deployed to detected network (network/artifact mismatch)
       at processTicksAndRejections (internal/process/task_queues.js:85:5)
       at Function.deployed (/usr/lib/node_modules/truffle/build/webpack:/packages/truffle-contract/lib/contract/constructorMethods.js:61:1)
       at Object.checkNetworkArtifactMatch (/usr/lib/node_modules/truffle/build/webpack:/packages/truffle-contract/lib/utils.js:256:1)
   truffle(development)>

為什麼我會遇到錯誤?我怎樣才能解決這個問題?

在 migrate 文件夾中的 HelloWorld 遷移文件中,您應該打開它並通過教程添加以下內容:

var HelloWorld = artifacts.require('HelloWorld');

module.exports = function(deployer) {
 // Use deployer to state migration tasks.
 deployer.deploy(HelloWorld);
};

您應該閱讀松露文件以獲取更多詳細資訊:https ://www.trufflesuite.com/docs/truffle/getting-started/running-migrations 。

看起來您已部署到development網路,但truffle console使用預設網路執行,因此假設它們是不同的。

您應該在同一網路上部署和使用控制台。

truffle migrate --network development

truffle console --network development

順便說一句,您不必使用 sudo。您可能需要考慮在本地安裝軟體包。

我只ganache-cli安裝了全域。

https://forum.openzeppelin.com/t/installing-packages-locally-rather-than-globally-npx/663

引用自:https://ethereum.stackexchange.com/questions/73756