ethers-simple-storage-fcc icon indicating copy to clipboard operation
ethers-simple-storage-fcc copied to clipboard

getting undefined error of JsonRpcProvider

Open drleavio opened this issue 1 year ago • 6 comments

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

drleavio avatar Apr 21 '23 14:04 drleavio

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

42maojin avatar Apr 24 '23 09:04 42maojin

not working still giving the same error

drleavio avatar Apr 24 '23 12:04 drleavio

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

42maojin avatar Apr 25 '23 07:04 42maojin

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

@42maojin thanx, worked for me

roostiqcopilot avatar May 29 '23 05:05 roostiqcopilot

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!

lalondelalonde avatar Jun 05 '23 21:06 lalondelalonde

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/"
);

Taruns123 avatar Feb 17 '24 03:02 Taruns123