Dapps

使用安全帽部署到 Ropsten 測試網時出現“TypeError: Cannot read property ‘sendTransaction’ of null”錯誤

  • April 8, 2022

我收到錯誤消息:

TypeError:無法在 ContractFactory 讀取 null 的屬性“sendTransaction”。(/Users/UserName/Downloads/nft-marketplace-ethereum/node_modules/@ethersproject/contracts/src.ts/index.ts:1237:38) 在步驟 (/Users/UserName/Downloads/nft-marketplace-ethereum/node_modules /@ethersproject/contracts/lib/index.js:48:23) 在 Object.next (/Users/UserName/Downloads/nft-marketplace-ethereum/node_modules/@ethersproject/contracts/lib/index.js:29:53 ) 在完成時 (/Users/UserName/Downloads/nft-marketplace-ethereum/node_modules/@ethersproject/contracts/lib/index.js:20:58)

執行命令時:

npx 安全帽執行腳本/deploy.js –network ropsten

我的 deploy.js 文件是

const hre = require("hardhat");

async function main() {
 const NFTmarket = await hre.ethers.getContractFactory("NFTmarket");
 const nftmarket = await NFTmarket.deploy();

 await nftmarket.deployed();

 console.log("nftmarket deployed to:", nftmarket.address);

 const NFTtoken = await hre.ethers.getContractFactory("NFTtoken");
 const nfttoken = await NFTtoken.deploy(nftmarket.address);
 await nfttoken.deployed();
 console.log("nfttoken deployed to:", nfttoken.address); 
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main()
 .then(() => process.exit(0))
 .catch((error) => {
   console.error(error);
   process.exit(1);
 });

我的 index.js 文件是

export default function Home() {

 const router = useRouter();
 const [nfts, setNfts] = useState([]);
 const [loadingState, setLoadingState] = useState("not-loaded");

 useEffect(() => {
   loadNFTs();
 }, []);
 async function loadNFTs() {
   const provider = new ethers.providers.JsonRpcProvider("https://ropsten.infura.io/v3/{project_id}");
   const tokenContract = new ethers.Contract(
     nftaddress,
     NFTtoken.abi,
     provider
   );
   const marketContract = new ethers.Contract(
     nftmarketaddress,
     NFTmarket.abi,
     provider
   );
   const data = await marketContract.unsoldNFTs();

   const items = await Promise.all(
     data.map(async (i) => {
       const tokenUri = await tokenContract.tokenURI(i.tokenid);
       const meta = await axios.get(tokenUri);
       let price = ethers.utils.formatUnits(
         i.selling_price.toString(),
         "ether"
       );
       let item = {
         price,
         tokenid: i.tokenid.toNumber(),
         seller: i.seller,
         owner: i.owner,
         image: meta.data.image,
         name: meta.data.name,
         description: meta.data.description,
       };
       return item;
     })
   );
   setNfts(items);
   setLoadingState("loaded");
 }
 async function buyNft(nft) {
   const web3Modal = new Web3Modal();
   const connection = await web3Modal.connect();
   const provider = new ethers.providers.Web3Provider(connection);
   const signer = provider.getSigner();
   const contract = new ethers.Contract(
     nftmarketaddress,
     NFTmarket.abi,
     signer
   );
   const price = ethers.utils.parseUnits(nft.price.toString(), "ether");
   const transaction = await contract.Sale(nftaddress, nft.tokenid, {
     value: price,
   });
   await transaction.wait();
   loadNFTs();
 }

我的 hardhat.config.js 是

require("@nomiclabs/hardhat-waffle");
const fs = require('fs');
const privateKey = fs.readFileSync("privatekey").toString()
// const infuraId = fs.readFileSync(".infuraid").toString().trim() || "";


module.exports = {
 networks:{
   hardhat: {
     chainId: 1337
   },
   ropsten: {
     url: "https://ropsten.infura.io/v3/{project_id}",
     account: [privateKey]
   },
   mainnet: {
     url: "https://mainnet.infura.io/v3/{project_id}",
     account: [privateKey]
   },
 },
 solidity: "0.8.4",
};

當我的私鑰沒有正確填充時,我看到了這個錯誤。

我也會使用環境變數process.env.PRIVATE_KEY,所以它不會硬編碼到您的安全帽配置中。

在你的 hardhat.config.js

mainnet: {
 url: "https://mainnet.infura.io/v3/{project_id}",
 account: [privateKey]
} 

應該是帳戶而不是帳戶。它對我有用。

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