PatrickAlphaC / ethers-simple-storage-fcc

90 stars 128 forks source link

getting undefined error of JsonRpcProvider #76

Open drleavio opened 1 year ago

drleavio commented 1 year ago
const ethers = require("ethers");
const fs = require("fs-extra");

async function main() {
  //   HTTP://127.0.0.1:7545

  const provider = new ethers.provider.JsonRpcProvider("http://127.0.0.1:7545");

  const wallet = new ethers.Wallet(
    0x51196181b8c4d0748a3307769b92ae85522b3610e7fbb56c81d783437d21ef40,
    provider
  );
  const abi = fs.readFileSync("./SimpleStorage_sol_Storage.abi", "utf8");
  const binary = fs.readFileSync("./SimpleStorage_sol_Storage.bin", "utf8");
  const contractfactory = new ethers.ContractFactory(abi, binary, wallet);
  console.log("wait....contract is deploying");
  const contract = await contractfactory.deploy();
  console.log(contract);
}
main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });
42maojin commented 1 year ago

const { ethers, JsonRpcProvider } = require("ethers") // and const provider = new JsonRpcProvider("http://0.0.0.0:7545")

drleavio commented 1 year ago

not working still giving the same error

42maojin commented 1 year ago

check ethers version, is the key,reason is version update

roostiqcopilot commented 1 year ago

const { ethers, JsonRpcProvider } = require("ethers") // and const provider = new JsonRpcProvider("http://0.0.0.0:7545")

@42maojin thanx, worked for me

lalondelalonde commented 1 year ago

Hey guys, I'll copy-paste an answer I gave, I was stuck on the same problem.

const { ethers } = require("ethers"); const fs = require("fs");

async function main() { const provider = new ethers.providers.JsonRpcProvider( "http://127.0.0.1:7545/" ); const wallet = new ethers.Wallet( "0xb357c12e59c02b0b6bde61434adda8ab38a29f2db7bb109ec360955b55404677", provider ); const abi = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.abi", "utf8"); const bin = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.bin", "utf8"); const contractFactory = new ethers.ContractFactory(abi, bin, wallet); console.log("Deploying, please wait..."); const contract = await contractFactory.deploy(); // Stop here and wait for the contract to deploy const deploymentReceipt = await contract.deployTransaction.wait(1); console.log(deploymentReceipt); }

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

I unistalled the node_modules and reinstalled it. Also I'm using Ethers 5.6.2 and modify a little bit of the code listening to some this and input from ChatGPT. Good luck bro!

Taruns123 commented 9 months ago

what i got to know in the later versions the file structure was more well defined so as to put the jsonRpcProvider in the providers , but in the earlier section the JsonRpcProvider was in the ethers itself

you can try this instead

const provider = new ethers.JsonRpcProvider(
"http://127.0.0.1:7545/"
);