0

I put my private key in .env file and then import the .env file using require("dotenv").config() but still it is not compiling the code and throws an error. I even installed the dotenv using "yarn add dotenv". In .env file, I put my private key as: PRIVATE_KEY = "(private-key)";

In const wallet, I call the private key as : process.env.PRIVATE_KEY

const ethers = require("ethers");
const fs = require("fs-extra"); //To import the 'fs' fuctionality of node.js to read the files of abi and bytecode
require("dotenv").config();
async function main() {
 
  await console.log("Smart contract address : 0x123456....");
  //include the provider and connect the local blockchain to our script
  const provider = new ethers.providers.JsonRpcProvider(
    "RPC-URL"
  );

  const wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider);
  const abi = fs.readFileSync("./Fundme_sol_Storage.abi", "utf8");

 //Here we've included our abi file and it readFileSync mainly takes two arguments obviously one is path of abi file and another is encoding for the smart contract

  const byteCode = fs.readFileSync("./Fundme_sol_Storage.bin", "utf8");

  //Now we have to use contract factory to deploy the smart contract on our local blockchain

  const contractFactory = new ethers.ContractFactory(abi, byteCode, wallet);
  console.log("Contract is deploying.....");
  const contractDeploy = await contractFactory.deploy();
  await contractDeploy.deployTransaction.wait(1);

  //This is going to give the number....

  const contractRet = await contractDeploy.retrieve();
  console.log(contractRet.toString());

  //This is going to increement the number..

  const contractStore = await contractDeploy.store("42");
  await contractStore.wait(1);

  //Now, we're going to retrieve number..

  const newNum = await contractDeploy.retrieve();
  console.log(`The update Fav number is:${newNum}`);

This is the error I am getting in terminal while deploying the code.

enter image description here

2 Answers 2

0

Change to PRIVATE_KEY="803b89dh9f89" sometimes it's okay with PRIVATE_KEY=803b89dh9f89 too

0

my .env file : enter image description here my deploy.js: enter image description here Hope that helps, I'm a beginner

1
  • The next time include code and snippets as text. That way it is much easier if someone wants to use them.
    – Ismael
    Commented Jan 22, 2023 at 5:21

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.