npm Hardhat 运行后连接到本地主机时出错

sul*_*c_t 9 ethereum ethers.js hardhat

我是使用 Hardhat 部署智能合约的新手,正在遵循https://dev.to/dabit3/the-complete-guide-to-full-stack-ethereum-development-3j13上的教程。但是,运行后npx hardhat run scripts/deploy.js --network localhost,我收到以下错误。关于如何解决连接问题有什么想法吗?

HardhatError: HH108: Cannot connect to the network localhost.
    Please make sure your node is running, and check your internet connection and networks config
        at HttpProvider._fetchJsonRpcResponse (/Users/cuneydtasoglu/Desktop/hardhat_list/blockchain/node_modules/hardhat/src/internal/core/providers/http.ts:176:15)
        at processTicksAndRejections (node:internal/process/task_queues:93:5)
        at HttpProvider.request (/Users/cuneydtasoglu/Desktop/hardhat_list/blockchain/node_modules/hardhat/src/internal/core/providers/http.ts:55:29)
        at GanacheGasMultiplierProvider._isGanache (/Users/cuneydtasoglu/Desktop/hardhat_list/blockchain/node_modules/hardhat/src/internal/core/providers/gas-providers.ts:302:30)
        at GanacheGasMultiplierProvider.request (/Users/cuneydtasoglu/Desktop/hardhat_list/blockchain/node_modules/hardhat/src/internal/core/providers/gas-providers.ts:291:23)
        at EthersProviderWrapper.send (/Users/cuneydtasoglu/Desktop/hardhat_list/blockchain/node_modules/@nomiclabs/hardhat-ethers/src/internal/ethers-provider-wrapper.ts:13:20)
        at Object.getSigners (/Users/cuneydtasoglu/Desktop/hardhat_list/blockchain/node_modules/@nomiclabs/hardhat-ethers/src/internal/helpers.ts:23:20)
        at getContractFactoryByAbiAndBytecode (/Users/cuneydtasoglu/Desktop/hardhat_list/blockchain/node_modules/@nomiclabs/hardhat-ethers/src/internal/helpers.ts:250:21)
        at main (/Users/cuneydtasoglu/Desktop/hardhat_list/blockchain/scripts/deploy.js:17:19)
    
        Caused by: FetchError: request to http://127.0.0.1:8545/ failed, reason: connect ECONNREFUSED 127.0.0.1:8545
            at ClientRequest.<anonymous> (/Users/cuneydtasoglu/Desktop/hardhat_list/blockchain/node_modules/node-fetch/lib/index.js:1461:11)
            at ClientRequest.emit (node:events:376:20)
            at Socket.socketErrorListener (node:_http_client:490:9)
            at Socket.emit (node:events:376:20)
            at emitErrorNT (node:internal/streams/destroy:188:8)
            at emitErrorCloseNT (node:internal/streams/destroy:153:3)
            at processTicksAndRejections (node:internal/process/task_queues:80:21)
Run Code Online (Sandbox Code Playgroud)

小智 19

Petr 拥有正确的解决方案 - 当您部署智能合约时,您的本地测试节点(您从 开始的npx hardhat node)需要继续运行。

换句话说,您应该:

  1. npx hardhat node在您的终端中运行。让进程保持运行。
  2. 打开一个新的终端窗口。
  3. 跑步npx hardhat run [script-name] --network localhost


小智 11

使用以下命令运行部署脚本时出现类似问题:

npx hardhat run scripts/deploy.js --network localhost
Run Code Online (Sandbox Code Playgroud)

我可以通过使用安全帽而不是本地主机来解决这个问题:

npx hardhat run scripts/deploy.js --network hardhat
Run Code Online (Sandbox Code Playgroud)