6

I am getting the error:

TypeError: Cannot read property 'sendTransaction' of null at ContractFactory. (/Users/UserName/Downloads/nft-marketplace-ethereum/node_modules/@ethersproject/contracts/src.ts/index.ts:1237:38) at step (/Users/UserName/Downloads/nft-marketplace-ethereum/node_modules/@ethersproject/contracts/lib/index.js:48:23) at Object.next (/Users/UserName/Downloads/nft-marketplace-ethereum/node_modules/@ethersproject/contracts/lib/index.js:29:53) at fulfilled (/Users/UserName/Downloads/nft-marketplace-ethereum/node_modules/@ethersproject/contracts/lib/index.js:20:58)

when running the command:

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

my deploy.js file is

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);
  });

my index.js file is

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();
  }

my hardhat.config.js is

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",
};

3 Answers 3

1

I have seen this error when my private key wasn't properly populated.

I also would use an environment variable like process.env.PRIVATE_KEY, so it's not hard-coded into your hardhat config.

1

In your hardhat.config.js

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

should be accounts instead of account. It worked for me.

0

I had this issue because I was using multiple signers:

const [deployer, account, ...] = await ethers.getSigners();

for testing on localhost, but I only added 1 private key when deploying to the testnet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.