You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用Charity合约fundingCharity交易触发ProviderError: HttpProviderError求助

本地测试Charity合约fundingCharity交易时的HttpProviderError排查

问题场景

部署Charity合约后,执行命令 npx hardhat run scripts/fund.js --network localhost 触发HttpProviderError错误,无法完成fundingCharity交易。

错误日志

ProviderError: HttpProviderError
    at HttpProvider.request (F:\fullstack-auction\smart-contract\node_modules\hardhat\src\internal\core\providers\http.ts:83:19)
    at AutomaticSenderProvider.request (F:\fullstack-auction\smart-contract\node_modules\hardhat\src\internal\core\providers\accounts.ts:344:34)
    at FixedGasProvider.request (F:\fullstack-auction\smart-contract\node_modules\hardhat\src\internal\core\providers\gas-providers.ts:28:34)
    at FixedGasPriceProvider.request (F:\fullstack-auction\smart-contract\node_modules\hardhat\src\internal\core\providers\gas-providers.ts:54:34)
    at ChainIdValidatorProvider.request (F:\fullstack-auction\smart-contract\node_modules\hardhat\src\internal\core\providers\chainId.ts:66:34)
    at BackwardsCompatibilityProviderAdapter.send (F:\fullstack-auction\smart-contract\node_modules\hardhat\src\internal\core\providers\backwards-compatibility.ts:36:27)      
    at EthersProviderWrapper.send (F:\fullstack-auction\smart-contract\node_modules\@nomiclabs\hardhat-ethers\src\internal\ethers-provider-wrapper.ts:13:48)
    at F:\fullstack-auction\smart-contract\node_modules\@ethersproject\providers\src.ts\json-rpc-provider.ts:255:34)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)

相关代码与配置

hardhat.config.js

require("@nomiclabs/hardhat-waffle")
require("@nomiclabs/hardhat-etherscan")
require("hardhat-deploy")
require("solidity-coverage")
require("hardhat-gas-reporter")
require("hardhat-contract-sizer")
require("dotenv").config()

const GOERLI_RPC_URL = process.env.GOERLI_RPC_URL
const BSC_RPC_URL = process.env.BSC_RPC_URL

const BSC_PRIVATE_KEY = process.env.BSC_PRIVATE_KEY
const GOERLI_PRIVATE_KEY = process.env.GOERLI_PRIVATE_KEY
const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  defaultNetwork: "hardhat",
  networks: {
    // localhost: {
    //   chainId: 31337,
    //   allowUnlimitedContractSize: true,
    //   gas: 2100000,
    //   gasPrice: 8000000000,
    // },
    hardhat: {
      chainId: 31337,
      allowUnlimitedContractSize: true,
      gas: 2100000,
      gasPrice: 8000000000,
    },
    goerli: {
      url: GOERLI_RPC_URL,
      accounts: [GOERLI_PRIVATE_KEY],
      chainId: 5,
    },
    binance: {
      url: BSC_RPC_URL,
      chainId: 0x61,
      accounts: [BSC_PRIVATE_KEY],
    },
  },
  namedAccounts: {
    deployer: {
      default: 0,
    },
  },
  etherscan: {
    apiKey: ETHERSCAN_API_KEY,
  },
  // gasReporter: {
  //   enabled: true,
  //   currency: "USD",
  //   outputFile: "./gas-report.txt",
  //   noColors: true,
  //   // coinmarketcap: process.env.COINMARKETCAP_API_KEY,
  // },
  contractSizer: {
    runOnCompile: false,
    only: ["Charity"],
  },
  solidity: {
    compilers: [{ version: "0.8.7" }, { version: "^0.8.0" }],
  },
}

合约部署脚本

const { network, ethers } = require("hardhat")
const { verify } = require("../utils/verify")
const { networkConfig, developmentChains } = require("../helper.hardhat.config")

const liveWallet = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"

module.exports = async ({ getNamedAccounts, deployments }) => {
  const { deploy, log } = deployments
  const { deployer } = await getNamedAccounts()
  const chainId = network.config.chainId
  let fundWallet

  if (developmentChains.includes(network.name)) {
    fundWallet = networkConfig[chainId].walletFund
  } else {
    fundWallet = liveWallet
  }

  const args = [
    fundWallet,
    ethers.utils.parseEther(networkConfig[chainId].minimumFund),
  ]

  log("deploying ...")
  const charity = await deploy("Charity", {
    from: deployer,
    args,
    log: true,
  })

  log("deployed successfull at", charity.address)
}

module.exports.tags = ["all", "charity"]

交易脚本fund.js

const { ethers, getNamedAccounts } = require("hardhat")
const { networkConfig } = require("../helper.hardhat.config")

const charity = ethers.utils.parseEther("3")

async function fund() {
  const { deployer } = getNamedAccounts()
  const charityContract = await ethers.getContract("Charity", deployer)
  const funding = await charityContract.fundingCharity({
    value: charity,
  })
  await funding.wait(1)
  const getFund = await charityContract.getFundingBalance()

  console.log(getFund.toString())
}

fund().catch((error) => {
  console.error(error)
  process.exitCode = 1
})

排查与解决方法

1. 确保本地Hardhat节点已启动

使用--network localhost时,必须先启动本地节点:

npx hardhat node

保持该终端窗口运行,再打开新终端执行fund脚本。

2. 启用localhost网络配置

取消hardhat.config.js中localhost配置块的注释,确保网络参数与本地节点匹配:

localhost: {
  chainId: 31337,
  allowUnlimitedContractSize: true,
  gas: 2100000,
  gasPrice: 8000000000,
},

3. 修复交易脚本中的await缺失

交易脚本中getNamedAccounts()是异步函数,必须添加await:

const { deployer } = await getNamedAccounts()

4. 保证合约部署与交易使用同一网络

部署合约时需指定--network localhost,确保合约部署在运行的本地节点上:

npx hardhat deploy --tags charity --network localhost

之后再执行fund脚本,确保连接同一节点。

内容的提问来源于stack exchange,提问作者mohdraazy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 04:47:03