Ether

使用 hardHat 在 Ropsten 上部署合約時,如何修復“TypeError:無法讀取屬性‘sendTransaction’ of null”?

  • March 18, 2022

我收到此錯誤:

TypeError: Cannot read property 'sendTransaction' of null

執行此命令時:

$ npx hardhat run scripts/deploy.js --network ropsten

在 Ropsten 網路上使用 HardHat。

這是我的 deploy.js(這是 HardHat 提供的):

const hre = require("hardhat");

async function main() {

 const Greeter = await hre.ethers.getContractFactory("Greeter");
 const greeter = await Greeter.deploy("Hello, Hardhat!");

 await greeter.deployed();

 console.log("Greeter deployed to:", greeter.address);
}

main()
 .then(() => process.exit(0))
 .catch(error => {
   console.error(error);
   process.exit(1);
 });

這是我的 App.js:

import { useState } from 'react';
import { ethers } from 'ethers';
import './App.css';
import Greeter from './artifacts/contracts/Greeter.sol/Greeter.json';

function App() {
 const greeterAddress = '0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512';
 const [greeting, SetGreetingValue] = useState('');

 async function requestAccount() {
   await window.ethereum.request({
     method: 'eth_requestAccounts',
   });
 }

 async function fetchGreeting() {
   if (typeof window.ethereum !== 'undefined') {
     const provider = new ethers.providers.Web3Provider(window.ethereum);
     const contract = new ethers.Contract(greeterAddress, Greeter.abi, provider);
     try {
       const data = await contract.greet();
       console.log('data: ', data);
     } catch (error) {
       console.error(error);
     }
   }
 }

 async function setGreeting() {
   if (!greeting) return;
   if (typeof window.ethereum !== 'undefined') {
     await requestAccount();
     const provider = new ethers.providers.Web3Provider(window.ethereum);
     const signer = provider.getSigner();
     const contract = new ethers.Contract(greeterAddress, Greeter.abi, signer);
     const transaction = await contract.setGreeting(greeting);
     SetGreetingValue('')
     await transaction.wait();
     fetchGreeting();
   }
 }

 return (
   <div className="App">
   <button onClick={fetchGreeting}>Fetch Greeting</button>
   <button onClick={setGreeting}>Set Greeting</button>
   <input placeholder='Set greeting' onChange={(e) => SetGreetingValue(e.target.value)} placeholder='Set Greeting' value={greeting}/>
   </div>
 );
}

export default App;

這是 hardhat.config.js:

module.exports = {
 solidity: "0.8.4",
 paths: {
   artifacts: './client/src/artifacts'
 },
 networks: {
   hardhat: {
     chainId: 1337
   },
   ropsten: {
     url: "https://ropsten.infura.io/v3/938ec750ced64edda8fc17c9e135d0f6",
     account: [`0x08c75f58dcad3bc9664F2ba9Db3C10b2555D6702`]
   }
 }
};

當程式碼中的任何地方都沒有 sendTransaction 時,我不明白為什麼會收到此 sendTransaction 錯誤

您需要在 hardhat.config.js 文件中提供您的私鑰,而不是您的公鑰/地址。

module.exports = {
 solidity: "0.8.4",
 paths: {
   artifacts: './client/src/artifacts'
 },
 networks: {
   hardhat: {
     chainId: 1337
   },
   ropsten: {
     url: "https://ropsten.infura.io/v3/938ec750ced64edda8fc17c9e135d0f6",
     account: [`YOUR_PRIVATE_KEY`]
   }
 }
};

在你的hardhat.config.js你應該有accounts而不是account。由於這個錯字,一旦簽名者為空,乙太就不能呼叫“sendTransaction”

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